Chapter 28: Coding Agents
Lecture
The narrated lecture for this chapter is coming soon on the YouTube channel.
Chapter principle
Key takeaways
- A coding agent is an ordinary agent loop over an executable environment.
- Select repository context; search before you edit; prefer patches.
- Tests are strong evidence, but not proof, and an agent can game them.
- Keep deciding tests out of its reach.
- Failures become observations; re-run the regression suite after every patch.
- Generated code is untrusted: sandbox it, and keep edit, test, push, and deploy as separate permissions.
- Repair loops need budgets, checkpoints, rollback, and no-progress detection.
Experiments
Every experiment in the book is small, runnable and printed with its real output. In the Lab you can replay a recorded real run of each one, with no programming, and watch what the model and the agent do.
Experiment 53: The smallest coding agent
What is the smallest agent that can fix a bug in code?
A tiny project has one bug: add(a, b) returns a − b, so its one test fails. A real model gets four tools: list the files, read a file, replace one exact piece of text in a file, and run the tests. It is told to make the tests pass by fixing the code, not the tests. The loop ends when the tests pass, not when the model says it is done. Afterwards the program checks that the file tools refuse a path outside the project folder. This is a recorded run of a real model. A new run may take different steps.
Experiment 54: Iterative repair
Can test failures guide an agent to working code?
A real model must write parse_price(text), which turns a price such as “$1,234.50” into a whole number of cents. The harness owns four tests the model never sees: a dollar amount, a plain number, a euro price with a decimal comma (“3,50 EUR”) and an empty string. After each attempt the whole test suite runs, and only the failures go back to the model. It gets at most four attempts. This is a recorded run of a real model. A new run may fail different cases, or none.
All the code for this chapter: github.com/agentic-engineering-book/code
Download the bookBrowse all chapters