Context management in Claude Code
Up to date as of April 2026.
A few weeks ago I spent two hours debugging an auth issue in a session that had already touched three unrelated features. By the end, Claude was making suggestions that contradicted what it had told me an hour earlier. The context was full of irrelevant junk and I was too stubborn to clear it.
Eventually cleared it. Fixed the bug in 20 minutes.
The main principle
The context window is your most important resource.
The easiest way to make Claude Code feel worse is to keep piling things into one session and hope it somehow stays sharp. Everything counts: every message, every file read, every command result, every answer back from Claude. As the window fills up, quality usually drops before you notice it.
Context size depends on the model you’re running. Some models have extended context (including 1M-token variants), but the only number that matters is the one in your session: check /context.
What loads automatically at startup
Before you type anything, some of the window is already spoken for:
| What |
|---|
| System prompt + built-in tools |
Your CLAUDE.md + any imported files |
| Auto memory (if enabled) |
| Skill list metadata |
| MCP server tool definitions (if enabled) |
That is why two sessions with the same prompt can still behave differently. Different MCP servers, different memory files, different imported rules. If you want the real breakdown, run /context. If you want to know which memory files loaded, run /memory.
Monitoring
I check these two more than any other commands when Claude starts feeling “off”:
/context # breakdown by category: what takes how much
/memory # which CLAUDE.md and auto memory files are loaded
The status line at the bottom shows fill % in real time.
Context management commands
/clear
Full reset. Start from scratch. CLAUDE.md reloads, conversation history disappears.
When: switching to a different task, too much irrelevant context accumulated.
/compact [instruction]
Claude summarizes the conversation, replaces history with a brief summary. You can specify what to keep:
/compact focus on the authentication changes we made
/compact keep the list of modified files and test commands
When: context is filling up but the task isn’t done.
/btw question
Quick side question without adding it to the main conversation.
/btw what type does this function return?
/btw is there a built-in for this in Python?
When: you need a quick clarification without polluting the main thread.
/rewind (or double Esc)
Opens a restore menu to a previous checkpoint. You can restore: conversation, code, or both.
When: Claude went the wrong way, you want to roll back and try differently.
Autocompact
Auto-compaction is the safety net, not the primary strategy.
It kicks in automatically when context usage gets close to full.
If you want it to happen earlier, Claude Code documents this env var:
// ~/.claude/settings.json
{ "env": { "CLAUDE_AUTOCOMPACT_PCT_OVERRIDE": "75" } }
That sets the threshold as a percentage of available context. Default behavior is roughly 95%. Lower values compact earlier. The setting applies to both the main conversation and subagents.
If you’re using an extended-context model and want compaction math based on a smaller effective window, Claude Code also documents CLAUDE_CODE_AUTO_COMPACT_WINDOW.
What survives /compact: Claude re-reads CLAUDE.md from disk, but ad-hoc instructions that existed only in the conversation can be lost. If something matters, move it into CLAUDE.md.
What eats the most context
1. MCP tool definitions
Every connected MCP server adds descriptions of all its tools at startup.
- 1 tool ~ 550-1400 tokens
- An MCP server with dozens of tools -> tens of thousands of tokens immediately
Mitigation: only connect the servers you need, disable the rest via /mcp.
2. Reading large files
Every file Claude reads — fully in context.
Mitigation: use subagents for exploration, and reference the exact file you care about instead of vaguely saying “look around the auth system.”
3. Long conversations
Accumulated message history.
Mitigation: /clear between tasks, /compact within a task.
Best practices
Most context problems are workflow problems in disguise.
Give Claude a way to verify the result
This is the most important one. Claude works significantly better when it can check its own work:
# Bad
"implement email validation"
# Good
"write validateEmail function. test cases: user@example.com -> true,
invalid -> false, user@.com -> false. run tests after implementing."
Without verification criteria Claude can produce something that looks right but doesn’t work.
Explore -> Plan -> Code -> Commit
Separate exploration and implementation. The session that reads widely is usually not the session you want doing the final edit.
- Plan mode — Claude reads files, builds a plan, changes nothing
- You approve the plan
- Normal mode — implements according to plan
- Commit
# Enable plan mode at launch
claude --permission-mode plan
# or Shift+Tab inside a session
Specific prompts save context
The more specific the prompt — the fewer iterations and less context spent:
# Bad — triggers many clarifying questions
"add a calendar widget"
# Good — everything is clear immediately
"look at HotDogWidget.php to understand widget patterns.
implement a calendar widget in the same style with month
selection and year pagination. no new libraries."
Subagents for exploration
Codebase exploration reads many files — all of it pollutes the main context:
# Bad — Claude reads everything in the main context
"investigate how our auth system handles token refresh"
# Good — subagent works in its own context
"use a subagent to investigate how our auth system handles
token refresh and whether we have OAuth utilities to reuse"
The subagent returns a brief summary — the main context stays clean.
/clear between unrelated tasks
# Done with auth -> starting UI work
/clear
# Now a clean context without leftovers from the auth task
After two failed corrections — reset and start over
If you’ve corrected Claude twice and it still gets it wrong — the context is polluted with failed approaches. /clear and write a better initial prompt with what you’ve learned.
Use @ to reference files
@src/api/auth.ts look at how this file works
Instead of describing where things are — give a direct reference.
Pipe data directly
cat error.log | claude -p "what's going on here?"
gh pr diff | claude -p "summarize this PR"
Workflows for different task sizes
This is the simplest mental model I know:
Small task (typo, rename, quick fix)
-> Just write what you need, no planning
-> Claude does it
-> You verify
Medium task (new endpoint, feature)
-> Plan mode: "read src/api/ and plan how to add X"
-> Approve the plan
-> Normal mode: "implement"
-> Run tests
-> Commit
Large task (refactoring, migration)
-> Separate session for exploration with subagents
-> Write spec to a file
-> New clean session for implementation
-> Worktrees for parallel parts
Anti-patterns
“Kitchen sink” session — started with one thing, then asked something unrelated, then came back. Context is full of irrelevant stuff.
-> Fix: /clear between unrelated tasks.
Endless corrections — Claude does it wrong, you correct, still wrong. Context is polluted with failed approaches.
-> Fix: after two corrections -> /clear and a better prompt.
Endless exploration — asked “investigate X” without boundaries, Claude reads hundreds of files. -> Fix: narrow the scope or use subagents.
Trust but don’t verify — Claude produced something plausible, you shipped to prod. -> Fix: always give Claude a way to verify (tests, screenshots, scripts).
If a session starts feeling muddy, it usually is. Reset earlier than your instincts tell you to.
Checkpoints and resume
Claude automatically creates checkpoints before every change. It’s not a replacement for git, but lets you roll back:
Esc + Esc # or /rewind — open checkpoint menu
Sessions are saved locally:
claude --continue # continue the last session in this directory
claude --resume # pick from recent sessions
claude --resume auth-refactor
/rename # name a session ("oauth-migration")
Parallel sessions for scaling
When you need more:
# Multiple worktrees — multiple independent Claude instances
git worktree add ../proj-auth feature/auth
git worktree add ../proj-ui feature/ui
cd ../proj-auth && claude &
cd ../proj-ui && claude &
Writer/Reviewer pattern:
- Session A: implements the feature
- Session B: reviews with a clean context (not biased toward the code it just wrote)
Fan-out for batch operations:
for file in $(cat files.txt); do
claude -p "migrate $file from React to Vue" \
--allowedTools "Edit" "Bash(git commit:*)"
done
Quick command cheatsheet
| Command | When |
|---|---|
/context | See what takes how much space |
/clear | New task or polluted context |
/compact | Task continues, need to free space |
/compact focus on X | Compaction preserving specific details |
/btw question | Quick side question outside the main thread |
/rewind or Esc+Esc | Roll back to a previous state |
Esc | Stop Claude mid-response |
/mcp | Disable unused MCP servers |
--permission-mode plan | Read-only mode before implementation |
claude --continue | Continue a previous session |