Loop Engineering · ~8 min
Two dials decide a loop's character: how much context it carries between iterations, and how much rope you hand it. Set both on purpose, not by habit.
Last lesson you learned to name which loop you are in. This one is about the two choices that shape it: the context strategy (what carries forward) and the autonomy level (how the loop knows when to stop). Get these wrong and even a perfect inner loop rots.
There are three ways a loop can carry context between iterations, and the right one depends on the workload — not on what your harness happens to default to.
| Strategy | Best for | Primary risk |
|---|---|---|
| Accumulated context | Synthesis, cross-referencing, discovery | Context rot degrades reasoning |
| Within-session compression | Medium-horizon mixed tasks | Lossy compression, objective drift |
| Fresh context per iteration | Execution-heavy, unattended work | Fragmented research coherence |
The reason fresh context is not just "starting over" is context rot: reasoning quality degrades as the
window fills. Anthropic calls this a "performance gradient" that appears "across all models," and BABILong benchmarks show
reasoning tasks retain only 10–20% effective context at high fill. Karpathy's
autoresearch agent — an accumulated-context loop running ~12 experiments per hour — began producing
spurious correlations that needed manual correction as its sessions lengthened. That is context rot in the wild.
The strongest real systems are hybrid. Research then implement: accumulate context while exploring, write findings to durable artifacts, then spin up fresh-context sessions per implementation task. Anthropic's multi-agent research system does exactly this — accumulated context at the orchestrator level, fresh context at each worker.
The fresh-context extreme has a name: the Ralph Wiggum Loop, named and popularized by Geoff Huntley.
Each iteration opens a clean window, reads persistent state from disk, completes one bounded unit of work, writes results
back, and restarts. State lives in files — task lists, specs, AGENTS.md, progress markers — never in
conversation history.
Two properties fall out for free. Natural recovery: a failed iteration leaves disk state at the last successful write, so the next cycle continues cleanly without inheriting the failed session's misconceptions. Unattended operation: a wrapper script restarts the agent each cycle and a human reviews from time to time rather than supervising continuously.
The cheapness is real but bounded. A fifty-iteration loop on a medium codebase typically runs $50–$100+ in API credits, architectural coherence suffers (the code reflects the path to a working state, not intentional structure), and an agent facing an impossible task can overbake — iterate destructively for hours. An iteration cap alone is a financial circuit breaker, not a quality gate. Every loop still needs a non-convergence detector (Lesson 4).
Ralph is one autonomy extreme: cheap, external, brute-force. The other end keeps a single accumulating session but steers it. The goal-driven autonomous loop — shipped in OpenAI's Codex CLI and Anthropic's Claude Managed Agents — injects a continuation prompt at each turn end that re-states the objective, reports remaining budget, and demands a completion audit before the agent may declare done.
| Pattern | Context model | Stop condition |
|---|---|---|
| Ralph Wiggum loop | Fresh per cycle, state on disk | External: empty task list or iteration cap |
| Goal-driven loop | Single session, accumulating | Goal-complete tool call OR token budget |
The sharpest autonomy gotcha lives here: who audits? When the worker is also its own auditor — Codex's design — long transcripts produce false-positive completion, the documented LLM-as-judge self-enhancement bias. Anthropic runs the grader in a separate context window "to avoid being influenced by the main agent's implementation choices." A separate-context grader is a structural defense the same-session self-audit cannot match.
No continuation prompt rescues an objective with no testable success criteria — it just burns budget without converging. And the budget cap that fires at the end is deterministic, not diagnostic: 200K stops at 200K whether the artifact is done or half-done. Pair the cap with the convergence detection you'll build in Lesson 4.
Retrieval practice — recall, don't peek
Question 1Accumulated context is the right strategy when the workload is mainly…
Question 2The Ralph Wiggum loop avoids the "dumb zone" by…
Question 3An iteration or budget cap is best described as a…
Question 4The structural defense against a worker rationalizing "done" is…
Question 5 · spaced recall from an earlier lessonBefore debugging a misbehaving loop, the first move is to…
while-true.