Stop conditions and the budget
A loop needs three exits: a stop written as evidence, a hard cap for when the evidence never comes, and a rule for repeated failure. The cost numbers from real runs show why the caps are not optional.
Three exits
A loop with one exit is a loop that will, sooner or later, run forever. The research on loop design agrees on three.
- Stop on evidence. The condition names something observable: the tests pass, the score is at least 90, the verifier approves.
- A hard cap. A limit on turns, time, tokens or cost, for the day the evidence never arrives.
- Stop on repeated failure. If the same check fails twice for the same reason, the loop stops and explains instead of trying a third time.
Marek Kaput adds a fourth for unattended loops: surprises escalate to a human. An unrelated file changing, or a verifier that cannot run, is not something the loop should decide about alone.
Writing the condition as evidence
Compare two stop conditions.
- "Stop when it looks good."
- "Stop when
npm testexits with code 0 and the Lighthouse score is at least 90."
The first is the agent's opinion about its own work, and models are generous about that. The second is something a program can check. Kaput's rule is blunt: stop on specific evidence, never "until it looks good".
The Claude Code team's example shows both halves in one line: a Lighthouse score at least 90, capped at 5 tries. The evidence ends the happy path. The cap ends the others.
$ claude> Make the dashboard pass the audit. Stop when the score is 90 or more, or after 5 tries, or if the same failure repeats twice.✻ Try 1: score 71, change image sizes✻ Try 2: score 84, defer a script✻ Try 3: score 84, same failing audit item✻ Try 4: same failing item again for the same reason# Stops and explains, instead of trying a fifth time.
Which stop condition is written as evidence?
Why the cap is not optional
The cap protects you from a loop that is working perfectly on a task that cannot be won. These are the cost numbers from the research, with what each one measured.
- The C compiler run. About 2,000 sessions, about 2 billion input and 140 million output tokens, just under 20,000 USD. The result was about 100,000 lines of Rust that builds Linux 6.9 on x86, ARM and RISC-V. Money well spent, but only because it was planned.
- The retro game maker. Alone, the agent took 20 minutes and 9 USD and produced something broken. The full harness took 6 hours and 200 USD and produced something playable. That is more than twenty times the cost.
- A music workstation run with a newer model and a simpler harness: about 3 hours 50 minutes and 124.70 USD.
- Multi-agent research. About 4 times the tokens of a chat for an agent, and about 15 times for a multi-agent system.
None of these is a warning against loops. They are scale references so that your cap is a number you chose, not a surprise.
Keeping the bill honest
The Claude Code team's cost advice fits on a short list.
- Use the simplest primitive and a cheaper model when possible. Model and effort are the largest levers.
- Pilot big runs small first, because dynamic workflows can spawn hundreds of agents.
- Put deterministic steps in scripts instead of paying a model to do them.
- Match a routine's interval to how often the watched thing changes. A check every minute on something that changes daily is waste.
- Watch
/usage,/goaland/workflows.
Sub-agents multiply tokens, so Addy Osmani suggests them only where a second opinion pays.
The same check fails twice for the same reason. What should the loop do?
- Designing Loops (Marek Kaput, Software Mansion)
- Loop engineering: Getting started with loops (Delba de Oliveira, Michael Segner)
- Building a C compiler with a team of parallel Claudes (Nicholas Carlini)
- Harness design for long-running application development (Prithvi Rajasekaran)
- How we built our multi-agent research system (Hadfield et al.)
- Run prompts on a schedule (Claude Code docs)
Chapter complete
You've finished chapter 5 of 6 in Loop engineering.
A loop that runs for hours forgets everything between sessions, so its memory has to live in files. Progress logs, a feature list the agent may only flip, a startup routine, and the habit of retesting each part when the model changes.
Next chapter