[FEATURE] Add programmatic context recovery for noninteractive mode when single tool call exceeds context limit

Resolved 💬 3 comments Opened Dec 12, 2025 by tadasant Closed Dec 12, 2025

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:

  1. A tool call (e.g., mcp__ide__getDiagnostics, Bash with verbose output, or Update on a large file) returns content that pushes context usage beyond 100%
  2. /compact fails because the conversation is already too large to compact
  3. In interactive mode, users can recover by pressing Esc+Esc (rewind) to go back to before the failed tool call
  4. 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:

  1. Automatically rewind to before the problematic tool call
  2. Log a warning about what was truncated
  3. Continue execution with a system note that the tool output was too large
  4. 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:

  1. Catch the context overflow error
  2. Inspect the conversation state
  3. Programmatically rewind to a safe point
  4. 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:

  1. Setting a maximum token budget per tool call
  2. Truncating/summarizing outputs that exceed the budget
  3. 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:

  1. Export the full session state to a file
  2. Include message indices and content
  3. 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.

View original on GitHub ↗

This issue has 3 comments on GitHub. Read the full discussion on GitHub ↗