Claude Code Context Limit - When to Compact, Clear, and Optimize Token Usage
Managing Claude Code Context Limits
The "context limit reached" message in Claude Code usually arrives at the worst possible moment - mid-implementation, when the agent has accumulated the most useful context. The fix isn't waiting for it to happen. It's managing context proactively.
The 30-40% Rule
Don't wait for Claude Code to hit its context limit and auto-compact. By that point, you've lost control over what gets kept and what gets dropped. Instead, manually run /compact when you're at 30-40% usage.
At this point, the conversation is still small enough that compaction produces a clean, accurate summary. Wait until 80-90% and the summary itself becomes lossy - important details about your codebase structure or earlier decisions get dropped.
When to Clear vs Compact
Compact when you're continuing the same task and need the accumulated context about file locations, architecture decisions, and implementation progress.
Clear when you're switching to an unrelated task. Carrying context from a database migration into a frontend styling task just wastes tokens and confuses the model.
Practical Token Optimization
Several habits reduce token burn without losing effectiveness:
- Be specific in prompts. "Fix the auth bug in src/auth/login.ts line 47" uses fewer tokens than "there's a bug somewhere in the auth system"
- Use CLAUDE.md files. Put project structure and conventions there instead of re-explaining them every session
- Close tangents early. If the agent starts exploring an unrelated file, redirect it immediately instead of letting it read 500 lines of irrelevant code
- Split large tasks. Five focused sessions beat one massive session that hits the context ceiling
The Hidden Cost
Every token spent on stale context is a token not available for reasoning about your current problem. Context management isn't just about avoiding errors - it's about keeping the agent's "thinking space" clean for the work that matters right now.
Fazm is an open source macOS AI agent. Open source on GitHub.