# Claude Code /compact vs /clear: Manage Context and Cost

> When to use /clear, /compact or /rewind in Claude Code, what compaction keeps, and how the prompt cache changes what each command costs you in practice.

Canonical: https://bridge-terminal.com/atlas/blog/claude-code-compact-clear-context-management/
Last updated: 2026-11-04

Published 4 November 2026 · Technical · How-to · 9 min read

## The short version

- Use /clear between unrelated tasks, /compact at natural breaks inside one task, and /rewind when you want to abandon a wrong path.
- Compaction re-injects the root CLAUDE.md, unscoped rules, auto memory and your plan, but path-scoped rules, nested CLAUDE.md files and early instructions can drop out, so put lasting rules in the root CLAUDE.md.
- A /compact right after a long break is the expensive case, because the prompt cache has expired and the whole history is reprocessed as uncached input.
- Add a Compact instructions section to CLAUDE.md and pass a focus to /compact so the summary keeps test output and code changes.
- Run /context to see what fills the window and /usage to see how often the prompt cache misses before you change anything.

If a Claude Code session is getting long, the short answer is: use `/clear` when the next task is unrelated, `/compact` at a natural break inside the same task, and `/rewind` when the last few turns went the wrong way. They look similar, but they cost different amounts and lose different things.

This guide covers how to manage the Claude Code context window with these commands: what each does, what survives compaction, how the prompt cache changes the price, and a setup you can paste into a project. The Claude Code details come from Anthropic's documentation, and version numbers are noted where a feature needs one.

## What fills the context window?

The window holds your conversation, file contents, command output, CLAUDE.md, auto memory, loaded skills and system instructions. Claude Code re-sends all of it with every request, so a one-line question late in a long session still draws on the whole history. When the prompt cache is warm, the re-read is billed at the cached rate, which is lower than the standard input rate.

Two habits follow from that. Keep the window small, and avoid the actions that make the cache start over.

Start by looking at what you have:

```text
/context
```

`/context` shows a live breakdown by category, with optimization suggestions, including which CLAUDE.md and auto memory files loaded. `/usage` shows token use for the session. On a Pro, Max, Ultra, Team or Enterprise plan it also shows a breakdown of what counts against your plan limits, with recommendations for behaviours that account for at least 10% of your recent usage. Subscribers should note that the dollar figure in the Session block is an estimate at list price and is not relevant to billing on a subscription. You can also show context use continuously in the status line.

## /clear, /compact or /rewind: which one?

| Situation | Command | Why |
| --- | --- | --- |
| Switching to unrelated work | `/clear` | Old context wastes tokens on every later message; `/clear` costs nothing |
| Same task, context is large, you are at a break | `/compact <focus>` | Keeps a summary, drops the bulk |
| The last few turns went down a wrong path | `/rewind` (or Esc twice) | Truncates back to a prefix that is already cached |
| One huge read or log is the problem | A subagent | The reads stay in its own window; only a summary returns |

Before you clear, run `/rename` so you can find the session later and return to it with `/resume`.

### /compact with a focus

Plain `/compact` lets Claude Code decide what matters. Give it a focus instead:

```text
/compact focus on the auth bug fix and the failing test output
```

You can also improve the default by adding 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
```

### /rewind to summarize part of a session

`/rewind` can also compact only part of the conversation: select a message and choose **Summarize from here** or **Summarize up to here**. If you want to throw a path away entirely, rewinding is better than compacting. Rewinding goes back to a prefix the cache already holds, while compaction builds a new one. Rewind can also restore file checkpoints, which cover file changes only, not actions on remote systems such as databases or deployments.

## What survives compaction

Compaction replaces the history with a summary. The docs list what happens to each kind of content:

- **Re-injected from disk:** project-root CLAUDE.md, unscoped rules, auto memory, and the plan Claude wrote in plan mode.
- **Rebuilt:** skill descriptions (v2.1.304 or later) and the git status snapshot, if the session started with one.
- **Re-read:** up to five files Claude read or edited, most recently modified first. A file over 5,000 tokens comes back as a path reference without its content.
- **Re-injected with caps:** invoked skill bodies, at 5,000 tokens per skill and 25,000 in total, oldest dropped first. Truncation keeps the start of the file, so put the key instructions at the top of `SKILL.md`.
- **Reloaded on demand, so possibly lost:** rules with `paths:` frontmatter and nested CLAUDE.md files. They enter the conversation when Claude touches a matching file, so compaction summarizes them away with everything else.
- **Keeps running:** background commands and background subagents.
- **Summarized, not kept:** context that hooks added earlier, and instructions you typed early in the conversation.

Two practical rules come out of this list:

1. Anything that must hold for the whole session belongs in the project-root CLAUDE.md, not in a message. If a path-scoped rule must persist, drop its `paths:` frontmatter or move it to the root file.
2. If you need something re-added after every compaction, a SessionStart hook that matches the `compact` source runs and adds its output to the compacted context.

Editing CLAUDE.md mid-session does not break the cache, but the edit also does not apply until the next `/clear`, `/compact` or restart.

## Why a /compact can be cheap or expensive

Claude Code relies on prompt caching: the API reuses the part of the request that has not changed (the prefix) and bills the re-read at the cached rate. The match is exact, so a change anywhere in the prefix recomputes everything after it.

Compaction sends a separate summarization request that repeats your system prompt, tools and the history being summarized.

- **Cache warm:** that request reads your prefix from the cache, so a mid-session `/compact` costs a fraction of what the context size suggests. The turn after it only has to cache the short summary.
- **Cache cold:** after a break longer than the cache lifetime there is nothing to read, so the history is reprocessed as uncached input. The docs give this as the reason `/compact` costs the most when you resume an old session.

The lifetime depends on how you sign in. On a Claude subscription within your plan's included usage, the main conversation gets a one-hour cache. With an API key, a cloud provider, or once you are drawing on usage credits, it defaults to five minutes. Subagents, forks and compaction requests default to five minutes, except a small set of server-controlled helper requests that get an hour on a subscription.

You can set the TTL yourself with Claude Code v2.1.242 or later:

```json
{
  "promptCacheTtl": "1h"
}
```

`CLAUDE_CODE_PROMPT_CACHE_TTL` does the same as an environment variable. `subagentPromptCacheTtl` and `CLAUDE_CODE_SUBAGENT_PROMPT_CACHE_TTL` cover everything outside the main conversation. Each accepts `5m` or `1h`. The one-hour TTL charges a higher rate for cache writes, so it pays off when you idle and come back, not on short bursts of work.

So the rule of thumb: compact when you are at a break in the work and the cache is still warm, not as the first action after lunch. If you are coming back to an old session and only need the result, `/clear` and a fresh, specific prompt is often cheaper than resuming and compacting.

### Idle compaction

If a long conversation sits idle on the one-hour TTL, Claude Code can compact it for you. To turn that off, set `idleCompaction` to `false` (v2.1.290 or later). Idle compaction can use tokens while you are away.

## Check whether the cache is working

After the first response, `/usage` adds a `Prompt cache (main)` line to the Session block (v2.1.251 or later). The docs give this example:

```text
Prompt cache (main):   14 requests · 91% of input tokens from cache · 2 misses (last 6m 10s ago, 310.2k tokens re-cached) · 1 expected rebuild (compaction or tool-result clearing) · warm (1h TTL, last activity 40s ago)
```

A compaction shows up as an "expected rebuild", not a miss. Real misses are the ones to chase. The documented causes include switching models with `/model`, changing effort on models where that resets the cache, turning fast mode on, adding or removing an MCP server whose tools load upfront, and upgrading Claude Code. From v2.1.260 the line can also name a likely cause, such as changed tool definitions. The line covers the main conversation only, not subagents.

Pick the model and effort at the start of a session and avoid changing them mid-task.

## Set when auto-compaction runs

Auto-compaction triggers as you approach the limit. You can move the point earlier:

```text
/autocompact 500k
```

That saves the value for the current model in your user settings and applies it to the session. `/autocompact auto` returns to the window tuned for your model. Other ways to set it:

```json
{
  "autoCompactWindow": 200000
}
```

```bash
claude --autocompact 500k
export CLAUDE_CODE_AUTO_COMPACT_WINDOW=200000
```

The `autoCompactWindow` setting applies to every model, and a window saved for a specific model with `/autocompact` takes precedence over it. The flag overrides your saved setting for one launch. The environment variable takes precedence over the command, the flag and the setting, which suits scripts and cloud environments. While it is set, the status line's `used_percentage` no longer shows when compaction will run, because that percentage measures against the model's full window.

## What if compaction keeps failing?

If you see `Autocompact is thrashing`, compaction worked but a file or tool output refilled the window several times in a row, and Claude Code stopped retrying. The documented recovery steps:

1. Ask Claude to read the oversized file in smaller chunks, such as a line range or one function.
2. Run `/compact keep only the plan and the diff`, or another focus that drops the large output.
3. Move the large-file work to a subagent.
4. Run `/clear` if the earlier conversation is no longer needed.

If it returns after a `/clear`, run `/context` and compare the `Messages` row with the others. If `Messages` is the largest row, a file or tool output in the new conversation is refilling the window. If the other rows together are larger, what loads at startup leaves too little room, so trim that.

## A small setup to copy

1. Keep the root CLAUDE.md short (the cost docs suggest under 200 lines) and add a `# Compact instructions` section.
2. Move workflow-specific instructions into skills, which load only when invoked.
3. Let a PreToolUse hook filter noisy test output so Claude sees failures, not 10,000 lines. The cost docs include a ready-made script for `npm test`, `pytest` and `go test`.
4. Delegate log reading and doc fetching to subagents, and choose a smaller model for simple ones.
5. Make `/clear` your default between tasks, and `/compact <focus>` your default at breaks.

## What Atlas does about this

Atlas is a team of AI engineers built on Claude Code, and every engineer is its own Claude Code session. Today, Atlas handles this for its own engineers. Between tasks an engineer starts fresh once its session has grown, unless the next task continues its own work. A thread idle past Claude's one-hour prompt cache is resumed only when it is small. On Atlas's own numbers, resuming a cold 378k-token thread to say "ok" re-wrote 353,191 tokens, while a fresh start with the task is about 11–25k. Each engineer also loads only the tools its role uses and reads only the parts of the project's memory its task needs.

What Atlas does not do today is give you the controls described above: choosing a compact focus, rewinding a wrong path or setting the auto-compact point. Those are coming to Atlas, built in, so they will need no setup. Until then, the habits in this guide are what you can do in a plain Claude Code session. You can read more on the [Atlas home page](https://bridge-terminal.com/atlas/).

## Conclusion

Context management comes down to three decisions. Clear when the topic changes, compact with a focus when the cache is warm and you are at a break, and rewind when the recent path was wrong. Put anything that must last in the root CLAUDE.md, check `/usage` for cache misses, and keep model and effort steady inside a task. Fewer tokens in the window means fewer tokens billed on every later message.

## Questions people ask

### What is the difference between /clear and /compact in Claude Code?

/clear starts a fresh conversation and costs nothing, so use it when the next task is unrelated. /compact replaces the history with a summary so you can keep working on the same task, and you can pass a focus such as /compact focus on the auth bug fix.

### What does Claude Code keep after /compact?

It re-injects the project-root CLAUDE.md, unscoped rules, auto memory and the plan-mode plan, and re-reads up to five recent files. Path-scoped rules and nested CLAUDE.md files reload on demand, so compaction can summarize them away, and early instructions typed in the chat can be lost.

### Why is /compact expensive after a long break?

The prompt cache expires after a period of inactivity, so the summarization request has to reprocess the whole history as uncached input. Compacting while the cache is warm costs a fraction of that.

### How do I make Claude Code compact earlier?

Run /autocompact with a value such as /autocompact 500k, set autoCompactWindow in settings, pass --autocompact for one launch, or set CLAUDE_CODE_AUTO_COMPACT_WINDOW. The environment variable takes precedence over the others.

### How do I see how full my Claude Code context is?

Run /context for a live breakdown by category, and /usage for session token use and a Prompt cache line. You can also show context use continuously in the status line.

## About Atlas: Context handled for Atlas's own engineers

Atlas starts each engineer fresh between tasks once a session has grown, and resumes a thread idle past Claude's one-hour cache only when it is small, so the plan's limits go further. Compact focus, rewind and auto-compact controls are coming to Atlas, built in, with no setup. — https://bridge-terminal.com/atlas/

## Sources

- [Manage costs effectively - Claude Code docs](https://code.claude.com/docs/en/costs)
- [How Claude Code works - Claude Code docs](https://code.claude.com/docs/en/how-claude-code-works)
- [How Claude Code uses prompt caching](https://code.claude.com/docs/en/prompt-caching.md)
- [Explore the context window - Claude Code docs](https://code.claude.com/docs/en/context-window.md)
- [Model configuration: auto-compact window](https://code.claude.com/docs/en/model-config.md)
- [Troubleshooting - Claude Code docs](https://code.claude.com/docs/en/troubleshooting.md)

_Researched and drafted with Claude, then checked against the sources above. Atlas is an independent product, not affiliated with Anthropic or OpenAI._

## Related

- [Claude Code Resume: Continue, Name and Branch Sessions](https://bridge-terminal.com/atlas/blog/claude-code-resume-sessions-name-branch/)
- [Claude Code Rules: Path-Scoped Instructions](https://bridge-terminal.com/atlas/blog/claude-code-rules-path-scoped-instructions/)
- [Claude Code OpenTelemetry: Track Cost and Tool Use](https://bridge-terminal.com/atlas/blog/claude-code-opentelemetry-monitor-cost-usage/)
