Claude Code /compact: Free Up Context, Keep Progress
Table of Contents
- What is /compact
- Understanding the Context Window
- How to Use It
- Basic Usage
- With Focus Instructions
- Partial Compaction
- Auto Compaction vs Manual Compaction
- Auto Compaction
- Manual Compaction (/compact)
- When to Use It
- When Switching Tasks
- After Long Debug Sessions
- After Reading Many Files
- When Claude Code Starts Making Mistakes
- Practical Tips
- Tip 1: Monitor Context Usage with /context
- Tip 2: Define Compaction Strategy in CLAUDE.md
- Tip 3: Use /btw for Quick Questions That Don’t Use Context
- Tip 4: Use Subagents for Exploration Tasks
- /compact vs /clear vs /rewind
- Final Thoughts
What is /compact
When using Claude Code, you might run into situations like these:
- After chatting for a while, Claude Code starts “forgetting” things you just discussed
- You ask it to edit a file, but it edits the wrong place or repeats something it already did
- Responses feel slower, as if Claude Code is struggling with too much information
The reason behind all of these is the same: the context window is getting full.
This is where the /compact command comes in.
/compact is a Claude Code slash command that compresses your current conversation history, freeing up context space. It summarizes lengthy conversations into concise summaries while preserving key information, letting Claude Code start fresh with a lighter load.
Understanding the Context Window
To understand /compact, you first need to know what the context window is.
The context window is like Claude Code’s “working memory” — a limited space that holds all information about the current conversation:
- Conversation history — every message you’ve sent and every response from Claude Code
- File contents — every file Claude Code has read
- Command outputs — results from every command that’s been executed
- System information — CLAUDE.md, auto memory, loaded skills, etc.
As the conversation progresses, this content accumulates. A single debug session might read dozens of files and run dozens of commands, easily generating tens of thousands of tokens.
When the context gets full, problems emerge:
- Claude Code may “forget” instructions from earlier in the conversation
- Reasoning quality may decline, leading to careless mistakes
- Irrelevant old content can interfere with judgment on the current task
The context window is Claude Code’s most critical resource. Managing it well directly determines your experience.
How to Use It
Basic Usage
In Claude Code’s interactive mode, type:
/compact
Claude Code will automatically summarize the current conversation, compressing lengthy history into a concise summary. After compaction, key code changes, file states, and important decisions are preserved, while process details (like intermediate debug output and resolved problem discussions) get trimmed.
With Focus Instructions
/compact accepts an optional parameter — a focus instruction that tells Claude Code what to prioritize during compression:
/compact Focus on the API changes
/compact Preserve the full list of modified files and test commands
/compact Only focus on the auth module refactoring progress
This parameter is incredibly useful. During default compression, Claude Code decides on its own what’s important. But if you know what you’ll be working on next, providing a clear focus ensures critical information isn’t lost.
Partial Compaction
If you only want to compress part of the conversation rather than all of it, use Esc + Esc or /rewind, select a message checkpoint, and choose Summarize from here. This compresses messages from that point forward while keeping earlier context fully intact.
Auto Compaction vs Manual Compaction
Claude Code has two compaction mechanisms:
Auto Compaction
When the conversation approaches the context limit, Claude Code automatically triggers compaction. It will:
- First clear older tool outputs
- If that’s not enough, summarize the entire conversation history
- Preserve your requests and key code snippets
Auto compaction is a safety net — you don’t need to worry about what happens when context overflows. Claude Code handles it automatically.
Manual Compaction (/compact)
But by the time auto compaction triggers, the context is already very full, and performance may have been degraded for a while.
The advantage of proactively using /compact is: you can solve the problem before it appears.
| Auto Compaction | Manual /compact | |
|---|---|---|
| Trigger | When approaching context limit | Anytime you choose |
| Control | Claude Code decides what to keep | You can specify focus areas |
| Proactive | Reactive response | Active management |
| Best for | Safety net | Fine-grained control |
When to Use It
When Switching Tasks
You just finished developing a feature and are about to start something unrelated. The previous conversation history isn’t helpful for the new task and is just taking up context space.
/compact Keep the overall architecture understanding, clear the login feature development details
After Long Debug Sessions
Debugging reads lots of files and runs lots of commands, producing tons of intermediate output. Once the issue is resolved, those intermediate steps don’t matter anymore.
/compact Keep the final fix and the list of modified files
After Reading Many Files
When Claude Code explores an unfamiliar codebase, it might read dozens of files. That file content takes up massive context, but you only need it to remember the key conclusions.
/compact Keep the architecture summary and key file paths
When Claude Code Starts Making Mistakes
If Claude Code starts forgetting things, repeating operations, or clearly deviating from your instructions, the context is probably too full. A /compact can provide immediate relief.
Practical Tips
Tip 1: Monitor Context Usage with /context
Before compacting, check the current context usage with /context:
/context
It displays a colored grid visualizing context usage and provides optimization suggestions. For example, it might tell you which MCP tools are consuming lots of space or whether there’s context bloat.
Look first, then decide whether and how to compact.
Tip 2: Define Compaction Strategy in CLAUDE.md
If you have consistent compaction preferences, add a Compact Instructions section to your CLAUDE.md:
# Compact Instructions
When compacting, always preserve:
- The full list of modified files
- All test commands that have been used
- Key architectural decisions made during the session
This way, whether it’s auto compaction or manual /compact, Claude Code will follow these instructions to decide what to keep.
Tip 3: Use /btw for Quick Questions That Don’t Use Context
Sometimes you just want to quickly confirm something small without it taking up context space. Use /btw instead of a normal prompt:
/btw What was that config file called again?
The /btw answer appears in a temporary overlay and never enters the conversation history, so it won’t consume any context.
Tip 4: Use Subagents for Exploration Tasks
If you need Claude Code to explore a large codebase, consider having it use a subagent:
Use a subagent to investigate how the auth module is implemented
Subagents have their own independent context window and only return a summary to your main conversation when done. You get the information without bloating your main context.
/compact vs /clear vs /rewind
These three commands all relate to conversation management, but they do completely different things:
| /compact | /clear | /rewind | |
|---|---|---|---|
| What it does | Compresses conversation history into a summary | Completely clears conversation, starts from zero | Rolls back to a previous checkpoint |
| Preserves info | Keeps a summary of key information | Keeps nothing | Fully preserves everything before the rollback point |
| Code state | Doesn’t affect code | Doesn’t affect code | Can also revert code changes |
| Best for | Conversation too long, needs slimming | Switching to a completely unrelated task | Something went wrong, want to go back |
| Analogy | Tidying up notes, keeping key points | Turning to a fresh page | Pressing Ctrl+Z to undo |
My recommendations:
- Conversation too long within the same task →
/compact, keep key context and carry on - Starting a completely unrelated new task →
/clear, clean slate - Claude Code messed up code or went the wrong direction →
/rewind, roll back to the right checkpoint - Quick small question →
/btw, zero context cost
Final Thoughts
/compact is an unassuming command that directly impacts your Claude Code experience. The problem it solves is fundamental: the context window is finite, but your work demands are not.
When Claude Code’s performance drops during long conversations, it’s usually not a model capability issue — it’s a context management issue. Just like even the smartest person would struggle to focus if their desk were buried under irrelevant papers.
/compact is the tool that helps you tidy up — archive what you don’t need, keep what matters within reach, and let Claude Code refocus its attention.
Next time you’re mid-conversation and Claude Code starts feeling “off,” don’t rush to restart the session. Try /compact first. More often than not, one compression is all it takes to get it back on track.
Related Articles
Claude Code Agent Loop: Dissecting the Heart of an AI Coding Assistant
How does Claude Code understand your requests, invoke tools, and self-recover step by step? A source-code deep dive into the Agent Loop's core architecture — streaming responses, parallel tool execution, auto-compaction, and error recovery.
Claude Code settings.json Explained (1): Where Config Files Live and Who Wins
A complete guide to Claude Code's configuration file system — five config sources, their file paths, priority rules, array merging vs value overriding, and enterprise managed settings delivery.
Claude Code settings.json Deep Dive (Part 2): The Permissions System
A thorough breakdown of Claude Code's permissions configuration — allow/deny/ask rule arrays, wildcard syntax, MCP tool permissions, defaultMode options, and additionalDirectories.
Claude Code settings.json Deep Dive (Part 3): The Hooks System
A thorough breakdown of Claude Code's hooks configuration — four hook types, core events (PreToolUse/PostToolUse/Stop/Notification), stdin/stdout protocol, exit code semantics, and practical examples.
Claude Code settings.json Deep Dive (4): env, Models, Auth, and Other Useful Fields
A comprehensive guide to the remaining settings.json fields in Claude Code — env variable injection, model configuration, authentication helpers, Git attribution, session cleanup, language and UI, thinking depth, auto-updates, memory system, and more.