Auto Compaction - Claude Code
Background context summarization when the window starts filling up.
Auto compaction is how Claude Code survives marathon sessions without losing the thread. When the window starts filling, Claude summarizes older turns in place.
What it does
As context approaches capacity, Claude runs a summarization pass on older turns - preserving key decisions, file paths, and state - while dropping verbose tool output and stale exchanges. The working memory shrinks without losing the important history. PreCompact and PostCompact hooks let you react around the event.
When to use it
- Long sessions that would otherwise hit the context ceiling.
- Any marathon where you want Claude to stay coherent across many turns.
- Working on tasks that need broad state but not full history.
- Pair with hooks when you want to snapshot, log, or replace compaction behavior.
Gotchas
- Compaction is lossy. Important details might get summarized away.
- Write critical decisions to disk (notes, TODO.md, commits) so compaction can't drop them.
- Custom compaction via hooks is powerful but easy to get wrong. Start conservative.
Official docs: https://code.claude.com/docs/en/how-claude-code-works.md#when-context-fills-up
Technical content at the intersection of AI and development. Building with AI agents, Claude Code, and modern dev tools - then showing you exactly how it works.
Get the weekly deep dive
Tutorials on Claude Code, AI agents, and dev tools - delivered free every week.
Was this helpful?




