Last updated: November 21, 2026
TL;DR
/compact replaces the conversation so far with a structured summary, so the session keeps going with room to spare. Claude Code also does it on its own as the context window fills (Anthropic's context window docs). Compact yourself, with a focus, before a long new task; let auto-compact catch the rest:
- ๐ฏ Compact with a focus:
/compact focus on the auth bug fixkeeps what you name instead of what the automatic pass guesses. - ๐งน Clear instead when the task changes:
/clearstarts a new conversation;/compactkeeps the current one going. - ๐ฆ Know what survives: project-root
CLAUDE.md, auto memory, the plan and recent files come back; path-scoped rules and hook output are summarized away. - โฑ๏ธ Set when auto-compact fires:
/autocompact 500kcompacts earlier for the current model.
Run /compact, with a focus
The command takes optional instructions: /compact [instructions] frees up context "by summarizing the conversation so far," and the instructions tell the summary what to focus on (commands reference).
- Finish the step you're on, so the summary has a clean state to describe.
- Run
/contextto see what is filling the window. It gives a live breakdown by category, including whichCLAUDE.mdand auto memory files loaded. - Run
/compactwith what the next task needs, for example/compact focus on the auth bug fix and the failing test. - Start the next task in the same session.
The docs recommend this before starting a long new task: "The summary keeps what you choose instead of what the automatic pass guesses is important" (context window docs).
/compact or /clear
| You want to | Run | What happens |
|---|---|---|
| Keep working on the same task with more room | /compact | The conversation becomes a summary; the session continues |
| Switch to unrelated work | /clear | A new conversation with empty context; project memory stays |
| Shrink only part of the conversation | /rewind, then Summarize from here or Summarize up to here | The chosen part becomes a summary; the rest stays as it was |
The docs' advice for unrelated work is to clear: "Old conversation crowds out the files you need next and costs tokens on every message." A cleared conversation isn't gone: /resume brings it back, and in the same Claude Code process the rewind menu lists it as the previous session (commands reference).
Summarize part of a session
/rewind, or Esc twice with an empty prompt, opens the rewind menu. Pick a message, then:
- Summarize from here compresses the conversation from that point forward. It suits a long debugging detour after your opening instructions.
- Summarize up to here compresses everything before that point and keeps later messages intact.
To guide either one, highlight it with the arrow keys and type instructions where the row reads add context (optional). Summarizing changes no files, and the original messages stay in the session transcript (checkpointing docs).
What survives compaction
Compaction summarizes the conversation, but much of what loaded at startup comes back from disk. Anthropic's table, in short:
| What | After compaction |
|---|---|
| System prompt and output style | Still apply |
Project-root CLAUDE.md and unscoped rules | Re-injected from disk |
| Auto memory | Re-injected from disk |
| The plan written in plan mode | Re-injected from disk |
| Files Claude read or edited | Up to five re-read, most recently modified first |
| Skills you invoked | Re-injected, up to 5,000 tokens per skill and 25,000 in total, oldest dropped first |
Rules with paths: frontmatter, nested CLAUDE.md | Reloaded on demand when Claude touches a matching file |
| Context that hooks added | Summarized with the conversation |
| Background commands and subagents | Keep running |
Source: What survives compaction.
Three details change how you set things up:
- A file over 5,000 tokens comes back as a path reference without its content, shown as
Referenced fileinstead ofRead. - A rule that must persist should drop its
paths:frontmatter or move to the project-rootCLAUDE.md, because path-scoped rules are summarized away with everything else. - A long skill is truncated from the end, so put its most important instructions near the top of
SKILL.md.
A SessionStart hook that matches the compact source runs after compaction and adds its output back, which is the way to restore context a hook gave the session. Our Claude Code hooks guide covers hook setup.
When auto-compact fires, and how to move it
Claude Code compacts automatically as you approach the limit, so a full window doesn't end the session. By default, models with a native 1M window, the Fable models, Sonnet 5 and later, Haiku 5.5, and Opus 4.7 and later, compact at about 967K tokens. Sonnet 4.6 and Opus 4.6 without extended context compact at the 200K boundary (model configuration docs).
To compact earlier, set the auto-compact window:
/autocompact 500ksaves a window for the current model and applies it now./autocompact autoreturns to the default.autoCompactWindowin a settings file sets one window for every model, such as"autoCompactWindow": 200000.- The
autocompactflag (two hyphens) at launch overrides the saved setting for one session. CLAUDE_CODE_AUTO_COMPACT_WINDOWin scripts and cloud environments takes precedence over the command, the flag and the setting.
Values must be 100K or more: 200000, 500k, 1M, or a bare number from 100 to 1000 meaning thousands in the command and the flag. Claude Code caps the window at the model's context window. One trap: in the environment variable, a number with no suffix means tokens, so 200 there is 200 tokens even though /autocompact 200 means 200,000.
Compacting earlier keeps each request smaller. If you need the opposite, a bigger window rather than a smaller conversation, our Claude Code context window guide covers the 1M window and /context.
Frequently asked questions
What does /compact do in Claude Code?
It replaces the conversation so far with a structured summary to free up context, and the session continues. You can pass instructions, such as /compact focus on the auth bug fix, so the summary keeps what the next task needs.
When does Claude Code auto-compact?
As the context window approaches its limit. Models with a native 1M window, including the Fable models, Sonnet 5 and later, Haiku 5.5 and Opus 4.7 and later, compact at about 967K tokens by default. You can compact earlier with /autocompact.
What is the difference between /compact and /clear?
/compact keeps the same conversation going with a summary in place of the history. /clear starts a new conversation with empty context while keeping project memory. Compact to continue a task; clear when you switch to unrelated work.
Does CLAUDE.md survive compaction?
Yes. The project-root CLAUDE.md, unscoped rules and auto memory are re-injected from disk. Nested CLAUDE.md files and rules with paths: frontmatter are reloaded only when Claude touches a matching file, so put rules that must persist in the root file.
How do I make Claude Code compact earlier?
Run /autocompact with a token count, such as /autocompact 500k, to set the window for the current model. Or set autoCompactWindow in settings for every model, or CLAUDE_CODE_AUTO_COMPACT_WINDOW in scripts, which takes precedence over both.
Can I compact only part of a conversation?
Yes. Run /rewind, pick a message and choose Summarize from here or Summarize up to here. Type instructions in the add context row to guide the summary. No files change, and the original messages stay in the transcript.
Keep what must survive on disk
What comes back after compaction is what lives in files: CLAUDE.md, auto memory, the plan, skills. A rule you only said in chat is one summary away from being lost. Writing it into CLAUDE.md the moment it matters is the habit behind our guide to compound engineering for Claude Code. For moving big reads out of the main window altogether, see our Claude Code subagents guide.
Stop guessing. Score your first workflow.
Score one workflow in about five minutes. Nine questions, a grade, and the specific gap holding it back - no call required.
Related Posts

Claude Code effort levels: which one for which task
Claude Code has five effort levels, from low to max, and the newest models start at medium. What each level is for, the default on each model, which setting wins when several apply, and how ultrathink raises a single turn.

Claude Code on GitLab: pipelines, merge requests and @claude
Claude Code runs on GitLab as a CI/CD job you add yourself: one job, one masked API key, and Claude opens merge requests from your runners. How to set it up, wire @claude mentions, run it on Bedrock or Google Cloud, and cap what each run can touch and spend.

Claude Code GitHub Actions: set it up, and decide what it can touch
Claude Code GitHub Actions runs Claude Code in your repository's workflows, on an @claude mention or a prompt on any GitHub event. How to install it, how it signs in, and the four layers that decide what a run can touch and spend.
