Managing context in Claude Code: /compact versus /clear and how to save tokens

Claude Code Published:

Why long Claude Code sessions slow down, when to use /compact, /clear and /context, how automatic compaction works, and habits that keep token usage down.

Verified on Sep 7, 2026 These tools change quickly. Please also check the latest official documentation.
Contents
  1. What fills the context window
  2. /compact: summarize and continue
  3. /clear: discard and start over
  4. Choosing between them
  5. Save state with a PreCompact hook
  6. Habits that reduce usage
    1. Keep CLAUDE.md short
    2. Remove MCP servers you do not use
    3. Do not feed it huge files or output
    4. Delegate research to subagents
  7. Resuming sessions
  8. Summary

Long sessions get slow, and Claude starts to act as if it forgot the rules you gave at the beginning. The usual cause is a crowded context window. Conversation history, files that were read and tool output all accumulate; as the limit approaches, quality drops and compaction kicks in, losing detail.

This article covers the three commands for inspecting, compressing and discarding context, plus the habits that reduce usage in the first place.

KEY POINT

What you will learn

  • What /context, /compact and /clear do and when to use each
  • How automatic compaction works and how to protect information you cannot afford to lose
  • Everyday habits that cut context consumption

What fills the context window

ComponentContentRoom to reduce
System promptClaude Code's own instructionsNone
CLAUDE.mdUser, project and local filesLarge (keep it short)
Tool definitionsBuilt-in tools plus every MCP server's toolsMedium (remove unused MCP servers)
Conversation historyPrompts, answers, files read, command outputLarge (/compact, /clear)

/context shows this breakdown and the remaining room. It is worth running once: people are often surprised by how much MCP tool definitions or a long CLAUDE.md take up.

/compact: summarize and continue

/compact summarizes the conversation so far and replaces the history with that summary. Use it mid-task when the context is getting tight.

/compact

You can tell it what to keep:

/compact Keep the list of files changed so far and the details of the failing tests

The summary is written by Claude, and without guidance it may drop specifics such as exact error messages or the naming convention you settled on. Naming what matters is the safe option.

用語解説

Automatic compaction: When usage nears the limit, compaction runs even if you never call /compact. Because it can happen at any moment, anything important should also live in CLAUDE.md or a notes file.

/clear: discard and start over

/clear wipes the conversation history entirely. CLAUDE.md and settings are reloaded, so this is the right command when you move on to a new task.

/clear

Carrying over context from the previous task makes Claude reach for unrelated files or stick to an old plan. Clearing at task boundaries helps both accuracy and cost.

Choosing between them

SituationCommand
Same task, but things are getting slow/compact
Feature finished, starting the next one/clear
Research done, implementation in a separate sessionHave the findings written to a file, then /clear
After compaction Claude seems to have forgotten the premiseRedo /compact with explicit keep instructions, or write it into CLAUDE.md

Save state with a PreCompact hook

The PreCompact hook fires right before compaction. Use it to dump the working state to a file:

{
  "hooks": {
    "PreCompact": [
      {
        "hooks": [
          {
            "type": "command",
            "command": "git status --short > .claude/last-status.txt"
          }
        ]
      }
    ]
  }
}

Hook basics are covered in Run lint and format automatically with Claude Code hooks.

Habits that reduce usage

Keep CLAUDE.md short

CLAUDE.md is loaded every session. Leave out directory listings and long specs; move documents behind @ imports or into per-directory CLAUDE.md files. See How to write CLAUDE.md.

Remove MCP servers you do not use

Every connected server's tool definitions cost context even when unused. Remove project-irrelevant servers with claude mcp remove, or keep them at local scope and enable them only when needed.

Do not feed it huge files or output

cat on a giant log or a search through node_modules burns context fast. Ask for "the first 50 lines" or "only the matching lines from grep". For long test runs, | tail -n 30 helps.

Delegate research to subagents

Broad codebase exploration in a subagent keeps the files it reads out of your main context; only the summary comes back.

Resuming sessions

claude --continue and claude --resume restore a previous session's context (the summary, if it was compacted). When you pick up work the next day, run /context first and /compact if needed before starting.

Summary

  • Use /context to see whether CLAUDE.md or MCP tool definitions have grown too large
  • /compact (with instructions on what to keep) continues a task; /clear switches tasks
  • Automatic compaction can happen any time, so keep important facts in CLAUDE.md or files
  • Avoid feeding large output, and push research into subagents

FAQ

Should I use /compact or /clear?
Use /compact to keep working on the same task (summarize and continue). Use /clear when you move to a different task (wipe the history and start fresh).
When does automatic compaction happen?
When context usage approaches the limit. A PreCompact hook can save important state right before it runs.
How can I see how much context is in use?
Run /context. It breaks usage down into system prompt, tool definitions, conversation history and so on.

Primary sources

This article was drafted by AI from official documentation and reviewed by the site operator before publishing. Found a mistake? Let us know via the contact page.