Agent loop non-termination: model retries identical failing tool calls indefinitely

Resolved 💬 2 comments Opened Mar 2, 2026 by zarak Closed Mar 2, 2026

Bug Category

After classifying ~200 open bugs, agent loop non-termination is the #2 category (~8 issues). The pattern: the model calls a tool, the tool fails, and the model retries the exact same call — burning tokens without converging.

Representative issues

  • Explore agent infinite loop on oversized file (#30014) — model keeps trying to read a 41K-token file, gets the same MaxFileReadTokenExceededError each time
  • Model retries identical failing Edit tool call multiple times without diagnosing error (#29944)
  • Agent wasted tokens in Playwright SQL injection loop (#30016)
  • Excessive token usage: agent regenerated entire reports instead of surgical edits (#29742)
  • Orchestrator parallelises work that must be sequential (#29852)

Root cause

The generate loop has no convergence detection. It checks:

  • Step count < max rounds ✓
  • Token usage < budget ✓
  • Hook decision (BRStop) ✓

But it does NOT check:

  • Whether the last N tool calls were identical (repetition detection)
  • Whether the model is making progress (output changed vs. previous round)
  • Whether a tool has returned the same error K times in a row

Proposed mitigation

1. Tool-level retry detection in the loop:

If the same (tool_name, args_hash) appears in the last K rounds:
  → inject a system message: "You have called {tool} with the same arguments {K} times. 
    The result was: {error}. Try a different approach."
  → If K > threshold: BRStop with diagnostic

2. Progress metric:
Track delta between rounds — if assistant text + tool results are identical to previous round for N consecutive rounds, force-stop with a diagnostic message rather than burning the remaining budget.

3. Per-tool error budget:
After a tool returns the same error class M times, temporarily remove it from the available tool set for the remainder of the session, forcing the model to use alternatives.

Why this matters

Non-terminating loops are the highest-cost bug class — they silently burn through token budgets and produce no useful output. Users don't notice until they check their usage.

Related issues

#30014, #29944, #30016, #29742, #29852

View original on GitHub ↗

This issue has 2 comments on GitHub. Read the full discussion on GitHub ↗