Claude Code Context Management: A Practical Guide
Claude Code context management: what fills the context window, how to check it with /context, when to /compact or /clear, and how subagents keep it clean.
Josh Pocock7 min read
AI Coding
Context
Stride AI Academy
Claude Code context management means keeping the context window filled with what Claude needs for the current task, and nothing else. Check usage with /context, run /compact to summarize a long conversation, run /clear when you switch to unrelated work, and send big reads to subagents. A smaller context costs less on every message and leaves room for the files that matter.
This guide covers what fills the window, how to see it, when to compact or clear, and the setup choices that keep it lean from the start. Everything here was checked against Claude Code's context window docs, the costs docs, the commands reference, the model configuration docs, the memory docs and the status line docs on November 18, 2026.
What fills the Claude Code context window
The context window holds everything Claude knows about your session: your instructions, the files it reads, its own replies and content you never see in the terminal.
Before you type anything, Claude Code loads:
- The system prompt and environment info
- Your CLAUDE.md files (user and project)
- Auto memory, the first 200 lines or 25KB of
MEMORY.md - MCP tool names and skill descriptions
As Claude works, every file read, search result, command output and reply adds more. Path-scoped rules load when Claude touches matching files, and hook output lands in context too.
The docs' example session reaches about 22,000 tokens after one bug fix. Their numbers are illustrative, but the pattern holds: file reads and command output are what grow fastest.
Recent models, including the Fable models, Sonnet 5 and later, Haiku 5.5 and Opus 4.7 and later, run with a 1 million token window by default. A bigger window doesn't make a long session free, though. The costs docs point out that Claude Code sends your full conversation with every request, so a one-line question in a session open all day still draws usage for the whole thing.
How to check your context usage
Run this at any point:
/contextIt shows current usage as a coloured grid, broken down by category, with suggestions for context-heavy tools, memory bloat and capacity warnings. It also lists which CLAUDE.md and auto memory files loaded. Add all to expand the full breakdown.
For a constant view, put it in your status line. The JSON Claude Code passes to your status line script includes context_window.used_percentage, context_window.remaining_percentage and context_window.context_window_size. A one-line version from the docs:
jq -r '"[\(.model.display_name)] \(.context_window.used_percentage // 0)% context"'/usage shows the session's token usage and cost on the API, and on a subscription plan it shows what's counting against your limits, including recommendations such as trimming a large CLAUDE.md.
Claude Code compact: summarize without starting over
/compact replaces the conversation so far with a structured summary, so you keep working in the same session with less in the window. Add instructions to steer what the summary keeps:
/compact focus on the auth bug fix and the failing test namesYou can also put standing instructions in your project's CLAUDE.md:
# Compact instructions
When you are using compact, please focus on test output and code changesTo summarize only part of a conversation, run /rewind, pick a message and choose Summarize from here or Summarize up to here.
What survives compaction
| Item | After compaction |
|---|---|
| Project-root CLAUDE.md and unscoped rules | Re-read from disk |
| Auto memory | Re-read from disk |
| The plan from plan mode | Re-read from disk |
| Skill descriptions | Rebuilt and re-added (v2.1.304 or later) |
| Files Claude read or edited | Up to five re-read, most recently modified first |
| Invoked skill bodies | Re-added, capped at 5,000 tokens each and 25,000 total |
| Nested CLAUDE.md files and path-scoped rules | Reload when Claude touches a matching file |
| Anything you said only in chat | Summarized with the rest of the conversation |
The practical lesson: if an instruction must survive, put it in the project-root CLAUDE.md, not in a chat message. A file over 5,000 tokens comes back as a path reference rather than its full content.
One cost note from the docs: /compact reads the whole conversation it summarizes, so compacting a large context is itself a large request.
Auto-compact and when it runs
You don't have to compact by hand. Claude Code compacts automatically as the conversation nears its limit, so a full window doesn't end your session. On models with a native 1M window, that happens at about 967K tokens by default.
To compact earlier, set the auto-compact window:
/autocompact 500k
/autocompact auto/autocompact 500k saves 500K as the window for your current model. auto returns to the default. For one launch, use the flag:
claude --autocompact 300kOr set it for every model in ~/.claude/settings.json:
{
"autoCompactWindow": 200000
}The window has to be 100K tokens or more, and Claude Code caps it at the model's context window. If you'd rather stay at 200K on every model, set CLAUDE_CODE_DISABLE_1M_CONTEXT=1.
When to /clear and start fresh
/compact keeps continuity. /clear throws the conversation away and starts with empty context, keeping your project memory. The costs docs put it plainly: when you want a fresh start instead of continuity, /clear costs nothing.
Clear when you switch to unrelated work. Old conversation crowds out the files you need next and costs tokens on every message. To come back later, name the session first:
/rename stripe-webhook-fix
/clearThen /resume brings it back from the picker. For a quick question that shouldn't land in the conversation at all, use /btw:
/btw what does the --frozen-lockfile flag do again?Compact or clear?
- 1Is the next task related to this one?If not, /rename then /clear
- 2Is the window getting full or noisy?
- 3Run /compact with a focus instruction
- 4Keep working in the same session
Use subagents to keep the main context clean
Exploring a big codebase or reading long logs fills your window fast. Hand it to a subagent instead:
use a subagent to investigate how our auth system handles token refreshThe subagent reads files in its own context window and only a summary comes back. In the docs' example, the subagent used about 9,900 tokens on its own while returning a 420-token summary to the main session. Its requests still count toward your usage, so for simple jobs give the subagent a smaller model.
Keep the starting context small
What loads at startup is paid for on every request, so trim it once and save tokens all session.
Keep CLAUDE.md short. The memory docs say to target under 200 lines per CLAUDE.md file, because longer files use more context and reduce how well Claude follows them. Splitting into @path imports helps organisation but doesn't reduce the cost. How Claude memory works covers the full setup.
Move workflow instructions into skills. CLAUDE.md loads every session. Skills load only when used, so put PR review steps or migration checklists there.
Watch MCP overhead. MCP tool definitions are deferred by default, so only tool names and server instructions load until Claude uses a tool. Still, run /mcp and disable servers you aren't using. The costs docs also note that CLI tools such as gh are more context-efficient than MCP servers because they add no per-tool listing.
Filter noisy output with hooks. A PreToolUse hook can trim test output to its last 100 lines, or grep a log for errors, before Claude ever sees it.
Learn to build with Claude Code
The Stride AI Coding course teaches Claude Code and Codex for building client tools, written for non-programmers, with weekly live calls to review your builds. Many of the free guides cover Claude Code setups, and the Claude Code community is where members compare theirs.
FAQ
How do I see how much context Claude Code is using?
Run /context for a coloured grid broken down by category, with suggestions for what to trim. For a constant view, add context_window.used_percentage to your status line script.
What is the difference between /compact and /clear?
/compact summarizes the conversation so you keep working in the same session with less context. /clear starts a new conversation with empty context. Use compact for continuity and clear when you switch to unrelated work.
Does Claude Code compact automatically?
Yes. It compacts as the conversation nears the limit, at about 967K tokens by default on models with a native 1M window. Run /autocompact with a size such as 500k to make it compact earlier.
What is the Claude Code context window size?
Recent models, including Sonnet 5 and later, Haiku 5.5, Opus 4.7 and later and the Fable models, run with a 1 million token window by default. Opus 4.6 and Sonnet 4.6 reach 1M only through their [1m] variant, depending on your plan.
Does CLAUDE.md survive /compact?
The project-root CLAUDE.md does: Claude Code re-reads it from disk after compaction. Nested CLAUDE.md files and path-scoped rules reload only when Claude touches a matching file again, and instructions given only in chat get summarized.
Do subagents use my context window?
No. Each subagent works in its own context window and returns a summary to yours. Its requests still count toward your usage, though.