Context Management

Understanding and managing Claude Code's context window, and effectively using commands like /compact and /clear

Updated 2026-10-15

Need an API key? Sign up →Endpoints & base URLs →

On This Page

Every Claude Code session runs within a context window. Understanding how context works helps you use Claude more efficiently and avoid errors caused by context overflow.

Understanding the Context Window

As of 2026-10-15, according to the Claude Code documentation: Fable 5.1, Fable 5, Sonnet 5 and later, Haiku 5.5, and Opus 4.7 and later run with a 1,000,000 token (1M) context window by default, with no [1m] suffix needed. Opus 4.6 and Sonnet 4.6 reach 1M only through their [1m] variant; otherwise they run at 200K. The commonly used models on QCode (claude-opus-5-5, claude-sonnet-5-5, claude-haiku-5-5) belong to the first group. To keep sessions at a 200K window, set the environment variable CLAUDE_CODE_DISABLE_1M_CONTEXT=1.

The context includes:

  • All conversation history (your messages + Claude's responses)
  • File contents referenced with @
  • Tool call inputs and outputs
  • System prompts and CLAUDE.md content

As the conversation progresses, the context grows continuously. When the context approaches its limit, Claude's response speed may slow down, and it will eventually trigger automatic compaction or produce errors.

Compacting Context: /compact

The /compact command compresses the current conversation history into a brief summary, freeing up context space while retaining important information.

/compact

You can also add custom instructions to control what the compaction focuses on:

/compact Keep all code examples and the completed task list

Automatic compaction: unless you set your own window, Claude Code compacts automatically as the conversation approaches the model's context limit; for models with a native 1M window the default is about 967K tokens (per the official docs). To compact earlier, run a command such as /autocompact 500k to set the window (auto restores the default; requires Claude Code v2.1.221 or later). You can also proactively run /compact when context reaches 70–80% to avoid waiting until the last moment.

Clearing History: /clear

The /clear command clears all conversation history and starts a fresh session:

/clear

Note: /clear removes all context, including project information Claude has already learned. Use it when switching to a completely unrelated new task.

Checking Context Status: /context

Use the /context command to view current context usage:

/context

@ File References

Use the @ symbol to add file contents to the context:

@src/main.ts Explain what this file does
@package.json Check dependency versions
@README.md

Best practices:

  • Only reference files relevant to the current task to avoid adding unnecessary context
  • Large files consume context space quickly — prioritize referencing key files
  • Use CLAUDE.md to provide project background instead of repeatedly pasting it in every conversation

Pipe Input

Pass command output or file contents into Claude via pipes:

# Pipe file contents in
cat error.log | claude "Analyze this error"

# Pipe command output in
git diff | claude "Generate a commit message"

# Multi-line input
echo "Please analyze the following code:
$(cat src/utils.ts)" | claude

Preventing Long-Session Overflow

Symptom: E015 Internal server error

When the conversation context approaches the model's capacity limit (~95%), the Anthropic API returns a 500 error. QCode.cc wraps this as a 429 response:

429 {"error":{"code":"E015","message":"Internal server error"},"status":500}

This is known behavior of the Anthropic API (not specific to QCode.cc). QCode.cc wraps such 5xx errors as 429 responses to take advantage of Claude Code's built-in retry mechanism.

Resolution Steps

  1. Try /compact:

  2. If successful, the conversation can continue normally

  3. If /compact itself errors (the compaction request also requires sending the full context), proceed to the next step

  4. Exit and restart Claude Code: bash # Press Ctrl+C or type /exit # Then restart claude

Prevention Tips

  • Compact regularly: Proactively run /compact when context reaches 70–80% rather than waiting for overflow
  • Break down tasks: Split large tasks into smaller ones, using a separate session for each
  • Use CLAUDE.md instead of repeated pasting: Write project background into CLAUDE.md — Claude reads it automatically on startup, so you don't need to repeat it in every conversation
  • Avoid importing large files: Referencing a large file consumes a lot of context — prefer referencing key sections only
  • Monitor with /cost: The /cost command shows the token usage for the current session

Quick Command Reference

Command Purpose
/compact Compact context (retains a summary)
/clear Clear all context
/context View context status
/cost View token usage
@<file path> Reference a file into context
## Context Engineering Hierarchy

"Context engineering" means deliberately organizing what Claude reads on every request — putting high-value, stable information up front and keeping noise out. Claude Code's context follows a clear precedence hierarchy (highest to lowest):

  1. Enterprise / managed policy — rules pushed down by your organization. Highest precedence; individuals cannot override them.
  2. Project memory CLAUDE.md / AGENTS.md — repo-root project instructions describing code conventions, build commands, and project background.
  3. Path-scoped rules — nested CLAUDE.md files or .claude/rules in subdirectories, applying only to work under that path.
  4. Live conversation history — the back-and-forth in the current session. Lowest precedence, and the part most likely to balloon.

Understanding this hierarchy helps you put instructions in the right place: general conventions go in the project-level CLAUDE.md, directory-specific rules go in path-scoped rules — rather than re-stating them in every conversation.

Practical tips

  • Keep instructions concise and high-signal: the leaner your CLAUDE.md, the easier it is for Claude to follow; long, off-topic content dilutes the key instructions.
  • /compact at logical breakpoints: compress history after finishing a phase — long, unfocused conversations degrade accuracy.
  • /clear between unrelated tasks: wipe history when switching to a completely unrelated task so old context can't interfere.
  • Reference by path instead of pasting: use @<file path> to let Claude read files on demand instead of stuffing large blocks into the conversation — it saves context and improves accuracy.
  • Put stable content first: unchanging content like system prompts, CLAUDE.md, and large background blocks should come first and stay unedited, so they can be hit by prompt caching (cached reads cost roughly 10% of normal input). This is exactly why a concise, stable CLAUDE.md plus reusing sessions saves money.

In short: push stable, high-value content to the top of the hierarchy and keep it unchanged, and periodically rein in the volatile conversation history. To go deeper, see CLAUDE.md Project Memory and Cost Optimization.

Details of the common context commands

As of 2026-10-15, according to the Claude Code command reference:

Command What the documentation says
/compact [instructions] Summarizes the conversation so far to free context; you can add instructions about what to keep
/clear Starts a new conversation with empty context (aliases /reset, /new); the earlier conversation can be found again with /resume
/context Shows current context usage as a colored grid with optimization suggestions for the biggest consumers; add all to expand details
/rewind Rewinds conversation and code to an earlier point; you can also choose "Summarize from here" or "Summarize up to here" to compress only one part and keep the other
/autocompact [auto\|size] Sets the auto-compact window, e.g. /autocompact 500k; auto returns to the default tuned for your model

According to the official memory documentation, the project-root CLAUDE.md is re-read from disk after /compact and is not lost; instructions given only in the conversation may disappear after compaction, so write anything that must persist into CLAUDE.md.

Notes when using QCode

  • Claude models have no separate long-context tier and are billed on actual tokens. For GPT models, when a single request's input (including cache-hit tokens) exceeds 272K, the whole request is billed at the long-context rate; the rule is in Billing. Compacting before a long session nears 272K is cheaper.
  • Every turn resends the entire current context to the model, so the longer the session, the more tokens each turn uses. A stable prefix (CLAUDE.md, system prompt) can be served from the cache, whereas changing early content often invalidates it.
  • Use /usage (alias /cost) for the current session's usage. QCode bills per token; per-model rates are listed at qcode.cc/models.

Common errors and how to troubleshoot

Symptom Cause Fix
429 with E015 / Internal server error in the body The context is near the model limit (about 95%): a long-session overflow Run /compact first; if that fails too, exit and restart, see "Preventing long-session overflow" above
/compact itself errors The compaction request must also carry the whole context, which no longer fits Exit and restart Claude Code; use /resume to return to an earlier conversation if needed
Claude "forgot" a requirement after compaction It was only stated in the conversation Put it into CLAUDE.md
You want to pin the 200K window The default window is already 1M Set the environment variable CLAUDE_CODE_DISABLE_1M_CONTEXT=1
Context grows unusually fast Large files were referenced, or tool output is long Use /context to find the big consumers and reference only relevant parts

FAQ

Should I use /compact or /clear?

Use /compact when you keep working on the same thing, since it keeps a summary. Use /clear only when switching to a completely unrelated task, since it starts from blank.

Does compaction drop CLAUDE.md?

No. The project-root CLAUDE.md is re-read after compaction; nested CLAUDE.md files in subdirectories and path-scoped rules are loaded again on demand.

How do I make auto-compaction trigger earlier?

Set the window with a command such as /autocompact 500k; auto returns to the default tuned for your model. The command requires Claude Code v2.1.221 or later. You can also run /compact yourself once usage reaches 70% to 80% (a suggested rule of thumb, not an official figure).

I compacted and lost something. Can I go back?

Yes. Use /rewind and pick an earlier point to restore the conversation, the code, or both. It relies on the session's checkpoints; see Workflow Tips.

Get started with QCode

One API key for Claude Code, Codex and more — billed per token, per-model prices on qcode.cc/models

Enterprise →