Context Window Management
The context window is Claude’s working memory (~200K tokens standard; Sonnet 5 supports a native 1M window, and opus[1m] selects 1M for Opus). Everything competes for it: system prompt, CLAUDE.md, auto memory, tool schemas, file reads, tool output, conversation. Managing it well is the #1 skill separating intermediate from pro users.
What Loads at Startup (before you type anything)
Section titled “What Loads at Startup (before you type anything)”| Item | Approx. cost | Notes |
|---|---|---|
| System prompt | ~4K tokens | Always, invisible |
| Auto memory (MEMORY.md) | varies | First 200 lines / 25KB |
| CLAUDE.md files | varies | User + project + local |
| Environment info | ~300 | cwd, platform, git status |
| MCP tools | small if deferred | Full schemas load on demand via tool search (ENABLE_TOOL_SEARCH) |
| Skill descriptions | ~20-60 each | Bodies load only when invoked |
Inspect: /context
Section titled “Inspect: /context”/contextShows a live breakdown of what’s consuming context. Run it when a session feels sluggish or before a big task.
Reclaim Space
Section titled “Reclaim Space”| Tool | What it does | When to use |
|---|---|---|
/clear | Empty context, fresh start | Switching to an unrelated task |
/compact [focus] | Replace history with summary | Mid-task, context near full |
/rewind → Summarize | Compress only part of the conversation | Verbose debugging detour you’re done with |
| Subagents | Verbose work happens in a separate window; only summary returns | Test runs, doc fetching, log analysis |
Rule of thumb: /clear between tasks, /compact within a task.
Auto-Compaction & Microcompaction
Section titled “Auto-Compaction & Microcompaction”- When context nears the limit, Claude Code auto-compacts: summarizes the conversation and continues. You’ll see a “context low” warning first.
- Microcompaction silently trims old large tool outputs (file dumps, test logs) before resorting to full compaction.
- What survives compaction: CLAUDE.md, system prompt, memory, the summary. What doesn’t: raw tool outputs, full message history (still in the transcript on disk).
Prompt Caching (why long sessions stay cheap)
Section titled “Prompt Caching (why long sessions stay cheap)”Claude Code caches the conversation prefix with the API (5-minute TTL). Each turn only pays full price for new tokens. Implications:
- Rapid iterations are cheap; a session idle >5 min re-reads everything uncached
- Switching models (
/model) invalidates the cache — the picker warns you - Big CLAUDE.md files cost you on every cache miss, not just once
Practical Hygiene Checklist
Section titled “Practical Hygiene Checklist”- Keep CLAUDE.md lean (< ~150 lines); push detail into skills that load on demand
- Don’t paste huge files into chat — give paths and let Claude read selectively
- Ask for targeted reads: “read only the auth middleware section”
- Delegate log-heavy commands to subagents
/contextwhen in doubt;/clearliberally between tasks- Watch the status line — configure it to show context usage (see 05-interactive-mode-shortcuts.md)
Real Case: Long Debugging Session Going Stale
Section titled “Real Case: Long Debugging Session Going Stale”Symptoms: slow responses, Claude forgetting earlier decisions.
1. /context → 78% used, mostly old test output2. /rewind → "Summarize up to here" on the point where debugging started → early exploration compressed, recent work intact3. Continue with full precision on the actual fixAlternative when the task is done: /clear, then start the next task with a one-line recap.
🧠 Quick Recall
Section titled “🧠 Quick Recall”- Trick: “Window = RAM, transcript = disk.”
- “Clear between tasks, compact within them.”
👨🏫 Teach It
Section titled “👨🏫 Teach It”- Show:
/contextbefore and after a big file read — make tokens visible. - They’ll trip on: the 5-minute cache TTL — a long lunch makes the next turn slow (that’s normal).
Learning path: ⬅ 08-sessions-and-checkpointing.md · Index · ➡ 10-plan-mode-and-thinking.md
Written by Fenil Patel