How to Use the /compact Command in Claude Code to Prevent Context Rot
Running /compact at 60% context capacity—not 95%—keeps your Claude Code sessions sharp. Learn when and how to compact with specific preservation instructions.
Why Your Claude Code Sessions Get Worse Over Time
If you’ve ever spent a couple of hours in a Claude Code session — debugging, refactoring, building out a feature — you’ve probably noticed something: the longer you go, the more the quality drifts. Responses get vaguer. The model starts forgetting decisions you made an hour ago. It suggests changes that contradict earlier ones. You end up re-explaining context you already gave.
This is context rot. And it’s one of the most underappreciated productivity killers in AI-assisted development.
The good news: Claude Code has a built-in command to address it. The /compact command compresses your conversation history into a structured summary, preserving what matters and clearing dead weight. But most people either don’t know it exists, or they use it wrong — waiting until they’re nearly out of context before running it, which is too late.
This guide covers what context rot actually is, how /compact works, and the specific approach that keeps your Claude Code sessions performing well from start to finish.
What Context Rot Actually Is
Context rot isn’t a formal term — it’s a practical description of what happens when a model’s context window fills up with low-signal information.
Here’s the mechanics: every AI model has a maximum context window. Claude’s models have large windows, but they’re not infinite. Every message you send, every code snippet Claude returns, every error log you paste — all of it consumes tokens. As the window fills up, the model has to work harder to surface the relevant parts of the conversation, and the signal-to-noise ratio drops.
The Signs You’re Experiencing Context Rot
- The model contradicts a decision it helped you make earlier in the session
- Responses become increasingly generic or hedge-heavy
- Claude “forgets” that you’re using a specific framework, library, or constraint
- You find yourself re-pasting the same context multiple times
- Code suggestions start introducing bugs that reverse earlier fixes
These aren’t hallucinations in the traditional sense — they’re failures of attention. With a dense, cluttered context, the model loses track of what actually matters.
Why Waiting Until 95% Is the Wrong Move
Most users either never compact, or they wait until the context limit warning appears. By then, you’re already operating in degraded mode. The model has been struggling to maintain coherence for the last 20–30% of your session.
The better approach — and the one this article is built around — is compacting at 60% capacity. More on exactly how to do that below.
How the /compact Command Works
/compact is a built-in command in Claude Code’s CLI interface. When you run it, Claude Code generates a structured summary of the current conversation and replaces the full conversation history with that summary.
Think of it as swapping out a cluttered whiteboard for a clean one with only the important notes transferred over.
What Gets Preserved by Default
Without additional instructions, /compact tries to preserve:
- The overall goal or task you’re working on
- Key decisions that have been made
- The current state of the codebase or problem
- Any constraints or requirements established earlier
- Active errors or issues being worked through
What Gets Dropped
Routine back-and-forth, redundant explanations, intermediate failed attempts, and verbose error logs that have already been resolved — these get cut. In most sessions, this is the majority of the context by token count.
The Compressed Output
After running /compact, you’ll see a summary block inserted into the conversation. This is Claude’s distillation of everything relevant. It’s worth reading it to verify nothing critical was missed. If something important was omitted, you can add it back manually before continuing.
Running /compact With Preservation Instructions
The real power of /compact comes from giving it specific instructions about what to keep. Without guidance, it makes reasonable guesses — but your session has context only you fully understand.
The Basic Syntax
/compact [optional instructions]
For example:
/compact Keep the full list of API endpoints we've defined, the auth middleware decisions, and the current error in the payment module.
Or:
/compact Preserve: the TypeScript interfaces we finalized, the database schema decisions, and the fact that we're using Prisma not raw SQL.
Writing Good Preservation Instructions
Good instructions are specific and categorical. Instead of “keep the important stuff,” name the actual things:
- Architecture decisions — “Keep the decision to use event-driven architecture with Redis pub/sub”
- Open issues — “Keep the unresolved issue with the WebSocket reconnect logic”
- Constraints — “Keep the constraint that we can’t use any new npm packages”
- Current state — “Keep the current state: auth is done, we’re halfway through the dashboard component”
- Naming conventions — “Keep the file naming conventions we established for the /api directory”
The more specific you are, the more useful the resulting summary will be.
What to Avoid Asking It to Preserve
Don’t try to preserve everything. If you’re asking /compact to keep 20 specific things, you’re defeating the purpose. Pick the 4–6 items that would genuinely derail the session if lost.
Also avoid asking it to preserve verbatim code you’re still actively working on — instead, keep that in your files where it belongs. /compact is for preserving context, not code.
The 60% Rule: When to Compact
This is the most important practical advice in this article: compact at 60% context capacity, not 95%.
Why 60% Works
At 60%, you still have plenty of room to continue productive work after compacting. The summary itself consumes tokens, so if you wait until 90%+ to compact, you’re left with very little room after the summary is inserted.
More importantly, context quality degrades gradually. By 70–80%, the model is already working harder to keep track of earlier decisions. Compacting at 60% keeps the whole session in a high-performance range.
How to Check Your Context Usage
Claude Code doesn’t always surface a percentage indicator prominently, but there are a few ways to gauge where you are:
- Claude Code will sometimes display context usage in the terminal output
- You can ask directly: “How full is our current context window?”
- Watch for early signs of context rot (vague responses, repeated clarifications) as informal signals
Some developers set a rough time-based rule: for complex sessions, compact every 45–60 minutes regardless of context percentage. This is a reasonable heuristic if you’re not checking capacity directly.
Practical Session Management
A structured approach that works well:
- Start the session with a clear context block — describe the project, current state, and goals upfront
- Check context around the 45-minute mark
- If you’re at 50–60%, run
/compactwith specific preservation instructions - Review the summary output before continuing
- Repeat if the session extends beyond another hour
For very long sessions (3+ hours), you may compact 2–3 times. Each compaction keeps the session sharp.
Step-by-Step: Running /compact Effectively
Here’s a concrete walkthrough of how to use /compact well.
Step 1: Identify What’s Critical Before You Compact
Before typing the command, take 30 seconds to think about what would hurt to lose. Ask yourself:
- What architectural decisions has this session established?
- What are the open issues or unresolved problems?
- What constraints or requirements have we talked about that aren’t in the codebase yet?
- What is the current task we’re in the middle of?
Write these down or keep them in mind for your preservation instructions.
Step 2: Write the /compact Command With Instructions
Be direct and specific. A good format:
/compact Current task: implementing rate limiting on the /api/messages endpoint. Preserve: the decision to use a sliding window algorithm, the Redis key structure we designed (user:{id}:rate_limit), the fact that we need to handle both authenticated and unauthenticated users differently, and the open issue with the burst handling logic.
This gives the model clear guidance on what the summary must include.
Step 3: Read the Summary Output
Don’t just run it and continue. Scan the summary. Verify:
- Your current task is accurately described
- Key decisions are captured correctly
- Open issues are listed
- No critical constraints were dropped
If something’s missing, add it back in your next message: “One thing the summary missed: we decided to avoid using any third-party rate limiting packages.”
Step 4: Continue Working
From here, continue the session normally. The compressed context means the model can focus more clearly on what remains. You should notice cleaner, more contextually accurate responses.
Common Mistakes to Avoid
- Compacting too late — By 90%, you’re already in degraded territory
- Using no preservation instructions — The default summary is decent but generic
- Not reading the summary — Errors in the summary will propagate forward
- Trying to preserve too much — Pick the most critical items, not everything
- Compacting during a complex mid-task — Finish the immediate task first, then compact. Mid-task compaction can lose intermediate reasoning
Context Management as a Development Habit
The /compact command is most effective when it’s part of a broader set of habits around context management in Claude Code sessions.
Start Sessions With Good Context
The quality of your compaction depends partly on the quality of what you put in. If you front-load each session with a clear description of the project, the current state, and the immediate goal, the model has better material to compress.
A simple session-opener template:
Project: [name and brief description]
Tech stack: [relevant technologies]
Current state: [what's done, what's in progress]
Today's goal: [specific task for this session]
Constraints: [any important limitations]
This takes 60 seconds and dramatically improves both the session quality and subsequent compaction quality.
Keep Code in Files, Not Just Context
A common mistake: treating Claude Code’s context window as a source of truth for your code. It isn’t. Commit regularly, keep your actual code in your editor, and use Claude Code for reasoning and generation — not storage.
This also reduces what needs to be preserved during compaction, since the codebase itself is always available.
Use /compact Between Major Tasks
Session transitions are natural compaction points. When you finish one major task and move to the next (e.g., you’ve finished the auth module and are starting on the dashboard), that’s a good moment to compact.
The completed work can be summarized briefly (“Auth module complete: JWT-based auth, refresh token rotation, role-based middleware”), and the new task becomes the active focus of the refreshed context.
Where MindStudio Fits Into AI-Assisted Development Workflows
Managing context in long-running Claude Code sessions is one layer of the problem. But development teams increasingly rely on AI not just for code generation in a terminal — they need AI working across multiple systems simultaneously: reading from project management tools, updating documentation, running tests, notifying teammates, triggering deployments.
That’s where MindStudio’s Agent Skills Plugin becomes useful for teams already working with Claude Code. It’s an npm SDK (@mindstudio-ai/agent) that lets Claude Code — or any AI agent — call over 120 typed capabilities as simple method calls. Things like agent.searchGoogle(), agent.runWorkflow(), agent.sendEmail(), or agent.generateImage() become first-class operations your AI agent can perform without you building the infrastructure to support them.
The practical result: while you’re managing Claude Code sessions and keeping context clean with /compact, MindStudio can handle the surrounding workflow — syncing progress notes to Notion, updating a Jira ticket when a task completes, sending a Slack message when a build passes. The agent handles infrastructure; you handle reasoning.
For teams building on top of Claude Code or extending it with custom agents, MindStudio’s no-code platform also lets you build the surrounding automation layer visually — no separate backend needed. You can try MindStudio free at mindstudio.ai.
Frequently Asked Questions
What does /compact actually do in Claude Code?
/compact replaces the full conversation history with a condensed summary generated by the model. It preserves key decisions, context, and active tasks while dropping low-signal content like resolved error logs and redundant back-and-forth. The result is a smaller context that gives the model more room to work with going forward.
When should I run /compact?
The recommended threshold is around 60% context capacity. Running it earlier than most users expect — before you see quality degradation — keeps performance consistent throughout a long session. Waiting until 90%+ means you’ve already been operating in degraded mode and have less room left after the summary is inserted.
Can I tell /compact what to keep?
Yes. You can pass instructions directly in the command: /compact [your instructions here]. Specify architectural decisions, open issues, constraints, naming conventions, or the current task. The more specific you are, the more accurate the summary will be.
Will /compact lose important code I’ve written?
/compact summarizes conversation history — it doesn’t delete files from your project. Any code Claude has written that you’ve saved to your editor or committed to version control is safe. The risk is losing conversational context (decisions, constraints, reasoning), not the code itself. This is another reason to commit frequently.
How often should I compact in a long session?
For sessions over 90 minutes, compacting once is usually sufficient. For very long sessions (3+ hours), you may compact 2–3 times. A time-based rule of thumb: consider compacting every 45–60 minutes during complex sessions, or whenever you finish a major task and start a new one.
Does /compact work the same across all Claude models?
The /compact command is a feature of Claude Code’s CLI, and the quality of the summary depends on the underlying model being used. More capable models (Claude 3.5 Sonnet, Claude 3 Opus) produce higher-quality summaries with better judgment about what to preserve. The command itself works the same way — the difference is in summary quality.
Key Takeaways
- Context rot is the gradual degradation of Claude Code’s output quality as the context window fills up with low-signal conversation history.
- The
/compactcommand compresses your conversation history into a structured summary, clearing space while preserving what matters. - Compact at 60% capacity, not 90%+. Waiting too long means you’ve already lost quality and have less room after compaction.
- Always include preservation instructions — name specific decisions, constraints, and open issues you need the summary to capture.
- Read the summary output before continuing. Errors in the summary propagate forward into the rest of your session.
- Use compaction as a session habit, not a last resort — between major tasks, after each hour of work, or whenever you notice quality slipping.
For teams building AI-assisted workflows that extend beyond a single Claude Code session, MindStudio offers a practical way to connect those sessions to the broader systems your work lives in — without building the infrastructure from scratch.