Context Compaction
Summarize old turns to keep recent detail.
Compaction summarizes early turns into a short briefing and keeps the recent N turns verbatim. Claude Code auto-compacts at ~90% of context. For your own agents, trigger compaction explicitly so the summary captures task-relevant state, not just a generic recap.
Real-world example: A multi-day SaaS support case
The agent has been working a customer's webhook issue for four days. Logs from days one and two are 60K tokens — too much to keep verbatim, too useful to discard. On day three, the agent runs an explicit compaction step:
Compact older messages into a brief:
- Goal: fix duplicated webhook events for tenant Acme.
- Tried: replay tool (no diff), dedup index check (clean).
- Currently blocked on: customer's consumer log.
- Hypothesis: signature mismatch under retry, not a dedup miss.This brief replaces 60K tokens with 200, while preserving everything the agent actually needs to make the next decision.
Why this matters: compaction is selective summarization. The brief should capture decisions, learned facts, and open threads — not a generic recap of who said what.
- Compaction summarizes older turns
- Recent N turns are preserved verbatim
- Auto-compaction triggers near 90% full
- Custom agents should compact explicitly
- Summary should capture task state, not chitchat
