Loop Engineering · ~7 min
A loop is not free iteration. It is a fixed setup cost plus per-turn waste — and it pays back only when four conditions all hold and the budget is bounded by one primitive that actually fires.
Every other lesson in this course asks how to build a loop. This one asks the upstream question: should you, and if so, what's the cheapest cap that still bites. A loop carries fixed costs — a verifier, persisted state, a skill capturing your conventions — and per-iteration waste a single prompt session never pays.
A loop earns its cost only when all four of these hold at once. Fail any one and the loop's marginal cost stays at or above the prompt-session cost forever — there is no iteration count that fixes it.
| Condition | What it means | If absent |
|---|---|---|
| Task cadence | The same task shape recurs ~weekly or better | Setup never amortizes; one prompt is cheaper |
| Objective verification | An external gate (test, build, typecheck, CI) grades "done" | The loop stops on vibes; maker grades own homework |
| Absorbable budget | You can spend on retries and re-reads without it changing behavior | Per-iteration waste dominates the savings |
| Real tooling | The agent has logs, a repro env, and runs what it writes | Loop iterates on a stale view; convergence is theater |
This sorts tasks cleanly. Good fits: CI failure triage, dependency bumps, repo-wide lint-and-fix, doc generation — all recur, all verify against a build or test the agent runs itself. No-go: architecture rewrites and auth/payment refactors (no automated verifier for "is this design right"), production deploys (the real environment is the verifier), and any solo, sub-weekly task where N is too small to amortize.
Tokens-spent is the wrong unit. Tasks-attempted is the wrong unit. The unit that captures both throughput and the reviewer time the loop actually consumes is cost per accepted, merged change.
A study of 567 Claude Code pull requests across 157 open-source projects measured the baseline: 83.8% of agent-assisted PRs are merged, but only 54.9% merge without further modification — roughly 45% of "successful" output still consumed reviewer time. Below ~55% no-modification acceptance, the loop is doing the review work it was supposed to remove.
The same metric catches the inverse trap: a smarter model with fewer turns at a higher per-token rate can be cheaper per accepted change than a cheap model burning 80 tool calls. Instrument cost-per-accepted-change weekly, and pull the loop if no-modification acceptance falls below that line.
Once the gate passes, bounding the loop is its own decision — and it's really two: which primitive caps the loop, and how budget is allocated across turns. Three primitives compete to fire first:
| Primitive | Right when… | Wrong when… |
|---|---|---|
| Iteration count | An external grader certifies correctness; you just need a runaway guard | No grader — a fixed turn count may fire mid-deliverable |
| Token / cost budget | Cost predictability matters more than completion | Single-session loops where context grows non-linearly |
| Wall-clock | Latency SLOs bind harder than spend; interactive agents | Background or batch work — it forces a premature stop |
Per-turn cost in a single-session loop is not uniform — each turn re-sends the full accumulated
history plus tool definitions, so the marginal cost of iteration N grows roughly with N. A 1,000-token-per-turn cap that
works at turn 1 is broken by turn 8. Enforce token budgets across the whole loop, and expose
(remaining_turns, remaining_tokens) to the controller so it can score the next action honestly.
Iteration caps are portable across model versions; token budgets need re-tuning on every model upgrade
(tokens-per-decision shifts between models). That's why Anthropic's managed outcome primitive ships
iteration-cap-only — max_iterations defaults to 3, caps at 20 — with the grader in a separate context.
With the cap chosen, how you spread compute across turns is the second lever. Front-loaded (the "reasoning sandwich") gives planning and verification extra-high compute and runs execution at high; even split gives every turn the same budget.
The reasoning sandwich is the only allocation with published numbers: extra-high planning / high execution / extra-high verification scored 66.5% on Terminal Bench 2.0, beating uniform-high (63.6%) and continuous-maximum (53.9%, penalized by timeouts). Planning errors propagate while execution errors stay local — so concentrating compute where ambiguity is highest beats spreading it uniformly.
The 66.5% vs 63.6% gap is small, and it requires planning, execution, and verification to be separable. Exploratory debugging interleaves all three — there front-loading degrades to noisy uniform compute with routing overhead. For execution-dominated work (bulk refactors, mechanical migrations, lint-and-fix passes), even split is the right default.
Retrieval practice — recall, don't peek
Question 1For a loop to earn its cost, how many of the four gate conditions must hold?
Question 2The operating metric for a loop is…
Question 3A flat per-turn token cap on a single-session loop is wrong because…
Question 4Front-loaded ("reasoning sandwich") allocation beats even split only when…
Question 5 · spaced recall from an earlier lessonFrom Termination & Convergence: you should stop a loop when…