How to Stop a Coding Agent Stuck in a Tool-Call Loop

2026-12-05来源 dev.toEnglish

Originally published on ClueTrove.

Set your caps yourself, because a cap only ends a loop after it has already cost you. Most agent toolkits document limits on turns, steps or spend. Few document anything that detects the same tool call repeating.

Three tools do: OpenHands (stuck detection on by default), Gemini CLI (a loop-detection service in its open source) and Goose (a --max-tool-repetitions flag). A 9 October 2026 user report of a coding agent hammering tools shows why this matters.

The short answer

  • Caps exist: the major toolkits document limits on turns, steps or spend.
  • Repeat detection is rare: most of the SDKs we checked document no setting for it.
  • Defaults differ widely: no limit in the Claude Agent SDK, 10 turns in the OpenAI Agents SDK source, 500 LLM calls in Google ADK.
  • Time: no documented cap on the total run time, only per-call timeouts.

What was reported about the MiMo loop?

This is one user report, not a verdict. On 9 October 2026 a user named SirAG183 opened issue 105 in the XiaomiMiMo/MiMo repository. It describes “repetition and failure to converge” in the MiMo-V2.6 series, mostly the flash model. The calls went through Xiaomi’s open platform API and an agent harness named DeepSeek Harness.

These are the reporter’s own numbers:

Case What the reporter says
Site 1 1,199 tool calls (93% exact repeats) and 262 million tokens
another session 1,126 calls (84%) and 467 million tokens
control sessions made 21 to 36 calls
Site 2 6 of 27 subagents failed; 45% to 93% repetition; worst case 587 calls
Site 3 Three chain-of-thought loops in one session; a fourth was stopped manually

As of 15 October the issue is open with no labels or assignee. Its only comment is the reporter’s own, with evidence archives attached. There is no maintainer reply. It shows one user’s experience, not a failure rate.

The scale is the lesson. By the reporter’s figures, a normal control session made 21 to 36 calls, while a looping one made 1,199. A cap in the low hundreds would have stopped the second long before it reached that size.

How is Anthropic’s 9 October report different?

On the same day, 9 October, Anthropic published a report on unintended actions its models took during evaluations. It groups them into four kinds. Examples are working around a restriction to reach gated data and using URL-shortening services to get around a fetch tool’s URL-length limit.

It says most cases were persistence: working around a blocker instead of stopping. Anthropic said it expanded its existing internet-access restriction to all its internal evaluations until monitoring is confirmed reliable. That applies to internal evaluations, not customer products. The report does not discuss loop limits or the MiMo issue.

It matters here only because “repeating without progress” and “persisting around an obstacle” call for different guards.

Which tools document caps on turns, steps and spend?

Tool Details
Claude Agent SDK Setting: maxTurns (TypeScript) / max_turns (Python) · Default: No limit (documented) · When hit: Result subtype error_max_turns
a single-shot query raises afterwards and the process exits nonzero
Claude Agent SDK Setting: maxBudgetUsd / max_budget_usd · Default: No limit · When hit: Result subtype error_max_budget_usd
a client-side estimate that can overshoot, so leave headroom
subagent spend counts
spend can pass the cap by up to the cost of one response, plus what running subagents spend before they stop
Claude Code CLI Setting: --max-turns, --max-budget-usd; env var CLAUDE_CODE_MAX_TURNS · Default: No limit · When hit: Print mode only for the flags; exits with an error
OpenAI Agents SDK (Python) Setting: max_turns · Default: 10 (DEFAULT_MAX_TURNS in the source file
the docs page does not state the number) · When hit: Raises MaxTurnsExceeded
an error handler can return a fallback output
None disables the limit
Google ADK Setting: max_llm_calls in RunConfig · Default: 500 LLM calls per run
zero or negative means unlimited and is not recommended for production · When hit: The page lists no end-of-run behavior
LangGraph Setting: recursion_limit · Default: Sources disagree. The concept page says 1000 steps since version 1.0.6
the GitHub source sets 10000 in tags 1.0.6 and 1.1.0 and 10007 in 1.2.0 and later
the errors page names no number. We suggest setting it yourself · When hit: Raises GraphRecursionError
Goose Setting: --max-turns on goose run · Default: No default listed · When hit: Limits turns without asking the user to continue (from a man-page mirror, not Goose’s own docs)

In the Claude Agent SDK, count each turn with tool calls, plus one for the final text-only answer. Without that extra turn, the run ends with error_max_turns and no final response. A max_turns of 2 would stop before the edit step in the docs’ own example. Units also differ across tools: turns, LLM calls, graph steps and dollars. A cap of 50 in one tool is not the same as 50 in another.

Which tools detect repeated calls?

Tool Details
OpenHands Software Agent SDK What it detects: A stuck_detection parameter on Conversation, on by default. Five patterns: same action and observation 4 or more times
same action erroring 3 or more times
3 or more consecutive agent messages with no user input
two action-observation pairs alternating for 6 or more cycles
repeated context-window errors · Notes: Docs say it can halt execution automatically when stuck patterns appear. Thresholds are hardcoded, per a GitHub issue (762) asking for them to be configurable
its status was not checked. That disabling it is stuck_detection=False is inferred from the name, not stated
Gemini CLI (open source) What it detects: The file loopDetectionService.ts has three loop types: identical consecutive tool calls (hashed by name and arguments
a cycle of 1 to 5 calls repeated 5 times), repeated content, and a second-model check after 30 turns · Notes: Constants from the code: TOOL_CALL_LOOP_THRESHOLD = 5, CONTENT_LOOP_THRESHOLD = 10, LLM_CHECK_AFTER_TURNS = 30. The file logs an event and returns the result
the caller’s reaction is outside that file, and no user-facing setting name appears there
Goose What it detects: --max-tool-repetitions: how many times the same tool may be called consecutively with identical parameters · Notes: No default listed; source is a man-page mirror

No documented repeat detection turned up for Claude Code or Codex CLI. Only part of Codex’s configuration reference was read. No official docs state it for Cline, Roo Code or Aider either.

Is there a limit on total run time?

No documented limit on the total wall-clock time of a run appears in these tools. What exists are partial timeouts:

  • Claude Agent SDK: abortController for cancellation, API_TIMEOUT_MS (600000 by default) and CLAUDE_STREAM_IDLE_TIMEOUT_MS (300000).
  • Claude Code: BASH_DEFAULT_TIMEOUT_MS (120000).
  • Codex: tool_timeout_sec for MCP servers (60 seconds).
  • OpenAI Agents SDK: ModelSettings.timeout, which raises ModelTimeoutError, with no default stated.

A loop of fast calls never trips a per-call timeout. Only a turn, step or spend cap, or a repeat detector, ends it.

Who needs what?

  • Interactive use with you watching: you are the loop detector. A turn cap is a backup.
  • Unattended or overnight runs: set both a turn cap and a spend cap. Add your own repeat counter if the tool has none.
  • Teams running many subagents: give each one a smaller cap than the parent. In the MiMo report, 6 of 27 subagents failed.

What can you do to guard against a loop?

These are our recommendations, and not all are tested.

  1. Set turn and spend caps explicitly for every unattended run. Do not rely on “no limit”.
  2. Give subagents smaller caps where the tool allows it. The Claude Agent SDK has a per-agent maxTurns.
  3. If your tool lacks repeat detection, count identical calls yourself (same tool and arguments). Stop or hand off to a person at a threshold you choose. The 3 to 6 repeats used by OpenHands and Gemini CLI are a reference point, not a rule.
  4. Treat hitting a cap as a review event: log what ran and the last step instead of failing silently.
  5. Verify the real outcome, not just that the agent stopped: see the EBO study.

Questions and answers

Does max_turns stop a tool-call loop?

Yes, but only when the count is reached, not before. In the Claude Agent SDK the default is no limit, so you must set it. In the OpenAI Agents SDK it is 10, per the source code.

Is the MiMo issue proof that the model is broken?

No. It is one user’s report with their own figures and no maintainer reply.

Do these tools detect repeated identical calls?

Some do: OpenHands (on by default), Gemini CLI (in its source) and Goose (by flag). The other SDKs we checked document no repeat detection, so add your own counter.

Is the Claude budget cap exact?

No. It is a client-side estimate that can pass the value, so leave headroom.

What happens when the OpenAI Agents SDK hits its turn limit?

It raises MaxTurnsExceeded. You can register an error handler for max_turns that returns a fallback final output, and passing None disables the limit.

Terms in plain language

  • Turn: one round in which the agent acts and gets a result.
  • Tool call: a request from the agent to run a tool, such as reading a file.
  • Subagent: a helper agent started by the main agent.
  • Token: a small piece of text that a model reads or writes, and the unit of usage.
  • Harness: the program that runs the model and connects it to tools.

Related reading

Sources

Facts last checked against the linked pages on 15 October 2026. The ADK, LangGraph and OpenAI running-agents pages were re-opened on 29 October 2026. On 30 October 2026 we re-read the LangGraph pages and source, the ADK, OpenAI and OpenHands pages, the OpenAI run_config.py file and the Claude Agent SDK loop page. Setting names are as they appeared on the pages.

原文:https://www.a8q9.cn/en/article/how-to-stop-a-coding-agent-stuck-in-a-tool-call-loop-f3235d.html

相关文章