Mastering Claude Code: Using the /compact Command to Manage Context
When working on complex architectural features in Claude Code CLI, long interactive sessions can quickly bloat your context window. As the context fills up, you might notice higher token costs, slightly slower response times, or “context degradation”—where the model starts forgetting initial instructions.
Instead of restarting your session with /clear and losing all progress, the /compact slash command allows you to compress your conversation while preserving critical context.
1. What is /compact and How Does It Work?
The /compact command analyzes your active chat history, extracts key technical details, decisions, and file edits, and summarizes them into a condensed context block.
It wipes out redundant output—such as long log dumps, temporary test runs, and repetitive tool outputs—while retaining:
- Key architecture decisions made during the session
- Current progress and pending tasks
- Files that were modified, created, or analyzed
Note: Compacting reduces token usage and frees up space in the model’s context window without forcing you to exit or re-explain the project state.
2. Command Variants & Usage
You can run /compact in two ways depending on whether you want a generic summary or a targeted compression.
Standard Compacting
Compresses the current session down to its most essential context automatically.
/compact
Focused Compacting Directs Claude to compress the session while keeping a specific task or focus top-of-mind for upcoming turns.
/compact focus on GraphQL mutation changes and checkout API refactoring
3. Expected Terminal Output
When you execute /compact, Claude Code processes the session history and provides a structured confirmation output.
Output Example: Plaintext Summarizing conversation history…
✔ Conversation compacted successfully.
Context Summary:
- Refactored Adobe Commerce GraphQL resolver for tiered pricing in
app/code/Vendor/Catalog/Model/Resolver. - Added unit tests in
Test/Unit/ResolverTest.php(All passing). - Current Objective: Update frontend GraphQL queries to support multi-source stock statuses.
Token usage reduced from 142,500 tokens (71% of context) to 18,200 tokens (9% of context).
4. How to Verify Success
To verify that the /compact step succeeded and optimized your workspace:
Check Token Usage: Run the /cost command in your terminal session.
/cost
Expected Verification: Look at the Context Window Usage %. It should reflect a significantly lower percentage compared to pre-compaction levels.
5. Best Practices: When to Use /compact
After Heavy Debugging: When you’ve pasted large stack traces or build logs that are no longer needed.
Before Switching Sub-tasks: When finishing a backend refactor and moving to frontend integration within the same session.
High Token Fill: When your session reaches 60–70% of the maximum context capacity.
Tip: You can also trigger compaction at any point using the keyboard shortcut Esc followed by entering /compact.
<Elicitations message="What would you like to do next?">
<Elicitation label="Create /init command guide" query="Can you write a similar detailed guide for the Claude Code /init command?"/>
<Elicitation label="Create keyboard shortcuts guide" query="Can you write a detailed blog post on Claude Code keyboard shortcuts?"/>
</Elicitations>