Why Your Claude Code Context Window Fills Up (2026)
Auto-compact fires mid-task and the session forgets what you were doing. Here is what fills the window, when compaction triggers, and what you can control.
Auto-compact fires in the middle of a refactor. Claude summarizes the conversation, drops detail it decided was not important, and the next message reads like it just walked in. You re-explain. That costs tokens and time.
This is a different problem from hitting a plan limit. If your 5-hour window is the thing running out, read Stop Hitting Claude Code Usage Limits instead. This post is about the context window itself: what goes in it, when compaction triggers, and which levers actually move the number.
What is in the window before you type
Claude Code loads a fixed block of context at session start. Anthropic’s context window page walks through it as a timeline:
| Loaded at startup | What it is |
|---|---|
| System prompt | Core instructions for behavior, tool use, and formatting. You never see it. |
| Auto memory | Claude’s own notes from earlier sessions. The first 200 lines or 25KB, whichever comes first, load into context. |
| Environment info | Working directory, platform, shell, OS version, git status. |
| MCP tool names | Names only. Full schemas stay deferred by default. |
| Skill descriptions | One description per skill. Skill bodies load only when invoked. |
~/.claude/CLAUDE.md and project CLAUDE.md | Loaded in full, every session, whether relevant or not. |
Anthropic publishes token counts for each of these in an interactive timeline, and the docs state plainly that the visualization uses representative numbers. Do not treat any figure you read in a blog post (including this one) as your session’s real cost. /context is the only source of truth for your setup. The docs describe it as a live breakdown by category with optimization suggestions, including which CLAUDE.md and auto memory files loaded. /memory opens those files to edit.
What fills it while you work
Everything after startup is additive and nothing leaves on its own:
- File reads. A read puts the whole file in context. Ten candidate files during a grep-and-read hunt cost ten files.
- Tool results. Test output, build logs,
git diff, command stdout. This is usually the largest uncontrolled category. - Conversation history. Your messages and Claude’s, including its reasoning-adjacent prose.
- Path-scoped rules and nested CLAUDE.md files. These load when Claude reads a matching file, not at startup.
- Invoked skill bodies. A skill costs its description until you invoke it, then it costs the whole file.
The mechanism that makes this expensive rather than merely full: Claude Code sends your full conversation with every request, and each tool call is another request carrying that batch of results. Anthropic states this directly on the costs page: a one-line question in a session that has been open all day still draws usage for the whole conversation. Prompt caching softens the price, not the size.
When auto-compact fires
The threshold is not one number. Per the model configuration docs, with no auto-compact window set, Claude Code compacts when the conversation reaches the model’s context limit, with named exceptions:
| Configuration | Compacts at |
|---|---|
| Sonnet 5 on the Anthropic API (1M window, default) | About 967K tokens |
| Sonnet 4.6 or Opus 4.6 without extended context | The 200K boundary |
| Opus 4.8 or Opus 5 running with a 200K window (Bedrock, Google Cloud’s Agent Platform, Microsoft Foundry) | The 200K boundary |
Native-1M models with CLAUDE_CODE_DISABLE_1M_CONTEXT=1 | The 200K boundary |
| Cloud sessions | As the conversation approaches the model’s limit |
| An unrecognized model ID, such as an LLM gateway alias | Whatever window Claude Code assumes for that ID |
Context window sizes come from the model, not from Claude Code. Fable 5, Sonnet 5, Opus 4.6 and later, and Sonnet 4.6 support a 1 million token window. Haiku 4.5 is 200K. If you are on a gateway and the threshold feels wrong, Claude Code is probably guessing your window from the model ID.
What compaction keeps and what it drops
Compaction is not a clean save point. The docs give the survival table:
| Mechanism | After compaction |
|---|---|
| System prompt and output style | Unchanged, not part of message history |
| Project-root CLAUDE.md and unscoped rules | Re-injected from disk |
| Auto memory | Re-injected from disk |
Rules with paths: frontmatter | Lost until a matching file is read again |
| Nested CLAUDE.md in subdirectories | Lost until a file in that subdirectory is read again |
| Invoked skill bodies | Re-injected, capped at 5,000 tokens per skill and 25,000 tokens total, oldest dropped first |
One row is missing from that table and catches people out: the startup listing of skill descriptions is not re-injected after /compact. Only skills you actually invoked are preserved.
That table explains the specific failure people report as “it forgot the rules.” Path-scoped rules and nested CLAUDE.md files live in message history, so the summary eats them. If a rule has to survive, drop the paths: frontmatter or move it to the project-root CLAUDE.md. And because skill re-injection truncates from the end, put the instructions that matter at the top of SKILL.md.
The levers, in order of effect
1. /clear between unrelated tasks. /compact reads the conversation it summarizes, so compacting a large context is itself a large request. /clear costs nothing. Use /rename first if you want to /resume later. This is the single biggest free lever and it is the one people skip.
2. Delegate large reads to a subagent. The subagent gets its own context window. It reads the ten files, you get the summary. This is the only lever that removes work from your window rather than shrinking it.
3. Keep CLAUDE.md small. It loads at session start and stays there for work it has nothing to do with. Anthropic’s guidance is to aim for under 200 lines and move workflow-specific instructions into skills, which cost a description until invoked.
4. Leave MCP tool schemas deferred. MCP tool definitions are deferred by default, so only names enter context until Claude uses a specific tool. That default is already the cheap one, so the lever here is not turning something on. It is not turning it off: ENABLE_TOOL_SEARCH=false loads every schema upfront, and ENABLE_TOOL_SEARCH=auto loads them upfront when they fit inside 10% of the context window. If someone set either on your machine, unset it. Better still, run /mcp and disable servers you are not using: deferred is cheap, absent is free. Where a CLI exists (gh, aws, gcloud), it adds no per-tool listing at all.
5. Point at files instead of pasting them. A path costs a path. A paste costs the file, twice if Claude reads it too. Specific prompts also prevent the broad scanning that vague ones trigger.
6. Set the compaction point yourself. /autocompact 500k sets how full the window gets before the automatic pass runs, accepting 100K to 1M, and saves it to your user settings for later sessions too. /autocompact auto returns to the window tuned for your model, and claude --autocompact sets it for one launch. Claude Code caps the value at the model’s real context window, so asking a 200K model for 1M changes nothing. If CLAUDE_CODE_AUTO_COMPACT_WINDOW is set in your environment it beats all three, and /autocompact will tell you so instead of changing anything. And when you know a compaction is coming, run /compact focus on the auth bug fix yourself so the summary keeps what you choose instead of what the automatic pass guesses.
Measure before you change anything
Two commands, and they answer different questions.
/context shows the live breakdown of the current session by category. Run it when the session feels heavy and act on what it names.
/usage shows what has been consuming your plan over time. On a paid plan it attributes recent usage to skills, subagents, plugins, and individual MCP servers as percentages, and flags behaviors such as long context or cache misses when one accounts for 10% or more of recent usage. Press d or w to toggle 24 hours and 7 days. The figures come from local session history on that machine, so other devices are not included.
Run both before you start deleting things. Most people guess CLAUDE.md is the problem when it is a 400-line test log that a hook could have filtered down to the failures.
Related
- Restart Claude Code Sessions at 200K Tokens : the cost curve behind resending full history
- Context Window Traps: Why Your Local Agent Breaks After 10 Prompts : the same mechanism on a local runtime, where the window is far smaller
- Why OpenClaw Uses 9,600 Tokens for a Simple Question : startup overhead in another harness
Need OpenClaw fixed live?
Remote rescue sessions for gateway, auth, tunnel, VPS, and model access problems.
See Rescue Session