Managing context in Claude Code: /compact versus /clear and how to save tokens
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.
Contents
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,/compactand/cleardo 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
| Component | Content | Room to reduce |
|---|---|---|
| System prompt | Claude Code's own instructions | None |
| CLAUDE.md | User, project and local files | Large (keep it short) |
| Tool definitions | Built-in tools plus every MCP server's tools | Medium (remove unused MCP servers) |
| Conversation history | Prompts, answers, files read, command output | Large (/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 500kFor 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
| Situation | Command |
|---|---|
| Same task, but things are getting slow | /compact |
| Feature finished, starting the next one | /clear |
| Research done, implementation in a separate session | Have the findings written to a file, then /clear |
| After compaction Claude seems to have forgotten the premise | Redo /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
/contextto see whether CLAUDE.md or MCP tool definitions have grown too large /compact(with instructions on what to keep) continues a task;/clearswitches 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?
Part of it is reloaded and part of it is not. The documentation describes each request in three layers, and compaction affects them differently.
| Layer | Content | Changes when |
|---|---|---|
| System prompt | Core instructions, tool definitions | The set of loaded tool definitions changes |
| Project context | CLAUDE.md, auto memory, unscoped rules | Session starts, or after /clear or /compact |
| Conversation | Your messages, Claude's responses, tool results | Every 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.