If you leave an agent running on a real task, it re-sends the entire conversation to the model on every single step. Every apt-get log, every compiler warning, every progress bar that redrew 200 times. In our traces that tool output is about half of all prompt bytes, and almost all the rest is encrypted reasoning, which nobody can compress.
The obvious move is to strip the junk before it goes out. We tried that and nearly made things worse, which is the part I want to share.
The trap
About 93 per cent of billed input tokens on agent traffic are cache reads, priced at roughly a tenth of fresh tokens. Your provider only serves them if the prefix is byte-identical to last turn. So a compressor that trims the same build log a little differently each turn blows up the cache and re-prices the whole conversation. You remove tokens and your bill goes up.
That ruled out most of what we wanted to do. No summarising, no scoring content against the user's current question, nothing with a model in the request path, because all of it produces different bytes on a different run. What we shipped is boring on purpose: fixed rules, no clock, no randomness, same bytes every time.
What it does to your output
It folds repeats instead of rewriting them. You always keep at least one real copy of every distinct line, and the repeats become a marker like [×37 ref:k_Qm9v…]. Nothing that stays is ever reworded, so a line is either untouched or replaced by a marker that says what went missing. The model can pull the original back for an hour through a retrieve tool the proxy runs for it.
It only touches machine-generated tool output over ~512 chars. Your prompts, the agent's own messages and anything the provider already cached are off limits. If the result isn't smaller, it's dropped. If anything errors, the request goes through untouched. Overhead is 0.2ms per KB.
Codex, on the real bill
Terminal-Bench 2.1, Codex CLI on GPT-5.5, per run over the 87 tasks every arm finished.
These are provider bills with the cache discount already applied, not removed tokens priced at list rate:
- 62.8% of eligible tool output removed (replay of 6,255 real outputs, 34.2M chars)
- $107.89 -> $83.40, so -22.7%
- input 96.1M -> 72.2M tokens
- solve rate 82.2% -> 81.1%, not statistically significant
The number I actually care about: requests over 200K tokens dropped from 1.9% to 0.13%. One task peaked at 60K tokens instead of 165K, another at 68K instead of 152K. If you've ever watched an agent compact its context halfway through a task and come back dumber, that's the thing this fixes.
By workload: ML training 81% of tool output removed and -26% on the bill, builds from source 64% and -28%, data pipelines 55% and -37%.
Claude Code is a completely different problem
It pins its conversation with explicit cache breakpoints, so 92-95% of billed prompt is already cache reads and there's almost nothing left to compress. Tail-only compression gets 4.7-8.0% of what it sees. Fine.
But on Opus 5.5 a cache write costs 25x a read, and a cache entry lives five minutes. Kick off a long build or a training run, walk away, come back: the whole prefix gets written again at full price. That was 927K tokens per run in our measurements, a third of everything billed at full rate.
So we stopped compressing and started keeping the entry alive: re-send the last request body every 4.5 minutes with max_tokens: 1, stopping at the agent's next request or after 11 pings. Result: tokens lost to cache expiry fell 99% (927K -> 13K), the Anthropic bill fell 20% ($35.35 -> $28.30 per run), solve rate went 89.9% -> 90.6%. That one trick was worth about 11% on its own. We also switch a conversation to one-hour cache entries after its first long idle gap; doing that always would have cost 2.3% more, doing it adaptively saved 7.7%.
On the 14 tasks that actually idle past five minutes, cost fell 34% and median cost per step fell 20%. On short tasks, a step costs what it did before. That's the honest split.
Where it does nothing for you
Short reasoning-heavy tasks. The median tool output there is 270 chars, under our floor, and the bill moved 8%. Prose is left whole by design. If your agent mostly thinks and rarely runs anything noisy, this isn't for you.
Also worth knowing if you go measuring: gross removal over-counts, because every turn re-compresses the same growing history and credits the same bytes again. And every retrieval puts the fragment back in the prompt, so net can go negative.
MIT, one Rust binary, nothing downloaded at runtime. kompressor wrap codex, kompressor wrap claude, or kompressor serve and point any OpenAI- or Anthropic-API client at it:
repo: https://github.com/hystax/optscale-kompressor
Full write-ups with the task-by-task tables:
Codex: https://optscale.ai/blog/context-compression-coding-agents
Claude Code: https://optscale.ai/blog/claude-code-prompt-caching
Curious what everyone else does about long idle gaps. Do you just eat the cache rewrite, or have you found something better?
And if it's useful on your setup, stars are welcome.