← All guides

Claude Code Context Window Management: Stay Productive in Long Sessions

How to manage Claude Code's context window in long sessions — when to compact, how to structure work to minimise context waste, and patterns for.

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:

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:


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:

What to do before compacting:


Strategy 2: Structure work to minimise context waste

The order of operations affects how much context you use:

High context waste (avoid):

  1. Read all project files at once
  2. Have a long conversation about what to build
  3. Write code
  4. Debug issues
  5. Repeat

Low context waste (prefer):

  1. Write the feature / fix the bug directly
  2. Read only the specific files needed
  3. Make focused changes
  4. 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:

  1. Build a comprehensive CLAUDE.md with project context
  2. Start each session fresh with /clear or by opening a new session
  3. The session begins with CLAUDE.md context — Claude already knows the project
  4. Work on a focused task within that session
  5. 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:


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


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.

→ Get Power Prompts 300 — $29

30-day money-back guarantee. Instant download.

AI Disclosure: Drafted with Claude Code; all patterns from daily Claude Code usage as of April 2026.

Tools and references