Build a coding agent that fixes its own mistakes
An AI agent is not a chatbot. It is a loop: a model that can call tools, run over and over until a job is done. In this mission you build a small coding agent in Python, hand it a failing test on a toy repository, and watch it read the code, write a fix, run the tests, read the failure, and try again until the tests pass or a step budget stops it. It runs entirely on your own machine against a local model through Ollama, so there is nothing to buy and nothing leaves your computer. You will see it fix a two-part bug it gets wrong on the first try, watch a weaker model get stuck and the step budget end the run, and build the safety rails that make an agent safe to run at all: a working directory it cannot escape, a step budget, and an approve-before-write switch.
- Ollama with a small, tool-capable model. Run AI on your PC installs Ollama in a few minutes. Then pull the model this mission uses:
ollama pull qwen2.5:3b(1.9GB). It runs on a normal laptop CPU; no GPU needed. - uv, the Python tool from Level 0. Install anything from GitHub, safely shows the one-line install. Check with
uv --version. - A little Python: functions, dicts and lists. You do not need to have built an agent before. Every file is complete and explained, and the whole agent is in the starter kit.
- A terminal you are comfortable in. The terminal survival kit covers the basics if they are new.
What an agent really is: a loop with tools
Pin down the one idea the hype hides before writing any code. An agent is a loop: a model decides, a tool acts, the result comes back, and round it goes until a stopping condition is met.
An agent is a program that gives a model a set of tools and runs it in a loop. The model looks at the situation, decides on one action, your program carries that action out, and the result is fed back so the model can decide again. That loop, observe, think, act, repeated until a goal is met or a limit is hit, is the whole idea. Everything else is detail.
Tool calling (also called function calling) is how the model takes an action in the real world. On its own a model only produces text. You give it a short menu of tools it is allowed to use, for example 'read a file' or 'run the tests', and the model replies with the name of a tool and the arguments to call it with. Your code runs the tool and hands back the result. The model never touches your files itself; it asks, and your code decides what to do.
For the agent you are about to build, one turn of the loop looks like this:
- Observe: the agent is told the task and which files are in the folder.
- Think: the model picks one tool: read a file, write a file, or run the tests.
- Act: your code runs that tool inside a sandbox folder and captures the result.
- Repeat: the result is fed back and the model decides again, until the tests pass or the step budget runs out.
The part people forget is the stopping condition. A loop with no way to stop is a program that burns time and money forever. This agent has two ways to stop: the tests pass (it won), or a step budget is used up (it gives up). You will build both, because the second one is what makes an agent safe to leave running.
TipThis is the same shape as the large coding agents. Claude Code, Cursor's agent and Aider are far bigger and cleverer, but underneath they are this loop: a model, a set of tools, and a condition that ends the run. Building the small version is the fastest way to understand the large ones.CarefulAn agent writes files and runs commands for real. Never point one at your home folder, your documents, or a project you care about, and never let one run arbitrary commands on a machine that matters. Everything in this mission stays inside one throwaway folder, and the agent is built so it cannot write outside it. Treat that confinement as the rule, not the exception.