Context Limit Reached error should auto-compact and queue user message instead of failing
Resolved 💬 9 comments Opened Feb 11, 2026 by cosmicflow-space Closed Mar 19, 2026
Description
When running long sessions with multiple parallel background agents (Task tool with run_in_background: true), sending a user message (especially with a screenshot attachment) while agents are still running throws a "Context Limit Reached" error instead of gracefully handling it.
This is on a Max plan — not a billing or rate limit issue. The model's context window is exhausted by accumulated agent results.
Expected Behavior
- User sends a message while agents are running
- System detects context is near/at limit
- System auto-compacts (summarizes prior messages)
- User's message is queued and processed after compaction
- Session continues seamlessly
Actual Behavior
- User sends a message while agents are running
- System throws "Context Limit Reached" error
- User cannot send any further messages
/compactmay also fail with the same error- Only option is
/clear— which loses all conversation history, agent results, and work in progress
Reproduction Steps
- Start a Claude Code session (Opus 4.6, Max plan)
- Launch 4-6 parallel background agents using the Task tool (e.g., SEO audit with 6 specialist subagents)
- While agents are running and returning results, send a new user message with a screenshot attachment
- Observe "Context Limit Reached" error
Root Cause Analysis
From investigating a crashed session (46MB JSONL transcript):
- 274 user messages averaging 164KB each
- 12 messages over 1MB, 39 over 100KB
- 97.5% of context came from subagent results returned as user messages
- The Task tool returns agent output as user-visible messages, which accumulate rapidly
Impact
- Loss of work: Hours of multi-agent analysis lost when
/clearis the only recovery - No recovery path:
/compactfails with the same error, creating a dead-end state - Session state destroyed: TaskList, agent results, conversation history — all gone
- Workaround burden on users: Must manually track when to
/compact, which defeats the purpose of autonomous agent workflows
Suggested Solutions
Priority 1: Graceful degradation
When context limit is approaching:
- Auto-trigger compaction before the hard limit is hit
- Queue the user's pending message
- Process the queued message after compaction succeeds
Priority 2: Pre-emptive compaction
- Add a threshold (e.g., 80% of context window) that triggers automatic compaction
- Or add a hook event like
PreContextLimitthat fires before the hard limit
Priority 3: Agent result management
- Task tool results from background agents should not expand into full user messages
- Consider: agent results as compressible system messages, or file-based result passing where agents write to disk and parent reads files
Current Workarounds
We've implemented partial mitigations via hooks and CLAUDE.md instructions:
SubagentStophook reminds to/compactafter agent workPreCompacthook preserves critical context- All custom skills use file-based result passing (agents write to files, return only brief summaries)
- But none of these prevent the hard crash when a user message arrives during heavy agent work
Environment
- Plan: Claude Max (Opus 4.6)
- Platform: macOS (Darwin 25.2.0)
- Claude Code version: Latest as of Feb 2026
- Model: claude-opus-4-6
This issue has 9 comments on GitHub. Read the full discussion on GitHub ↗