Skip to content
Guides/Safety

How to Stop Agent Loops Safely

Short answer

Safe loops define hard stop rules before execution: max iterations, budget caps, repeated failure limits, and human approval gates.

Reviewed 2026-07-14
First-hand site test
Stop rules compared with generated workflows

The matrix below was checked against the PR Review and Bug Fix presets so every exit produces a bounded next step.

Site evidence · maintained by TianMingAI · reviewed 2026-07-21

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

Only a success stop condition, none for failure
No budget cap on a scheduled loop
Irreversible actions taken without approval

Practical evidence

Stop-condition matrix

Success

Stop when named validation passes and the independent checker finds no unresolved blocker.

Repeated failure

Stop after three identical failures without new evidence; do not pay for the same attempt again.

Budget

Stop at the iteration, token, time, or money cap even when the agent wants one more try.

Uncertainty

Stop when required context, permissions, or a success criterion cannot be established.

Irreversible action

Stop before merge, deploy, delete, purchase, or external communication and require a human decision.

Related templates

Sources & further reading