Skip to content
Developers

What is /compact? The command that fights context rot in Claude Code

Manage context rot with the /compact command to keep Claude Code session health by replacing verbatim history with structured, high-quality summaries.

Tuan Tran Van
7 min read
Contents (7 sections)
  1. What is context rot, and why does performance decay?
  2. What the /compact command actually does to your session
  3. Automatic compaction (~83%) vs. proactive 60% compaction
  4. What compacting loses — and the "cascading compaction" trap
  5. Writing effective /compact instructions: KEEP/SUMMARIZE/DROP
  6. Managing context proactively: a context meter, CLAUDE.md, and HANDOFF.md
  7. References

The /compact command is your primary tool for managing session health in Claude Code: it replaces your verbatim conversation history with a structured summary to fight context rot.

As a conversation grows, the accumulating history of prompts, tool outputs, and code blocks saturates the model's available memory. Left unmanaged, this leads to a predictable failure mode where the model's reasoning and technical precision sharply decline.

Instead of the model struggling to parse thousands of tokens of stale logs and abandoned attempts, /compact hands it a concise distillation of the most relevant information. You trade absolute conversation fidelity for context quality — so the model keeps its grasp on foundational architectural decisions and active goals, even during multi-hour development sessions.

Long context makes Claude forget and make mistakes (context rot), while the /compact command condenses conversation history to keep the AI sharp.

What is context rot, and why does performance decay?

Context rot is the predictable failure mode where an AI's "working memory" becomes saturated, causing foundational information to be compressed or pushed out of the active window. In long-running sessions, Claude Code accumulates noise: stale exploration logs, verbose tool outputs, and resolved error messages. As these tokens fill the window, the model enters the "dumb zone" — the upper range of context where the signal-to-noise ratio is poor and the model wastes cycles sorting through stale logs and abandoned attempts on every turn.

The U-shaped curve of LLM recall: accuracy is high at the start and end of the context window and lowest in the middle (lost-in-the-middle).

Research on LLM recall identifies a "U-shaped curve" of performance. Models show the highest accuracy for information at the very beginning and very end of the context window, while information in the middle is frequently ignored. OpenAI's MRCR test illustrates this decay, with accuracy dropping from 84% at 8K tokens to as low as 50% at 1M tokens for certain models. Claude models typically decay more slowly, but they remain susceptible to this structural limitation as the window approaches its limit.

What the /compact command actually does to your session

When you run /compact, Claude reads back the whole conversation, generates a summary wrapped in <summary> tags, and creates a "compaction block." Everything before that block is then dropped from the active history, so the model works from the distillation rather than the raw transcript.

Before and after compaction: a ~167K-token conversation history condensed into a single ~25K-token summary block, about an 85% reduction.

The effect is roughly an 85% reduction in context size — for example, shrinking a 167K-token session down to about 25K tokens. All of that reclaimed space becomes fresh short-term memory for the complex logic that comes next.

The key difference between running /compact yourself and letting the system auto-compact is control. When you compact deliberately, you can hand the summarizer a retention policy. When the system fires automatically because it hit the memory ceiling, it summarizes blindly — usually favoring the most recent messages and dropping the architectural constraints you established at the very start of the session.

Automatic compaction (~83%) vs. proactive 60% compaction

By default, Claude Code triggers automatic compaction at roughly 83.5% of the window (about 167,000 tokens of a 200,000-token limit). Waiting for that threshold is often counterproductive. At 83%, the model is already operating in the "dumb zone," which means the summary it generates may be based on a degraded, compressed view of the conversation history.

A color-zone context meter — Green, Yellow, Orange (compact around 60%) and Red — versus the ~83% automatic compaction threshold.

The recommended strategy is to run /compact manually when context usage reaches 60%. At that threshold, the model still has full, uncompressed access to every message, so it can produce a high-fidelity summary. It helps to treat the session as a set of zones:

  • Green (0–30%): the session is fresh — let it run naturally.
  • Yellow (30–50%): start watching for natural task boundaries.
  • Orange (50–60%): the ideal window to run /compact. Stop starting new features and prepare to compact.
  • Red (>60%): the danger zone. Don't kick off a complex refactor before you compact.

If you work with a 1M-token window, the percentages need rethinking: 20% of a million is already 200K tokens — enough to dilute any subtle logic. Don't let the big number fool you; signal density matters more than total capacity.

What compacting loses — and the "cascading compaction" trap

Compaction is inherently lossy. Precise technical details are the first casualties: exact values like a 30-second timeout or a 512KB payload cap, specific file paths, and the reasoning chains behind a decision. If the summary describes a "retry mechanism" without preserving the actual 40-line implementation, the model is less useful later.

The more severe risk is "cascading compaction." If a session triggers repeated compactions, Claude summarizes an already-summarized history, and information blurs cumulatively — like photocopying a photocopy. After a few rounds, Claude may still know which project you're working on, but the implicit rules quietly disappear.

Be especially careful with configuration files. A known bug means compaction sometimes fails to preserve nested CLAUDE.md or project-context.md content, so path-scoped rules vanish after a compaction until that file is read again.

Writing effective /compact instructions: KEEP/SUMMARIZE/DROP

Never just type /compact on its own. Turn it into a retention policy using the KEEP/SUMMARIZE/DROP framework — it gives the summarizer a clear contract and prevents the loss of path-scoped rules or project context from nested CLAUDE.md files.

The KEEP / SUMMARIZE / DROP framework: three buckets for sorting content when writing a /compact instruction.

  • KEEP: verbatim architectural decisions, active bugs, and specific constraints (e.g. "must stay compatible with Node 18").
  • SUMMARIZE: exploratory tangents that reached a conclusion, and resolved discussions.
  • DROP: stale logs, verbose tool outputs, and reverted code drafts.
text
/compact KEEP: the decision to use JWT for auth and the current 500 error in POST /api/orders.
SUMMARIZE: our discussion on UI frameworks.
DROP: all prior terminal build logs and failed deployment attempts.

Managing context proactively: a context meter, CLAUDE.md, and HANDOFF.md

Effective context hygiene is one piece of the broader discipline of context engineering; it rests on three habits that prevent state loss.

Status-line context meter: monitor usage like a battery indicator. A terminal status line lets you spot natural task boundaries for compaction before you hit the 60% orange zone. A minimal version pulls the percentage straight from Claude Code:

bash
#!/bin/bash
# Show context usage in the status line
PERCENT=$(claude context --json | jq -r '.percent_used')
if [[ -n "$PERCENT" ]]; then
  echo "ctx:${PERCENT}%"
fi

CLAUDE.md retention policy: add a "Compact Instructions" section to your root CLAUDE.md. Claude Code refers to this encoded policy during summarization so your permanent rules aren't discarded.

HANDOFF.md habits: before compacting, ask the model to write the current state to a HANDOFF.md file. That makes session state durable and inspectable on disk.

After any compaction, you need to "rehydrate" the model. Compaction summaries often keep a reference (knowing a plan exists) without the data (the plan's actual content). Explicitly tell Claude to re-read PLAN.md or HANDOFF.md after compacting so the active window is repopulated with the real data it needs to execute.

References

Read more

Share this article