AI Agents
Build the agent loop in twenty lines, then follow it through real work: filling its context, planning, recovering from errors, keeping memory and reaching tools over a shared protocol. You read traces of agents that failed, find the step where it went wrong, and learn the guardrails and tests that make one safe to leave running.
- Advanced
- 4 units, 12 lessons, 145 exercises
- Text, JSON, Python
- Builds on Building with LLMs
Unit 01 · 3 lessons
The agent loop
See an agent as a short loop around a model, and read what it did
- 01An agent is a loop
Call the model, run its tool, append, repeat until it answers
- 02Context is working memory
The window fills as the agent works; clip, filter and compact
- BOSSRead a trace
Walk a failed run step by step and find where it went wrong
Read the unit guide
An agent is a model in a loop. The program calls the model; if the reply asks for a tool, the program runs it, appends the request and the result to the message list, and calls the model again. When the reply is an answer instead, the loop ends. The model decides, the program acts, and that is the whole machine.
Each model call can only ask for the next action and cannot see its result until the following call. So a task that needs three tool calls takes at least four model calls, and every one of them re-sends the entire log. The cost of a run grows much faster than its number of steps.
A loop needs more than one way out. The answer is the happy exit; a step limit, a token or time budget and a check for repeated calls are the others. When a limit ends the run, the program should say so plainly instead of inventing an answer.
The context window is the agent's working memory: instructions, tool definitions, the task and every request and result so far. It fills as the agent works. Large tool results should be clipped or filtered at the tool, and old steps can be compacted into a summary. Compaction buys room and costs detail, so a good summary keeps decisions, dead ends and names, and anything exact must be read again.
A trace is the full list of replies and results in order. To debug an agent, walk the trace and ask at each step what the model knew, what it asked for and what came back. The final answer is a claim; the tool results are the evidence. Most failures are visible several steps before the end: a vague tool description, an error that was ignored, the same call made twice, or an answer given without a check.
Unit 02 · 3 lessons
Plans and recovery
Keep an agent on course through long work, errors and context resets
- 01Plan, then act
A todo list in the window keeps a long task on course
- 02Errors and recovery
Retry, change course or ask, and check before claiming
- BOSSLonger than the context
Finish a 200-file job across many windows without losing your place
Read the unit guide
A plan is a todo list the agent writes into its own context and keeps up to date: what is done, what it is doing now, what is left. It works because the model re-reads it on every call, so after thirty steps of tool output the list still says what the job is. Plans pay off when a task has many steps, when steps depend on each other, or when it will outlast the window. For a one-step job a plan is overhead.
A plan is a guess about work the agent has not seen yet. Thoughts and actions come from the model; observations come from the world, and only they carry new facts. When an observation contradicts the plan, the plan changes. Put irreversible steps last, after the check that makes them safe.
Failures come in three kinds. A timeout or a rate limit is about timing: retry, perhaps after a wait. A wrong name or a bad argument is about the call: read the message and change it, because the same call brings the same error. A missing permission, credential or decision is something only a person can supply: stop and say what is blocked. Three attempts with an unchanged error mean the agent is fixing the wrong thing.
A model saying "that should work" is a prediction. Evidence is a tool result that could have proved it wrong: the failing test now passes, the file read back holds the change. Check before claiming, and never weaken the check to get a pass.
Work longer than the window needs state outside it. A progress file holds the goal, what is done, what comes next and what was learned. A fresh session reads it, checks it against the real state, and carries on. Record progress after the work is done and checked: recording first can lose work silently, while doing first can only repeat it, which is harmless when steps are safe to repeat. Before saying "done", check the goal itself, not the note about it.
Unit 03 · 3 lessons
Memory and tools
Decide what an agent keeps between sessions and what it can reach
- 01Memory
Write now, read later, and distrust notes that have aged
- 02The Model Context Protocol
One protocol between AI apps and tools: hosts, servers and JSON-RPC
- BOSSWire up an agent
Pick tools, read a schema, load on demand and set permissions
Read the unit guide
An agent has two memories. The window is short-term: everything in it is in view, and it is gone when the session ends or is compacted. Long-term memory is anything stored outside, such as a notes file, a database or a vector index. It survives, but the agent only knows what gets loaded back in. Memory is two actions, writing now and reading later, and both have to happen.
Save what is hard to rediscover, stays true and changes what the agent does next time: a correction, a preference, a trap in the build. Do not save trivia, things one tool call can look up, or secrets. A note records what was true when it was written, so date notes, check the cheap things before relying on them, and fix or delete a wrong note instead of piling a correction beside it. When memory and the current state disagree, the current state wins.
The Model Context Protocol (MCP) is a shared protocol between AI applications and the things they connect to, so that each side is built once. The host is the AI application; it creates one client per server, and each client talks to exactly one server. Servers offer tools (the model decides to call them), resources (data the application attaches as context) and prompts (templates the user picks).
MCP messages are JSON-RPC. A request has an id, a method such as tools/list or tools/call, and params; the response carries the same id and either a result or an error. A request the server cannot process gets a JSON-RPC error. A tool that ran and failed returns a normal result with isError set to true and a message the model can act on. Local servers usually speak over stdio, one message per line on stdin and stdout with logs on stderr; remote ones over HTTP.
Tool definitions are prompt text, sent on every call. Many tools cost tokens and make the model choose badly, so give an agent only what its job needs, or load definitions on demand through a small search tool. Set permissions per tool: reads run freely, changes ask first, and anything not listed is denied. Annotations on a tool are hints from its author, to be believed only for servers you trust.
Unit 04 · 3 lessons
Many agents, safely
Know when to split work among agents, how to fence them in and how to measure them
- 01Subagents
A lead, workers with their own windows, and what a handoff loses
- 02Guardrails
Prompts advise, the harness enforces: sandbox, permissions, approvals
- BOSSIs the agent any good?
Grade outcomes and paths, count the cost, and distrust one lucky run
Read the unit guide
A subagent is a second agent loop that the first one starts, the way a function is called. The lead splits the job, writes a brief for each part, and each worker runs in its own fresh context and sends back a short report. The gain is context and wall-clock time: the worker's forty steps of reading never enter the lead's window, and independent parts can run at once. The cost is tokens, since every agent pays its own overhead, and a lossy handoff in both directions.
A worker knows only its brief and what its own tools tell it. A good brief gives the goal, what is already ruled out, the constraints and the shape of the reply. A report is a claim from another model: a wrong one sounds as sure as a right one, so the lead checks before passing it on. Delegate work that is independent and easy to verify; keep tightly linked work in one context, and never let two workers write the same file at once.
Guardrails live in the harness, the program around the model. A rule in the prompt is advice the model usually follows; a rule in the code is enforced. A sandbox limits what the agent can reach, permissions limit what it may do, approvals put a person before anything that cannot be undone or that leaves the system, and logs record what happened.
Content an agent reads, such as a web page, an email or a tool result, can carry text that looks like an instruction, and a model cannot reliably tell data from orders. The dangerous mix is private data, untrusted content and a way to send data out. Take one away and an injected instruction has little to work with. Least privilege does most of the work: a tool the agent does not hold cannot be misused.
To know whether an agent is any good, grade the outcome with code that looks at the end state, then look at the trajectory: steps, cost, and anything that must never happen. Runs vary, so repeat each task and report a rate, and remember that a 20-task set moves 5 points per flip. Reliability multiplies across steps. Keep the checks out of the agent's reach, read the failed traces, and change one thing at a time.
Learn AI Agents on your phone.
Bitpath is an iOS app in development. Join the waitlist and you get one email when it is ready to try.