When to reach for it
- The result can be iteratively improved.
- External validation triggers a retry.
- Tool results necessitate new iterations.
Repeat until good enough, or budget runs out.
One or more steps are repeated until a specific budget, quality bound, or exit condition is reached.
A body step produces output; a check step decides whether to exit or loop again.
| Round | Action | Failing tests | Exit check |
|---|---|---|---|
| 1 | Fixes a broken import | 7 | Continue |
| 2 | Fixes the date parsing | 3 | Continue |
| 3 | Changes the rounding | 3 | Continue, one round without progress |
| 4 | Changes the rounding again | 3 | Stop: no progress in two rounds |
def run_until_green(task, max_rounds=6, patience=2):
best, stale = float("inf"), 0
for _ in range(max_rounds): # exit 1: the budget
apply_fix(task)
failing = run_tests()
if failing == 0: # exit 2: the goal
return "done"
stale = stale + 1 if failing >= best else 0
best = min(best, failing)
if stale >= patience: # exit 3: no progress
return "stopped: no progress"
return "stopped: budget used up"When to reach for it
When it backfires
The check step always returns 'retry' because the quality threshold is set impossibly high.
Countermeasure
Combine quality gate with a hard iteration cap. Treat cap exhaustion as a normal outcome, not an error.
Each loop appends to the same context. A bad intermediate gets baked in and the model compounds it rather than fixing it.
Countermeasure
Keep only the latest iteration in context, or use a scratchpad that the model must explicitly update.
Search patterns, frameworks, and pages.