Bash tool output with ANSI escape sequences causes context overflow and unrecoverable API errors
Description
When a Bash tool call captures large output containing ANSI escape sequences (e.g., colored log output from a subprocess), the raw escape sequences are stored in the context window without stripping. This can quickly consume ~35KB+ of context, trigger repeated API errors, and leave Claude Code completely unresponsive to any commands except /exit.
Steps to Reproduce
- Run a command via the Bash tool that produces verbose colored stderr output (e.g., a Rust service with
tracingproducing DEBUG-level logs with ANSI color codes) - The command output contains hundreds of lines of ANSI-encoded log lines like:
````
[2m2026-02-17T18:25:15.784395Z[0m [34mDEBUG[0m [1;34menoch::job_runner[0m...
- The Bash tool captures all of this (stdout + stderr) into the context
- Context window fills up rapidly due to the verbose escape sequences
Expected Behavior
One or more of:
- The Bash tool should strip ANSI escape sequences from captured output before storing in context
- The Bash tool's 30,000 character truncation should be applied more aggressively when escape sequences are detected
- When context overflow triggers API errors, Claude Code should remain responsive (allow
/compact,/clear, or other recovery commands — not just/exit)
Actual Behavior
- ANSI escape sequences are stored verbatim in the context window
- ~35KB of colored log output triggers context overflow
- Repeated API errors occur with no way to recover
- Claude Code becomes completely unresponsive to all commands except
/exit - The entire session's work is effectively lost (must start a new session)
Workaround
Redirect output to a file and read it separately:
./run.sh > /tmp/output.txt 2>&1; echo "Exit: $?"
Then use the Read tool to examine the file. This avoids the Bash tool capturing the large output directly.
Environment
- macOS (Darwin 24.6.0)
- Claude Code CLI
- Model: Claude Opus 4.6
- The subprocess producing the output was enoch (Rust binary using
tracingwith Bunyan-style colored JSON output)
Impact
This is particularly problematic for long-running sessions where significant work has been done. The context overflow makes the session unrecoverable, forcing a fresh start. The ANSI sequences add significant byte overhead compared to plain text — stripping them before storing in context would substantially reduce the risk.
This issue has 4 comments on GitHub. Read the full discussion on GitHub ↗