[BUG] Context limit reached - Autocompact spiral background agents return results simultaneously

Resolved 💬 4 comments Opened Feb 16, 2026 by tanguc Closed Mar 20, 2026

Preflight Checklist

  • [x] I have searched existing issues and this hasn't been reported yet
  • [x] This is a single bug report (please file separate reports for different bugs)
  • [x] I am using the latest version of Claude Code

What's Wrong?

Claude Code enters an unrecoverable death spiral when multiple background agents (Task tool with run_in_background) complete and return results simultaneously. The returned results spike the main conversation's token count
past both the autocompact threshold (167K) AND the API's 200K hard limit in a single burst. The compact agent then fails because it also can't process a 200K+ conversation, creating an infinite loop of failed compactions.
The session becomes permanently stuck -- it can neither compact nor continue. In my case, the session churned for 42 minutes doing nothing, then sat idle for hours while it was supposed to be working autonomously
overnight.

The core issue: background agent result injection is not gated by context budget. Token count jumped from 154K to 188K in 13 seconds when agents returned simultaneously, skipping right past the 167K autocompact threshold
and 180K effective window.

What Should Happen?

  1. Autocompact should trigger proactively when background agents are pending, not reactively after results have already been injected
  2. Background agent result injection should be gated: if injecting a result would push past the compact threshold, compact first, then inject
  3. The compact agent should have a fallback for oversized conversations (e.g., drop/summarize oldest messages first) rather than failing with a 400 error
  4. After 2-3 failed compaction attempts, Claude Code should exit gracefully with a session handoff file rather than churning for 42 minutes
  5. The session should NOT silently die and sit idle -- it should at minimum persist its state to disk so progress isn't lost

Error Messages/Logs

2026-02-16T00:05:04.825Z [DEBUG] autocompact: tokens=188318 threshold=167000 effectiveWindow=180000

  2026-02-16T00:05:05.542Z [ERROR] API error (attempt 1/11): 400 400 {"type":"error","error":{"type":"invalid_request_error","message":"prompt is too long: 208666 tokens > 200000 maximum"}}

  2026-02-16T00:05:05.548Z [ERROR] Error: Conversation too long. Press esc twice to go up a few messages and try again.
      at bET (/$bunfs/root/claude:3159:939)
      at async QyB (/$bunfs/root/claude:3244:2545)

  2026-02-16T00:05:21.138Z [ERROR] API error: prompt is too long: 229367 tokens > 200000 maximum
  2026-02-16T00:05:21.300Z [ERROR] Error: Conversation too long.

  2026-02-16T00:06:14.364Z [ERROR] Error: Bad substitution: type.toString
      at h (/$bunfs/root/claude:62:21198)

  2026-02-16T00:13:41.793Z [ERROR] API error: prompt is too long: 230621 tokens > 200000 maximum
  2026-02-16T00:13:41.800Z [ERROR] Error: Conversation too long.
  2026-02-16T00:13:42.329Z [ERROR] API error: prompt is too long: 229476 tokens > 200000 maximum

  Full death spiral token progression:
  00:04:51  154,974 tokens  -- agents still returning
  00:05:04  188,318 tokens  -- SPIKE, compact triggered
  00:05:05  compact FAILS   -- 208,666 > 200K limit
  00:05:06  compact FAILS   -- 207,517 > 200K limit
  00:05:20  179,858 tokens  -- partial recovery after esc
  00:05:21  compact FAILS   -- 229,367 > 200K limit
  00:05:21  compact FAILS   -- 228,222 > 200K limit
  00:08:11   54,346 tokens  -- briefly recovered
  00:13:41  180,698 tokens  -- agents STILL returning, blows up again
  00:13:41  compact FAILS   -- 230,621 > 200K limit
  00:13:42  compact FAILS   -- 229,476 > 200K limit
  00:13:42  SESSION DEAD    -- caffeinate stopped, idle forever

Steps to Reproduce

  1. Create a session with a substantial system prompt (~10K+ tokens from CLAUDE.md, memory files, etc.)
  2. In a single turn, spawn 10+ background agents using the Task tool with run_in_background: true, each performing research tasks that return 5-15K tokens of results (e.g., file reads, grep results, web searches)
  3. Let all agents run concurrently until they start completing within seconds of each other
  4. Observe: autocompact token count jumps from ~90K to 188K+ in a single check as agent results are injected
  5. Observe: compact agent spawns but fails with prompt is too long: 208666 tokens > 200000 maximum
  6. Observe: session enters death spiral -- repeated failed compaction attempts, "Conversation too long" errors
  7. Session eventually stops responding. Shows "Context limit reached" to user. No work is saved.

Claude Model

Sonnet (default)

Is this a regression?

I don't know

Last Working Version

_No response_

Claude Code Version

2.1.42

Platform

Anthropic API

Operating System

macOS

Terminal/Shell

iTerm2

Additional Information

Session details:

  • Session ID: 9d0399a8-a080-435e-8972-98a30bbf135a
  • Debug log: 23,682 lines
  • 30 subagent files (15 research agents + 17 compaction attempts)
  • Session started 23:22 UTC, died at 00:13 UTC (~51 min runtime)
  • Churned for 42 min 33 sec before dying (visible in UI as "Churned for 42m 33s")
  • After death, process sat idle for 7+ hours (MCP cloudflare keepalive disconnects every 5 min are the only log activity from 00:17 onward)

The autocompact configuration during the session:

  • threshold=167000 (92.7% of effective window)
  • effectiveWindow=180000
  • API hard limit: 200,000 tokens
  • Buffer between threshold and API limit: only 33K tokens -- easily exceeded by a burst of agent results

Suggested fix priorities:

  1. Critical: Gate background agent result injection on context budget
  2. High: Compact agent fallback for oversized conversations (chunked summarization)
  3. High: Graceful exit after N failed compaction attempts (write session state, notify user)
  4. Medium: Lower autocompact threshold when background agents are pending (e.g., 75% instead of 92.7%)

View original on GitHub ↗

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