Bug: Sub-agent stuck in infinite loop, consumed ~27M tokens

Resolved 💬 3 comments Opened Dec 31, 2025 by sanimexsa Closed Feb 15, 2026

Description

A sub-agent (Task tool with subagent_type=general-purpose) got stuck in an infinite loop, repeatedly executing the same failing command approximately 300+ times until it timed out after ~4.6 hours.

What happened

  1. Launched a background sub-agent to execute multiple tasks (install npm package, create test files, etc.)
  2. The agent attempted to run npm install -D @vitest/coverage-v8
  3. The command likely timed out or failed silently
  4. Instead of stopping after 2-3 retries, the agent kept retrying the exact same command 300+ times
  5. Agent finally timed out after consuming ~27 million tokens

Evidence

From the agent output:

[Tool: Bash] {"command":"npm install -D @vitest/coverage-v8","description":"Install vitest coverage dependency","timeout":180000}
[Tool: Bash] {"command":"npm install -D @vitest/coverage-v8","description":"Install vitest coverage dependency","timeout":180000}
[Tool: Bash] {"command":"npm install -D @vitest/coverage-v8","description":"Install vitest coverage dependency","timeout":180000}
... (repeated 300+ times)

--- RESULT ---
Request timed out

Expected behavior

  • Agent should detect repeated failures of the same command
  • After 2-3 failed attempts, agent should:
  • Try an alternative approach, OR
  • Skip the task and continue with others, OR
  • Report the failure and stop

Actual behavior

  • Agent retried the same command indefinitely
  • No circuit breaker or retry limit
  • Consumed ~27M tokens before timing out

Environment

  • Claude Code CLI
  • Model: claude-opus-4-5-20250101
  • Date: 2025-12-31
  • Platform: macOS Darwin 25.1.0

Impact

  • ~27 million tokens consumed unnecessarily
  • ~4.6 hours of wasted compute time
  • User billed for tokens that provided no value

Suggested fix

Sub-agents should have built-in retry limits:

  1. Maximum 3 retries for any single command
  2. Detect identical consecutive commands and break the loop
  3. Implement exponential backoff or alternative strategies on failure

Request

Please investigate whether token reimbursement is possible for this incident, given the tokens were consumed by a bug in agent behavior rather than productive work.

View original on GitHub ↗

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