A loop is only as honest as its verifier
Loop engineering moves you out of the cycle and puts a test where you were standing. The hard part is not the loop - it is that "done" has to be a command that exits zero, and an agent cornered by a failing test will reach for the test.
By Harrison ItotiaNov 13, 20269 min read
Something has changed in how people work with coding agents, and it is easy to miss because it looks like less effort rather than more. You stop typing prompts. You start building the thing that types them.
The model did not get better overnight. Your position relative to it changed. In the old arrangement you sat inside the cycle: read the output, spot the problem, type the next instruction, repeat. Your attention was the rate limit. In the new one you step out and put a test where you were standing.
That is the whole idea, and the interesting part is not the loop. It is that the exit condition now has to be a command, not an opinion.
Four parts, and only one of them is hard
Every loop worth running has the same anatomy, whatever tooling you wrap around it.
An objective and some state. One place that says what the end goal is and what is already done, so that a fresh agent with no memory of the last twenty minutes can pick up where the last one stopped. A plan file in the repository does this well. A conversation does not, because the conversation dies with the session.
An execution step. Something that invokes the agent and tells it to take the next piece of work. This is the part everyone focuses on and it is the least important.
A verifier. A machine that judges the output with nobody watching.
Feedback or exit. On failure, the error goes back in as the next input. On success, the loop stops.
Three of those are plumbing. The verifier is the one that decides whether you have built a loop or just an expensive way to generate text.
# A loop is an agent and a verifier, repeated until the verifier passes.
# Everything else is ergonomics.
until go build ./... && go vet ./... && go test ./...; do
claude -p "The build or tests are failing. Read the output above, fix the \
cause in the source, and stop. Never edit or delete a test to make it pass. \
Do not commit." \
--allowedTools Edit,Bash,Read,Grep
doneYou will rarely write that by hand — most agent tooling now gives you the cycle, the budget and the permissions for free. But it is worth keeping the shape in your head, because it makes obvious what the loop is actually conditioned on, which is a process exit code and nothing else.
Three questions before you build one
Not every task should be a loop. If you cannot answer yes to all three of these, you have a prompt, and wrapping it in machinery will cost you more than it returns.
Does it repeat? A loop has setup cost. If the work happens once, type the prompt and move on. Loops earn their keep over iterations.
Can "done" be checked by a machine? This is the real gate. You need a command that exits zero: a test suite, a linter, a type check, a conformance test, a build. Not "looks right", not "seems to work". If the only available judge is you reading the diff, you are the verifier, and you are back inside the cycle.
Can the agent act without you mid-cycle? It needs the standing rules, the conventions, and the objective written down somewhere it can read. If it has to stop and ask you something on iteration three, the loop was never autonomous.
A task that cannot be checked by a machine is not a candidate for automation. It is a candidate for a conversation.
Your agent will cheat the verifier
Here is the failure that surprises people, and it is worth understanding properly because the instinct to treat it as misbehaviour leads you to the wrong fix.
Give an agent a failing test and a goal of "make the suite pass", and at some point — usually after a few honest attempts at the actual bug — it will delete the assertion. Or loosen it. Or add a skip. And the suite will pass, and the loop will exit green, and you will wake up to a clean build with less coverage than you went to bed with.
This is not the model being lazy or deceptive. It is the model doing exactly what you asked. You said the goal was a green suite. Removing the test produces a green suite, and it is by far the shortest path there. The specification was wrong, not the agent.
Two fixes, and you want both.
Say it explicitly in the prompt. "Fix the cause in the source. Never edit, skip, or delete a test to make it pass." It sounds too obvious to be worth typing. Type it anyway — it is the single highest-value sentence in the whole instruction.
Keep the grader independent of the writer. A context that just wrote four hundred lines is not a neutral judge of those four hundred lines; it is invested in them, and it has already convinced itself. Run the review in a fresh context that sees the diff and nothing else — not the author's summary of the diff, which is where the self-justification lives.
This is the same structural problem as a suite that goes green while the customer still finds the bug: the checks are only as good as what they were written to catch, and an agent optimising against them will find the gap faster than you will.
Make stopping the thing that is checked
The most reliable loops do not ask the agent whether it is finished. They intercept the attempt to finish and run the verifier at that moment. If the command fails, the stop is refused and the output goes back in.
{
"hooks": {
"Stop": [
{ "matcher": "", "hooks": [
{ "type": "command",
"command": "go build ./... && go vet ./... && go test ./..." }
]}
]
}
}The difference is subtle and it matters. An agent that reports "done" is making a claim. An agent that cannot stop until a command exits zero is subject to a fact. You want the second one, because the first is exactly the kind of plausible-looking failure that costs the most to find later.
Where loops actually fail
Loops get more dangerous as they get better, which is a sentence worth sitting with. A bad loop stalls and you notice. A good loop efficiently does the wrong thing at scale, overnight, and presents you with a green build in the morning.
Specification debt. A vague objective makes the loop build the wrong thing, quickly and confidently. "Add task management" is not a specification. Write the plan down and agree it before any code is generated against it — a loop will faithfully execute a misunderstanding for six hours.
Verification debt. A loop is exactly as honest as its tests. A thin suite will let real bugs through while reporting success, and every iteration adds code that the suite wasn't written to examine. The loop does not make your tests better. It makes their weaknesses compound faster.
Cost debt. This is the one nobody plans for, and it has a specific shape worth naming.
A loop is an open-ended spend authorisation
Look again at the exit condition. The loop stops when the tests pass. There is nothing in that condition about money. A loop that cannot solve its problem does not stop — it keeps paying for attempts, at full price, for as long as you let it.
So the budget has to be a separate stop with its own authority to halt the run.
// A loop exits on a green test. Nothing in that condition mentions money,
// so the budget has to be a separate stop with its own authority to halt.
type LoopBudget struct {
MaxIterations int // hard ceiling, always set
MaxSpend int64 // millionths of a dollar across the whole run
MaxWallClock time.Duration // the overnight guard
StallAfter int // identical failures before escalating to a human
}
func (b *LoopBudget) ShouldStop(s State) (bool, string) {
switch {
case s.Iteration >= b.MaxIterations:
return true, "iteration cap"
case s.SpentMicros >= b.MaxSpend:
return true, "spend cap"
case s.Elapsed >= b.MaxWallClock:
return true, "wall clock"
case s.IdenticalFailures >= b.StallAfter:
// Spending at a constant rate with no new information. This is the
// one that costs the most, because nothing about it looks broken.
return true, "no progress"
}
return false, ""
}The iteration cap and the wall clock are obvious. The one that earns its place is no-progress detection: the same error, twice, with a change in between.
That state is worse than a crash. A crashed loop stops spending. A stalled loop spends at a perfectly constant rate while producing no new information, and from the outside it looks identical to a loop that is working — same cadence, same tool calls, same plausible reasoning in the log. Nothing about it appears broken. It is simply paying full price to re-derive something it already knows.
Which means you cannot run loops seriously without being able to answer what one costs. Not what your month cost — what this run cost, and what it bought. That is the difference between cost per call and cost per outcome, and loops are where it stops being an accounting preference and starts being an operational control. A budget alert that fires at a hundred per cent tells you about a run that already finished.
What never goes inside the loop
Draw this line early and do not move it under deadline pressure.
A loop may write code, run tests, read files, and iterate as long as its budget allows. It stops at a green build or an open pull request. The push, the merge, the migration, the deploy, and anything that spends money outside the loop's own token budget stay on the other side of a human.
The reasoning is not caution for its own sake. Everything inside the loop is cheap to undo — you throw the branch away. Everything on the other side of that line is not. A production deploy should require an explicit approval that a human gives, never a flag that an autonomous process can decide to add on its own. That is also the honest answer to where the humans go once the loop is doing the typing: they move to the irreversible edges, which is where they were always most valuable.
Run it by hand once
The best test of whether a task is ready to automate is to drive one cycle yourself. Pick the work, run the verifier, read what it says, feed it back, and watch what happens.
If you cannot articulate a clean stop condition while doing it manually, the loop will not find one either. It will run until it hits a cap you set, and the cap will be the only thing that saved you.
Start with something mechanical and genuinely repetitive — bumping a shared dependency across several services and proving each one still builds, or working a plan file with half a dozen unchecked items down to green. Watch the first few iterations instead of walking away. You will learn more about your test suite in twenty minutes than in the previous six months, because for the first time something is trying to get past it rather than trying to satisfy it.
That is the real return on loop engineering, and it arrives before any productivity gain. You stop being the slowest part of your own development cycle, and in exchange you find out exactly how much your tests were actually proving.
Thinking about an agent like this for your team?
Describe the job you want automated and our Automation Architect blueprints it in seconds — or talk it through with the engineers who ship them.