Claude Code /compact vs /clear: Managing Context Without Losing Your Place
When to compact, when to clear, and when to let auto-compaction handle it. What /compact actually does, custom compaction instructions, the auto-compact window and /autocompact, why compacting a huge session is itself expensive, and a workflow that keeps sessions sharp.
Long Claude Code sessions degrade in two ways: they get more expensive (every request carries the whole conversation) and less focused (old, irrelevant detail competes with what matters now). Claude Code gives you two tools for this — /compact and /clear — plus automatic compaction. Knowing which to use, and when, is one of the highest-leverage habits you can build.
Behaviour described here matches Anthropic's Claude Code documentation as of October 2026.
The two commands
/clear starts a fresh session. The conversation history is dropped from context; Claude starts again with only your CLAUDE.md, settings and the files on disk. It costs nothing to run. The old session isn't deleted — you can return to it with /resume.
/compact summarises the conversation so far and replaces the history with that summary. You keep continuity — decisions made, files touched, what's left to do — in far fewer tokens.
/compact
/compact Focus on the migration plan, the failing test output, and open TODOs
The optional text tells Claude what to preserve. In a brand-new session, /compact replies Not enough messages to compact.
When to use which
| Situation | Use |
|---|---|
| Switching to an unrelated task | /clear |
| Same task, conversation is long and noisy | /compact with focus instructions |
| You went down a wrong path and want to start over | /clear (or rewind) |
| Long-running task that must keep its thread | /compact at a natural milestone |
| Quality has dropped and you're not sure why | /context first, then decide |
The rule of thumb from Anthropic's cost guidance: clear between unrelated tasks. Stale context isn't free — you pay for it on every subsequent message, and it can mislead Claude.
Auto-compaction
You don't have to compact manually. When the conversation approaches the auto-compact window, Claude Code summarises older history automatically. On models with a native 1-million-token context window, that happens at about 967K tokens by default.
You can make it trigger earlier — useful because a giant context is slow and costly long before it's full:
/autocompact 300k # in a session
claude --autocompact 300k # at startup
export CLAUDE_CODE_AUTO_COMPACT_WINDOW=300000
Compaction isn't free
/compact has to read the conversation it summarises. Compacting a huge session is itself a large request. Two consequences:
- Compact at milestones, not at the brink. Summarising 150K tokens is cheaper than summarising 900K, and the summary is better because there's less to squeeze.
- If you don't need continuity,
/clearinstead. It's free.
Tell Claude what to keep
Generic summaries lose specific things you'll need: an exact error message, a command that worked, a decision and the reason for it. Give focus instructions every time you compact manually, or set defaults in your CLAUDE.md:
# Compact instructions
When compacting, preserve: the current task and acceptance criteria,
decisions made and why, exact failing test names and error messages,
and the list of files changed.
(How to write a CLAUDE.md that actually helps.)
Put durable knowledge on disk, not in the conversation
Compaction is lossy by design. Anything that must survive — the plan, the architecture decision, the list of remaining steps — belongs in a file:
- write the plan to
docs/plan.mdbefore a long implementation, - keep a
TODO.mdor checklist the agent updates as it goes, - put permanent project conventions in
CLAUDE.md.
Then /clear becomes painless: start fresh and say "continue from docs/plan.md". This is the same principle behind spec-driven development and context engineering.
Diagnose with /context
Before compacting or clearing because "it feels slow", run /context. It shows what's occupying the window: a large CLAUDE.md, many MCP tool definitions, one enormous file read an hour ago, or a pasted log. Sometimes the right fix is disabling an MCP server or moving instructions into skills, not compaction.
A workflow that keeps sessions sharp
- One task per session. Start with
/clear(or a new session) for each new task. - Plan first in plan mode; save the plan to a file.
- Compact at milestones — after a phase is done and tests pass — with focus instructions.
/renamesessions you might want again, then/clear, and later/resumeto find them. (How to resume a Claude Code session.)- Watch usage with
/usage; long sessions draw on plan limits faster than their activity suggests.
Cloud sessions
In Claude Code cloud sessions, /compact and /context work, but /clear doesn't — start a new session from the sidebar instead. (Claude Code on the web, desktop or terminal.)
The summary
/clear= fresh start, free, history kept for/resume. Use between unrelated tasks./compact= summarise and continue. Use at milestones, with focus instructions.- Auto-compaction runs near the window limit; lower it with
/autocompactif you prefer. - Keep durable knowledge in files so clearing never loses anything important.
EasySpawn runs Claude Code on a persistent server where your plans, TODO files and project history live on disk between sessions — so clearing context never means losing your place. See how it works or join the waitlist.
Related: How to Keep Claude Code Costs Down · Why Your AI Agent Keeps Forgetting · Claude Code Slash Commands · How to Change the Model in Claude Code
Keep reading
How to Keep Claude Code Costs Down (Without Making It Worse)
Claude Code usage is driven less by how much you ask and more by how much context every request carries. Where the tokens actually go, how to see them, and the habits that cut usage — clearing between tasks, picking the right model, trimming CLAUDE.md, and planning before building.
The Claude Agent SDK: Building Your Own Agents on Claude Code's Engine
The Claude Agent SDK gives you the agent loop behind Claude Code as a library: file and shell tools, permissions, MCP, subagents and sessions. When to use it instead of the plain API or claude -p, a minimal TypeScript example, custom tools, permissions, and running it safely.