Managing context in Claude Code: /compact versus /clear and how to save tokens

Claude Code Published: Updated:

Why long Claude Code sessions slow down, when to use /compact, /clear and /context, how automatic compaction works, and habits that keep token usage down.

Verified on Sep 30, 2026 These tools change quickly. Please also check the latest official documentation. How articles are researched and verified
Contents
  1. What fills the context window
  2. /compact: summarize and continue
  3. /clear: discard and start over
  4. Choosing between them
  5. Save state with a PreCompact hook
  6. Habits that reduce usage
    1. Keep CLAUDE.md short
    2. Remove MCP servers you do not use
    3. Do not feed it huge files or output
    4. Delegate research to subagents
  7. Resuming sessions
  8. Summary

Long sessions get slow, and Claude starts to act as if it forgot the rules you gave at the beginning. The usual cause is a crowded context window. Conversation history, files that were read and tool output all accumulate; as the limit approaches, quality drops and compaction kicks in, losing detail.

This article covers the three commands for inspecting, compressing and discarding context, plus the habits that reduce usage in the first place.

KEY POINT

What you will learn

  • What /context, /compact and /clear do and when to use each
  • How automatic compaction works and how to protect information you cannot afford to lose
  • Everyday habits that cut context consumption

What fills the context window

ComponentContentRoom to reduce
System promptClaude Code's own instructionsNone
CLAUDE.mdUser, project and local filesLarge (keep it short)
Tool definitionsBuilt-in tools plus every MCP server's toolsMedium (remove unused MCP servers)
Conversation historyPrompts, answers, files read, command outputLarge (/compact, /clear)

/context shows this breakdown and the remaining room. It is worth running once: people are often surprised by how much MCP tool definitions or a long CLAUDE.md take up.

/compact: summarize and continue

/compact summarizes the conversation so far and replaces the history with that summary. Use it mid-task when the context is getting tight.

/compact

You can tell it what to keep:

/compact Keep the list of files changed so far and the details of the failing tests

The summary is written by Claude, and without guidance it may drop specifics such as exact error messages or the naming convention you settled on. Naming what matters is the safe option.

用語解説

Automatic compaction: When usage nears the limit, compaction runs even if you never call /compact. Where that point sits depends on your model and configuration. Because it arrives without warning, anything important should also live in CLAUDE.md or a notes file.

Claude Code shows how much context is left before that pass runs — the indicator people search for as "context left until auto-compact". The exact wording of that indicator could not be confirmed in the official documentation, but moving the point where compaction fires is documented: pass a token count to /autocompact to set how full the context window gets before the automatic pass runs.

/autocompact 500k

For the accepted values and the overrides, see Changing when Claude Code auto-compacts.

/clear: discard and start over

/clear wipes the conversation history entirely. CLAUDE.md and settings are reloaded, so this is the right command when you move on to a new task.

/clear

Carrying over context from the previous task makes Claude reach for unrelated files or stick to an old plan. Clearing at task boundaries helps both accuracy and cost.

Choosing between them

SituationCommand
Same task, but things are getting slow/compact
Feature finished, starting the next one/clear
Research done, implementation in a separate sessionHave the findings written to a file, then /clear
After compaction Claude seems to have forgotten the premiseRedo /compact with explicit keep instructions, or write it into CLAUDE.md

Save state with a PreCompact hook

The PreCompact hook fires right before compaction. Use it to dump the working state to a file:

{
  "hooks": {
    "PreCompact": [
      {
        "hooks": [
          {
            "type": "command",
            "command": "git status --short > .claude/last-status.txt"
          }
        ]
      }
    ]
  }
}

Hook basics are covered in Run lint and format automatically with Claude Code hooks.

Habits that reduce usage

Keep CLAUDE.md short

CLAUDE.md is loaded every session. Leave out directory listings and long specs; move documents behind @ imports or into per-directory CLAUDE.md files. See How to write CLAUDE.md.

Remove MCP servers you do not use

Every connected server's tool definitions cost context even when unused. Remove project-irrelevant servers with claude mcp remove, or keep them at local scope and enable them only when needed.

Do not feed it huge files or output

cat on a giant log or a search through node_modules burns context fast. Ask for "the first 50 lines" or "only the matching lines from grep". For long test runs, | tail -n 30 helps.

Delegate research to subagents

Broad codebase exploration in a subagent keeps the files it reads out of your main context; only the summary comes back.

Resuming sessions

claude --continue and claude --resume restore a previous session's context (the summary, if it was compacted). When you pick up work the next day, run /context first and /compact if needed before starting.

Summary

  • Use /context to see whether CLAUDE.md or MCP tool definitions have grown too large
  • /compact (with instructions on what to keep) continues a task; /clear switches tasks
  • Automatic compaction can happen any time, so keep important facts in CLAUDE.md or files
  • Avoid feeding large output, and push research into subagents

FAQ

Should I use /compact or /clear?
Use /compact to keep working on the same task (summarize and continue). Use /clear when you move to a different task (wipe the history and start fresh).
When does automatic compaction happen?
When context usage approaches the limit. A PreCompact hook can save important state right before it runs.
How can I see how much context is in use?
Run /context. It breaks usage down into system prompt, tool definitions, conversation history and so on.
What does "context left until auto-compact" mean?
It is how much context remains before automatic compaction runs. To move that point, pass a token count to /autocompact, for example /autocompact 500k.

Questions & answers

Q. After resuming from a compacted context, the agent starts work without reading the files CLAUDE.md tells it to read first. Are instruction files not reloaded after compaction?

Answer by the editorsSource: GitHub anthropics/claude-code Issue #102190

Part of it is reloaded and part of it is not. The documentation describes each request in three layers, and compaction affects them differently.

LayerContentChanges when
System promptCore instructions, tool definitionsThe set of loaded tool definitions changes
Project contextCLAUDE.md, auto memory, unscoped rulesSession starts, or after /clear or /compact
ConversationYour messages, Claude's responses, tool resultsEvery turn

CLAUDE.md itself is reloaded. The prompt-caching documentation says of compaction: "It reloads project context from disk, which cache-hits only if CLAUDE.md and memory are unchanged since the session started." From disk, explicitly.

What is not reloaded is the files CLAUDE.md told Claude to read. Those arrived through the Read tool, so they live in the conversation layer — and "Compaction replaces your message history with a summary." The instruction survives; the content it pulled in gets folded into the summary.

Survives compaction:  the CLAUDE.md line "read the memory index first"
Does not survive:     the actual contents of the memory index and protocol files

Subdirectory CLAUDE.md files and rules with paths: frontmatter are in the conversation layer too: "After it loads, the content is part of the conversation history."

A PostCompact hook cannot add context

If you are looking for a way to force the re-read automatically, check the hook event table first. PostCompact fires "After context compaction completes", but its exit-code-2 behavior is "Shows stderr to user only" and it has no JSON decision fields. The events that can inject context, such as SessionStart with additionalContext, do not include PostCompact.

In practice, the reliable move is one explicit instruction right after compaction: "re-read the memory index before continuing." Running /compact yourself at a break between tasks is also easier to manage than letting auto-compaction fire mid-task.

Separately, Claude Code v2.1.293 fixed a bug where Claude sometimes treated its own actions from before a compaction as done after it. If you are on an older version, update first.

More detail: Context management with /compact and /clear.

Primary sources

This article was drafted by AI from official documentation and reviewed by the site operator before publishing. Found a mistake? Let us know via the contact page.