Compression of Context
In our earlier articles, we explored context engineering — the idea that an AI agent is only as good as the context it receives — and writing context, where we used CLAUDE.md files and persistent memory to give Claude the right information from the very start. Those strategies focus on what goes into the context window. This article tackles the other side of the problem: what happens when the context window starts filling up.
Claude Code operates within a finite context window. Every message you send, every file Claude reads, every command output, and every response — all of it accumulates inside this window. During a long session, especially one involving debugging or codebase exploration, the window can fill with tens of thousands of tokens. And here is the critical insight: LLM performance degrades as context fills. A cluttered context window causes Claude to "forget" earlier instructions, miss details, and make more mistakes.
The third strategy for effective context engineering is compression — efficiently representing information so that you squeeze maximum value out of the available space. Claude Code provides several built-in commands for this: /clear to wipe the slate clean, /compact to intelligently summarize the conversation, /rewind to selectively compress portions of the conversation, and /btw to ask side questions without growing context at all. Together, these tools let you actively manage your most precious resource.
Why Context Compression Matters
The Overflowing Window Problem
Think of the context window like a desk. At the start of a session, the desk is clean. As you work, you pile on papers — file contents, error logs, conversation history, command outputs. Eventually, the desk is so cluttered that you can't find the important documents anymore. Your productivity drops.
The same thing happens to Claude. When the context window fills with old debugging attempts, irrelevant file reads, and stale conversation history, Claude's ability to focus on your current task diminishes. Two common failure patterns emerge:
- The kitchen sink session — You start with one task, pivot to something unrelated, then go back. The context is now full of irrelevant information from the detour.
- Correcting over and over — Claude does something wrong, you correct it, it is still wrong, you correct again. The context is polluted with multiple failed approaches, making it harder for Claude to find the right one.
Context compression is the discipline of keeping that desk clean — removing what is no longer needed and summarizing what is still relevant, so Claude can always focus on the task at hand.
The Core Principle
The Anthropic team states this clearly in their best practices guide:
"A clean session with a better prompt almost always outperforms a long session with accumulated corrections."
This is the guiding principle. Compression is not about losing information — it is about keeping the right information in context while freeing space for new, relevant data.
The /clear Command: Starting Fresh
What It Does
The /clear command wipes the conversation history from the context window entirely. It is the most aggressive form of context management — a hard reset. After running /clear, Claude starts from scratch with no memory of the previous conversation.
This command has two aliases that do the exact same thing:
All three commands are interchangeable. Use whichever feels most natural to you.
What Gets Cleared and What Survives
When you run /clear, the conversation history is erased. However, not everything disappears:
| Cleared | Survives |
|---|---|
| All conversation messages (your prompts and Claude's responses) | System prompt and output style settings |
| File contents that Claude read during the session | Project-root CLAUDE.md and unscoped rules (re-injected from disk) |
| Command outputs and error logs | Auto memory notes (re-injected from disk) |
| Input history (resets for the new session) | The previous session itself (preserved and can be resumed) |
This is important: /clear starts a new session, but the old session is not deleted. You can always go back to it using claude --resume. Think of /clear as opening a new tab, not closing the browser.
When to Use /clear
Use /clear in these scenarios:
- Between unrelated tasks. If you finished implementing a feature and now want to debug something completely different,
/clearfirst. The feature implementation context is irrelevant to the debugging task and will only confuse Claude. - After two failed corrections. If you have corrected Claude twice on the same issue and it is still not getting it right, the context is polluted with failed approaches. Run
/clearand write a better initial prompt that incorporates what you learned from the failures. - Before a complex, focused task. When you are about to ask Claude to do something that requires deep focus (like a multi-file refactoring), start with a clean context so Claude can devote its full attention to the task.
The /compact Command: Intelligent Summarization
What It Does
The /compact command replaces the current conversation with a structured, AI-generated summary. Unlike /clear, which erases everything, /compact preserves the essence of the conversation — the key decisions, code patterns, and file states — while dramatically reducing the token count.
Think of it like this: /clear is shredding all the papers on your desk. /compact is replacing a stack of 50 pages with a one-page executive summary. You lose the details, but you keep the important conclusions.
Custom Compaction Instructions
You can optionally pass instructions to /compact to tell Claude what to focus on when creating the summary:
This is extremely powerful. If you know that the next phase of your work depends on specific information from the current conversation, you can ensure that information survives the compaction.
Auto-Compaction
You do not always have to run /compact manually. Claude Code automatically triggers compaction when you approach the context window limits. This auto-compaction:
- Preserves important code and decisions while freeing space
- Happens transparently — you will see a brief notification that compaction occurred
- Uses the same summarization logic as manual
/compact
However, relying entirely on auto-compaction is a passive approach. Proactive compaction — running /compact with specific instructions at strategic moments — gives you better control over what survives.
What Survives Compaction
Understanding what survives a /compact is critical. Not everything makes it through:
| Context Type | What Happens After Compaction |
|---|---|
| System prompt and output style | Unchanged (not part of message history) |
| Project-root CLAUDE.md and unscoped rules | Re-injected from disk — always present |
| Auto memory | Re-injected from disk — always present |
Rules with paths: frontmatter (scoped rules) | Lost until a matching file is read again |
| Nested CLAUDE.md files in subdirectories | Lost until a file in that subdirectory is read again |
| Invoked skill bodies | Re-injected, but capped at 5,000 tokens per skill and 25,000 tokens total. Oldest skills dropped first |
| Hooks | Not applicable (hooks run as code, not context) |
The key takeaway is that your project-root CLAUDE.md and auto memory are always safe — they re-load from disk after compaction. But scoped rules and nested CLAUDE.md files get lost and are only re-injected when Claude happens to access a matching file. If a rule must persist across compaction, either remove the paths: frontmatter to make it unscoped or move it to the project-root CLAUDE.md.
Customizing Compaction in CLAUDE.md
Since CLAUDE.md is re-injected from disk after every compaction, you can include compaction instructions directly in it. These instructions tell Claude what to prioritize when summarizing:
This ensures that every time compaction occurs — whether manual or automatic — Claude's summary includes the information you care about most.
The /rewind Command: Targeted Compression
What It Does
The /rewind command (alias: /checkpoint) opens a rewind menu that shows every prompt you have sent during the session. From this menu, you can select a specific point and choose one of four actions:
- Restore code and conversation — Revert both the files on disk and the conversation to that point.
- Restore conversation — Rewind only the conversation while keeping the current code changes.
- Restore code — Revert file changes while keeping the full conversation.
- Summarize from here — Compress the conversation from the selected point forward into a summary, freeing context window space.
You can also open the rewind menu by pressing Esc twice (Esc + Esc).
Partial Compaction with "Summarize from Here"
The "Summarize from here" option is particularly powerful for context management. Unlike /compact, which summarizes the entire conversation, "Summarize from here" lets you target a specific section:
- Messages before the selected point stay intact in full detail.
- Messages from the selected point forward get replaced with a compact AI-generated summary.
- No files on disk are changed.
- You can type optional instructions to guide what the summary focuses on.
This is ideal when you had a productive early conversation (setting up requirements, discussing architecture) followed by a verbose debugging session. You can keep the early context in full detail and only compress the debugging portion.
Checkpointing: The Safety Net
Every time you send a prompt, Claude Code automatically creates a checkpoint — a snapshot of the file state at that moment. This means you can always rewind to any previous point without losing work. Key facts about checkpoints:
- Checkpoints persist across sessions — they are available even in resumed conversations.
- They are automatically cleaned up after 30 days (configurable).
- They only track changes made by Claude's file editing tools — bash commands like
rmormvare not tracked. - Checkpoints are not a replacement for Git. Think of them as "local undo" and Git as "permanent history."
The /btw Command: Zero-Cost Side Questions
What It Does
The /btw command lets you ask a quick side question without adding anything to the conversation history. The answer appears in a dismissible overlay, and once dismissed, it vanishes — zero impact on your context window.
How /btw Works Internally
Understanding how /btw works under the hood helps you use it effectively:
- Full visibility — The
/btwresponse has full access to the current conversation context. It can see everything Claude has seen so far. - No tool access —
/btwcannot read files, run commands, or make edits. It can only answer based on what is already in context. - Single response — You get one answer. There is no back-and-forth conversation within a
/btwquery. - Ephemeral — The question and answer never enter the conversation history. Once you dismiss the overlay, they are gone.
- Low cost — Thanks to prompt cache reuse,
/btwqueries are inexpensive in terms of API costs. - Works while Claude is processing — You can fire a
/btwquestion even while Claude is in the middle of working on something.
Think of /btw as the inverse of a subagent: a subagent has its own context but has access to tools; /btw sees the full conversation but has no tool access.
When to Use /btw
Use /btw when you need a quick answer that does not warrant growing the context:
- Checking a syntax detail while Claude is implementing a feature
- Confirming what Claude has already read or done in the current session
- Asking a clarification question about something visible in the conversation
- Quick conceptual questions while waiting for Claude to finish a task
The /context Command: Visualizing Usage
What It Does
The /context command visualizes your current context window usage as a colored grid, showing how much space is consumed and by what. It also provides optimization suggestions.
This is your diagnostic tool. Run it whenever you want to understand how full the context is and what is consuming the most space. It helps you make informed decisions about when to /compact or /clear.
Session Management: Preserving Work Across Clears
Resuming Previous Sessions
When you /clear or close Claude Code, your sessions are not deleted. You can always go back:
Naming Sessions
Give your sessions descriptive names so you can find them later:
Treat sessions like branches: different workstreams get separate, persistent contexts. When you /clear, you are not losing the work — you are just switching to a clean workspace.
Forking Sessions
The /branch command (alias: /fork) creates a copy of the current conversation at its current point. This is useful when you want to explore a different approach without losing your current progress:
You can also fork when resuming a previous session from the command line:
Subagents: Compression by Delegation
How Subagents Protect Your Context
Though not a compression command per se, subagents are one of the most effective tools for context management because they offload exploration to a separate context window. When Claude researches a codebase, it reads lots of files — all of which consume your context. A subagent does this exploration in its own isolated context and reports back only a concise summary.
The subagent reads dozens of files in its own context window. Your main conversation receives only the summary — a fraction of the tokens that direct exploration would have consumed.
Choosing the Right Compression Strategy
Decision Guide
With multiple tools available, here is a quick guide to choosing the right one:
| Situation | Command | Why |
|---|---|---|
| Switching to an unrelated task | /clear | Old context is entirely irrelevant |
| Same task, but context is getting large | /compact | Preserve decisions and progress in summary form |
| Early discussion is valuable, later part is verbose | Esc+Esc → Summarize from here | Keep detailed early context, compress only the noise |
| Quick question that should not grow context | /btw | Zero context cost |
| About to explore a large codebase | Subagent | Exploration happens in separate context |
| Claude keeps making the same mistake | /clear + better prompt | Context is polluted with failed approaches |
| Want to try a different approach | /branch or /rewind | Preserve current progress while exploring alternatives |
| Want to check context fullness | /context | Diagnose before deciding on a strategy |
A Practical Workflow
Here is a realistic example of context compression in action during a multi-task development session:
- Start a session. Ask Claude to implement a new API endpoint.
- Claude reads files, implements the endpoint, runs tests. Context is growing.
- Run
/contextto check how full the context is. - Run
/compact Focus on the API endpoint implementation and test resultsto compress while preserving key information. - Continue working on the same endpoint — adding error handling, improving tests.
- Endpoint is done. Run
/clearto start a fresh session for the next task. - Start a new task: debugging a database connection issue.
- Midway through debugging, use
/btwto check a syntax detail without growing context. - Debugging gets verbose. Use Esc+Esc → "Summarize from here" on the initial debugging setup to compress the trial-and-error portion.
- Finish the fix.
/clearbefore the next task.
Common Pitfalls and Best Practices
Pitfalls to Avoid
- Never using /clear or /compact. Relying entirely on auto-compaction means you have no control over what gets preserved. Proactive management produces better results.
- Correcting Claude more than twice without clearing. After two failed corrections, the context is cluttered with failed approaches. Start fresh with
/clearand a better prompt. - Mixing unrelated tasks in one session. Every pivot adds irrelevant context. Use
/clearbetween unrelated tasks. - Forgetting what gets lost in compaction. Scoped rules (with
paths:frontmatter) and nested CLAUDE.md files disappear after compaction. Move critical rules to the project-root CLAUDE.md if they must always be present. - Not using /btw for quick checks. Every question you ask as a normal prompt grows the context. If you just need a quick fact-check, use
/btwto keep context clean.
Best Practices Summary
- Run /clear between unrelated tasks. This is the single most impactful habit for context management.
- After two failed corrections, /clear and rewrite the prompt. A clean session with a better prompt outperforms accumulated corrections.
- Use /compact with custom instructions at strategic moments to control what information survives.
- Add compaction instructions to CLAUDE.md so that both manual and auto-compaction preserve what matters.
- Use /btw for side questions that do not need to persist in context.
- Delegate exploration to subagents so file reads do not consume your main context.
- Use /context regularly to monitor how full your context window is.
- Name your sessions with /rename so you can resume them later when needed.
- Use "Summarize from here" via Esc+Esc for surgical compression of verbose sections.
- Keep critical instructions in project-root CLAUDE.md so they always survive compaction.
Command Reference
Here is a complete reference of all context compression commands covered in this article:
| Command | Aliases | Purpose |
|---|---|---|
/clear | /reset, /new | Clear conversation history and free up context entirely |
/compact [instructions] | — | Summarize the entire conversation with optional focus instructions |
/rewind | /checkpoint, Esc+Esc | Open the rewind menu to restore or summarize from a selected point |
/btw <question> | — | Ask a side question without adding to context (ephemeral overlay) |
/context | — | Visualize current context usage as a colored grid |
/branch [name] | /fork | Create a branch of the conversation at the current point |
/rename [name] | — | Give the current session a descriptive name |
Conclusion
Context compression is not an optional optimization — it is a fundamental skill for effective agentic coding. The context window is your most important resource, and managing it actively is the difference between a productive session and one where Claude slowly loses track of what matters.
The tools are simple: /clear for hard resets, /compact for intelligent summarization, /rewind for surgical compression, and /btw for zero-cost questions. The discipline is knowing when to use each one. Start by building the habit of running /clear between tasks. Then add strategic /compact calls with custom instructions. Over time, context management becomes second nature — and your sessions with Claude become dramatically more effective.