Guides
Claude Code context window explained: what fills it, and what /compact keeps
The context window holds everything Claude knows about your session: your instructions, the files it reads, its own responses, and content that never appears in your terminal. This page walks through Anthropic's Explore the context window documentation: what loads at startup, how to check usage, what happens when the window fills, what is still there after /compact, and how /compact, /clear and /rewind each work. The documentation was checked on October 30, 2026.
- The context window holds more than your conversation: CLAUDE.md, auto memory, MCP tool names and skill descriptions all load before you type anything.
- Run
/contextfor a live breakdown by category with optimization suggestions. - Claude Code compacts automatically as you approach the limit, so a full context window does not end your session.
- After compaction, the project-root CLAUDE.md and auto memory are re-injected from disk. Rules with
paths:and nested CLAUDE.md files are reloaded on demand instead. - To make the summary keep what you care about, run
/compactwith instructions, such as/compact focus on the auth bug fix. Use/clearwhen switching to unrelated work.
What goes into the context window
The documentation uses an interactive timeline that plays a session from startup to compaction. Its token counts are representative, not your real usage. As the documentation describes it, the order is roughly this:
- Before you type anything: CLAUDE.md, auto memory, MCP tool names, and skill descriptions all load into context. AGENTS.md files can load in place of CLAUDE.md. Your own setup may add more here, like an output style or text from
--append-system-prompt. - As Claude works: each file read adds to context, path-scoped rules load automatically alongside matching files, and a PostToolUse hook fires after each edit.
- The follow-up prompt: a subagent handles the research in its own separate context window, so the large file reads stay out of yours. Only the summary and a small metadata trailer come back.
- At the end of the timeline: you run
/compact, which replaces the conversation with a structured summary. Most startup content reloads automatically; the table below shows what happens to each mechanism.
For writing subagents, see Claude Code subagents. For skills, see Claude Code skills. For CLAUDE.md, see how to write CLAUDE.md.
How to check context usage
The documentation says to run /context at any point to see your actual usage: a live breakdown by category with optimization suggestions, including which CLAUDE.md and auto memory files loaded. Run /memory to open and edit those files.
The commands reference describes the two commands like this:
| Command | What the documentation says |
|---|---|
/context [all] | Visualizes current context usage as a colored grid, with optimization suggestions for context-heavy tools, memory bloat, and capacity warnings. When the conversation exceeds the context window, the output includes a warning showing how far over the limit you are and which command frees space. In fullscreen mode, /context collapses the per-item breakdown to keep the grid visible. Pass all to expand it. |
/memory | Edits CLAUDE.md files, enables or disables auto memory, and views auto memory entries. |
The costs documentation adds that /usage checks your current token usage, and that you can configure your status line to display it continuously. /usage shows session cost, plan usage limits, and activity stats.
What happens when the context fills up
Claude Code compacts automatically as you approach the limit, so a full context window does not end your session. The automatic pass works the same way as the /compact step in the timeline.
The point where automatic compaction runs depends on your model and configuration. The documentation keeps the per-model default thresholds, and the fix for when Claude Code assumes the wrong window for your model ID, on the model-config page. This page gives no specific numbers.
If you need a larger window rather than a smaller conversation: the documentation says Fable models, Sonnet 5 and later, Haiku 5.5, Opus 4.6 and later, and Sonnet 4.6 support a 1 million token context window. Availability by plan and how to select a [1m] model variant are in its Extended context section. It also says compaction works the same way at the larger limit.
What survives compaction and what does not
When a long session compacts, Claude Code summarizes the conversation history to fit the context window. As of v2.1.198, the summarization request inherits your session's extended thinking configuration, so it reasons with thinking enabled when your session has it enabled and stays off otherwise. Thinking affects only how the summary is produced; your session settings are unchanged afterward. What happens to each kind of content depends on how it was loaded:
| Mechanism | After compaction |
|---|---|
| System prompt and output style | Both still apply |
| Project-root CLAUDE.md and unscoped rules | Re-injected from disk |
| Auto memory | Re-injected from disk |
| Skill descriptions | Rebuilt from your current skills and re-injected. Requires Claude Code v2.1.304 or later |
| Git status snapshot | If the session started with one, Claude Code reads a fresh one from your repository |
| The plan Claude wrote in plan mode | Re-injected from disk |
Rules with paths: frontmatter | Claude Code reloads them on demand |
| Nested CLAUDE.md in subdirectories | Claude Code reloads them on demand |
| Files Claude read or edited | Claude Code re-reads up to five, most recently modified first |
| Invoked skill bodies | Re-injected, capped at 5,000 tokens per skill and 25,000 tokens total; oldest dropped first |
| Background commands and background subagents | Keep running. Claude Code reminds Claude which ones are still running so it does not start a duplicate |
| Context that hooks added earlier | Summarized with the rest of the conversation |
SessionStart hooks that match the compact source | Claude Code runs them and adds their output to the compacted context |
Three details the documentation calls out:
- File re-reads have a size limit. Right after compaction, Claude Code re-reads up to five of the files Claude has read or edited in the session, choosing the ones modified most recently. A file over 5,000 tokens comes back as a path reference without its content, shown as
Referenced fileinstead ofRead. - Path-scoped rules and nested CLAUDE.md files get summarized away. They load into message history when Claude reads, writes, or edits their trigger file, so compaction summarizes them away with everything else. If a rule must persist across compaction, drop the
paths:frontmatter or move it to the project-root CLAUDE.md. - Skill bodies can be truncated. Skill bodies are re-injected, but large skills are truncated to fit the per-skill cap, and the oldest invoked skills are dropped once the total budget is exceeded. Truncation keeps the start of the file, so put the most important instructions near the top of
SKILL.md.
What you can do before it compacts
You can act before the automatic pass runs. The documentation lists five ways:
| Approach | How |
|---|---|
| Compact with a focus | Run /compact with instructions, like /compact focus on the auth bug fix, before starting a long new task. The summary keeps what you choose instead of what the automatic pass guesses is important. |
| Compact part of the conversation | Run /rewind, select a message, and choose Summarize from here or Summarize up to here. |
| Compact earlier | Run /autocompact with a token count, like /autocompact 500k, to set how full the context window gets before the automatic pass runs. |
| Clear between tasks | Run /clear when switching to unrelated work. Old conversation crowds out the files you need next and costs tokens on every message. |
| Delegate large reads | Send research to a subagent so the file contents stay in its context window, not yours. |
The commands reference says this about the commands involved:
| Command | What the documentation says |
|---|---|
/compact [instructions] | Frees up context by summarizing the conversation so far. Optionally pass focus instructions for the summary. |
/clear [name] | Starts a new conversation with empty context. Pass a name to label the previous conversation in the /resume picker. To free up context while continuing the same conversation, use /compact instead. Resume the previous conversation with /resume. Aliases: /reset, /new. |
/rewind | Rewinds the conversation and/or code to a previous point, or summarizes from a selected message. Aliases: /checkpoint, /undo. |
/autocompact [auto|<tokens>] | Sets the auto-compact window: how full the context window gets before Claude Code compacts automatically. Pass a size such as 500k, or auto to return to the window tuned for your model. Claude Code saves the value to user settings and applies it to the current session. Requires Claude Code v2.1.221 or later. |
Keeping context lean
The context-related advice from the costs documentation:
/clearbetween tasks. Stale context wastes tokens on every subsequent message. Use/renamebefore clearing so you can find the session later, then/resumeto return to it.- Add custom compaction instructions.
/compact Focus on code samples and API usagetells Claude what to preserve during summarization. You can also put this in the CLAUDE.md at the root of your project:# Compact instructions When you are using compact, please focus on test output and code changes - Watch MCP. MCP tool definitions are deferred by default, so only tool names and server instructions enter context until Claude uses a specific tool. Run
/contextto see what is consuming space. - Prefer CLI tools when available. Tools like
gh,aws,gcloud, andsentry-cliare more context-efficient than MCP servers because they do not add any per-tool listing. - Trim CLAUDE.md. It is loaded into context at session start. If it contains detailed instructions for specific workflows, those tokens are present even when you are doing unrelated work. Skills load on demand only when invoked, so moving specialized instructions into skills keeps your base context smaller. The documentation suggests keeping CLAUDE.md under 200 lines by including only essentials.
- Send verbose output to subagents. Running tests, fetching documentation, or processing log files can consume significant context. Delegate these to subagents so the verbose output stays in the subagent's context while only a summary returns to your main conversation.
- Preprocess data with hooks. The documentation's example: instead of Claude reading a 10,000-line log file to find errors, a hook can grep for
ERRORand return only matching lines, reducing context from tens of thousands of tokens to hundreds.
For what these habits do to your bill, see our guide to saving tokens, and for cache behavior see our cache guide. This page stays on how the context itself works.
FAQ
Does a full context window end my session?
No. The documentation says Claude Code compacts automatically as you approach the limit, so a full context window does not end your session. You can also run /compact yourself before that happens and use focus instructions to decide what the summary keeps. Where automatic compaction runs depends on your model and configuration.
Do the rules in my CLAUDE.md survive compaction?
The project-root CLAUDE.md and unscoped rules are re-injected from disk. Rules with paths: frontmatter and nested CLAUDE.md files in subdirectories differ: they are reloaded on demand, and compaction summarizes them away. If a rule must persist across compaction, the documentation says to drop the paths: frontmatter or move it to the project-root CLAUDE.md.
What is the difference between /compact and /clear?
/compact frees up context by summarizing the conversation, and the conversation continues. /clear starts a new conversation with empty context, and the previous one can be resumed with /resume. The commands reference is direct about it: to free up context while continuing the same conversation, use /compact. For switching to unrelated work, the documentation recommends /clear.
Sources
- Claude Code: Explore the context window (what loads at startup, what survives compaction, when context fills up,
/context) - Claude Code: Commands (
/context,/compact,/clear,/rewind,/autocompact,/memory,/usage) - Claude Code: Manage costs effectively (context-control practices, custom compaction instructions)