Context Management
Independent educational resource, not an official Anthropic course. Facts about the API, prices and limits were checked against Anthropic's documentation on 15 November 2026 and the code was run against a local mock of the API, not the live service. Check the linked official pages before relying on a number, and test with your own key.
Everything Claude knows during a session lives in one place: the context window. Your instructions, the files it reads, its own replies, tool output, MCP tool names, skill descriptions, all of it. It's large on current models, so running out is rare now. But a bloated context still costs you money on every request and makes answers worse. This lesson explains what fills the window, what the commands do, and what survives when a session gets compacted.
What you'll learn
- What goes into Claude Code's context window and what it costs.
- How to see it (
/context) and shrink it (/clear, /compact, /rewind).
- What survives compaction and what doesn't.
- How skills, subagents and MCP affect context.
- How to resume and name sessions.
Before you start
What's in the window
Anthropic's documentation says the window holds everything Claude knows about your session, including content that never appears in your terminal. Before you type anything, these load:
- your
CLAUDE.md files and auto memory;
- the names of MCP tools and server instructions (the definitions are deferred and loaded on demand through tool search);
- skill descriptions;
- the system prompt, and any output style or
--append-system-prompt text.
Then it fills as you work: every file Claude reads, every command output, its own replies, path-scoped rules loaded alongside matching files, and anything hooks add. Subagents are the exception, because their reading happens in their own window, with only a summary coming back.
It's the same principle as lesson 1.5, with the same warning: the docs call it context rot, where accuracy and recall degrade as the token count grows. Curating the context matters as much as its size.
On models with a 1M-token window (most current ones) you'll rarely run out. Anthropic's best-practices page lists "manage the context window aggressively" among advice you can stop following in its old form. You can still pay for context you don't need. Every request sends the whole conversation, so a session that has collected three unrelated tasks re-reads all three on every turn of the fourth. That's cheaper while the prompt cache is warm, but it isn't free.
See it: /context
Run /context for a live breakdown by category with suggestions, including which CLAUDE.md and auto memory files loaded. /context all lists the memory files in full. /memory opens them for editing. /usage breaks your usage down by skill, subagent, plugin and MCP server on supported plans, and flags patterns such as long contexts or frequent cache misses once they pass 10 percent of your usage.
Shrink it: five commands
| Command | What it does | Use it when |
/clear | Starts a new conversation. Resume the old one with /resume. /clear oauth-migration names the one you're leaving. | Switching to unrelated work, or after two failed corrections. |
/compact | Replaces the conversation with a summary. Add a focus: /compact focus on the auth bug fix. | Mid-task, when you want to keep going but trim history. |
/rewind (or Esc twice) | Rolls code, conversation or both back to an earlier prompt. Can also summarise from or up to a chosen message. | An approach didn't work out; or part of the conversation has gone stale. |
/autocompact 500k | Sets how full the window gets before automatic compaction. | You want compaction earlier than the default. |
| Delegate to a subagent | Large reads stay in the subagent's window. | Research or test runs with long output. |
Anthropic's guidance on steering is a good habit to adopt: watch the first minute, press Esc the moment Claude drifts, and after two failed corrections run /clear and write a better prompt instead of a third. A third correction mostly adds to a conversation that already contains two failed attempts for Claude to keep re-reading.
A caution about /rewind: checkpoints cover files Claude edited with its own file tools in the main conversation. A file that only a shell command touched isn't restored, and remote effects such as databases can't be rewound. Checkpoints work alongside git, not instead of it.
Automatic compaction, and what survives it
As you approach the limit, Claude Code compacts for you, so a full window doesn't end your session. The summary keeps your requests and key code snippets, but detailed instructions from early in the conversation may be lost. If a single file or tool output is so large that context refills after each summary, Claude Code stops auto-compacting after a few attempts and shows an error rather than looping.
This table, from Anthropic's context-window page, is the one to remember:
| Mechanism | After compaction |
| System prompt and output style | Still apply |
Project-root CLAUDE.md and unscoped rules | Re-injected from disk |
| Auto memory | Re-injected from disk |
| Skill descriptions | Rebuilt and re-injected |
| The plan Claude wrote in plan mode | Re-injected from disk |
Rules with paths:; nested CLAUDE.md files | Reload on demand, when a matching file is touched again |
| Files Claude read or edited | Up to five re-read, most recently modified first; a file over 5,000 tokens comes back as a path reference |
| Invoked skill bodies | Re-injected, capped at 5,000 tokens per skill and 25,000 total, oldest dropped first |
| Background commands and subagents | Keep running; Claude is reminded they exist |
| Instructions given only in conversation | Summarised with everything else, and may be lost |
| Context that hooks added earlier | Summarised with the rest |
SessionStart hooks matching compact | Run again; their output is added back |
Three practical conclusions. Put lasting rules in CLAUDE.md, not in conversation. If a path-scoped rule must persist through compaction, drop its paths or move it to the root file. And because truncation of a large skill keeps the start of the file, put the most important instructions at the top of SKILL.md.
Hooks can help directly. A SessionStart hook whose matcher is compact can re-inject a short reminder, such as the current sprint's rules, every time compaction happens:
{
"hooks": {
"SessionStart": [
{
"matcher": "compact",
"hooks": [
{ "type": "command", "command": "echo 'Reminder: use Pest, money is cents, run vendor/bin/pint --dirty before finishing.'" }
]
}
]
}
}
Anthropic's hooks guide lists "re-inject context after compaction" among the standard recipes, and the documented behaviour is that hooks matching the compact source run after compaction and their output is added to the compacted context. For deliberate control of what a summary keeps, add a "Compact Instructions" section to CLAUDE.md or use /compact with a focus.
Skills, subagents and MCP
- Skills cost only their description until used. For skills you only run by hand,
disable-model-invocation: true keeps even the description out of context.
- Subagents read in their own window. Your context only receives a summary and a small metadata trailer.
- MCP tool definitions are deferred by default; only names and server instructions use context until a tool is used.
Sessions: resume, name, branch
Each conversation is saved locally as a JSONL file under ~/.claude/projects/. claude --continue resumes the most recent session in the current directory, claude --resume lets you pick one (or /resume inside a session), and claude --resume oauth-migration resumes by name after you've run /rename oauth-migration. Forking (--fork-session or /branch) copies a session into a new ID and leaves the original unchanged. Sessions are tied to directories, so parallel work in git worktrees stays separate. Switching git branches shows Claude the new branch's files, but the conversation history stays the same.
Real-world use case (illustrative scenario)
This scenario is an invented example to show how the ideas fit together. It is not a case study and its details are not measured results.
A developer spends an afternoon in one session: first debugging a flaky test, then adding a feature, then updating docs. By the third task, replies are slower and Claude keeps referring to the flaky test. They run /clear between tasks from then on, using /rename to label the unfinished feature so claude --resume can pick it up tomorrow. For one long refactor they keep the session but run /compact keep the API changes and the test commands before starting the risky part, because the summary now preserves what they chose, not what an automatic pass guessed.
Mistakes to avoid
- One endless session for unrelated tasks.
- Correcting the same mistake three times in the same conversation instead of clearing.
- Telling Claude important rules only in chat and expecting them to survive compaction.
- Relying on
/rewind for shell-made changes or database changes.
- Pasting huge logs into the main session instead of having a subagent read them.
Security notes
Sessions are saved as plaintext on your machine, including whatever was pasted or printed, so don't paste secrets, and know that ~/.claude/projects/ may hold sensitive content. Anthropic documents where this data lives and how to clear it. Auto-compaction summarises tool output too, so a secret that appeared once can end up in a summary.
Cost and speed
Costs follow context length, because each request resends the conversation. The prompt cache lasts about an hour on a subscription and five minutes on an API key, a cloud provider or usage credits. After it expires, the whole history is billed at full price again, so a session you return to after lunch is more expensive than a fresh one. Anthropic notes that on the one-hour cache, an idle session beyond about 200K tokens is compacted shortly before the cache expires.
Key takeaways
- The window holds instructions, memory, file reads, tool output and replies. More isn't better (context rot) and every request resends it.
/context shows it; /clear, /compact [focus] and /rewind shrink or reset it.
- After compaction, root
CLAUDE.md, auto memory and recent files return; conversation-only instructions may not.
- Clear between unrelated tasks, delegate noisy reads, name sessions you'll return to.
Lesson recap: see the window, shrink it, and know what survives compaction.
Quick quiz
- Which command gives a live breakdown of what's using context?
- What does
/compact focus on the auth bug fix do?
- Does an instruction you typed only in conversation survive compaction?
- Why start a new session after two failed corrections?
- What does a subagent send back to your main context?
Answers and explanations
/context. /context all also lists the memory files.
- It compacts the conversation and tells the summary to keep what relates to the auth bug fix.
- Not reliably. It's summarised with the rest, so lasting rules belong in
CLAUDE.md.
- The conversation then contains two failed attempts that Claude keeps re-reading. A fresh prompt that folds in what you learned works better.
- A summary and a small metadata trailer. The files it read stay in its own window.
Official references and further reading
Related tutorials