Context Management
Understanding and managing Claude Code's context window, and effectively using commands like /compact and /clear
Context Management¶
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¶
Claude Code uses a 200,000 token context window by default. The Opus 4.6 and Sonnet 4.6 models support expansion to 1,000,000 tokens (1M context).
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: Claude Code automatically triggers compaction when the context reaches 95% capacity by default. 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:
/clearremoves 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.mdto 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 upstream 5xx errors as 429 responses to take advantage of Claude Code's built-in retry mechanism.
Resolution Steps¶
-
Try
/compact: -
If successful, the conversation can continue normally
-
If
/compactitself errors (the compaction request also requires sending the full context), proceed to the next step -
Exit and restart Claude Code:
bash # Press Ctrl+C or type /exit # Then restart claude
Prevention Tips¶
- Compact regularly: Proactively run
/compactwhen 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/costcommand 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 |