Tool-output compression #4442
zhouzhuojie
started this conversation in
Ideas
Replies: 1 comment
|
Doesn't bash minifier already cover this? |
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Tool output compression: trim before context, not after
Most CLI output has the same shape: important stuff at the top and bottom, repetitive noise in the middle. A 200-line
git diffteaches the model nothing from line 150 that it didn't already learn from line 10.Strip the middle, keep the bookends. The model can re-run with
greportailif it needs a specific line from the trimmed region. That costs one extra tool call, which is cheaper than stuffing 500 lines of redundant output into context every turn.oh-my-pi already compresses history after it enters the conversation (pruning, shake, compaction). This does the other direction: trim tool output before it enters the conversation in the first place.
What it does
For every non-error tool result:
Levels
off(default)conservativeaggressiveConservative is the safe starting point for real coding. Aggressive is what caveman-code ships with.
Tool exclusions
The
readtool delivers file content that the model needs in full to reason about or edit. Truncating it defeats the purpose.compressToolOutputskips truncation forread(ANSI stripping and blank collapsing still apply). The exclusion set isSKIP_TRUNCATION_TOOLSintool-output-compression.ts.Why it is safe
The agent always sees the first and last N lines of output. Errors appear at the top or bottom. Test results appear at the bottom. The middle is almost always format-repeated noise. And the model can always ask for more with
head/tail/grep.Token savings
Assuming a 15-turn session with typical tool output (bash: 200 lines/turn, read: 400 lines/turn, grep: 150 lines/turn):
Conservative trims the obvious junk (ANSI, blank lines) and only truncates the longest outputs. Aggressive truncates everything to tight budgets.
Implementation
packages/agent/src/compression/tool-output-compression.ts(~150 lines, zero deps, 18 tests).Key functions:
Budgets live in two maps (
CONSERVATIVE_BUDGETS,AGGRESSIVE_BUDGETS), selected by thelevelparameter. Unknown tools fall back to a default budget.Gated on
AgentLoopConfig.toolCompression("off"|"conservative"|"aggressive", defaultundefined). Wired intoagent-loop.tsat theToolResultMessageconstruction point. Error results are never compressed.Ported from caveman-code (MIT).
All reactions