Logo Vincent
Back to all posts

Claude Code /compact: Free Up Context, Keep Progress

Claude
Claude Code /compact: Free Up Context, Keep Progress

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:

  1. First clear older tool outputs
  2. If that’s not enough, summarize the entire conversation history
  3. 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 CompactionManual /compact
TriggerWhen approaching context limitAnytime you choose
ControlClaude Code decides what to keepYou can specify focus areas
ProactiveReactive responseActive management
Best forSafety netFine-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 doesCompresses conversation history into a summaryCompletely clears conversation, starts from zeroRolls back to a previous checkpoint
Preserves infoKeeps a summary of key informationKeeps nothingFully preserves everything before the rollback point
Code stateDoesn’t affect codeDoesn’t affect codeCan also revert code changes
Best forConversation too long, needs slimmingSwitching to a completely unrelated taskSomething went wrong, want to go back
AnalogyTidying up notes, keeping key pointsTurning to a fresh pagePressing 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.

© 2026 vincentqiao.com . All rights reserved.