Each notebook is a worked example you can read here or download and run. They all run offline with no API key (see Running the notebooks); set MODEL to repeat them against a real model.

#NotebookWhat you buildModule
01Notebook 1: The agent loopA fixed workflow, then a 20-line agent loop built from scratch: tools, observations, budgets, and the trace.Module 1
02aNotebook 2a: Tokens and BPE from scratchBuild a byte-level BPE tokenizer, see how vocabulary size trades off against compression, and count what an agent’s prompts really cost.Module 2
02bNotebook 2b: A tiny language model, sampling, and constrained decodingTrain a character n-gram model, implement temperature, top-k and top-p sampling, and force valid tool-call JSON with a grammar mask.Module 2
02cNotebook 2c: The LLM clientMessages and statelessness, one client for every provider, validated structured output, concurrency, retries, caching and a cost budget, each built as a small layer.Module 2
03aNotebook 3a: Function calling from scratchA tool call on the wire, schemas generated from Python signatures, argument validation, a registry whose errors become observations, a linter for tool definitions, and a search tool sized for a token budget.Module 3
03bNotebook 3b: MCP, a standard plug for toolsBuild an MCP server with FastMCP, talk to it in memory, by hand over stdio, and with the SDK, then wrap its tools for an agent and check what you are trusting.Module 3
04Notebook 4: The ReAct agentReAct as a text protocol and as native tool calls; an agent built from its parts (context, events, tools, run/step/think/act); stop rules; structured output; a mini benchmark.Module 4
05aNotebook 5a: Retrieval from scratchChunking a small handbook three ways, BM25 written from the formula, hashed vectors in a numpy vector store, hybrid search with reciprocal rank fusion, and recall@k on 24 labelled questions.Module 5
05bNotebook 5b: Agentic RAGRetrieval as a tool the agent calls with its own queries, a multi-hop question, file tools that follow the corpus’s folders, tool callbacks that log, cache, guard and compress, and a human approval step before an email goes out.Module 5
06aNotebook 6a: Context managementWhat an agent stores against what it sends, counting tokens, a sliding window that does not break tool calls, compaction, summarisation, a replaceable rule for when to act, and four strategies compared on one long task.Module 6
06bNotebook 6b: Sessions and long-term memorySessions stored as JSON files that survive a restart, a paused tool call resumed in a new process, and long-term memory: structured extraction, de-duplication, a vector store per user, and memories injected into a later conversation.Module 6
07Notebook 7: Planning and reflectionOne multi-part expense question with a failing rate service, given to a ReAct agent, an agent with a plan tool, one with a reflection tool and one with both: what each tool adds to the context, what it fixes, and what it costs in steps and tokens.Module 7
08aNotebook 8a: CodeAct and a local sandboxBuild a layered sandbox for model-written code, wrap it as an execute_python tool, and compare CodeAct with JSON tool calls on the same task.Module 8
08bNotebook 8b: Workspaces and skillsGive an agent a folder, a shell and four file tools with a path check, then teach it new tasks with SKILL.md folders that load only when needed.Module 8
09aNotebook 9a: Workflows, agents as tools, and handoffsSplit a help-desk task across several agents three ways (a fixed workflow, an orchestrator calling agents as tools, and handoffs) and measure what each agent sees.Module 9
09bNotebook 9b: Agents over the network with A2AServe an agent over a small, faithful subset of the Agent2Agent protocol (an agent card and JSON-RPC message/send on 127.0.0.1), call it by hand, keep a remote conversation going, and let a local agent use two remote agents as tools.Module 9
10aNotebook 10a: Tracing an agentSpans for every agent run, step, model call and tool call, with tokens, estimated cost and latency, a tree view, metrics, and a JSONL export.Module 10
10bNotebook 10b: Evals, graders and judgesA task suite with code-based graders, repeated trials scored with pass@k and pass^k, error analysis, a rubric judge checked against human labels, and a CI gate that blocks a regression.Module 10
11aNotebook 11a: The data flywheel and prompt optimisationTurn production traces and feedback into a labelled dataset, then improve a prompt automatically with bootstrapped few-shot demos, random search and a reflective instruction search, scored on a held-out test set.Module 11
11bNotebook 11b: GRPO on a toy tool routerWrite REINFORCE, a baseline, GRPO’s group-relative advantages, ratio clipping and a KL penalty in numpy, train a tool router with them, then fit a Bradley-Terry reward model to biased preferences and watch the policy exploit it.Module 11
12Notebook 12: Prompt injection and guardrailsWatch a browsing agent leak a secret because a web page told it to, then stop it with layers that do not depend on the model: a permission policy, an egress allowlist, the quarantine pattern and taint tracking, and see why a heuristic detector is not enough.Module 12
13Notebook 13: A mini coding agentBuild a bash-only coding agent that fixes a small repository until its tests pass, and engineer the harness around it: the tool’s output format, truncation, a step budget, a progress file that survives a context reset, and a sandbox.Module 13