Managing Claude Code's Context Window

How Claude Code's context window works, when to use /compact vs /clear, how to structure long sessions, and how to avoid losing work when context fills up.

💥 50p impulse-buy: Power Prompts PDF (first 10 buyers) 30 battle-tested Claude Code prompts · 8-page PDF · paste into CLAUDE.md and never re-type a prompt again · 50p impulse-buy, no commitment

Claude Code's context window is a shared resource: every file you read, every shell output, every conversation turn consumes space. Managing it well means longer, more productive sessions.

Context window basics

ModelContext windowPractical session limit
Claude Sonnet 4.6200K tokens~60–100 turns (varies by file size)
Claude Opus 4.7200K tokens~60–100 turns (varies by file size)
Claude Haiku 4.5200K tokens~60–100 turns (varies by file size)

/compact — preserve context, free space

# Basic compact — Claude summarizes everything:
/compact

# Focused compact — preserve specific context in summary:
/compact Focus on the auth middleware refactor. Summarize file changes made, not the exploration.

# After /compact:
# - All prior turns replaced with a dense summary (~500–1000 tokens)
# - Claude remembers what was done but loses fine-grained details
# - Full 200K window available for the next phase of work
# - CLAUDE.md is re-read fresh (it's always injected at session start)

/clear — full reset

# Clear everything — fresh session:
/clear

# When to use /clear vs /compact:
# /clear: Starting a completely unrelated task.
#         You don't need Claude to remember what just happened.
# /compact: Continuing the same task or codebase.
#           You want Claude to remember the refactor you just did.

# After /clear, CLAUDE.md is re-read automatically —
# Claude Code re-injects your project context.

Strategies to extend sessions

# 1. Use CLAUDE.md for stable context (doesn't burn session tokens each turn)
#    Put architecture, commands, conventions in CLAUDE.md.
#    Don't re-explain things that belong there.

# 2. Compact after completing a logical sub-task:
#    "I've finished the database migration. /compact before we start the API layer."

# 3. Avoid reading large files unnecessarily:
#    Instead of: "read package-lock.json"
#    Ask: "What version of express is installed?" (Claude uses grep instead)

# 4. Use --print for one-shot queries (no session accumulated):
git diff HEAD~5 | claude --print "What changed in auth?"

# 5. Break large tasks into multiple sessions:
#    Session 1: Plan + scaffold
#    Session 2 (with CLAUDE.md): Implement service layer
#    Session 3 (with CLAUDE.md): Write tests

Token-heavy operations to watch for

# Operations that use many tokens in one turn:
# - Reading large files (log files, lock files, generated code)
# - Running commands with verbose output (npm install, docker build)
# - Diffing large changesets
# - Loading entire test suites into context

# Better patterns:
# Instead of reading the whole file:
"Show me only the exports from src/utils/auth.js"

# Instead of running verbose installs:
"Run npm install --silent"

# Instead of full test output:
"Run tests and show only failures"

Checking context usage

# Built-in cost/context display:
/cost

# Shows: tokens used this session, estimated cost, % of context window used.
# Check this when a session is starting to feel sluggish — it's usually
# context pressure, not network latency.

For a complete list of Claude Code commands including /compact and /clear, see Claude Code slash commands. For setting up project memory that persists across sessions, see Claude Code memory guide.

Frequently asked questions

What happens when Claude Code's context window is full?
Claude Code warns you as you approach the limit. Once at the limit, you must either /compact (summarize prior context) or /clear (reset). If you don't act, new messages may be rejected or earlier context will be silently dropped.
What is the difference between /compact and /clear?
/compact summarizes prior conversation turns and replaces the full history with a dense summary, freeing up context while preserving memory of what was done. /clear resets the context entirely — Claude starts fresh with no memory of the session.
How many tokens does Claude Code use per session?
Depends on the model — Claude Sonnet 4.6 has a 200K token context window. A typical Claude Code session with file reads and edits uses 2K–10K tokens per turn; long sessions with large file reads can exhaust the window in 20–50 turns.
Can I use /compact to focus Claude on specific context?
Yes. /compact with a focus hint — e.g. /compact Focus on the database schema changes and ignore the test fixes — tells Claude what to preserve in the summary. This is more accurate than an unfocused /compact.
How do I structure a long coding session to avoid context issues?
Use CLAUDE.md for stable project context (doesn't count against session context). Periodically /compact after completing sub-tasks. Start new sessions with /clear for unrelated tasks. Avoid reading very large files unnecessarily.

Free tools

Cost Calculator → API Cookbook → Diff Summarizer → Skills Browser →

More examples

Claude API Python QuickstartClaude API Node.js / TypeScript QuickstartClaude API Streaming in PythonClaude API Streaming in Node.js / TypeScriptClaude API Tool Use in PythonClaude API Tool Use in Node.js / TypeScript