Back to Blog

/compact in Claude Code: when to compact, and what auto-compact keeps

Alex Kim
7 min read
/compact in Claude Code: when to compact, and what auto-compact keeps

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 fix keeps what you name instead of what the automatic pass guesses.
  • ๐Ÿงน Clear instead when the task changes: /clear starts a new conversation; /compact keeps 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 500k compacts 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).

  1. Finish the step you're on, so the summary has a clean state to describe.
  2. Run /context to see what is filling the window. It gives a live breakdown by category, including which CLAUDE.md and auto memory files loaded.
  3. Run /compact with what the next task needs, for example /compact focus on the auth bug fix and the failing test.
  4. 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 toRunWhat happens
Keep working on the same task with more room/compactThe conversation becomes a summary; the session continues
Switch to unrelated work/clearA new conversation with empty context; project memory stays
Shrink only part of the conversation/rewind, then Summarize from here or Summarize up to hereThe 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:

WhatAfter compaction
System prompt and output styleStill apply
Project-root CLAUDE.md and unscoped rulesRe-injected from disk
Auto memoryRe-injected from disk
The plan written in plan modeRe-injected from disk
Files Claude read or editedUp to five re-read, most recently modified first
Skills you invokedRe-injected, up to 5,000 tokens per skill and 25,000 in total, oldest dropped first
Rules with paths: frontmatter, nested CLAUDE.mdReloaded on demand when Claude touches a matching file
Context that hooks addedSummarized with the conversation
Background commands and subagentsKeep 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 file instead of Read.
  • A rule that must persist should drop its paths: frontmatter or move to the project-root CLAUDE.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 500k saves a window for the current model and applies it now. /autocompact auto returns to the default.
  • autoCompactWindow in a settings file sets one window for every model, such as "autoCompactWindow": 200000.
  • The autocompact flag (two hyphens) at launch overrides the saved setting for one session.
  • CLAUDE_CODE_AUTO_COMPACT_WINDOW in 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.

#claude-code#Compact#Context#Token Usage#ai-agents
Free assessment

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.