[BUG] Multiple background agents completing simultaneously overflow main context — API returns empty response
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?
When multiple background agents (via Agent tool with run_in_background: true) complete near-simultaneously, their combined output is injected into the main conversation context in a single turn. This causes context window overflow (100%) before auto-compaction can trigger. The API then returns an empty/malformed response (HTTP 200 with no content), rendering the entire session unusable with no recovery except /clear.
There is no flow control between background agent completion and main context injection:
- No output size budget: Each agent's full result is injected without checking whether the main context has room
- 2. No serialization of agent results: Multiple agent completions in the same turn are batched, not queued
- 3. Auto-compaction can't intervene mid-turn: Compaction only runs between turns, so a single oversized turn bypasses it entirely
- 4. No truncation/summarization of agent output: Unlike
TaskOutput(which truncates to 30K chars), the Agent tool injects the full transcript
What Should Happen?
Before injecting agent results, the system should:
- Check context capacity — estimate if injection would overflow
- 2. Run auto-compaction first if near threshold — free up space before injection
- 3. Serialize concurrent completions — inject one agent result at a time with compaction checks between each
- 4. Truncate or summarize if still too large after compaction
Expected: Agent results should be injected safely without overflowing the context window, even when multiple agents complete simultaneously.
Error Messages/Logs
API Error: API returned an empty or malformed response (HTTP 200) — check for a proxy or gateway intercepting the request
Steps to Reproduce
- Start a Claude Code session with moderate context usage (~40-60%)
- 2. Launch 2+ background agents via Agent tool with
run_in_background: true, each returning substantial output (~50-100K tokens each) - 3. Wait for both agents to complete within a short time window
- 4. Both agent results are injected into the main context in the same turn
- 5. Context jumps from ~50% to 100% in one step
- 6. API returns empty response
- 7. Session is stuck — no recovery possible except
/clear(losing all context)
In my case: two background agents (Write 13 interview cards + Rewrite 12 interview cards) completed simultaneously, each consuming ~100K tokens. The main context was overflowed instantly.
Claude Model
Not sure / Multiple models
Is this a regression?
No, this never worked
Last Working Version
_No response_
Claude Code Version
2.1.177
Platform
Anthropic API
Operating System
macOS
Terminal/Shell
Terminal.app (macOS)
Additional Information
This is the convergence point of several related issues that were either closed or remain unresolved:
- #17208 — Proposed
output_modeparameter for Task tool, closed as "not planned" - - #52390 — AUTOCOMPACT_PCT_OVERRIDE not triggering, still open
- - - #53065 — advisor() inflating reported input tokens, still open
- - - - #34556 — Memory loss across compactions
The fundamental problem: background agent results have no flow control when entering the main context. Sub-agents are isolated during execution, but their outputs are injected atomically on completion. When multiple agents finish at the same time, the main thread receives all their outputs in a single turn, bypassing auto-compaction entirely.
Suggested fix priority:
- Short-term: Serialize agent result injection — inject one result at a time, run compaction checks between each
- 2. Medium-term: Add output size budget — estimate tokens before injection, compact first if needed
- 3. Long-term: Add
output_modeparameter to Agent tool (revisit #17208)
This issue has 7 comments on GitHub. Read the full discussion on GitHub ↗