Master Claude Code's context window. Strategies for managing context, reducing token usage, and working with large codebases without hitting limits.
Claude Code Context Window: Maximizing Your Token Limit
Every AI tool has limits. Claude Code's context window is generous but not infinite.
Here's how to work within it effectively.
Understanding Context Windows
What Is Context?
Context is everything Claude Code "remembers" during a session:
- Your prompts
- Its responses
- Files you've added
- Code it's read
- Conversation history
All of this consumes tokens.
Current Limits
Claude Code uses Claude's models with large context windows:
- Claude 3.5 Sonnet: 200K tokens
- Claude 3 Opus: 200K tokens
200K tokens is roughly:
- ~150,000 words
- ~500 pages of text
- ~50,000 lines of code
Generous, but large projects can exceed it.
When You Hit Limits
Signs you're running low:
- Claude Code warns about context length
- Responses slow down
- Earlier conversation details forgotten
- "Context too long" errors
Measuring Context Usage
Check Current Usage
/cost
Shows token usage for the current session.
Watch for Warnings
Claude Code warns when context is getting full. Pay attention to these messages.
Estimate File Sizes
Rough estimates:
- 1 line of code ≈ 10-15 tokens
- 100 lines ≈ 1,000-1,500 tokens
- 1,000 lines ≈ 10,000-15,000 tokens
Reducing Context Usage
1. Use .claudeignore
Exclude files that don't need to be in context:
# .claudeignore
# Dependencies (huge, never needed)
node_modules/
vendor/
.venv/
# Build output
dist/
build/
.next/
out/
# Generated files
*.min.js
*.bundle.js
*.map
# Large data
*.csv
*.json
fixtures/
seed/
# Logs
*.log
logs/
# IDE
.idea/
.vscode/
# Cache
.cache/
__pycache__/
.pytest_cache/
Impact: Can reduce context by 90%+ for typical projects.
2. Selective File Loading
Don't load everything. Be specific:
Bad:
Look at the entire src/ directory
Good:
/add src/services/PaymentService.ts
/add src/types/payment.ts
Now look at these specific files.
3. Compact Regularly
The /compact command summarizes your conversation and frees space:
/compact
Use it:
- After completing a feature
- Before starting new work
- When you get context warnings
- Every 20-30 exchanges
4. Start Fresh When Needed
/clear
Full reset. Loses history but frees all context.
Use when:
- Switching to unrelated work
- Context is hopelessly cluttered
- Starting a new major task
5. Remove Unused Files
/remove src/old-file.ts
Remove files you've added but no longer need.
Working With Large Codebases
Slice and Focus
Don't try to load a 500-file project at once.
Approach:
We're working on the payment module only.
Relevant files are in src/payments/.
Ignore everything else for now.
Module-by-Module
Work on one module at a time:
Session 1: Auth module
Session 2: Payment module
Session 3: User module
Session 4: Integration points
Reference Without Loading
You can reference files without loading full content:
The error handling pattern is in src/utils/errors.ts.
Follow that same pattern—don't load the file, just match the approach.
Create Context Files
Summarize complex areas:
<!-- src/payments/.claude-context.md -->
# Payments Module Summary
## Purpose
Handles Stripe payments and subscriptions.
## Key Classes
- PaymentService: Main orchestrator
- StripeClient: Stripe API wrapper
- WebhookHandler: Processes Stripe webhooks
## Patterns
- All amounts in cents
- Idempotency keys required
- Errors extend PaymentError
- Logging via PaymentLogger
## Dependencies
- stripe npm package
- Our UserService for user lookups
- Our OrderService for order management
Load this summary instead of all the files.
Optimizing Prompts
Concise Instructions
Verbose (wastes tokens):
I would like you to please look at the UserService class
which is located in the src/services folder and I'm wondering
if you could add a new method that would allow users to update
their email preferences...
Concise (saves tokens):
Add email preference update method to src/services/UserService.ts
Avoid Repetition
Don't repeat context Claude Code already knows:
Bad:
Remember when I asked you to create the UserService?
And then we added the createUser method? Well now I want...
Good:
Add getUser method to UserService.
Claude Code remembers. You don't need to recap.
Structured Prompts
Clear structure uses fewer tokens than rambling:
Task: Add pagination to user list endpoint
File: src/api/users.ts
Requirements:
- Page size: 20
- Return total count
- Support cursor-based pagination
Multi-Session Strategies
Session Continuity
claude --continue
Resume where you left off. Context preserved.
Parallel Sessions
Run multiple terminal sessions for independent work:
- Terminal 1: Frontend work
- Terminal 2: Backend work
- Terminal 3: Testing
No context collision between sessions.
Session Handoff
Before ending a session, create a summary:
Summarize what we've accomplished and what remains.
Format as a status update I can use to continue later.
Start next session with that summary.
Advanced Techniques
Lazy Loading
Only load files when you actually need them:
When I ask about payment logic, load src/payments/.
Until then, don't load it.
Reference Architecture
Keep architecture docs that describe the system:
# System Architecture
## API Layer
- Express routes in src/api/
- Middleware in src/middleware/
- Validators in src/validators/
## Service Layer
- Business logic in src/services/
- Each service is a singleton
- Services can depend on other services
## Data Layer
- Prisma models in prisma/schema.prisma
- Repositories in src/repositories/
- Migrations in prisma/migrations/
Load this instead of browsing files.
Diff-Based Updates
For long sessions, work with diffs:
Here's what changed since last session:
[paste git diff]
Continue from where we left off with these changes in mind.
When Context Gets Tight
Immediate Actions
/compact - Summarize and free space
- Remove unnecessary files
- Focus on immediate task only
Mid-Session Reset
I'm running low on context.
Before we continue, let's:
1. Summarize current state
2. List remaining tasks
3. Clear and reload essentials only
Emergency Measures
If you're stuck:
/clear
[Start fresh with only essential context]
I'm working on X. The relevant files are:
/add file1.ts
/add file2.ts
Continue where I left off: [brief description]
Context-Aware Prompting
Tell Claude Code What Matters
For this task, you only need to know:
1. The API structure in src/api/
2. The types in src/types/
3. Our error handling pattern
You don't need the tests, utils, or configs.
Explicit Scope
Focus only on the AuthService class.
Don't consider other services for now.
Minimize Back-and-Forth
Ask complete questions:
Wastes context (multiple exchanges):
User: What does this function do?
Claude: [explanation]
User: How would I modify it?
Claude: [explanation]
User: Can you show me the code?
Claude: [code]
Efficient (single exchange):
User: Explain this function, suggest improvements,
and show the modified code.
Quick Reference
| Action |
Command |
When to Use |
| Check usage |
/cost |
Periodically |
| Summarize |
/compact |
After major tasks |
| Full reset |
/clear |
New major task |
| Add file |
/add <file> |
Need specific context |
| Remove file |
/remove <file> |
Done with file |
| List files |
/files |
See what's loaded |
The Bottom Line
Context management is a skill. With practice:
- Exclude early with comprehensive
.claudeignore
- Load selectively with explicit
/add
- Compact regularly to stay fresh
- Focus narrowly on current task
- Summarize before clearing for continuity
Master context management and you'll never hit limits.
Need help optimizing AI workflows for large projects? Cedar Operations helps teams scale their AI development. Book a consultation →
Related reading: