
Claude Code /compact: How It Works, Auto-Compact and /compact vs /clear
What Compaction Keeps, What It Loses, and How to Use It Well
Disclosure: Membase is our product. You don’t need it to use /compact well, and we cover the built-in commands first. Facts were checked against Anthropic’s Claude Code documentation in November 2026.
Quick answer
/compact replaces your Claude Code conversation with a structured summary, so a long session can keep going with a much smaller context.
- Run it yourself: type /compact, or add what to keep: /compact keep the API decisions and the failing tests.
- Auto-compact: Claude Code also compacts on its own when the context approaches the limit. It’s on by default; turn it off in /config.
- What survives: your CLAUDE.md, auto memory, skills and plan reload from disk. The conversation itself becomes a summary, so exact details from the chat can be lost.
- /compact vs /clear: /compact keeps a summary of the session; /clear starts a fresh session with nothing from the conversation.
Rule of thumb: /compact to continue the same task, /clear to start a different one. Save important decisions before either.
In this guide: What /compact does · What survives compaction · Auto-compact · /compact vs /clear · How to compact well · Fix “Autocompact is thrashing” · Keep decisions after compaction · FAQ
What /compact does
Everything in a Claude Code session uses context: your messages, Claude’s replies, every file it reads, command output and tool results. As a session grows, the context fills, costs rise, and answers can get less accurate, an effect known as context rot.
/compact summarizes the conversation history and starts the session again from that summary. You stay in the same session, with the same files and plan, but carry far fewer tokens.
You can tell it what matters most:
/compact Focus on code samples and API usage
To set standing instructions for every compaction in a project, add a section to the CLAUDE.md at your project root:
# Compact instructions
When you are using compact, please focus on test output and code changes
Check how full your context is with /context, or /usage for token usage.
What survives compaction
Compaction doesn’t treat everything the same way. Some things reload from disk; the conversation is summarized.
| What | After compaction |
|---|---|
| System prompt and output style | Still apply |
| Project-root CLAUDE.md and unscoped rules | Reloaded from disk |
| Auto memory | Reloaded from disk |
| Skill descriptions | Rebuilt and reloaded |
| The plan from plan mode | Reloaded from disk |
| Nested CLAUDE.md files and path-scoped rules | Reloaded when needed |
| Files Claude read or edited | Up to five re-read, most recently modified first |
| Skills you invoked | Reloaded, up to 5,000 tokens each and 25,000 in total |
| Background commands and subagents | Keep running |
| Your conversation, and context hooks added | Summarized |
What can get lost: anything that lived only in the conversation. Exact error messages, why you rejected an approach, a decision you agreed in chat, a command that finally worked. The summary keeps the gist; details it leaves out are gone from the session.
That’s why the safest habit is to write important decisions to a file or to memory before you compact.
Auto-compact
You don’t have to run /compact yourself. With auto-compact, Claude Code compacts the conversation automatically when the context approaches the limit.
- On by default. Turn it off or on in /config, under Auto-compact. This saves “autoCompactEnabled” to your settings.
- The manual command still works when auto-compact is off.
- Choose when it triggers. Run /autocompact with a size, for example /autocompact 500k, to compact earlier on models with a large context window. /autocompact auto goes back to the default for your model.
- Idle compaction. If you step away from a long conversation, Claude Code can compact it before your next message, so it sends a summary instead of the whole history. Turning auto-compact off also turns this off.
To turn auto-compact off in settings:
{
"autoCompactEnabled": false
}
Auto-compact keeps long sessions running, but you don’t choose what the summary keeps. If a session matters, compact yourself first, with instructions.
/compact vs /clear
| /compact | /clear | |
|---|---|---|
| What it does | Replaces the conversation with a summary | Starts a new session with an empty conversation |
| Keeps from the conversation | A summary | Nothing |
| CLAUDE.md, auto memory, skills | Reloaded | Loaded fresh |
| Best for | Continuing the same task with less context | Switching to unrelated work |
| Can you go back? | The session continues from the summary | Yes: /rename first, then /resume the old session |
Anthropic’s own advice is to use /clear when you switch to unrelated work, because stale context wastes tokens on every later message. Use /compact when you’re mid-task and need room.
How to compact well
- Compact at a natural break, after a step is done, rather than in the middle of debugging.
- Say what to keep. /compact keep the migration plan, the schema changes and the two failing tests.
- Save decisions first. Ask Claude to write key decisions to a notes file, CLAUDE.md or memory, then compact.
- Use subagents for big reads. A subagent reads large files or logs in its own context and returns only a summary, so your session fills more slowly.
- Clear instead of compacting when the next task has nothing to do with the last one.
- Check the result. After compacting, ask Claude to restate the plan and the open issues before continuing.
Fix “Autocompact is thrashing”
If you see Autocompact is thrashing: the context refilled to the limit…, compaction worked, but a large file or tool output filled the context again straight away, several times in a row. Claude Code stops retrying.
To recover:
- Ask Claude to read the large file in smaller pieces, such as a line range or one function.
- Run /compact with a focus that drops the large output, for example /compact keep only the plan and the diff.
- Move the large-file work to a subagent.
- Run /clear if you no longer need the earlier conversation.
Keep decisions after compaction
Compaction and /clear both drop what was only said in chat. Claude Code has two built-in places that survive them:
- CLAUDE.md for rules you write. It reloads after every compaction.
- Auto memory for notes Claude saves itself. It reloads too, but stays on one machine.
More on both: Claude Code Memory: How It Works and How to Add Persistent Memory.
For decisions that should also survive a new machine, a teammate’s session or another AI tool, use a memory layer. Membase saves your Claude Code sessions to one memory outside the context window, and with its Claude Code hooks, brings the relevant parts back at session start and on every prompt, including after /compact or /clear.
Connect it once:
claude mcp add --transport http membase https://api.app.membase.com/mcp-http \
--header "Authorization: Bearer $MEMBASE_API_KEY"
Then install the hooks for automatic recall: Membase Use Case 01. Get a free key at app.membase.com; Membase is free during its beta.
Quick links
- Context Rot: What It Is, Why It Happens and How to Fix It: why long sessions get worse
- Claude Code Memory: How It Works and How to Add Persistent Memory: CLAUDE.md and auto memory
- AGENTS.md vs CLAUDE.md vs SKILL.md: keep instruction files short
- Explore the context window: Claude Code’s official documentation
- Manage costs effectively: official tips on /clear and /compact