Claude Code context window: how to stop hitting the limit mid-task
You're 45 minutes into a refactor. Claude has touched 12 files, read your full codebase, and is about to write the final piece.
Then: context window exceeded.
Everything stops. You lose the thread. You start over.
This guide shows you how to stop that from happening.
Why the context window fills up fast
Claude Code's context window isn't just your prompt — it includes:
- Every file Claude has read (full content)
- Every tool call result (grep output, file listings, error messages)
- Every message in the conversation
- Every code change Claude has made or reviewed
A single grep -r that returns 500 lines eats 500 lines of context. Reading 10 files averaging 200 lines each = 2,000 lines gone before you've written a word.
The five techniques that actually work
1. Start a new session for each task
The single most effective technique is also the simplest: one session per task.
Don't carry context from yesterday's debugging session into today's feature work. Each /compact or /reset is free — use them.
Work pattern:
# Start fresh
claude
> You are working on the auth module. The task is: add rate limiting to /api/login.
> Start by reading auth.js and the relevant middleware files only.
Be explicit about scope upfront. "Read auth.js and middleware" is 2 files. "Understand the codebase" might read 40.
2. Use /compact before you need it
Claude Code has a /compact command that summarizes the conversation history, dropping the verbose tool call logs while keeping the semantic thread.
Don't wait for the warning. Use /compact proactively:
# After each major phase of work
/compact
> Good. We've set up the database schema and migrations.
> Now let's work on the API layer. Read api/users.js.
Post-compact, re-anchor Claude with a brief summary of where you are. This prevents drift.
3. Control what Claude reads
Claude will read everything you let it read. Give it boundaries:
# Bad: vague scope
> understand the project structure
# Good: explicit scope
> read ONLY these files: src/auth.js, src/middleware/rateLimit.js, tests/auth.test.js
Also use .claudeignore to permanently exclude irrelevant directories:
# .claudeignore
node_modules/
dist/
.git/
coverage/
*.lock
Every file in .claudeignore is a file that can't accidentally bloat your context.
4. Break large tasks into staged sessions
If you're doing a big refactor across multiple modules, structure it as a multi-session pipeline:
Session 1: Audit and plan
> Read the current auth module. List every function.
> Output a migration plan as a checklist. Do not change anything.
# Save the plan output to PLAN.md
Session 2: Execute
> Read PLAN.md. Implement step 1 only: extract the JWT validation logic.
> Do not touch anything outside auth.js.
Session 3: Test + fix
> Read the test file and the changes from the last session (auth.js).
> Run the tests. Fix any failures.
Each session starts clean. The output of one session (a plan file, a modified file) becomes the minimal context for the next.
5. Use CLAUDE.md to avoid re-explaining every session
Anything you find yourself explaining in every session belongs in CLAUDE.md:
# CLAUDE.md
## Stack
- Node.js 22, Express 5, PostgreSQL 16
- Tests: Jest, run with `npm test`
- Lint: ESLint, run with `npm run lint`
## Key files
- Entry: src/server.js
- Auth: src/routes/auth.js
- DB: src/db/pool.js
## Rules
- Never edit migrations directly - always create a new migration file
- All API endpoints require JWT middleware unless marked public
Claude reads this at session start. You don't burn 500 tokens re-explaining your stack every time.
The nuclear option: pipe output to a file
When you need Claude to process something large (a diff, a log file, test output), don't paste it inline:
# Instead of pasting 800 lines of logs into Claude:
npm test 2>&1 | tee /tmp/test-output.txt
# Then in Claude:
> Read /tmp/test-output.txt and fix the failing tests
Claude reads the file reference, not the inline content. Same information, slightly cleaner context accounting. For very large outputs, summarize first:
npm test 2>&1 | tail -50 > /tmp/test-summary.txt
What to do when you're already at the limit
If you hit the context warning mid-task:
- Don't panic. The work Claude has done is still in the filesystem.
-
/compactimmediately — this buys you more runway. - If compact doesn't help, start a new session and re-anchor:
> We were refactoring auth.js to add rate limiting.
> The changes so far are already saved to auth.js.
> Continue from where we left off: now add the middleware registration in server.js.
Claude can pick up mid-task if you give it the minimal context: what was done, what file it's in, what's next.
Rate limits are a separate problem
Context limits and rate limits are different things:
- Context limit = conversation too long, Claude can't continue this session
- Rate limit = too many API calls in a time window, Claude returns "overloaded"
For rate limits, setting ANTHROPIC_BASE_URL to a proxy removes the cap entirely:
export ANTHROPIC_BASE_URL=https://simplylouie.com/api/proxy
SimplyLouie runs Claude at $2/month with no rate limits — useful if you're running long parallel sessions and keep hitting overloaded errors. The context window limit is unchanged, but at least the "overloaded" interruptions disappear.
Summary
| Problem | Fix |
|---|---|
| Session bloated with old context |
/compact or start fresh |
| Claude reading too many files | Explicit scope in prompt + .claudeignore
|
| Re-explaining stack every session | Put it in CLAUDE.md
|
| Large log/diff bloating context | Write to file, let Claude read it |
| Big task hitting limit mid-way | Break into staged sessions |
| Rate limit "overloaded" errors | Set ANTHROPIC_BASE_URL to proxy |
Context management is the skill that separates people who find Claude Code frustrating from people who use it to ship real things.
Top comments (0)