Claude Code Context Window Management: Stay Productive in Long Sessions
Claude Code has a finite context window — approximately 200,000 tokens. As a session grows, responses slow, quality degrades slightly, and eventually Claude Code warns you that context is full. The solution isn't stopping work; it's managing what's in context. The key insight: context accumulates with every message and every file read, but you can compact, restart, and structure work to keep sessions productive regardless of length.
Why context management matters
In practice, a 200K context window fills faster than you'd expect:
- A medium-sized TypeScript file: ~2,000–5,000 tokens
- A 30-message conversation with code outputs: ~20,000–50,000 tokens
- A typical 2-hour Claude Code session: 80,000–150,000 tokens
When context is full, Claude Code either starts compressing automatically or warns you. The warning typically appears after 100,000–150,000 tokens depending on the model.
Signs of a full context:
- Responses become shorter than usual
- Claude starts repeating earlier work
- Claude seems to "forget" things discussed earlier in the session
- The
/costcommand shows high token usage
Strategy 1: /compact before switching tasks
The most reliable context management habit: use /compact at natural task boundaries.
[Working on feature A]
... many messages ...
/compact
[Now working on feature B]
/compact generates a summary of the conversation so far, replacing the full history with the summary. You lose detail but keep the key context. Total tokens drop dramatically.
When to compact:
- After completing a feature or bug fix (before starting the next)
- When Claude Code suggests context is getting long
- After a large file-reading session (Claude reads many files, context fills fast)
- After running a build and reviewing all the output
What to do before compacting:
- Save any important outputs to files (the summary won't include full code snippets)
- Run tests and commit completed work
- Note any decisions you'll need to reference later
Strategy 2: Structure work to minimise context waste
The order of operations affects how much context you use:
High context waste (avoid):
- Read all project files at once
- Have a long conversation about what to build
- Write code
- Debug issues
- Repeat
Low context waste (prefer):
- Write the feature / fix the bug directly
- Read only the specific files needed
- Make focused changes
- Test and verify
The more you read before acting, the more context you consume before doing any work. Read files only when you're about to modify them.
Strategy 3: Explicit file reading control
By default, when you describe a task, Claude Code may proactively read many files to understand the context. For large codebases, this burns context quickly.
Control what Claude reads:
Fix the bug in app/components/ArticleCard.tsx where the date displays incorrectly.
Only read the files you need to make this specific fix — don't explore the broader codebase.
Contrast with:
Fix the date display bug.
The first prompt reads ~2 files and 5,000 tokens. The second may read 10+ files and 30,000 tokens as Claude explores to understand the codebase.
Strategy 4: Use /clear and CLAUDE.md for multi-day projects
For projects spanning multiple sessions, the pattern is:
- Build a comprehensive CLAUDE.md with project context
- Start each session fresh with
/clearor by opening a new session - The session begins with CLAUDE.md context — Claude already knows the project
- Work on a focused task within that session
- Commit work before ending the session
CLAUDE.md is always read fresh at session start — it never takes up conversation context. This is why a thorough CLAUDE.md is so valuable: it gives Claude full project context without consuming the conversation window.
Strategy 5: Checkpoint important information to files
During long sessions, if Claude produces important analysis, architectural decisions, or complex implementations, save them to files immediately:
> Write that architectural analysis to docs/architecture-decision.md
When you compact or start a new session, the file persists. Claude can re-read it on demand without you needing to reproduce the work.
Files that benefit from checkpointing:
- Architectural decisions ("why we chose X over Y")
- Bug analysis ("the root cause is in X because...")
- Implementation notes ("this approach requires Y to be done first")
- Test results ("these tests pass; these are failing because...")
Monitoring context usage
# In Claude Code, check current usage:
/cost
The output shows tokens used in the current session. Watch the input token count — when it approaches 100,000, start thinking about compacting.
For sessions where you need to track usage precisely (expensive API calls), check the usage every hour or after major operations.
When to start fresh vs compact
| Situation | Recommendation |
|---|---|
| Completed one task, starting another related task | /compact |
| Completed one task, starting unrelated task | /clear |
| Session has run 3+ hours | /clear or new session |
| Claude seems confused about project state | /clear |
| Need to reference earlier conversation specifically | Keep session, don't compact |
| Context warning appears | /compact immediately |
| About to read many large files | /compact first |
Frequently asked questions
Does /compact lose important information? Yes — detail is lost in compression. The summary retains key decisions and the overall state of work, but not verbatim code that was written earlier. Save important outputs to files before compacting.
How do I know if Claude Code has forgotten something important after /compact? The compact summary usually starts with a brief description of what was accomplished. Read it to verify the key points are retained. If something critical was omitted, add it back: "Also important context: [the thing that was lost]."
Does context window size vary by model? Claude Sonnet has a 200K token context window. Claude Haiku also has 200K. In practice, Claude Code's optimal performance tends to degrade noticeably after 100K tokens for complex reasoning tasks.
Can I increase the context window by paying more? No — context window size is a model property, not a pricing tier. All users of the same model get the same context window.
Does /compact cost tokens? Yes — the compact operation itself uses tokens to generate the summary. But the savings from reducing future context are typically 10–20x the cost of the compact operation. Always compact before a long piece of work.
Related guides
- Setting Up a New Project with Claude Code: The Right Way — CLAUDE.md setup for multi-session projects
- Claude Code Tips from Power Users — advanced session management patterns
Take It Further
Power Prompts 300: Claude Code Productivity Patterns — Section 5 covers Session Management: 20 prompts for context-efficient task structuring, the file-reading protocol that minimises token waste, checkpoint templates, and the multi-day project workflow.
30-day money-back guarantee. Instant download.