Coding agents are where agents first became everyday tools, and they use nearly everything in this course at once. The loop is the one from Module 4. The single tool is a shell, the most general tool there is. Every command is code execution, so it needs the sandbox of Module 8. Test output and file contents fill the context fast, which is the problem of Module 6. The test suite is a verifier, the kind of grader Module 10 prefers and the kind of reward Module 11 trains on. And an agent that runs arbitrary commands is exactly the agent Module 12 warns about. This capstone builds a small one and spends most of its effort on the part that is easy to overlook: the code around the model.

Notebooks

Notebook 13: A mini coding agent copies a toy repository with three bugs into a sandbox, gives an agent one bash tool, and runs it until the tests pass. It then breaks the harness on purpose (truncating the wrong end of the output), stops the agent with a step budget and lets a fresh agent resume from a progress file, and probes what the sandbox blocks.

A loop with a shell

Module 1 defined the harness (Definition 1.3) as everything around the model that turns it into a working agent. For a coding agent its parts are concrete: the tools and the format of what they return, how much of that output the model is shown, the limits and budgets on the loop, the state kept outside the context, and the sandbox the commands run in. The CS329Z guest lecture on Terminal-Bench puts the case for a shell in one slide: bash is the one tool that covers the rest. Reading a file is cat, searching is grep, editing is a heredoc or a short script, and running the tests is pytest. The notebook’s agent has a single tool, bash(command), whose whole definition is one string parameter and the sentence “Run a bash command in the repository root. Returns the exit code and output.” Everything else the model learns about the tool, it learns from what comes back.

agent harness decides model repo, no network sandbox status, scrub, truncate observation bash(command) raw output what the model reads step budget: max_steps = 15 PROGRESS.md survives a reset

The repository is textkit: three small modules (word counts, statistics, URL slugs) with eleven tests, three of them failing. A slug keeps hyphens at its ends, the median of an even number of values returns one middle value instead of their average, and the word count splits on single spaces, so a double space counts as an extra word.

The model is not a language model. As everywhere in the course, the notebook runs offline, and here the “model” is a scripted fixer that knows the three bugs by name. It follows the routine a real coding agent follows (read the progress file, run the tests, open the file of the first failure, check that the code it expects is there, patch, run the tests again), and it acts only on what the harness shows it. That makes the harness experiments below real, but it cannot show a model reasoning its way to an unfamiliar bug; for that, point the notebook at a live model. With the default harness the run takes 12 steps: read the (missing) progress file, then test, read, patch three times, a final test run and the answer. All 11 tests pass, and the diff is three one-line changes.

The observation is the interface

Yang et al. call the commands an agent can run and the feedback they produce the agent-computer interface, and show with SWE-agent that designing it well changes how often the same model solves real GitHub issues, just as a user interface changes how well people work. The notebook’s format_observation makes four choices. The status line comes first (“exit code 1”, or “killed: wall-clock time limit”), because it is the one fact the model must not miss. Standard output comes next, then standard error under a marker. Anything that changes between identical runs is scrubbed: the repository’s absolute path, temporary directories, timings such as “in 0.03s”. That makes the observation deterministic, which keeps tests reproducible and lets a provider’s prompt cache reuse earlier turns. Finally the output is truncated to a budget of characters.

Truncation is not optional, because every observation stays in the context and every later model call reads it again. If the system prompt and task take tokens, each command at most and each truncated observation at most , then after steps the context holds

tokens, and a run of steps reads, over all its model calls, at most

With , , (about 2,000 characters) and , equation caps the context at 9,200 tokens and equation caps the whole run at 66,000. Without a cap, one verbose test run of 50,000 characters would add about 12,500 tokens to every call after it.

Where to cut matters as much as how much. Pytest prints its verdict, the FAILED lines and the counts, at the end. The notebook’s default keeps the first third of the budget and the last two thirds. Cutting only the tail keeps the part the model needs least:

budget (characters)keepstepstest runsresult
2,000head and tail12411 passed
600head and tail12411 passed
600head only13511 passed

With head-only truncation the first test run showed none of the three failing test ids. The fixer noticed, re-ran the tests piped through tail, and kept doing so, at the price of one extra step; a model that did not notice would patch blind or give up.

Remark 13.1 (A pipe can hide a failure).

The workaround pytest ... | tail -n 8 reports exit code 0 even though three tests failed: a shell pipeline returns the status of its last command, here tail. A harness that trusted the status line would think the tests passed. Run commands with set -o pipefail, or have the harness run the tests itself and report the result separately.

The same pitfall (Remark 13.1) applies to graders: decide success from the verifier’s own result, not from whatever the agent printed last.

Budgets, and state that survives the context

Every agent needs a budget. A model that never declares itself done is a bill that never stops, and a loop that repeats a failing command is common. The notebook’s budget is max_steps, the number of model calls. Production agents also cap tokens, money and wall time. When the budget ends a run in the middle, or the context fills up, the next session starts with an empty context, and anything the last one learned is lost unless it was written down outside the model.

Definition 13.1 (Progress file).

A progress file is a file in the environment where the agent records what it has done and what comes next. A new session reads it first, so work can resume after a context reset, a crash or a step budget, without relying on the model’s memory.

The notebook’s system prompt asks for one line in PROGRESS.md per fix and tells the agent to read the file first. A first session with a budget of 8 steps fixed two bugs, wrote two lines and was stopped with 10 tests passing and 1 failing. A second, fresh agent read the two lines, ran the tests and fixed the last bug in 6 steps. In this toy the test suite alone would also have told it what was left. A progress file (Definition 13.1) earns its keep on longer tasks, where it records what the code does not show: an approach that was tried and abandoned, a test known to be flaky, the step that was about to be taken. Anthropic’s guidance for long-running coding agents builds the harness around such files: a first session writes a feature list and a progress log, and each later session reads them and the git history, works on one item, commits, and updates the log.

The sandbox and the verifier

The agent can type any command, so the sandbox, not the model, decides what a command can reach. Hur and Song give three reasons for a sandbox: security (secrets and system files out of reach), resource limits (a runaway loop is killed), and recoverability (a broken environment is thrown away and recreated). The notebook’s CodingHarness runs every command in a LocalSandbox rooted in a fresh copy of the repository, with a 10-second wall clock, 5 seconds of CPU, 512 MiB of memory and no network. Four probes check it: sleep 30 is killed at the time limit, writing to /etc fails with “Operation not permitted”, opening a socket fails with a PermissionError, and writing inside the repository works. The copy gives recoverability for free: the fixture stays broken for the next run. On macOS the isolation comes from sandbox-exec; on Linux use a container or a micro-VM, as production coding agents do.

The test suite is the verifier, and it raises the question Module 11 raised about rewards: what stops the agent from changing the verifier? Nothing in the notebook’s harness prevents it from editing tests/ until they pass.

Remark 13.2 (Keep the verifier out of reach).

An agent optimised, or simply instructed, to make tests pass can delete or weaken them. Benchmarks guard against this by keeping the grading tests hidden from the agent and running them in a separate environment after the agent has finished. A harness used for real work can check that test files are unchanged, or run a held-back set of tests.

This is not hypothetical. The CS329Z lecture on agent data collects cases from training: coding models that learned to exit early with status 0 so the test run looked green, and SWE-bench repositories whose git history still held the real fix, which an agent could simply look up. Later training setups wipe the history and restore the test files before grading. Both benchmarks the field uses follow that rule (Remark 13.2). SWE-bench (Jimenez et al.) takes real issues from Python projects on GitHub; the agent edits the repository, and hidden tests from the fix that was actually merged decide whether it resolved the issue. Terminal-Bench, presented in the CS329Z lecture, packages each task as an instruction, a Dockerfile for the environment, a reference solution and a test script, and its Harbor framework runs many such tasks in parallel sandboxes. The lecture’s advice on writing tasks applies to any suite: map each test to a requirement in the instruction, lock the agent’s environment down, and keep the verifier’s environment separate from it.

From the toy to a real coding agent

this notebooka production coding agent
a scripted fixer that knows three bugsa model reading unfamiliar code
one bash toolbash, often with file viewing and editing tools that show line numbers
a repository copy in a macOS sandboxa container per task, snapshot and reset
11 tests, about a second per runminutes per run, so choosing which tests to run matters
PROGRESS.mdprogress notes, a feature list and git commits
max_steps = 15budgets in steps, tokens, money and wall time
pass or fail on one toy repositorySWE-bench and Terminal-Bench: hundreds of tasks

The same loop, with the tests at the end, is also the environment used to train coding agents with reinforcement learning: the reward is whether the hidden tests pass. In this course you have now built each piece of that loop by hand, from the model call in Module 2 to the verifier here.

Try this

  • Add a fourth bug to a copy of fixtures/coding/toy_repo without adding it to the fixer’s FIXES table. What does the scripted fixer do, and what would you want a real agent to do instead?
  • Use equation to compare and tokens for a 15-step run. Then rerun the notebook with max_chars=600. Is the saving worth the risk that a long traceback is cut?
  • Make CodingHarness.bash prefix every command with set -o pipefail, and check the workaround’s status line again.
  • Add a check to CodingHarness that refuses to report success if any file under tests/ differs from the original repository.
  • Replace the scripted fixer with a live model (set MODEL to a LiteLLM model string) and the toy repository with a small project of your own. Which harness setting matters most?

Readings