Read tool can overflow context window, making autocompact unrecoverable
Summary
A single Read tool call on a large file can spike token count past the entire context window, causing autocompact to fail with "Conversation too long" and the session to become unrecoverable.
Steps to reproduce
- Start a Claude Code session (Opus 4.6, 200k context / 180k effective window)
- Work normally until context is at ~50-90k tokens (well under the 167k autocompact threshold)
- Have Claude read a large file (in my case, a 367KB / 7,889-line TypeScript file)
- Session immediately shows "Context limit reached" despite having ~110k tokens free moments earlier
Debug log evidence
From ~/.claude/debug/<session-id>.txt:
17:53:56.721 autocompact: tokens=91760 threshold=167000 effectiveWindow=180000
17:54:37.914 executePreToolHooks called for tool: Read
17:54:37.929 autocompact: tokens=464024 threshold=167000 effectiveWindow=180000
17:54:37.935 Forked agent [compact] finished: 1 messages, types=[assistant], totalUsage: input=0 output=0
17:54:37.936 [ERROR] Error: Conversation too long. Press esc twice to go up a few messages and try again.
Token count jumped from 91k → 464k in one step (a +372k spike from a single Read result). The autocompact agent immediately failed because the conversation was already 2.5x the context window.
What the user sees
/contextshows only 27% usage (54k/200k) — but this is after the failed compact dropped the conversation back down- The "Context limit reached" message persists even though there's 110k+ tokens of free space
- The session is effectively stuck — user must
/clear
Root cause
Claude Code doesn't pre-validate whether a Read tool result will fit in the remaining context before appending it. A single large file (367KB ≈ 372k tokens) exceeded the entire 180k effective window, and autocompact cannot recover because it needs to send the full conversation to the API to generate a summary.
Suggested fixes
- Pre-check file size before reading: Estimate token count from file size and auto-apply
offset/limitif it would overflow remaining context - Truncate oversized tool results: Cap tool result size to remaining context space (with a warning to the user)
- Allow autocompact to drop the offending result: If a single tool result caused the overflow, autocompact could remove/truncate it before attempting compression
- Warn before reading large files: Show a confirmation prompt for files estimated to consume >50% of remaining context
Environment
- Claude Code version: latest (as of 2026-02-10)
- Model: claude-opus-4-6 (200k context window)
- Platform: macOS Darwin 25.3.0
- The file that caused it: a 367KB / 7,889-line single React component (
app.tsx)
This issue has 3 comments on GitHub. Read the full discussion on GitHub ↗