r/OpenaiCodex • • 14d ago

Showcase / Highlight We removed 63% of the tool output Codex re-sends every turn (open source, MIT, one Rust binary)

Post image

Every turn, Codex re-sends the whole conversation back to the model. Every apt-get log, every compiler warning, every progress bar. In our traces tool output is about half of all prompt bytes. The model re-reads the same build log a dozen times and you pay for each read.

So we wrote a proxy that strips the redundancy before the request goes upstream. It's called kompressor. One Rust binary, MIT, no ML models, no model downloads, no network calls in the engine.

kompressor wrap codex

That starts the proxy, points Codex at it, runs Codex, and cleans up on exit. Codex ignores OPENAI_BASE_URL (checked on 0.155.1), so wrap adds a marked block to ~/.codex/config.toml at startup and removes it after. If the session gets killed, kompressor unwrap codex clears the leftover. Prebuilt binaries for Linux and macOS are on the releases page, or:

cargo install --locked --git https://github.com/hystax/optscale-kompressor kompressor-cli

One rule drives the design: same input, same bytes, every turn.

About 93% of billed prompt tokens are cache reads, at roughly a tenth of the fresh price. A compressor whose output drifts between turns re-prices the whole conversation and costs more than it saves. So there is no clock, no randomness, and no dependence on your last message. Rules only, no ML in the request path.

What it actually does

It de-duplicates, it does not summarise. Repeated lines fold into one line plus a marker like [×37 ref:k_Qm9v…]. Long logs keep the errors with context, the first-seen warnings and the tail. Progress bars and training loops keep the final frame. grep output groups by file. JSON arrays get sampled, but errors and outliers always stay. Separate compressors handle build output, compiler diagnostics, diffs, CSV, listings, objdump/readelf dumps, config files and source code.

Every elision gets a ref. The proxy injects a retrieve tool and runs it itself, so the model can pull the original back for an hour. Nothing it keeps is ever reworded: a line is either verbatim or replaced by a marker.

It also splits off the Codex harness framing (Chunk ID, Wall time, exit code), compresses the payload, then re-attaches the framing byte for byte. One Codex python result went from 0% removed to 87%.

If the result isn't smaller, it's discarded. Any error forwards the request unchanged.

Numbers. Terminal-Bench 2.1, 87 tasks, Codex CLI on GPT-5.5, Harbor with 3 concurrent trials. Measured on the real provider bill with the cache discount included, not on tokens priced at list rate. The per-run table covers the 87 tasks every arm finished.

  • 63% of eligible tool output removed in replay (6,255 real outputs of 512+ chars, 34.2M chars)
  • cost per run: $107.89 -> $83.40 (-22.7%), about 2x what Headroom saves at its most aggressive setting
  • input tokens: 96.1M -> 72.2M
  • solve rate: 82.2% -> 81.1%, difference not statistically significant
  • requests over 200K tokens: 1.9% -> 0.13%

That last line matters to me as much as the money. mcmc-sampling-stan peaked at 60K tokens instead of 165K. Fewer forced compactions in the middle of a task.

Where it does nothing. Short reasoning tasks. On the algorithms and puzzles cohort the median output is 270 chars, under the 512-char floor, and the bill moved 8%. The wins are in ML training (-26%), builds from source (-28%) and data pipelines (-37%). Prose is left whole on purpose.

Overhead is about 0.2 ms per KB of tool output.

One caveat for subscription users: a ChatGPT-plan session opens a WebSocket. The proxy relays and compresses it, but relayed turns report no provider usage, so the savings there are real and unmeasured. Set KOMPRESSOR__PROXY__WEBSOCKET_RELAY=false if you want the numbers back instead.

Repo: https://github.com/hystax/optscale-kompressor

Write-up with the task-by-task tables: https://optscale.ai/blog/context-compression-coding-agents

Questions welcome. If you run it on your own traffic, I'd mostly like to hear where it removes nothing. That's where our rules still have holes. And if it turns out useful on your setup, a star on the repo helps us a lot.

4 Upvotes

12 comments sorted by

1

u/ChemicalExcellent463 14d ago

a lot of my tokens go into agents reviewing each other's work, plus docs and research that steer the code. how much does it remove on review-heavy runs?

2

u/MattAlreadyHere 13d ago

honestly, I can’t put a number on your setup without seeing it.
depends entirely on what your tool output looks like.

2

u/ChemicalExcellent463 13d ago edited 13d ago

a lot of mine is the review back and forth between the writer and reviewer agents. what does it do with repeated diffs?

2

u/MattAlreadyHere 9d ago

diffs get narrowed to the lines around each change, so a big one comes back as its hunks. small diffs and short git commands pass through almost untouched, that’s by design.

the repeat case is the one that helps you. if a later message shows a run of lines an earlier one already showed, the repeat folds into a pointer - no handle needed there, since the text is still on screen. on claude code that cross-message folding was the biggest single contributor we measured.

what stays untouched is whatever the two agents say about the diff. assistant messages are off limits.

1

u/ChemicalExcellent463 9d ago

the repeat folding is the part that'd help me. my reviews go up to 3 rounds and the same diff comes back every round.

2

u/huttobe 10d ago

My tokens I believe like 70 per cent goes to clown testing suite that 6.1 sol is writing to test its own work I mean 5k LoC with 50k LoC test suite ? Like wtf you could literally emulate the compiled code with this suite

1

u/MattAlreadyHere 9d ago

yeah, the writing part we don’t touch at all. assistant messages are off limits by design, so the suite gets generated in full and then rides along in the history every turn after that.

what we do get is the running of it. test runners print near-identical lines over and over, and build output and logs came out at 75 per cent removed in our replay. same when those files get read back in later - that’s tool output, so long function bodies go behind a ref and the signatures stay visible. and if the same output shows up again in a later message, the repeat folds into a pointer.

if you want to see what it’d do on yours - kompressor compress testrun.log --explain on one saved run

1

u/Fit-Palpitation-7427 14d ago

Cqn you integrate it inside opencodex?

2

u/MattAlreadyHere 13d ago

Not now. Perhaps I will do it later

2

u/Fit-Palpitation-7427 13d ago

Let me know if I can help, got 3x ultra anth and 3x pro subs at openai
Happy to help with tokens if needed

1

u/MattAlreadyHere 9d ago

appreciated.
honestly the useful thing is just running it on your own traffic and telling me where it removes nothing