claudecodeguide.dev

Foundations

Claude Code Context Window: Size, /context and /compact

Opus 5 and Sonnet 5 hold 1M tokens, Haiku 4.5 holds 200K. What fills the window, how to read the breakdown with /context, and when /compact is the wrong fix.

What Is the Context Window?

Think of it like working memory. Everything in your current session (your messages, Claude's responses, file contents it has read, command outputs) occupies space in this window. When it fills up, earlier parts of the conversation start getting pushed out. Claude Code begins forgetting things you said or files it already read.

How Big Is It?

Depends which model you are on, and the difference is larger than most people expect:

ModelContext windowRoughly
Claude Opus 51,000,000 tokens~750,000 words
Claude Sonnet 51,000,000 tokens~750,000 words
Claude Haiku 4.5200,000 tokens~150,000 words

So Haiku gives you a fifth of what Opus and Sonnet do. If you route cheap background work to Haiku, that is the model that will hit the wall first, and it will hit it five times sooner. A million tokens sounds like more than you could ever fill. It still goes faster than you think in agentic work, where Claude reads files, runs commands, and feeds the output back into the loop.

When your context window fills up

A rough sense of scale:

ContentApproximate tokens
A typical source file (200-400 lines)500-2,000
A large source file (1,000 lines)3,000-5,000
Reading 50 files in a session~50,000 (25% of your window)
A long back-and-forth conversation (30+ exchanges)20,000-40,000
Command output from a failing test suite5,000-15,000
Delegating that 50-file read to a sub-agent instead~300-800 (a summary comes back, not the files)

A focused single task usually fits fine. A marathon session with multiple tasks, lots of file reading, and long debugging conversations will hit the wall.

Signs You Are Running Out

You'll notice these before any explicit warning:

  • Responses get less accurate. Claude Code "forgets" constraints you mentioned earlier or re-reads files it already looked at.
  • It loses track of decisions. You agreed on an approach 20 messages ago. Now it suggests something different.
  • Responses get shorter or cut off. The model is running out of room to generate.
  • You see context length warnings. Claude Code will tell you when things are getting large.

How Tokens Work

Roughly 4 characters equals 1 token. A word averages about 1.3 tokens. Code tends to be slightly more token-dense than prose because of syntax, indentation, and special characters.

Quick math: a 300-line TypeScript file is about 1,200 tokens. If Claude Code reads 40 files during a session, that's 48,000 tokens just from file content, before counting any conversation.

The /context Command

Before you decide whether to compact, look at what is actually filling the window. Run /context in Claude Code and it prints the current breakdown: how much the system prompt takes, how much your CLAUDE.md and other memory files take, how much the tool definitions take, and how much is left for the conversation itself.

This is the command most people never find, and it changes what you do next. A session sitting at 60% is not one problem, it is several possible problems, and they have different fixes:

  • Memory files are the biggest slice. Your CLAUDE.md is too long, or you are loading rule files you do not need for this task. Trim the file, do not compact the session.
  • Tool definitions are the biggest slice. You have too many MCP servers connected. Disconnect the ones this task does not need.
  • The conversation is the biggest slice. This is the case /compact actually fixes.

Checking first takes five seconds and stops you compacting a session whose real problem is a 2,000-line CLAUDE.md that will refill the window immediately.

The /compact Command

When context gets large, run /compact. This summarizes the entire conversation so far and starts fresh with just the summary. Think of it as taking notes from a meeting, then starting a new meeting with only the notes.

When to use it:

  • After completing a task, before starting a new one
  • When you notice Claude Code forgetting earlier context
  • When you get a context length warning
  • Proactively, after any long debugging session

What it preserves: Key decisions, file modifications, current state of work.

What it loses: Exact file contents, detailed error messages, nuanced back-and-forth reasoning. If you need to reference specific details after compaction, Claude Code will re-read the relevant files.

Strategies to Stay Under the Limit

1. One session, one task

Don't let a single session accumulate context from five different tasks. Finish a task, start a new session for the next one. This is the single most effective strategy and most people never do it.

2. Use handoffs instead of marathon sessions

Instead of one 3-hour session, work in focused 30-60 minute blocks. End each block with a handoff (what was done, what's next). Start a fresh session that reads only the handoff. Fresh context every time.

See the session lifecycle guide for handoff protocols.

3. Point to files instead of pasting them

Let Claude Code read files on demand. Don't paste file contents into the chat. When you paste, that content stays in the conversation forever. When Claude Code reads a file with its tools, it's more efficient.

4. Use sub-agents for research

Sub-agents (via the Task tool) get their own context window. If you need Claude Code to explore a large codebase or research something, offload it to a sub-agent. The main conversation only gets the summary back. Concretely: the 50-file read in the table above costs ~50,000 tokens if it happens in the main thread; delegated to a sub-agent, the main thread only pays for the summary that comes back. This is one wall in a broader approach: see the subagent context-isolation pattern for how isolation scales across memory, agents, and cron state.

5. Be specific in your prompts

Vague prompts cause Claude Code to read more files searching for what you mean. "Fix the auth bug" makes it read every auth-related file. "Fix the token refresh logic in src/auth/refresh.ts where expired tokens are not being caught" sends it straight to the right file.

6. Use /compact between tasks

Even within one session, compact between distinct tasks. Finished adding tests? Compact. Now moving to a different feature? Clean slate, prior work summarized.

The Handoff Strategy

This is the power move for heavy users. Instead of fighting the context window, work with it:

  1. Work in a focused block (30-60 minutes, one task)
  2. End the session with a handoff: what was done, what's next, key decisions
  3. Start a new session. Load only the handoff and relevant CLAUDE.md
  4. Fresh 1M tokens, zero wasted context

You lose nothing because the handoff captures everything that matters. You gain a full context window for the next task. Over a full day of work, this approach is dramatically more effective than one long session that degrades as context fills up.

A Million Tokens Is a Lot. You Still Need Strategy.

Opus 5 and Sonnet 5 give you a million tokens of working memory, which is enormous. The strategies above still matter, they just start biting later in a long session rather than sooner. Two things keep them relevant. Haiku 4.5 caps at 200,000, so anything you route there hits the limit far earlier. And every token you carry is a token you pay for on the next turn, so a bloated session costs money on every message, not just once.

Common questions

How big is the Claude Code context window?

Opus 5 and Sonnet 5 hold 1,000,000 tokens, roughly 750,000 words. Haiku 4.5 holds 200,000, about 150,000 words. Haiku fills first, at a fifth of the capacity.

What does the /context command do?

It shows you what is filling the window right now, broken down by system prompt, memory files, tool definitions and conversation. It changes nothing. Run it before compacting, because if your CLAUDE.md is the problem then compacting buys you about one turn.

How do I summarize the context?

/compact. It keeps decisions, file changes and current state, and drops the verbose middle.

What is the difference between /context and /compact?

/context is the diagnosis, /compact is the treatment. Checking first stops you treating the wrong thing.

Why does Claude Code forget things I said earlier?

The window filled and the oldest turns were pushed out. You notice it as drifting accuracy before you ever see a warning.

Does a bigger window mean I can stop worrying?

No. You re-send and re-pay for the whole window on every turn, and accuracy starts sliding well before the window is actually full.

New guides, when they ship

One email, roughly weekly. CLAUDE.md templates, workflows I actually use, and the cut-for-length stuff that does not make the public guides. One-click unsubscribe.

Or follow on Substack

On this page