[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
This issue has 7 comments on GitHub. Read the full discussion on GitHub ↗