How to Use the /compact Command in Claude Code to Manage Context Efficiently
Problem
When I work on long coding sessions in Claude Code, my context window fills up. Claude warns me when I’m near the limit. But by then, performance has already degraded.
I used to just run /compact when warned. But I kept losing important context from earlier in the session.
Environment
- Claude Code CLI
- Typical session: 2-4 hours
- Tasks: refactoring, debugging, feature development
What the /compact Command Does
The /compact command summarizes and condenses conversation history to free up context space. It compresses token usage while trying to preserve essential information.
The problem: Without instructions, it produces a generic summary that may lose critical details.
The Solution: Two Key Principles
Principle 1: Timing Matters
Don’t wait for Claude’s warning. Run /compact at 70-75% context usage.
Why? Claude operates less effectively in the last 20% of context capacity. When you wait for the warning, you’ve already lost optimal performance.
/context
# Output shows:# System prompts: 15,000 tokens# Conversation: 65,000 tokens# File contents: 70,000 tokens# Total: 150,000 / 200,000 (75%)
# At 75%, it's time to compactPrinciple 2: Instructions Matter
Never run bare /compact. Always tell it what to preserve.
/compact
# Result: Generic summary# May lose critical implementation details/compact focus on the auth module implementation, ignore migration files
# Result: Auth details preserved# Migration noise removedPractical Examples
Focus on a Feature
/compact preserve all API endpoint discussions, summarize CSS work
# Keeps: API routes, request/response handling# Removes: Styling discussionsExclude Noise
/compact ignore migration files and test outputs, keep main implementation
# Keeps: Core logic discussions# Removes: Migration history, test output logsPreserve Debugging Context
/compact preserve the debugging steps for the database connection issue
# Keeps: Full troubleshooting trail# Summarizes: Other discussionsPrioritize Recent Work
/compact prioritize the last 10 messages about API refactor
# Keeps: Recent refactoring context# Compresses: Earlier session contentThe Complete Workflow
Here’s how I manage context in a typical session:
# 1. Check context at start of complex work/context
# 2. Work on implementation...# (coding, file reads, debugging)
# 3. Check again after major operations/context
# 4. At 70-75%, compact with focus/compact focus on authentication module, ignore test fixtures
# 5. Continue work...# Claude responds faster, more accurately
# 6. Use /btw for quick tangential questions/btw How do I escape quotes in JSON?# Answer doesn't add to main conversation
# 7. Check context periodically/contextWhy This Matters
Performance
Claude performs worse near context limits. You get:
- Slower responses
- Less accurate code
- Missed details
Cost
More context = higher API costs. Efficient management reduces your bill.
Accuracy
Context noise degrades response quality. When irrelevant context mixes with relevant, Claude struggles to prioritize.
Common Mistakes
Mistake 1: Waiting for the warning
By the time Claude warns you, performance has already dropped. Check /context and compact proactively.
Mistake 2: Running without instructions
You get a generic summary. Critical details get lost.
Mistake 3: Not checking /context first
You don’t know what’s consuming space. You might compact the wrong things.
Mistake 4: Over-compacting
Don’t compact too frequently. You lose session continuity. 70-75% is the sweet spot.
The /btw Command Synergy
Use /btw for questions that don’t need project context:
/btw How do I check if a file exists in Python?
# Answer: Use os.path.exists() or Path.exists()# Doesn't add to conversation historyThis keeps your main conversation focused. Less context used means less frequent compacting.
Summary
In this post, I showed how to use /compact effectively in Claude Code. The key points are:
- Proactive timing - Run at 70-75% context usage, not when warned
- Focused instructions - Always tell
/compactwhat to preserve and what to ignore - Combine with /context - Check usage before compacting
- Use /btw - Keep tangential questions out of main conversation
The /compact command is the gamechanger for long Claude Code sessions. With proper timing and instructions, it transforms from a cleanup tool into a proactive context management strategy.
Final Words + More Resources
My intention with this article was to help others share my knowledge and experience. If you want to contact me, you can contact by email: Email me
Here are also the most important links from this article along with some further resources that will help you in this scope:
Oh, and if you found these resources useful, don’t forget to support me by starring the repo on GitHub!
Comments