[BUG] Stack Overflow Near Maximum Context Length
Preflight Checklist
- [x] I have searched existing issues and this hasn't been reported yet
- [x] This is a single bug report (please file separate reports for different bugs)
- [x] I am using the latest version of Claude Code
What's Wrong?
Claude Code CLI crashes with a RangeError: Maximum call stack size exceeded when the session approaches maximum context length. The crash occurs during tool execution and message processing, making the CLI completely unusable as sessions get longer.
The error happens after streaming tool results have begun processing, and the application terminates without gracefully handling the situation.
What Should Happen?
The CLI should handle sessions approaching maximum context length gracefully without crashing. It should continue to function normally even as context accumulates.
Error Messages/Logs
RangeError: Maximum call stack size exceeded
(Use `node --trace-uncaught ...` to show where the exception was thrown)
Node.js v20.19.3
**Stack trace location:**
- File: `/@anthropic-ai/claude-code/cli.js`
- Line: 3437
**Related functions in stack:**
- `yF1()`, `GZ7()`, `ZZ7()` - Tool execution and result processing
- `yN()` - Main query function (async generator)
- Post-tool hooks and context modification handlers
Steps to Reproduce
- Start a Claude Code session and begin working on a task
- Execute multiple tool operations and continue the conversation
- Build up the conversation context over multiple turns
- Continue until the session approaches maximum context length
- Execute additional tools or request further processing
- Application crashes with stack overflow error
Claude Model
Opus
Is this a regression?
Yes, this worked in a previous version
Last Working Version
2.0.76
Claude Code Version
2.1.1
Platform
Anthropic API
Operating System
macOS
Terminal/Shell
Terminal.app (macOS)
Additional Information
The recursive nature of the yN async generator function (main query loop) causes deep call stacks when:
- Processing recursive queries with tool results
- Managing context modifications across nested tool calls
- Operating near maximum context length where additional recursion depths are accumulated
The recursion depth accumulates with each:
- Tool execution cycle
- Message attachment/hook processing
- Recursive
yNcall for continued processing
At maximum context length, the overhead of context management + recursive tool processing exceeds Node.js's call stack limit.
Related Code Paths
The crash occurs in the interaction between:
- Main async generator that recursively calls itself for continued processing
- Context compaction and autocompact logic
- Message queuing and hook processing
🤖 Generated with Claude Code
Found 3 possible duplicate issues:
- https://github.com/anthropics/claude-code/issues/16368
- https://github.com/anthropics/claude-code/issues/15340
- https://github.com/anthropics/claude-code/issues/14607
This issue will be automatically closed as a duplicate in 3 days.
- If your issue is a duplicate, please close it and 👍 the existing issue instead
- To prevent auto-closure, add a comment or 👎 this comment
🤖 Generated with Claude Code
happening to me too
Seeing what may be the same root cause in #16649 - compaction fails on sessions with pasted screenshots (base64 images).
The ~467KB base64 payload from a single screenshot pushes context usage up faster, which seems to trigger this stack overflow during compaction sooner. Compaction attempts repeatedly fail at 0% with no progress.
Both issues appeared in the 2.0.76 → 2.1.x timeframe.
Still reproducing in 2.1.2 (macOS Darwin 25.2.0, Opus 4.5).
Crash occurred during normal conversation — no long context, no unusual tool usage. Just a simple discussion about symlinks when the stack overflow hit.
RangeError: Maximum call stack size exceeded
file:///.npm/_npx/.../node_modules/@anthropic-ai/claude-code/cli.js:3362
This confirms the regression persists from 2.1.1 → 2.1.2.
This issue has been inactive for 30 days. If the issue is still occurring, please comment to let us know. Otherwise, this issue will be automatically closed in 30 days for housekeeping purposes.
Closing for now — inactive for too long. Please open a new issue if this is still relevant.