C Claude Code Internals
| EN | ES

上下文与压缩

Claude Code 主动管理其上下文窗口。当 token 不足时,它通过总结历史自动压缩。微压缩在整个会话中在行内清理旧的工具结果。

200K 默认上下文窗口 1M Sonnet 4.x / Opus 4.6 ~13K 剩余 token → 自动压缩 50K 压缩后 token 预算

上下文窗口大小

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

自动压缩流程

当接近上下文限制时,Claude 自动总结对话历史以释放空间。

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.

微压缩

微压缩与自动压缩不同。它在整个会话中在行内发生,专门针对旧的工具结果。

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.

输出 token 限制

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
在达到限制之前使用 /compact
自动压缩会自动触发,但你可以在达到阈值之前手动运行 /compact。 手动压缩往往比在最后一刻触发的自动压缩产生更清晰、更有意图的总结。