Bash output compression
Bash output compression condenses the output of safe, read-only CLI commands into compact summaries, so routine git status, pytest, and lint runs stop flooding your context.
What it does
Section titled “What it does”Many commands produce far more output than the model needs to act. A test run prints every passing case; a lint run repeats the same rule across hundreds of lines; a directory listing scrolls for pages. Bash output compression catches these after each command runs, inside the consolidated PostToolUse dispatcher, and routes the captured output through a compressor that keeps the signal and drops the repetition. A 564-token pytest run becomes about 115 tokens. Savings depend on command mix and whether the output is eligible; fixture ratios are not universal session coverage.
The hook only touches commands it recognizes as safe and read-only. Anything ambiguous passes through untouched and full.
Handler families
Section titled “Handler families”The compressor has a dedicated handler per command family, so each kind of output is summarized the way that family needs.
| Family | Covers | How it compresses |
|---|---|---|
| Version control | git status, git diff, git log | Counts and groups instead of full listings; default-format git log becomes one line per commit |
| Path lists | find | Grouped by directory with counts; large and noise directories collapsed; total always stated |
| Test runners | pytest, plus cypress, playwright, mocha, karma routed through the same path | Failures and summary, passing cases collapsed |
| Lint | eslint, ruff, flake8, shellcheck, rubocop, golangci-lint | Grouped by rule code |
| Listings | ls, pip list, npm ls, docker ps | Top-N with a tail marker |
| Logs | log tails | Adjacent duplicate lines collapsed |
| Build | JS/TS and Go build output | Errors and summary view |
| Misc | tree, docker pull | Depth truncation, progress filtering |
The same PostToolUse pass also covers build, test, and run commands that are not read-only (gcc, cargo, npm, make, go, mvn, and more). The command already ran, so the hook collapses repetition in the captured output while preserving every distinct error line, exit status, and summary. A credential-redacted, size-capped recovery copy is archived behind an expand pointer.
When it fires
Section titled “When it fires”Automatically, after a recognized read-only command runs, inside the consolidated PostToolUse dispatcher. The compressor tokenizes the command with shlex.split(), checks it against a whitelist, and returns a compact summary of the captured output. Commands it does not recognize keep their full output. Build and test commands are compressed on the not-read-only branch of the same pass.
Default state
Section titled “Default state”On by default on Claude Code (CLI and VS Code), where it runs after each command in the PostToolUse dispatcher. On Codex it is an experimental opt-in (a gated PreToolUse rewrite); on Grok Build a gated PreToolUse rewrite is contract-tested. Other platforms vary; see the capability matrix.
How to turn off
Section titled “How to turn off”For a single run or in CI, set the environment variable:
TOKEN_OPTIMIZER_BASH_COMPRESS=0 python3 measure.py reportTo persist the change:
cd ~/.claude/skills/token-optimizer/scriptspython3 measure.py v5 disable bash_compressThe variable and its config key are defined in the configuration reference.
Security model
Section titled “Security model”The wrapper applies fixed command safety gates. Credential preservation is pattern-based, not a guarantee for every secret shape.
- No shell execution.
shell=Trueis never used. Commands are parsed withshlex.split(), not run through a shell. - Compound commands excluded. Anything containing
;,&&,||, a pipe,$(), backticks,>, or>>is categorically skipped, along withsudoand interactive flags. Only single, safe, read-only commands are eligible. - Credentials preserved verbatim. Output is scanned for credential patterns before compression, and any match is kept intact. The scan covers AWS keys, GitHub personal access tokens, Slack tokens, Stripe keys, OpenAI keys, and HTTP basic-auth URLs. A secret in command output is never summarized away.
- Errors survive. Multilingual error lines are routed through the preservation path. Partial output from a timed-out command is returned raw, never compressed.
Custom filters (TOML)
Section titled “Custom filters (TOML)”The built-in allowlist has 89 entries: 45 single command names and 44 two-word entries. Some overlap, so this is not a count of distinct commands. The compressor registers 25 output handlers, including a raw passthrough. You can add or exclude commands through a TOML file:
- Default location:
<runtime-home>/token-optimizer/command-filters.toml - Override the path with
TOKEN_OPTIMIZER_COMMAND_FILTERS
# Add a read-only command to an existing compression handler.# handler must be one of the built-in pattern names (pytest, lint, ls, ...).[filters.add]my-tests = { command = "cargo test", handler = "pytest" }my-lint = { command = "ruff check", handler = "lint" }
# Exclude commands from compression (glob patterns supported).[filters.exclude]commands = ["git status", "ls -la"]Your filters compose with the built-ins rather than replacing them. Built-in detection always runs first, so an add entry only extends the set, never overrides a built-in handler for the same command.
Custom additions are checked when loaded and again when matched. The guard rejects known destructive commands, dangerous wildcard matches, shell substitutions and redirects. Its lists are bounded, so it is not a general shell-safety proof. A malformed file skips custom filters; built-ins keep working.
Defaults and thresholds
Section titled “Defaults and thresholds”| Setting | Value |
|---|---|
| Default state | On |
| Eligible commands | Recognized read-only commands only |
| Compound commands | Always excluded |
| Credential handling | Scanned and preserved verbatim |
| Timeout output | Returned raw, uncompressed |
| Command coverage | Built-in allowlist plus safety-checked user additions; workload-dependent |
Risk rating
Section titled “Risk rating”Low. The hook fails open: unrecognized commands, compound commands, and timed-out commands all return full output. The narrow risk is a recognized command whose summary omits a detail the model needed, which you resolve by disabling compression for that one run. Credentials and errors are explicitly preserved, so the failure mode is missing verbosity, not missing safety information.
Related environment variables
Section titled “Related environment variables”TOKEN_OPTIMIZER_BASH_COMPRESS and the config key v5_bash_compress, plus TOKEN_OPTIMIZER_COMMAND_FILTERS to point at a custom filter file. Defined in the configuration reference.
Platform availability
Section titled “Platform availability”Full on Claude Code CLI and VS Code, where compression runs in the PostToolUse dispatcher. Codex has an experimental opt-in PreToolUse rewrite; Grok Build has a contract-tested, not live-verified PreToolUse rewrite. See the capability matrix.
Related pages
Section titled “Related pages”- Active compression: the suite this belongs to.
- Read cache: the same idea for file re-reads.
- Trust and safety: why nothing in your context gets edited or removed.
- Configuration reference: the canonical variable table.
