← Back to Blog

Claude Code Context and Compact Management Guide

Claude Code LLMs Context Management

Every file Claude reads, every command it runs, and every message you exchange consumes token space in the model's active working memory. Without proactive management, long sessions degrade as the context window approaches its limits. This guide covers how to monitor your context, configure auto-compaction rules, and use manual compaction tools to keep Claude sharp.

1. Context Commands & Usage

Claude Code provides built-in terminal commands to inspect, manage, and compress conversation history:

Command Sample Usage Description
/context /context Displays a visual bar chart of active context usage and breakdowns (messages, files, tool outputs).
/compact /compact Manually triggers compression of your conversation history to reclaim space.
/clear /clear Resets the entire session memory, starting with a completely clean slate.
/autocompact /autocompact Displays the token threshold for auto-compaction and the source setting that set it.
/usage /usage Shows the details and percentages of API costs and limits eaten by context.

For example, to manually query your active auto-compaction window and its source, execute:

/autocompact

2. Auto-Compact Window Configuration

Auto-compaction fires automatically once conversation size reaches a designated token threshold. Four distinct sources can define this window, and they resolve in a strict hierarchy:

Source Priority Formats Accepted Validation & Quirks
CLAUDE_CODE_AUTO_COMPACT_WINDOW (Env Var) 1 (Highest) Bare integers only (e.g., 200000) No validation: Accepts invalid values (e.g. "banana") without error, but overrides all commands.
--autocompact (CLI Flag) 2 Integers, shorthand (500k, 200), auto Overrides configuration settings for a single run. Validates inputs between 100k and 1M.
/autocompact (Command/Settings) 3 Integers, shorthand (500k, 200), auto Saves to user config files. Yields to CLI flags and environment variables.
Unset Default 4 (Lowest) N/A Uses the default token size tuned for the specific model. Recommended for best cost.

Shorthand numbers (e.g., 200) are automatically parsed as 200000 (200k tokens). If your configured threshold is greater than the model's actual physical capacity, it is capped, and the command output appends ยท capped to X by model.

How to Reset Overridden Settings

If an environment variable override is active, the interactive command cannot change it. You must find it in your environment:

env | grep AUTO_COMPACT

Remove the export from your startup files (e.g., ~/.zshrc or ~/.bashrc), unset the variable, and open a fresh terminal session. Then run:

/autocompact auto

3. Context Rot & The 60% Rule

Context Rot is the gradual degradation of session quality as the model's active window fills up. Older foundational guidelines, prompt files, and architecture rules get compressed or discarded automatically.

The 60% Rule

Do not wait for Claude's automatic warning indicators (which display around 80%โ€“95% capacity). Run the /compact command proactively when context utilization hits **60%**.

At 60%, Claude still has clean, uncompressed access to your entire conversation. The summary it generates will be highly accurate. At 90%, Claude is already summarizing a degraded memory, causing important details to slip away.

Note that compaction requires a new LLM call to summarize your history, which increases active token usage. Overriding settings to compact too frequently (e.g., at 10%โ€“20%) is inefficient. For precise control, use the CLAUDE_AUTOCOMPACT_PCT_OVERRIDE environment variable to adjust the auto-compaction percentage.

4. Preservation Instructions Guide

Running /compact with no arguments lets the model decide what details to keep. To prevent losing critical data, append explicit preservation instructions.

An example command with preservation parameters:

/compact Keep: current directory layout, the decision to use optimistic UI updates, and the open error in auth.ts.

Focus your instructions on these four key categories:

  1. Architectural decisions: Choices that aren't apparent from reading the raw source files (e.g., "We handle authentication at the middleware level, not in controllers.").
  2. Active debugging targets: Unresolved problems and things you have already tried (e.g., "Debugging a 500 error on POST /api/orders; we verified connection pool is active.").
  3. Active scope context: Which files have been modified, and which areas of the codebase are currently in scope.
  4. Technical constraints: Constraints limiting the solution space (e.g., "Must maintain compatibility with Node 16.").

What to leave out: Resolved logs, failed exploration paths that didn't work out, verbose terminal logs, or obsolete drafts of code.

5. Practical Compaction Workflow

Incorporate these steps into your daily coding flow to maintain consistent session quality:

  1. Provide a Session Brief: Paste a short list of 2โ€“5 bullets detailing your objectives and conventions at the start of your terminal run.
  2. Monitor and Pause: Periodically run /context. When it reaches 60%, prepare your preservation list.
  3. Run Compaction: Execute /compact with your preservation bullet points.
  4. Verify Post-Compaction State: Run a quick sanity check by asking Claude: "Summarize where we are and what we are working on next." Add back any critical items that were dropped.
  5. Reset: Continue working. The 60% threshold resets against the newly cleaned context allocation.

6. Best Practices for Saving Context Space