Skip to content

Bash output compression

Bash output compression intercepts safe, read-only CLI commands and replaces their verbose output with a compact summary, so routine git status, pytest, and lint runs stop flooding your context.

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 through the PreToolUse Bash hook and routes them through a compressor that keeps the signal and drops the repetition. A 564-token pytest run becomes about 115 tokens. Across a session, the handlers cover roughly 90% of the verbose CLI output real work produces.

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
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

Automatically, on the PreToolUse Bash hook, when you run a recognized read-only command. The hook tokenizes the command with shlex.split(), checks it against a whitelist, and rewrites the output through the compression wrapper. Commands it does not recognize run normally with full output.

On by default on Claude Code (CLI and VS Code). Approximated or unavailable on platforms with reduced PreToolUse hook support; 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 hook is built so that compression never widens your attack surface and never hides a secret.

  • 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.
SettingValue
Default stateOn
Eligible commandsRecognized read-only commands only
Compound commandsAlways excluded
Credential handlingScanned and preserved verbatim
Timeout outputReturned raw, uncompressed
Command coverageAbout 90% of verbose CLI output

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. Defined in the configuration reference.

Full on Claude Code CLI and VS Code. Other platforms vary with their PreToolUse Bash hook support; see the capability matrix.