One of the most disruptive issues in long Claude Code sessions is hitting the context window limit mid-task. You're deep into a refactor, files are being read, edits are piling up — then everything stops.
As an indie developer, I use Claude Code daily — running the automated content pipelines for Dolice Labs, building AdMob reporting scripts, and maintaining four production sites in parallel. Context exhaustion is the error I hit most consistently in long sessions. Once you understand the mechanics, it becomes straightforward to work around.
What the Error Looks Like
As Claude Code approaches the context limit, it surfaces in a few forms depending on how far over you are.
Warning phase (around 80% capacity):
Claude's context window is getting full.
Use /compact to compress the conversation.
Hard stop (limit exceeded):
This conversation is too long to continue.
Please start a new conversation or use /compact.
If the limit is exceeded at the API level before Claude Code can intercept it, you'll see a raw error in the terminal:
{
"error": {
"type": "invalid_request_error",
"message": "prompt is too long: 210741 tokens > 200000 maximum"
}
}Why It Happens
Claude Sonnet 4's context window is 200,000 tokens. Every Claude Code session accumulates the following in that space:
- Conversation history: your messages and Claude's responses, in full
- Tool results: file contents from Read calls, bash command output
- CLAUDE.md: your project instructions, loaded at session start
- Edit history: before/after content for each file modification
The first time this became a real problem for me was when I tried to audit and fix a series of issues across multiple source files in one session. Ten sequential Read calls into a moderately sized TypeScript project and I was already past 150,000 tokens before any edits had even started.
Fix 1: Use /compact to Compress In-Place
When you want to keep the current session alive, /compact is the right first move.
/compact
This prompts Claude to summarize the conversation history, replacing detailed tool results and back-and-forth exchanges with a condensed summary. Token count drops significantly, and the session can continue.
Confirm the summary before proceeding: Claude sometimes presents the compressed summary for review before finalizing. Check that your current working state — which files are being modified, what decisions have been made — is captured accurately.
Important: /compact is irreversible. Once compressed, the detailed conversation history is gone. If you need to trace back through earlier reasoning, do that before compressing.
Fix 2: Start a New Session with --continue
If you've reached a natural stopping point, starting a fresh session is often cleaner than compressing a long history.
# Resume the most recent session
claude --continue
# Resume a specific session by ID
claude --resume <session-id>
# List available sessions
claude --list-sessions--continue carries forward a summary of the previous session, not the full history. This means file contents, earlier tool outputs, and intermediate reasoning are not directly available. At the start of the new session, re-state the important context explicitly: which file you were working in, where you left off, and what the next step is.
Fix 3: Design Sessions to Stay Small
The most reliable long-term solution is scoping each session tightly enough that context exhaustion rarely happens.
One session = one commit's worth of change
Rather than opening a session with "improve the overall quality of this repo," start with "add error handling to the fetchArticle function in src/lib/content.ts." Narrow scope means fewer files get read, fewer decisions get made, and the session ends before the limit is approached.
Read only what you need
# Costly: asks Claude to load everything
# "Look at the whole src/ directory and find the problem"
# Efficient: direct targeting
# "Read src/lib/content.ts and check the return type of getArticle"Commit often
Frequent commits create natural restart points. When a new session begins, git log --oneline -5 gives Claude the full context of what was completed in a few dozen tokens — far cheaper than re-reading files.
Prevention Habits Worth Building
Keep CLAUDE.md lean
Project instruction files get loaded at session start on every run. If yours has grown to cover every edge case and historical decision, it may be consuming tens of thousands of tokens before any work begins. Move stable reference material into separate documents and link to them rather than inlining everything.
Trim command output
# Token-heavy
git log --stat # includes full file diff stats per commit
# Token-efficient
git log --oneline -10 # just the last ten commit summariesUse offset/limit when reading large files
Claude Code's Read tool accepts offset and limit parameters. Instead of reading an entire 800-line file, read only the relevant section. This alone can reduce per-task token usage substantially on large codebases.
/compact vs. /clear: Knowing the Difference
These two commands look similar but behave very differently:
/compact— replaces conversation history with a summary, preserving context while reducing tokens/clear— completely resets the conversation, as if starting fresh
Use /compact when you're mid-task and want to continue with the same thread. Use /clear when you're switching to an entirely different task and the current history is no longer relevant.
Summary
When you hit the context limit, the decision tree is straightforward:
Still mid-task → /compact to compress and continue in place.
At a stopping point → claude --continue or --resume to start a clean session.
Long-term fix → keep sessions scoped to single commits and commit frequently.
The "one session = one commit" approach has made the biggest difference in practice. Context issues drop, work is easier to track, and sessions end in a clean state rather than getting compressed under pressure.