ES · Leer en español Explore for free

Recipe · Intermediate · 10 min

/clear vs /compact in Claude Code, and idle compaction

When to use /clear or /compact in Claude Code, what survives a compaction and how to turn off idle compaction with the idleCompaction setting.

Cover: /clear vs /compact in Claude Code, and idle compaction

Tested with Claude Code 2.1.316 · reviewed

See the files on GitHub Free · MIT license

The problem

Context is everything Claude knows about your session: CLAUDE.md, files read, tool results and the conversation itself. When it fills up, Claude Code compacts: it replaces the history with a summary, and details get lost there. And since October 2026 it can also compact on its own while you are away, so suddenly Claude "forgets" the exact error or the reason for a decision.

The solution

You decide when to start from scratch (/clear), when to summarize while steering what to keep (/compact with instructions), when to summarize only part (/rewind) and when to bring the automatic compaction forward (/autocompact). And you understand idle compaction so you can keep it, tune it or turn it off with idleCompaction: false.

Cheat sheet inside the session:

/context                         # how much have I used and what weighs most?
/clear task-name                 # new unrelated task (free; /resume brings it back)
/compact keep X, Y and Z         # same task, less context
/rewind  (or Esc Esc)            # Summarize from here / up to here
/autocompact 500k                # compact earlier (auto = model default)
/branch try-another-idea         # fork the conversation
/config                          # Idle compaction row

1. /clear or /compact: the one-line rule

Are you about to do something unrelated to what came before? /clear. Still on the same task but the context is heavy? /compact with instructions.

/clear starts a new conversation with an empty context and costs nothing; it accepts a name (/clear login-fix) to label the previous conversation in the /resume picker, and its aliases are /reset and /new. The previous conversation is not lost: /resume brings it back and, within the same Claude Code process, the /rewind menu shows a "/resume <id> (previous session)" entry at the top.

/compact [instructions] summarizes what has been said so far to free up space and stays in the same conversation; it is a big request, because it reads everything it summarizes. With a warm cache it is quite a bit cheaper than its size suggests; after a long pause the cache has expired and it reprocesses everything. That is why it is best to compact at a natural break (between tasks) and not to resume a huge, cold session just to compact it.

If what you want is to throw away a wrong path, /rewind to an earlier turn is better than compacting: it goes back to a prefix that is already cached.

2. What survives a compaction (and what doesn't)

The official documentation details what happens to each thing.

WhatWhat happens when compacting
System prompt and output styleKept
Root CLAUDE.md and rules without paths:Reloaded from disk
Auto memoryReloaded
Skill descriptions (since 2.1.304)Reloaded
The plan written in plan modeReloaded from disk
Git statusA fresh snapshot, if the session started with one
Files read or editedUp to five come back, the most recently modified; above 5,000 tokens, only the path
Invoked skillsRe-injected, up to 5,000 tokens per skill and 25,000 in total, from the top of the file
Rules with paths: and subfolder CLAUDE.md filesReloaded when needed
The conversation, context added by hooks and anything not on diskSummarized, may lose detail

What is not on disk, such as the exact text of an error or the reason for a decision, is what gets lost most. Put the important part at the top of SKILL.md. If a rule must always survive, remove its paths: or move it into the root CLAUDE.md (see a CLAUDE.md that works).

3. Compact yourself, and tell it what to keep

You have four ways to control compaction instead of waiting for the automatic one:

  1. /compact with a focus: "/compact keep the exact error of the payments test, the paths touched and why we ruled out Redis".
  2. Fixed instructions in CLAUDE.md: a "# Compact instructions" section, which the official documentation shows as an example of customizing compaction.
  3. Compact only part: /rewind (or Esc twice with an empty prompt), pick a message and press "Summarize from here" or "Summarize up to here"; you can type instructions in the "add context (optional)" row. The original messages stay in the transcript on disk.
  4. Bring the automatic one forward: /autocompact 500k sets how full the window gets before compacting; /autocompact auto goes back to the model's default.

The section for CLAUDE.md:

# Compact instructions
When compacting, always keep:
- the exact text of errors and failing tests
- the paths of modified files
- the decisions made and why the alternatives were ruled out
- the pending next steps

Recommended trick before a big compaction: ask Claude to write the state to a file. That file is on disk and you can ask it to reread it later; it is more reliable than trusting the summary.

Before we compact, write to NOTES.md: goal, decisions made (with the reason), errors found with their exact text, files touched and next steps. Don't change any code.

4. Idle compaction, explained

Since 2.1.299 (14 Oct 2026) in the terminal, and since 2.1.314 (30 Oct) in VS Code and in the Code tab of the desktop app, Claude Code compacts on its own a long conversation you leave idle. The idea is to save: on a subscription the conversation cache lasts one hour; if you come back later, your next message would reprocess everything. By compacting before it expires, the next message sends only the summary.

It kicks in when all of these conditions are met:

How to tell: in the terminal you will see a line starting with "Compacted while idle, before the prompt cache expired"; in VS Code and desktop, a notice with "a summary written while you were away". The price is the same as any compaction: summarized details. Issue #98747 describes a case where context from several sessions was lost.

5. Turn it off, tune it or live with it

If you prefer to decide yourself when to summarize, you have three options:

  1. From the session: /config and change the "Idle compaction" row (2.1.302 or later); it is saved to your user settings and applies immediately. If the row does not appear, another settings source (managed, --settings, project or local) sets the key, or your machine cannot fetch flags and then it does not run.
  2. In ~/.claude/settings.json: "idleCompaction": false (requires 2.1.290 or later; on earlier versions, turn off autoCompactEnabled). The DISABLE_AUTO_COMPACT variable turns it off together with automatic compaction.
  3. Raise or lower the threshold with CLAUDE_CODE_IDLE_COMPACT_MIN_TOKENS (200000 by default; the real minimum is 100000).

Turn it off:

{
  "idleCompaction": false
}

Keep it on, but with a 150 k threshold:

{
  "env": {
    "CLAUDE_CODE_IDLE_COMPACT_MIN_TOKENS": "150000"
  }
}

Keep in mind the cost of turning it off: if you come back from lunch to a 300 k-token conversation, the first message reprocesses everything without cache and that counts against your limit (see usage limits). Practical advice: leave it on and, before stepping away in the middle of a delicate task, run a /compact with instructions yourself or save NOTES.md. That way you decide the summary.

6. When to change strategy: /branch, subagents and 1M context

If you want to try another approach without losing the current conversation, /branch [name] creates a branch of the conversation and keeps the original so you can go back with /resume; /fork copies the session into a background session.

For big reads (logs, lots of documentation, exploring a repository), delegate to a subagent: the files stay in its context and only the summary reaches you.

If you really need a bigger window, the Fable, Sonnet 5 and later, Haiku 5.5, Opus 4.6 and later and Sonnet 4.6 models support 1 million tokens; compaction works the same, just later.

To see where you are at any moment: /context shows the grid by category with suggestions, and context_window.used_percentage can go in the status line (see usage limits). And if you use plan mode, turn on showClearContextOnPlanAccept: when you approve the plan, an option clears the context and starts implementing with only the plan (see plan mode).

Check it

In any session, /context shows how much you have used. We checked it with Claude Code 2.1.316 on Linux: claude -p "/context" returned the table by category, with 2 % used in a new session. On the other hand, /compact with -p on an empty session answers "Not enough messages to compact", so the real test is done in an interactive session:

  1. Read several large files and look at /context.
  2. Run /compact with instructions and look at /context again.
  3. Ask about something you asked it to keep and about something you didn't.

For idle compaction, the method would be to set CLAUDE_CODE_IDLE_COMPACT_MIN_TOKENS=100000, fill more than 100 k tokens, leave the session idle a bit over an hour with the PC awake and look for the "Compacted while idle" line; then repeat with "idleCompaction": false. We have not run this test yet (it takes more than an hour and uses limit), so here we only report what the documentation says.

Why it works

Claude Code sends the whole context with every request and the window has a fixed size. Compacting swaps the history for a summary and reloads from disk what lives on disk (CLAUDE.md, memory, plan, recent files). That is why what you steer with instructions or save to a file survives better than what was only in the conversation. Idle compaction does the same before the one-hour cache expires. Sources: context window, commands, idleCompaction, caching when compacting, checkpointing, costs and the CHANGELOG.

Common mistakes

FAQ

Does /clear delete my conversation?

No. It starts a new one; the previous one comes back with /resume and, within the same process, from the /rewind menu.

Why does Claude "forget" things after compacting?

Because the conversation is replaced by a summary. CLAUDE.md, memory, the plan and up to five recent files are reloaded; the rest is summarized. Steer the summary with /compact <instructions> or "# Compact instructions" in CLAUDE.md.

I saw "Compacted while idle": what happened?

Idle compaction summarized a conversation of more than 200 k tokens that had been idle for about an hour, so your next message does not reprocess everything. If you don't want it: /config > Idle compaction, or "idleCompaction": false.

Does compacting use limit?

Yes: the summary request reads the conversation. With a warm cache it is cheap; with a cold session, expensive. /clear costs nothing.

Is it a good idea to turn off automatic compaction?

Only if you manage the context yourself with /context and /compact. Without it you will have to watch how full it gets and compact or clear by hand. If what you want is for it to happen earlier, use /autocompact instead of removing it.

I get "Error during compaction: conversation too long"

That case is covered in the common errors recipe (full context). As prevention: compact earlier with /autocompact, or split the work with /clear and NOTES.md.

The whole library