How to Stop Agent Loops Safely
Safe loops define hard stop rules before execution: max iterations, budget caps, repeated failure limits, and human approval gates.
The matrix below was checked against the PR Review and Bug Fix presets so every exit produces a bounded next step.
Which terminal states stop a loop safely when success is absent, delayed, or impossible under the current conditions?
What we tested
We added success, three-attempt, repeated-identical-failure, budget, and human-approval stops to one CI artifact, then modeled the same budget with one, three, five, and ten maximum iterations while all other assumptions stayed fixed.
Observed result
The artifact had several legitimate exits instead of one success-only exit. The example monthly estimate scaled from $2.376 to $23.76 across the iteration range, showing why a forgotten retry path can multiply cost even before side effects are considered.
What this does not prove
The cost inputs are illustrative and the experiment does not measure whether more iterations improve success. The infinite-loop study cited below is a new preprint; its repository sample and static-analysis findings should inform risk review, not be generalized to every agent system.
Why it matters
An agent that never stops can burn budget, thrash on the same error, or take an irreversible action. Stop conditions are not an afterthought — they are part of the loop design, decided before the agent starts.
Practical checklist
- Set a maximum iteration count
- Set a budget or token cap
- Stop after N repeated failures, not just on success
- Require human approval before merge, deploy, delete, purchase, or external communication
- Define what to report when stopping
Example
Stop rule: halt when validation passes, after 5 failed iterations, or when the per-run budget cap is hit — and always stop for human approval before merging.
Common failure modes
Practical evidence
Stop-condition matrix
Stop when named validation passes and the independent checker finds no unresolved blocker.
Stop after three identical failures without new evidence; do not pay for the same attempt again.
Stop at the iteration, token, time, or money cap even when the agent wants one more try.
Stop when required context, permissions, or a success criterion cannot be established.
Stop before merge, deploy, delete, purchase, or external communication and require a human decision.