Long test and build output in Claude Code: why it burns context and how to cut it
Downshift by Tiago Vilas Boas. Updated
Downshift here is a Go tool for AI coding agents, not the React downshift autocomplete library and unrelated to any car transmission.
TL;DR
Every command result Claude Code reads becomes part of the conversation and goes out again with each later request. A passing suite prints a line per package or test that says nothing a single PASS line would not. Run fewer tests, use quiet flags, treat | tail with care, and keep failures whole. downshift compact automates the last part: it shortens clean passing output, keeps every failure line byte for byte and returns the real exit code.
Why does long test output burn context in Claude Code?
A green test run is a stack of identical receipts. Each line says the same thing, passed, and Claude Code keeps the whole stack in its pocket for the rest of the session.
That is how the context works. When Claude runs a command through the Bash tool, the output enters the conversation as a tool result. Anthropic's cost page says Claude Code sends the full conversation with every request, and each tool call sends another request carrying its results. Prompt caching re-reads that history at a cheaper rate, but it is still read, it still fills the context window, and it brings auto-compaction closer.
Passing output is mostly repetition: one line per package with go test, one line or dot per test with most runners, timings and a summary. The model needs one fact from it: everything passed, with exit code 0. A failing run is the opposite. The assertion, the diff and the stack trace are the information, so a failure must stay whole.
So the rule for the rest of this guide is simple: shrink the success, never the failure.
How do I reduce test output tokens without installing anything?
Start with what Claude Code and your test runner already give you.
| Lever | What it cuts | The catch |
|---|---|---|
| Run focused tests | go test ./pkg/ -run TestName, one pytest node, one spec file instead of the whole suite | You still need the full suite before you merge |
| Quiet flags | go test without -v, pytest -q, a dot or summary reporter | Some quiet modes also shorten failure output; check what a failure looks like first |
| tail -N | Keeps only the last N lines | The pipe returns tail's exit code, and a failure printed above the last N lines is gone |
| Delegate the run to a subagent | Verbose output stays in the subagent's context; only a summary comes back (sub-agents docs) | The subagent's own requests still draw on your usage |
| /compact and /clear | /compact summarizes the conversation; /clear starts fresh at no cost | Both act after the fact. /compact itself reads the whole context, and the next long run fills it again |
| A hook that filters output | Anthropic's cost page shows a PreToolUse hook that rewrites test commands to show only failure lines | You own the script, and its grep and head pipe decides which lines and which exit code Claude sees |
The | tail catch deserves a closer look, because agents reach for it constantly. In bash and zsh a pipeline reports the exit status of its last command, so a failing suite piped to tail looks green unless pipefail is on:
go test ./... | tail -5 # status is tail's, usually 0
set -o pipefail; go test ./... | tail -5 # status is go test's when it failsNone of these levers knows the difference between a pass and a failure. They cut both, or they cut neither.
What does downshift compact do to test output?
Downshift is an open-source Go binary that runs as hooks inside coding agents. Its Compactor is one command: downshift compact -- <command>. It runs your command and looks at the result before the agent does.
- Exit 0 and output it recognizes: the output is replaced by a short PASS summary plus a marker line that says how many bytes were cut.
- A failed go test, vitest or playwright run: only the lines of tests that passed are cut, and every failure line stays byte for byte.
- Any other failure, non-zero exit or output it does not recognize: passed through byte for byte.
- The exit code is always the command's own.
It works on an allowlist: go test, pytest, vitest, jest, tsc, cargo test, phpunit, playwright test, cypress run, gradle, maven, flutter test and more. The allowlist also includes npm test; commands outside it are not wrapped. The exact forms are in the harness matrix. To switch it off, set "exec": false in the Downshift config or DOWNSHIFT_EXEC=0 in the environment.
What does it look like on a real run?
Single run, 2026-10-24, on the Downshift repository: three Go packages, plain go test without -v, macOS.
Before: raw output, 202 bytes
$ go test ./internal/execwrap/ ./internal/telemetry/ ./internal/classify/
ok github.com/tiagovilasboas/downshift-ai/internal/execwrap 26.559s
ok github.com/tiagovilasboas/downshift-ai/internal/telemetry 1.732s
ok github.com/tiagovilasboas/downshift-ai/internal/classify (cached)After: downshift compact, 167 bytes including the marker line
$ downshift compact -- go test ./internal/execwrap/ ./internal/telemetry/ ./internal/classify/
Go test: PASS (3 packages ok) under github.com/tiagovilasboas/downshift-ai/internal/
ok execwrap
ok telemetry
ok classify
[downshift compact: compacted 205 to 119 bytes]202 to 167 bytes, about 17% fewer, on output that was already short. The marker line counts its own run (205 to 119 bytes before the marker is added); package timings differ between the two runs. Plain go test is terse by design. The larger cuts come from runners that print a line per test, verbose flags and build tools.
Source: the two commands above, run once on 2026-10-24 with downshift v0.1.2-65 (commit 480391dc). Byte counts by wc -c. Your output will differ.
Does Claude Code rewrite test commands automatically?
On Claude Code, yes. Downshift's PreToolUse hook sees the Bash command before it runs and rewrites allowlisted commands into the compact form, including the shapes agents type most often:
| Command the agent writes | What runs |
|---|---|
cd api && go test ./... | cd api && downshift compact -- go test ./... |
go test ./... 2>&1 | tail -20 | downshift compact -- go test ./... 2>&1 | tail -20 |
go build ./... && go test ./... | each segment wrapped; && still stops at the first failure |
gofmt -l .; go test ./pkg | only the go test segment wrapped |
A deny or ask rule that matches the command, or any part of it, stops the rewrite. A command that already holds downshift compact is never wrapped twice. With | tail -N the pipe still reports tail's status, exactly as before; the rewrite does not change exit semantics.
OpenCode also supports rewriting supported commands through its bridge. Integration varies across other harnesses; an agent can run downshift compact -- <command> directly.
How much does output compaction save?
In benchmark measurements across the command corpus, Compactor delivers an overall reduction of 53.0% fewer bytes without CCR and 67.1% fewer bytes with Command Context Recovery (CCR, enabled by default). On lossless test runners and clean passing outputs, it cuts 60.3% fewer bytes, with best cases reaching 90%–95% fewer bytes (cargo test 93%, flutter test 92%, npm install 89%, pytest 87%, cypress 83%, playwright 82%, turbo 80%, tsc 78%, next build 77%, vitest 74%). With CCR enabled by default, eligible output is stored locally for retrieval, subject to retention and secret redaction, with downshift retrieve <id>. These are bytes, not LLM tokens; your mix of commands will differ. Read the benchmark report.
Frequently asked questions
Does compacting test output hide failures from Claude?
No. A clean run is cut to a summary. In a failed go test, vitest or playwright run only the lines of passing tests are cut, and every failure line stays byte for byte. Any other failure, non-zero exit or unknown output passes through unchanged. With Command Context Recovery (CCR, on by default), eligible output is stored locally (0600, 24 h TTL) and can be retrieved, subject to redaction and retention limits, with downshift retrieve <id>.
Does downshift compact change the exit code?
No. It returns the command's own exit code. In a pipe such as | tail -N the shell still reports the last command's status, the same as without Downshift.
Is piping to tail enough?
It cuts size, but the pipe returns tail's exit code unless pipefail is set, and a failure printed above the last lines is lost. It also trims failing output, which is the part the model needs.
Does it work outside Claude Code?
Yes, as a command. Claude Code and OpenCode support automatic command rewriting. In Codex, Cursor and other harnesses, agents can run downshift compact -- <command> directly.
How do I turn it off?
Set "exec": false in the Downshift config or DOWNSHIFT_EXEC=0 in the environment. A deny or ask rule that matches a command also stops its rewrite.
Stop paying context for green checkmarks
Install Downshift, preview every change with downshift setup --dry-run, and the next passing suite comes back as one summary line while failures stay whole. downshift setup --undo restores your backups.
curl -fsSL https://raw.githubusercontent.com/tiagovilasboas/downshift-ai/main/install.sh | sh && downshift setupClaude Code plugin
/plugin install downshift --marketplace tiagovilasboas/downshift-aiRelated guides
Sources
- Anthropic docsmeasured
code.claude.com/docs/en/costs
full conversation sent with every request, /compact reads the context, /clear, delegating verbose operations, test-output hook example (fetched 2026-10-24) - Anthropic docsmeasured
code.claude.com/docs/en/sub-agents
isolating high-volume operations in a subagent - Anthropic docsmeasured
code.claude.com/docs/en/hooks
PreToolUse hooks - Downshift repomeasured
docs/reference/bench.md
compaction benchmark: 53.0% without CCR, 67.1% with CCR, 60.3% lossless, bytes not tokens - Downshift repomeasured
docs/reference/harness-matrix.md
allowlist, compound shapes and chains, command rewriting varies by harness - Downshift repomeasured
docs/features/compaction.md
compacts a clean run and a failed go test, vitest or playwright run; failures preserved - Downshift CLI outputmeasured
go test and downshift compact, single run on 2026-10-24
before and after byte counts