Claude Code hit a wall:
Error: Context length exceeded. Maximum context length is 200,000 tokens.
Or responses are getting worse as context grows. Here is how to fix it.
Fix 1: Use /compact regularly
Claude Codeβs built-in context compressor:
/compact
This summarizes the conversation and frees up context space. Use it when:
- Context feels sluggish
- You get βcontext lengthβ errors
- After completing a major task
Fix 2: Select files manually
Instead of letting Claude read everything, specify files:
# Add specific files to context
/add src/auth/login.ts
/add src/api/routes.ts
# Remove files from context
/drop src/old-module.ts
Only load files relevant to the current task.
Fix 3: Use smaller models for exploration
Use Sonnet for initial exploration, Opus for implementation:
/model claude-sonnet-5
# Explore the codebase, understand structure
/model claude-opus-4-8
# Implement the changes
Fix 4: Break tasks into smaller pieces
Instead of one large task, split it:
# Bad: Too much context
claude "refactor the entire auth system"
# Good: Focused tasks
claude "add password validation to login.ts"
claude "add rate limiting to API endpoints"
claude "update tests for auth changes"
Fix 5: Use git context
Claude Code can read git history efficiently:
# Ask about recent changes
claude "what changed in the last 5 commits?"
# Ask about specific files
claude "explain the changes in src/auth/login.ts"
Git context is more efficient than reading entire files.
Fix 6: Exclude large files
Add to .claudeignore:
# .claudeignore
node_modules/
dist/
*.min.js
large-data.json
test/fixtures/
Claude Code will skip these files during context building.
Fix 7: Use CLAUDE.md for persistent context
Put project overview in CLAUDE.md:
# CLAUDE.md
## Project structure
- src/auth/ - Authentication module
- src/api/ - REST API routes
- src/models/ - Database models
## Key decisions
- Using TypeScript strict mode
- PostgreSQL for database
- JWT for authentication
## Current task
Working on adding rate limiting to API endpoints.
This provides context without using conversation tokens.
Still overflowing?
For very large codebases:
- Use repository map β Claude Code builds a map of your codebase
- Focus on one module β Work on one part at a time
- Use Aider β Better at handling large codebases with repo map
- Split into microservices β Smaller codebases are easier to manage
FAQ
What is the context limit for Claude Code?
Claude Sonnet 5 has a 1M token context window. However, Claude Codeβs effective limit is lower because it includes system prompts, tool definitions, and conversation history. In practice, you may hit limits around 200-300K tokens of actual content.
How do I know how many tokens I am using?
Claude Code does not show token counts directly. Monitor your API usage in the Anthropic console. If you are hitting limits frequently, use /compact to reduce context.
Can I increase the context limit?
Not directly. The context limit is set by the model. However, you can work around it by: using /compact regularly, selecting specific files with /add, and breaking tasks into smaller pieces.
Related: Claude Code Complete Guide Β· Aider Complete Guide Β· Context Window Management Β· Best AI Models for Large Codebases Β· Claude Code vs Aider