/compact and Context Trimming
The primary lever for managing long sessions. When to compact, what gets preserved, and strategies for keeping context relevant.
You're 45 minutes into a productive session. Claude Code is nailing every task. Then it starts repeating itself. Forgetting decisions. Asking about files it already read. Your context window is full, and Claude Code is about to auto-compact on you.
This is the worst time for it to happen. You just lost control of what gets remembered and what gets thrown away.
What /compact Actually Does
The /compact command tells Claude Code to summarize the entire conversation into a compressed form. Like writing meeting notes: the full transcript gets replaced by a summary, freeing up space for more work.
Claude Code will auto-compact when the conversation approaches the context window limit. This happens whether you want it to or not. The difference between a proactive /compact and a forced auto-compact? You get to choose the timing and focus.
When compaction fires, two categories of information exist.
What gets preserved:
- CLAUDE.md files (re-read from disk and re-injected fresh, not summarized)
- Key decisions and architectural choices (summarized)
- Code patterns and file states (summarized)
- The general direction of the session
What gets lost:
- Detailed conversation history
- Precise tool outputs (file contents, grep results, build logs)
- Nuanced corrections you made mid-conversation
- The reasoning chain that got you to your current approach
That first bullet in the preserved list is huge. CLAUDE.md files survive compaction perfectly because they aren't part of the conversation at all. They're read from disk every time. If an instruction disappeared after compaction, it was never in CLAUDE.md. It was only said in conversation.
How to Compact Strategically
Don't let compaction be forced on you. Proactive beats reactive every time.
Signs you need to compact:
- Claude Code starts repeating explanations it already gave
- Responses get slower
- You notice subtle forgetting (re-reading files it already processed)
- You just finished a major milestone and are shifting to something new
Basic compaction:
/compactFocused compaction tells Claude Code what to prioritize in the summary:
/compact Focus on the API changes and the new endpoint signaturesYou're telling Claude Code: "When you write the summary, make sure these specific things are front and center." Everything else still gets summarized, but your focus areas get extra attention. This single technique makes the biggest difference in post-compaction quality.
Partial compaction uses the Esc+Esc shortcut. Pick a message, choose "Summarize from here." Everything from that message forward gets compressed while earlier context stays intact. The selected message and everything after it become an AI-generated summary. No files on disk change.
Try it
Open a Claude Code session and start a moderately complex task. After 10-15 exchanges, run:
/compact Focus on the file modifications and any test commands discussedThen ask Claude Code to list what it remembers. Compare that to what you actually discussed. Try the same thing without the focus instruction next time. The retention gap is real.
Best Practices for Context Management
Put critical instructions in CLAUDE.md, not in conversation. If something must survive compaction, it belongs in a file. You can even add compaction-specific instructions:
When compacting, always preserve the full list of modified files and any test commandsThat line in your CLAUDE.md ensures every compaction retains what you care about.
Compact at natural breakpoints. Finished implementing a feature? Compact before starting the next one. Finished debugging? Compact before moving to tests. These transitions are natural moments where detailed history matters less than outcomes.
Use /clear when compaction isn't enough. After two failed correction attempts, the conversation is probably carrying too much conflicting context. A /clear wipes everything and starts fresh. A clean session with a better initial prompt will outperform a degraded session every time.
/clearFeels wasteful but it's actually faster. Stop trying to salvage a poisoned context.
Don't over-compact. If your session is short and focused, compaction adds overhead for no benefit. Only compact when the pressure is real or when you're making a major task transition.
Watch the context window indicator. When it crosses roughly 60-70%, start thinking about compacting. Don't wait for 95%.
Focused compaction beats generic compaction. A bare /compact works fine. /compact Focus on the database schema changes and migration plan works noticeably better. Be specific about what matters for your next chunk of work.
What Matters
- Compact before you're forced to. You control the timing and focus, which means you control what survives.
- CLAUDE.md is re-read from disk after compaction, not summarized. If an instruction vanished, it was never in CLAUDE.md to begin with.
- Adding focus to
/compacttells Claude Code what to prioritize. This single habit separates people who fight context loss from people who don't. - Two failed corrections means the context is poisoned.
/clearand a better prompt. Don't negotiate with a confused model.
Mastering Claude Code
Lesson 17 of 70