Context & Compaction
Claude Code actively manages its context window. When tokens run low, it auto-compacts by summarizing history. Micro-compaction cleans old tool results inline throughout the session.
200K default context window 1M for Sonnet 4.x / Opus 4.6 ~13K tokens left → auto-compact 50K post-compact token budget
Context window sizes
| Context | Size | Available for |
|---|---|---|
| Default | 200,000 tokens | All users |
| 1M context | 1,000,000 tokens | Sonnet 4.x [1m] and Opus 4.6 [1m] |
| Override | Custom | Set via CLAUDE_CODE_MAX_CONTEXT_TOKENS |
Auto-compaction flow
When approaching the context limit, Claude automatically summarizes the conversation history to free up space.
1
~13,000 tokens remaining trigger
Compaction threshold reached. Auto-compact triggers automatically. 2
Fork agent to summarize
A background agent reads the full history and produces a compressed summary. 3
Replace history with summary
The conversation history is replaced with the summary. The context window is freed. 4
Post-compact budget: 50,000 tokens 50K budget
Claude gets 50K tokens to restore important files and skills. Max 5 files, 5,000 tokens per file. 5
Circuit breaker: 3 failures
If compaction fails 3 consecutive times, it stops trying to avoid infinite loops. Micro-compaction
Micro-compaction works differently from auto-compaction. It happens inline throughout the session, targeting old tool results specifically.
When a tool result (Read, Bash, Grep, Glob, WebSearch, WebFetch, Edit, Write) becomes old enough, its content is replaced with:
[Old tool result content cleared]
Images are estimated at a fixed 2,000 tokens regardless of actual size, a conservative estimate that helps budgeting.
Output token limits
| Constant | Value | Used when |
|---|---|---|
| Default max output | 32,000 tokens | Standard responses |
| Capped (slot-reservation) | 8,000 tokens | When slots are reserved for other operations |
| Escalated (recovery) | 64,000 tokens | Recovery mode after errors |
| Compaction output | 20,000 tokens | The summary written by the compaction agent |
→ Use /compact before you hit the limit
Auto-compact triggers automatically but you can run
/compact manually
before reaching the threshold. A manual compact tends to produce a cleaner, more
intentional summary than an automatic one triggered at the last moment.