[FEATURE] Add programmatic context recovery for noninteractive mode when single tool call exceeds context limit
Problem
When running Claude Code in noninteractive mode (--print or via SDK), if a single tool call returns output that exceeds the context limit, the session becomes irrecoverable. There is no programmatic way to recover from this state.
The specific failure mode:
- A tool call (e.g.,
mcp__ide__getDiagnostics,Bashwith verbose output, orUpdateon a large file) returns content that pushes context usage beyond 100% /compactfails because the conversation is already too large to compact- In interactive mode, users can recover by pressing
Esc+Esc(rewind) to go back to before the failed tool call - In noninteractive mode, there is no equivalent mechanism - the session simply fails with no recovery path
Real-world impact:
- Multi-agent orchestration workflows become fragile - a single unexpected large output can crash an entire automated pipeline
- Users running headless CI/CD integrations with Claude Code have no fallback
- The problem is exacerbated with MCP servers that may return unexpectedly large outputs
Proposed Solutions
Option 1: Add --rewind-on-context-overflow flag (Recommended)
claude --print --rewind-on-context-overflow "run diagnostics and fix errors"
When context limit is exceeded by a single tool call:
- Automatically rewind to before the problematic tool call
- Log a warning about what was truncated
- Continue execution with a system note that the tool output was too large
- Optionally retry with truncated output if tool supports it
Option 2: Add SDK/CLI option to specify rewind point
# Resume from a specific message index, discarding everything after
claude --continue --rewind-to-message 5 --print "try a different approach"
This would allow orchestration systems to:
- Catch the context overflow error
- Inspect the conversation state
- Programmatically rewind to a safe point
- Continue with an alternative strategy
Option 3: Pre-flight context budget per tool call
claude --print --max-tool-output-tokens 50000 "run build and fix errors"
Prevent the issue proactively by:
- Setting a maximum token budget per tool call
- Truncating/summarizing outputs that exceed the budget
- Never allowing a single tool call to consume more than X% of remaining context
Option 4: Export session state for external recovery
claude --print --export-session-on-error ./session-backup.json "complex task"
When an error occurs:
- Export the full session state to a file
- Include message indices and content
- Allow external tools to manipulate and resume
Related Issues
- #12054 - Claude Code ingests massive tool outputs without truncation (closed as dup of #11155)
- #8047 - Update tool on large file causes irrecoverable context
- #3968 - Context Truncation: Unable to Compact Conversation Due to Length Limit
- #9279 - Add option to rewind conversation without creating a fork
- #6559 - Context limit exceeded makes tool unusable in longer sessions
Environment
- Use case: Multi-agent orchestration with Claude Code in noninteractive mode
- Affected modes:
--print, SDK programmatic usage, headless automation
Additional Context
The interactive mode already has excellent recovery UX with Esc+Esc rewind. This feature request is specifically about exposing equivalent functionality for programmatic/automated use cases where interactive recovery is not possible.
This would make Claude Code significantly more robust for production automation workflows where unexpected large outputs are a reality.
This issue has 3 comments on GitHub. Read the full discussion on GitHub ↗