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.
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.
| Model | Context window | Practical session limit |
|---|---|---|
| Claude Sonnet 4.6 | 200K tokens | ~60–100 turns (varies by file size) |
| Claude Opus 4.7 | 200K tokens | ~60–100 turns (varies by file size) |
| Claude Haiku 4.5 | 200K tokens | ~60–100 turns (varies by file size) |
# 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 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.
# 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
# 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"
# 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.