/compact frees up space in Claude Code's context window by replacing the conversation so far with a summary. Run it on its own, or add focus instructions such as /compact focus on the API changes so the summary keeps what matters to you. Claude Code also compacts automatically as you approach the limit, and /clear is the better choice when you're starting unrelated work.
This guide covers how to run /compact, how to write focus instructions, when auto-compaction runs and how to change it, what survives, when to clear instead, and what to do when compaction fails. It's based on Anthropic's commands reference, context window page and model configuration docs, read on 26 October 2026.
What does /compact do in Claude Code?
The commands reference describes /compact [instructions] as: "Free up context by summarizing the conversation so far. Optionally pass focus instructions for the summary."
After it runs, the conversation is a structured summary instead of the full history. Anthropic's docs say the summary keeps your requests and key code snippets, but detailed instructions from early in the conversation may be lost. Startup content such as your project CLAUDE.md is then reloaded from disk.
Three related commands help:
| Command | What it does |
|---|---|
/compact [instructions] |
Summarize the conversation now, with optional focus |
/context |
Show context usage as a colored grid with optimization suggestions |
/autocompact <size> or /autocompact auto |
Set how full the context gets before automatic compaction |
/clear |
Start a new conversation with empty context (aliases /reset, /new) |
Run /context first if you're not sure compaction is needed. It shows what's filling the window, which CLAUDE.md and memory files loaded, and suggestions for context-heavy tools. Pass /context all to expand the breakdown in fullscreen mode.
How do you write good focus instructions?
Focus instructions tell the summary what to keep instead of leaving it to guesswork. Examples from Anthropic's docs:
/compact focus on the auth bug fix
/compact Focus on the API changes
/compact Focus on code samples and API usage
/compact keep only the plan and the diff
Compacting with a focus is most useful right before you start a long new phase of the same task: you choose what carries forward.
You can also set standing instructions in CLAUDE.md so every compaction, including automatic ones, follows them. The costs docs show this pattern:
# Compact instructions
When you are using compact, please focus on test output and code changes
The best practices page gives another example: "When compacting, always preserve the full list of modified files and any test commands." The Claude Code memory guide explains where CLAUDE.md files live.
When does Claude Code auto-compact?
Claude Code compacts automatically as you approach the context limit, so a full window doesn't end your session. The default point depends on the model, per the model configuration docs:
| Session | Default auto-compact point |
|---|---|
| Models with a native 1M window (Fable models, Sonnet 5 and later, Haiku 5.5, Opus 4.7 and later) | About 967K tokens |
| Sonnet 4.6 and Opus 4.6 without extended context | The 200K boundary |
CLAUDE_CODE_DISABLE_1M_CONTEXT=1 on a native 1M model |
The 200K boundary |
| Unrecognized model ID, such as a gateway alias | The window Claude Code assumes for it (200K) |
| Other sessions | The model's context limit |
To compact earlier, run /autocompact with a size from 100K to 1M tokens, such as /autocompact 500k. Claude Code saves it for the current model; /autocompact auto returns to the tuned default. Other ways to set the same window:
autoCompactWindowin a settings file, for every modelclaude --autocompact 300kfor one launchCLAUDE_CODE_AUTO_COMPACT_WINDOW=200000in scripts and cloud environments, which takes precedence over the othersCLAUDE_AUTOCOMPACT_PCT_OVERRIDE, a percentage that can only lower the trigger point
Note that a bare number means different things: /autocompact 200 means 200,000 tokens, while CLAUDE_CODE_AUTO_COMPACT_WINDOW=200 means 200 tokens.
Idle compaction. Claude Code can also compact a long conversation you've stepped away from, shortly before a one-hour prompt cache expires. It applies after about 54 minutes idle once context has reached 200,000 tokens by default, and shows a line starting with Compacted while idle, before the prompt cache expired. Set idleCompaction to false, or use the Idle compaction row in /config, to turn it off.
Turning auto-compaction off. Set autoCompactEnabled to false, toggle Auto-compact in /config, or set DISABLE_AUTO_COMPACT=1. Manual /compact still works. DISABLE_COMPACT=1 disables both automatic and manual compaction.
What survives compaction?
The context window docs list what happens to each kind of content. The short version:
- Reloaded from disk: project-root CLAUDE.md and unscoped rules, auto memory, and the plan Claude wrote in plan mode
- Rebuilt: skill descriptions (v2.1.304 or later) and, if the session started with one, the git status snapshot
- Re-read: up to five files Claude read or edited, most recently modified first; files over 5,000 tokens come back as a path reference only
- Re-injected with caps: invoked skill bodies, up to 5,000 tokens per skill and 25,000 total
- Reloaded on demand: nested CLAUDE.md files and rules with
paths:frontmatter, when Claude next touches a matching file - Summarized: everything said only in conversation, including context hooks added
So if a rule must survive, put it in the project-root CLAUDE.md rather than in chat, and keep the most important instructions near the top of a SKILL.md. The Claude Code context window guide goes deeper on what fills the window in the first place.
Should you clear or compact?
| Situation | Use |
|---|---|
| Same task, long session, want to keep going | /compact with a focus |
| Starting unrelated work | /clear |
| Only the early part of the session is noise | /rewind, then Summarize up to here |
| A detour at the end went nowhere | /rewind, then Summarize from here or restore the conversation |
| A huge file or log is hogging space | Re-read it in chunks, or hand the work to a subagent |
Cost matters too. The costs docs note that /compact reads the conversation it summarizes, so compacting a large context is itself a large request, while /clear costs nothing. Old conversation also crowds out the files you need next. After /clear, you can get back to the earlier conversation with /resume.
To compact only part of a conversation, run /rewind or press Esc twice with an empty prompt, pick a message, and choose a summarize option. Type guidance where the row reads add context (optional) to steer the summary. The Claude Code checkpoints guide covers the rest of the rewind menu, and the subagents guide shows how to keep large reads out of your main context.
What if compaction fails or keeps repeating?
Not enough messages to compact.The conversation is a single exchange, often one huge paste. Run/clearand resend with less content.Autocompact is thrashing. A file or tool output refilled the window right after each summary, so Claude Code stopped retrying. Ask Claude to read the file in smaller pieces, run/compact keep only the plan and the diff, move the work to a subagent, or/clear.Request too large (max 32MB)from piled-up screenshots./compactdrops accumulated images and attachments; see the Claude Code images guide.- Hooks. A
PreCompacthook can block compaction, and aSessionStarthook matching thecompactsource can add context back afterward. The hooks docs cover both.
If rate limits rather than context are the problem, see the Claude Code rate limits guide.
How do you keep compact instructions handy?
Focus instructions you reuse, like a "keep the plan, the diff and the failing test names" summary prompt, are worth saving. Kipt keeps prompts as Markdown in a private GitHub repo you own, with blanks such as {{focus:the current bug}}, so you can copy a ready /compact line into any session.