Skip to content

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.

Bash output compression: a verbose command condensed to a compact summary, credentials preserved

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.

The compressor has a dedicated handler per command family, so each kind of output is summarized the way that family needs.

FamilyCoversHow it compresses
Version controlgit status, git diff, git logCounts and groups instead of full listings; default-format git log becomes one line per commit
Path listsfindGrouped by directory with counts; large and noise directories collapsed; total always stated
Test runnerspytest, plus cypress, playwright, mocha, karma routed through the same pathFailures and summary, passing cases collapsed
Linteslint, ruff, flake8, shellcheck, rubocop, golangci-lintGrouped by rule code
Listingsls, pip list, npm ls, docker psTop-N with a tail marker
Logslog tailsAdjacent duplicate lines collapsed
BuildJS/TS and Go build outputErrors and summary view
Misctree, docker pullDepth 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.

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.

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.

For a single run or in CI, set the environment variable:

Terminal window
TOKEN_OPTIMIZER_BASH_COMPRESS=0 python3 measure.py report

To persist the change:

Terminal window
cd ~/.claude/skills/token-optimizer/scripts
python3 measure.py v5 disable bash_compress

The variable and its config key are defined in the configuration reference.

The wrapper applies fixed command safety gates. Credential preservation is pattern-based, not a guarantee for every secret shape.

  • No shell execution. shell=True is never used. Commands are parsed with shlex.split(), not run through a shell.
  • Compound commands excluded. Anything containing ;, &&, ||, a pipe, $(), backticks, >, or >> is categorically skipped, along with sudo and 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.

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.

SettingValue
Default stateOn
Eligible commandsRecognized read-only commands only
Compound commandsAlways excluded
Credential handlingScanned and preserved verbatim
Timeout outputReturned raw, uncompressed
Command coverageBuilt-in allowlist plus safety-checked user additions; workload-dependent

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.

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.

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.