Claude Code's Compaction Engine: What the Source Code Actually Reveals
A few months ago I wrote about context engineering - the invisible logic that keeps AI agents from losing their minds during long sessions. I described the patterns from the outside: keep the lates...

Source: DEV Community
A few months ago I wrote about context engineering - the invisible logic that keeps AI agents from losing their minds during long sessions. I described the patterns from the outside: keep the latest file versions, trim terminal output, summarize old tool results, guard the system prompt. I also made a prediction: naive LLM summarization was a band-aid. The real work had to be deterministic curation. Summary should be the last resort. Then Claude Code's repository surfaced publicly. I asked Claude to analyze its own compaction source code. The prediction held. And the implementation is more thoughtful than I expected. Three Tiers, Not One Claude Code's compaction system isn't a single mechanism - it's three tiers applied in sequence, each heavier than the last. Tier 1 runs before every API call. It does lightweight cleanup: clearing old tool results, keeping only the most recent five, replacing the rest with [Old tool result content cleared]. Fast, cheap, no model involved. Tier 2 opera