# Claude Code /compact: How It Works, Auto-Compact and /compact vs /clear

*What Compaction Keeps, What It Loses, and How to Use It Well*

Published: 2026-11-18 · Author: Membase Team  
Source: https://membase.com/blog/claude-code-compact

> 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](https://membase.com/blog/claude-code-compact#what-compact-does) · [What survives compaction](https://membase.com/blog/claude-code-compact#what-survives-compaction) · [Auto-compact](https://membase.com/blog/claude-code-compact#auto-compact) · [/compact vs /clear](https://membase.com/blog/claude-code-compact#compact-vs-clear) · [How to compact well](https://membase.com/blog/claude-code-compact#how-to-compact-well) · [Fix “Autocompact is thrashing”](https://membase.com/blog/claude-code-compact#fix-autocompact-is-thrashing) · [Keep decisions after compaction](https://membase.com/blog/claude-code-compact#keep-decisions-after-compaction) · [FAQ](https://membase.com/blog/claude-code-compact#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](https://membase.com/blog/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:

```text
/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:

```markdown
# 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:

```json
{
  "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

1. **Compact at a natural break**, after a step is done, rather than in the middle of debugging.
1. **Say what to keep.** /compact keep the migration plan, the schema changes and the two failing tests.
1. **Save decisions first.** Ask Claude to write key decisions to a notes file, CLAUDE.md or memory, then compact.
1. **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.
1. **Clear instead of compacting** when the next task has nothing to do with the last one.
1. **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:

1. Ask Claude to read the large file in smaller pieces, such as a line range or one function.
1. Run /compact with a focus that drops the large output, for example /compact keep only the plan and the diff.
1. Move the large-file work to a subagent.
1. 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](https://membase.com/blog/claude-code-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:

```bash
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](https://membase.com/blog/membase-use-case-01-coding-agents). Get a free key at [app.membase.com](https://app.membase.com/); Membase is free during its beta.

## Quick links

- [Context Rot: What It Is, Why It Happens and How to Fix It](https://membase.com/blog/context-rot): why long sessions get worse
- [Claude Code Memory: How It Works and How to Add Persistent Memory](https://membase.com/blog/claude-code-memory): CLAUDE.md and auto memory
- [AGENTS.md vs CLAUDE.md vs SKILL.md](https://membase.com/blog/agents-md-vs-claude-md): keep instruction files short
- [Explore the context window](https://code.claude.com/docs/en/context-window): Claude Code’s official documentation
- [Manage costs effectively](https://code.claude.com/docs/en/costs): official tips on /clear and /compact

## Frequently Asked Questions

### What does /compact do in Claude Code?

It replaces the conversation with a structured summary, so the session can continue with far less context. CLAUDE.md, auto memory, skills and your plan reload from disk.

### What is auto-compact in Claude Code?

Claude Code compacts the conversation automatically when the context approaches the limit. It’s on by default; turn it off in /config or with “autoCompactEnabled”: false.

### What is the difference between /compact and /clear?

/compact keeps a summary of the conversation and continues the session. /clear starts a new session with none of the conversation. Use /compact mid-task and /clear when switching to unrelated work.

### Does /compact lose information?

It can. Anything that existed only in the conversation, such as exact errors or decisions made in chat, is reduced to a summary. Save important decisions to a file or memory first.

### Can I tell /compact what to keep?

Yes. Add instructions after the command, such as /compact focus on the API changes, or add a “Compact instructions” section to your project’s CLAUDE.md.

### How do I turn off auto-compact in Claude Code?

Toggle Auto-compact in /config, or set “autoCompactEnabled”: false in your settings. The manual /compact command still works.

### What does “Autocompact is thrashing” mean?

Compaction succeeded but a large file or tool output refilled the context several times in a row. Read the file in smaller pieces, compact with a narrower focus, use a subagent, or /clear.

### Does CLAUDE.md survive /compact?

Yes. The project-root CLAUDE.md and auto memory are reloaded from disk after compaction.
