Claude Code /compact: Auto Compact and What Survives It
Claude Code compacts automatically as context fills, so a full window doesn't end your session. How /compact works, how to tune auto compact, and what it keeps.
- When the context window nears its limit, Claude Code summarizes the conversation (compaction) so the session keeps going. Check usage with /context.
- Run /compact yourself before a long task and pass instructions to choose what the summary keeps. Put anything you need every time in the project-root CLAUDE.md, which is re-read from disk after compaction.
- Set the auto-compact window with /autocompact (100K to 1M tokens), --autocompact or CLAUDE_CODE_AUTO_COMPACT_WINDOW; DISABLE_AUTO_COMPACT=1 turns automatic compaction off.
Claude Code’s context window has a limit. As a session approaches it, Claude Code summarizes the conversation history to make room. That’s compaction, and it’s why a full context window doesn’t end your session. It’s also why an instruction Claude was following a minute ago can seem to vanish. This guide covers checking usage, running /compact, tuning automatic compaction, what survives a summary, /compact versus /clear, keeping important things outside the summary, and the cost side.
In our test environment (macOS, Claude Code 2.1.311, on 2026-10-27) we ran claude --version and claude --help only. We did not compact a real session (not run). Everything about compaction behavior below comes from the docs.
Check how full the context is
| Command | What it shows |
|---|---|
/context | Context usage as a colored grid, with suggestions for context-heavy tools, memory bloat and capacity warnings. It also shows which CLAUDE.md and auto memory files loaded |
/usage | Session cost, plan usage limits and activity stats. /cost and /stats are aliases |
/memory | Opens the loaded CLAUDE.md and auto memory files for editing |
Sources: https://code.claude.com/docs/en/commands and https://code.claude.com/docs/en/context-window, checked 2026-10-27
You can also keep usage on screen all the time with a status line; see How to Set Up the Claude Code Status Line.
Running /compact
/compact replaces the conversation so far with a structured summary. Pass instructions to steer what it keeps:
/compact focus on the auth bug fix and the list of failing tests
The automatic pass guesses what matters. Running /compact yourself before a long new task lets you decide. Since v2.1.198, the summary request inherits your session’s extended thinking setting.
To summarize only part of the conversation, run /rewind, or press Esc twice with the input empty, then select a message:
- Summarize from here compresses everything from that message forward
- Summarize up to here compresses everything before it and keeps later messages intact
Either way, a Summarized conversation marker appears where the compressed messages were.
Sources: https://code.claude.com/docs/en/context-window and https://code.claude.com/docs/en/checkpointing, checked 2026-10-27
If you want the same focus every time, put compaction instructions in your project-root CLAUDE.md. The docs’ example:
# Compact instructions
When you are using compact, please focus on test output and code changes
Source: https://code.claude.com/docs/en/costs, checked 2026-10-27
Tuning auto compact
Claude Code compacts on its own as you approach the limit, and the automatic pass works the same way as /compact. When it runs depends on your model and configuration. The per-model defaults live on the docs’ model configuration page, which we didn’t have a current copy of, so we don’t quote a number here.
Ways to change it:
/autocompact 500ksets the auto-compact window, meaning how full the context gets before the automatic pass runs. It saves the value to your user settings and applies it now./autocompact autoreturns to the window tuned for your model, and/autocompactwith no argument opens a dialog showing the current value. Needs v2.1.221 or later.--autocompact <auto|tokens>at launch sets it for that session only, without touching saved settings. Same values as/autocompact, v2.1.221 or later. Ourclaude --helpon 2.1.311 lists it as “auto, or 100k–1M tokens”.CLAUDE_CODE_AUTO_COMPACT_WINDOWsets it from the environment, from100000to1000000tokens. Suffixed values such as200kneed v2.1.304 or later.CLAUDE_AUTOCOMPACT_PCT_OVERRIDE(1-100) triggers compaction at a percentage of the window. It can only lower the threshold, never raise it, and applies to subagents too.
To stop automatic compaction, set DISABLE_AUTO_COMPACT=1. It overrides the autoCompactEnabled setting and also turns off idle compaction. The manual /compact command still works. DISABLE_COMPACT=1 turns off both automatic compaction and /compact.
If you route through an LLM gateway with a model ID Claude Code doesn’t recognize, it compacts at the window it assumes for that ID. CLAUDE_CODE_MAX_CONTEXT_TOKENS corrects the assumed window, and CLAUDE_CODE_DISABLE_UNKNOWN_MODEL_WINDOW_ENFORCEMENT=1 (v2.1.223 or later) skips proactive compaction for such IDs.
Sources: https://code.claude.com/docs/en/commands, https://code.claude.com/docs/en/cli-reference, https://code.claude.com/docs/en/env-vars and https://code.claude.com/docs/en/context-window, checked 2026-10-27
Idle compaction
Claude Code can also compact a long conversation while your terminal session sits idle, shortly before a one-hour prompt cache expires. It applies once the context reaches CLAUDE_CODE_IDLE_COMPACT_MIN_TOKENS (default 200000; values below 100000 are raised to 100000) and needs v2.1.290 or later. It’s on by default for conversations that qualify; set idleCompaction to false to turn it off. It’s a flag-gated feature, so it can be missing in the first session after an install or upgrade.
Source: https://code.claude.com/docs/en/env-vars, checked 2026-10-27
What survives compaction
The rows that matter most day to day, from the docs’ table:
| Content | After compaction |
|---|---|
| System prompt and output style | Still apply |
Project-root CLAUDE.md and rules without paths: | Re-injected from disk |
| Auto memory | Re-injected from disk |
| Skill descriptions | Rebuilt from your current skills and re-injected (v2.1.304 or later) |
| The plan written in plan mode | Re-injected from disk |
| Git status snapshot | A fresh one is read, if the session started with one |
Rules with paths: and nested CLAUDE.md files | Summarized away, then reloaded on demand when Claude touches a matching file |
| Files Claude read or edited | Up to five re-read, most recently modified first. A file over 5,000 tokens comes back as a path reference (Referenced file) without its content |
| Invoked skill bodies | Re-injected, capped at 5,000 tokens per skill and 25,000 in total; oldest dropped first, and truncation keeps the start of the file |
| Background commands and subagents | Keep running; Claude is reminded which ones are still going |
| Context hooks added earlier | Summarized with the rest of the conversation |
SessionStart hooks matching the compact source | Run again, and their output is added to the compacted context |
Sources: https://code.claude.com/docs/en/context-window and https://code.claude.com/docs/en/memory, checked 2026-10-27
If an instruction disappeared, it was given only in conversation, lives in a nested CLAUDE.md that hasn’t reloaded, or is a path-scoped rule that hasn’t matched a file since. To make a rule survive, drop its paths: frontmatter or move it to the project-root CLAUDE.md. For skills, put the most important instructions near the top of SKILL.md, or invoke the skill again to restore its full content. To keep CLAUDE.md small enough to re-inject cheaply, see Trim Your CLAUDE.md.
/compact or /clear
| Situation | Use |
|---|---|
| Same task, but the conversation got long | /compact, with instructions for what to keep |
| Switching to unrelated work | /clear |
The docs recommend /clear between unrelated tasks: old conversation crowds out the files you need next and costs tokens on every message. /compact reads the conversation it summarizes, so compacting a large context is itself a large request. When you don’t need continuity, /clear costs nothing. Run /rename before clearing if you want to find the old session again with /resume.
Sources: https://code.claude.com/docs/en/context-window and https://code.claude.com/docs/en/costs, checked 2026-10-27
If you keep correcting Claude on the same point, a fresh start often beats another summary; see Don’t Make It Fix the Same Thing Thrice.
Keep important things outside the summary
You can steer a summary, but you can’t fully control what it keeps. Put what matters where compaction can’t lose it:
- Write decisions to a plan or notes file. Files can be read again after compaction
- Keep facts you need every session in the project-root CLAUDE.md, which is re-read from disk
- Hand large reads to a subagent. The file contents stay in its own context window, and only the summary comes back to yours
For setting up subagents, see our Claude Code subagents guide.
Source: https://code.claude.com/docs/en/context-window, checked 2026-10-27
The cost side
Token costs scale with context size. Claude Code sends your full conversation with every request. Prompt caching re-reads that history at the cached token rate, so a one-line question in a session that has been open all day still draws usage for the whole conversation. Auto compaction is one of the ways Claude Code keeps that in check.
Source: https://code.claude.com/docs/en/costs, checked 2026-10-27
If you’re running into plan limits, see When You Hit Claude Code’s Usage Limit.
Related articles
-
Claude Code Setup and Daily Operations: where config files, instructions, daily commands and IDE setup live.
Summary
- Check usage with
/context./usageadds cost and plan limits. /compactwith instructions lets you choose what the summary keeps. Use/rewindto summarize only part of the conversation.- Move the auto-compact window with
/autocompact,--autocompactorCLAUDE_CODE_AUTO_COMPACT_WINDOW, from 100K to 1M tokens.DISABLE_AUTO_COMPACT=1turns it off. - The project-root CLAUDE.md, auto memory and your plan are re-read after compaction. Path-scoped rules and nested CLAUDE.md files are not, until a matching file is touched.
- Use
/compactto continue a task and/clearto switch tasks. - Write decisions to files and send heavy reads to subagents.
FAQ
- What does /compact do in Claude Code?
- It summarizes the conversation so far to free context space. You can pass instructions, such as /compact focus on the auth bug fix, so the summary keeps what you choose instead of what the automatic pass guesses is important.
- When does Claude Code auto compact?
- As the context window approaches its limit. The exact point depends on your model and configuration, and you can move it with /autocompact, which accepts 100K to 1M tokens or auto.
- How do I turn off auto compact in Claude Code?
- Set DISABLE_AUTO_COMPACT=1. It turns off automatic and idle compaction and overrides the autoCompactEnabled setting, while the manual /compact command still works. DISABLE_COMPACT=1 turns off /compact as well.
- Why did Claude forget my instructions after compaction?
- Instructions given only in conversation, rules with paths: frontmatter and nested CLAUDE.md files are summarized away. The project-root CLAUDE.md and unscoped rules are re-read from disk, so move rules that must persist there.
- Should I use /compact or /clear?
- Use /compact to continue the same task in a long conversation, and /clear when switching to unrelated work. /compact is itself a large request because it reads the conversation, while /clear costs nothing.