Claude Code Loop Engineering
Build Self-Correcting Agentic Loops That Ship Trusted Code, Not a $200 Overnight Bill
You start an overnight loop and go to bed. You wake up to a $200 bill and code you still can't trust. Meanwhile builders are running nine-hour loops and waking up to clean, merged pull requests. The difference isn't a smarter model, it's a loop that knows when to stop, how to verify its own work, and when to quit before it empties your wallet. This is the complete loop-engineering system: build your first self-correcting loop in Chapter 4, add a metrics-driven exit condition and hard cap that end a runaway in 1-4 iterations, wire a deterministic verification gate so 'done' means tested, layer an adversarial Generator-Evaluator review, cap the spend with a budget gate and model router, and run a checkpointed loop overnight that survives a dropped connection. Stop babysitting your agent.
Every week more “agentic AI” books hit the Kindle Store, written by the very agents they claim to explain. This one is different: roughly 190 pages of dense, build-every-step content, every command run before it reached the page, in a human voice you’ll actually want to read. By Chapter 4 you have a working self-correcting loop. By Chapter 6 “done” means tested, not guessed. By Chapter 9 a hard budget cap kills the loop before $200 ever happens. By Chapter 10 it ships real work overnight and survives a dropped connection. Stop babysitting your agent.
What You'll Build
Get the mental model: why unbounded loops fail and bounded ones ship, and the four things every trustworthy loop has.
Untangle workflows, subagents, teams, /loop, and /goal on one anatomy diagram you can annotate.
Fill a lifecycle worksheet for your own task: start trigger, max-steps ceiling, stop criteria, and where tool calls get permissioned.
Build a loop that fixes a failing test and stops on green by itself, no human turn in between.
Add a metrics-driven exit and a hard cap so a runaway ends in 1-4 iterations instead of a $200 night.
Wire a test + typecheck + lint + build gate the loop must clear before it can claim success.
Add a separate evaluator that tries to break the answer and a judge that grades it against the original spec.
Apply a one-page rubric that sorts any task into single-call, loop, or fan-out before you spend a token.
Add a budget kill-switch and a cheap/expensive model router so the loop halts at a set dollar ceiling.
Schedule a checkpointed overnight loop that survives a dropped connection and ships a diff you'd actually merge.
Reproduce real failure modes (oscillation, hallucination-promotion, taste-arguing) and suppress each with a specific control.
Assemble the full bounded, verified, cost-capped loop into a reusable template you point at any new project.
Free Articles from this Book
How to Give an Agentic Loop an Exit Condition
An agentic loop exit condition has two halves: a measurable pass criterion and a hard cap. Wire both in Claude Code so a runaway loop stops in 1-4 turns.
from: Claude Code Loop Engineering
How to Cap a Claude Code Loop's Spend
Set a Claude Code budget limit per run so an unattended loop can't burn $200 overnight: a hard --max-budget-usd cap, a model router, and context trimming.
from: Claude Code Loop Engineering
/loop vs /goal in Claude Code: The Real Difference
Claude Code /loop vs /goal confuses everyone. /goal is the self-correcting loop that fixes code; /loop is a scheduler that polls. Here's each in its place.
from: Claude Code Loop Engineering
Build a Self-Correcting Loop in Claude Code
A Claude Code self-correcting loop fixes a failing test and stops on green by itself. Build one in 10 minutes with /goal, a turn cap, and the test as oracle.
from: Claude Code Loop Engineering
Agent Loop vs Single Prompt vs Fan-Out: When to Use Each
When to use an agent loop vs a single prompt vs fan-out comes down to task shape. A 3-question test routes any coding task to the cheapest tool that fits.
from: Claude Code Loop Engineering