Open source: context compression for coding agents, 63% less tool output ⭐ optscale-kompressor on GitHub →
AI Cost Optimization

OptScale Kompressor, Part 2: On Claude Code, the Cache Is the Bill

November 3, 2026

~ 10 min

On this page
    Stop paying twice for Claude Code’s cache
    Book a Demo

    What changed when we took context compression from Codex to Claude Code on Opus 5.5: compress only what the provider has not cached yet, keep the cache alive while tools run, and price every decision on the real bill.

    −20%Anthropic API bill vs no compression
    −99%prompt tokens lost to cache expiry
    ~1Mprefix tokens kept warm per run
    −34%cost on long-running tasks

    In Part 1 we measured OptScale Kompressor on Codex and GPT-5.5. It took 22.7% off the real bill, about twice Headroom's savings, by rewriting repetitive tool output deterministically so the prompt cache kept working. Claude Code was the obvious next target, and the most stubborn. It pins its whole conversation to the prompt cache, so there is almost nothing left to compress. Its bill turned out to be driven less by what the agent reads than by how often the cache expires while the agent waits for a tool. This part is about what we changed to handle that, and what it measurably bought.

    ‍

    Where we left off

    Part 1 ended with a list of things still to do. Here is where they stand:

    • Claude Code was forwarded untouched. It is now compressed, but only the part the provider has not cached yet (section 2.1).
    • Feedback from the open-source release. Readers put the engine through real logs and edge cases, and their findings are fixed (section 2.4).
    • More agents and models than Codex on GPT-5.5. This part measures Claude Code on Opus 5.5.

    optscale-kompressor is open source under the MIT licence. Everything described here is in the current release, and that release is what we measured.

    TL;DR

    • Claude Code needs different levers. It keeps 92–95% of its prompt in the provider's cache. On Opus 5.5 a cached token costs $0.20 per million, but writing it back after the cache expires costs $5. The dominant waste is cache expiry during long tool runs, not repeated output.
    • On Terminal-Bench 2.1 with Claude Code on Opus 5.5, the Anthropic API bill fell 20%: $35.35 per 89-task run without compression, against $28.30 with Kompressor. The solve rate is unchanged (89.9% vs 90.6%).
    • Three quarters of that is cache writes that never had to happen. Prompt tokens lost to cache expiry fell from 927K per run to 13K. Keep-warm pings bridged about 28 idle gaps per run, for $0.73 per run in pings.
    • The effect concentrates where tools run long. On the 14 tasks with idle gaps longer than five minutes, cost fell 34%, and the median cost per step fell 20%. On the other 72 tasks, a typical step costs the same as before.
    • Compression itself plays a smaller part here. It removes 5–8% of the uncached tail, and that is all it should touch.

    Run optscale-kompressor with Claude CodeOpen source. One deterministic Rust binary for Linux, macOS and containers.
    ⭐ Get it on GitHub →

    1. Why Claude Code is a different problem

    Codex re-sends its history and lets the provider cache whatever prefix matches. Claude Code marks its conversation with explicit cache breakpoints, so almost every request is a long cached prefix plus a short new tail. Three facts follow:

    1. Almost everything is already cached. In our measurements, 92–95% of billed prompt tokens were cache reads. Rewriting any byte inside the cached prefix invalidates it, so the old rule from Part 1 holds even more strongly: never change what the provider has already cached.

    2. Cache reads are nearly free, and cache writes are not.

    Opus 5.5, per million tokens Price
    Uncached input $4.00
    Cache read $0.20
    Cache write, 5-minute entry $5.00
    Cache write, 1-hour entry $8.00
    Output $20.00

    A token written to the cache costs 25 times a token read from it.

    3. The cache expires while the agent waits. A default cache entry lives five minutes. A training run, a full build or a long simulation can keep the agent idle for longer. When it comes back, the whole prefix is written to the cache again at full price. Without compression, that happened 33 times in an 89-task run and re-wrote 927K tokens. That is a third of everything billed at full price.

    So on Claude Code the question is less "how much can we remove?" and more "how much can we avoid paying twice?".

    ‍

    2. What changed

    2.1 Compress only what the provider has not cached yet

    Kompressor now tracks, per conversation, which messages the provider has already seen and cached. On Anthropic, every message inside that frozen prefix is left byte-identical. Only the new tail is compressed, the first time it is sent. In a typical 89-task run about 1,800 messages, or roughly 2.2M tokens, were protected this way. That is the correct outcome, not a missed opportunity.

    Two Anthropic-specific settings keep compression from costing more than it saves:

    • A minimum saving of 4,000 characters per elision. On Claude Code, a retrieval round re-sends a large prefix, so a small elision that triggers even one retrieval costs more than it saved. Anything smaller ships whole.
    • No function-body elision. On Opus 5.5, eliding code bodies cost more in retrieval rounds than it saved, so it is switched off for this provider.

    2.2 Keep the cache alive while tools run

    Keep-warm. While the agent is running a tool, Kompressor re-sends the last request body every 4.5 minutes, with max_tokens: 1. The provider reads the cached prefix, which costs $0.20 per million tokens instead of $5, and the entry's five-minute clock restarts. Keep-warm stops:

    • at the agent's next request;
    • after 11 pings, about 50 minutes;
    • at the first failed ping;
    • if a ping reads no cached tokens, because that ping has already paid for a full write.

    Adaptive one-hour TTL. Anthropic also offers one-hour cache entries, at a higher write price. Kompressor switches a conversation to one-hour entries after its first idle gap longer than five minutes. On a replay of earlier Anthropic runs:

    • always using one-hour entries cost 2.3% more overall;
    • switching adaptively cost 7.7% less, and never more than 1.6% more on any single run.

    2.3 Price every decision on the real bill

    Kompressor sometimes has to decide whether to rewrite a message the model keeps retrieving. It now prices that decision on what Anthropic actually charges:

    • per-model cache prices, so Opus 5.5 reads at a twentieth of its input price;
    • the system prompt and tool definitions it never compresses, about 13.6K tokens per Claude Code request;
    • Anthropic's 20-block cache lookback.

    Before this, a hit-rate bug made every such decision look as if the whole prompt were already cached, and requests billed at about 23K tokens looked like 9.3K.

    2.4 Engine fixes from the field

    • Honest markers. A fold over lines that differ now reads [×N similar ref:…] instead of claiming exact repeats. Plain text folds only adjacent runs.
    • Better routing:
      • code behind a listing (ls && cat x) is found: one 621-line interpreter went from 17,577 to 3,223 characters;
      • several files printed by one cat are split back into separate files;
      • JavaScript, C and Java are no longer mistaken for Java, Rust and Go;
      • assembly ships whole;
      • long symbol-list lines, such as nm output, are clipped.
    • Errors stay visible in uniform logs. A long, uniform log such as a training run used to be sampled head-and-tail, so an error in the middle could end up behind the marker. Such logs now go to the error-scored path when they carry error signal, with a wider error vocabulary that includes crashes, OOM kills, timeouts and full disks. The marker reports error counts only when it actually searched for them.
    • No silent losses. If one fragment was too large to retain, compression for the whole payload used to be discarded silently. Now only that fragment is declined, and the rest still folds.

    ‍

    3. How we measured

    Benchmark Terminal-Bench 2.1, 89 tasks
    Agent and model Claude Code on Opus 5.5
    Arms No compression · OptScale Kompressor with the Anthropic profile (sections 2.1–2.3)
    Metric What Anthropic charges: every API request priced at the table in section 1, including keep-warm pings and Claude Code's calls to other models
    Analysis Run totals, plus per-task means paired across arms, and cost per agent step
    Exclusions Trials that failed agent setup

    We count every API request because Harbor's own cost only sees the requests in the agent's trajectory. Without compression, Harbor reported $32.00 against $35.35 billed. On the Kompressor runs it missed the keep-warm pings.

    Agents do not take the same path twice, and that matters for how the results are read (section 4.4).

    ‍

    4. Results

    4.1 The bill

    Per 89-task run No compression Kompressor
    Anthropic API bill $35.35 $28.30 (−20%)
    Tasks solved 89.9% 90.6%
    Prompt tokens served from cache 92.0% 94.8%
    Cache writes 2.78M tokens 1.67M (−40%)
    Prompt tokens lost to cache expiry 927K 13K (−99%)
    Output tokens 660K 596K
    Agent steps 1,104 1,080

    Individual Kompressor runs cost between $27.86 and $28.68 and solved 80–81 tasks.

    4.2 Where the money went

    Per run No compression Kompressor Change
    Cache writes, 5-minute $14.16 $8.57 −$5.59
    Cache writes, 1-hour — $0.14 +$0.14
    Cache reads $7.37 $6.46 −$0.91
    Output $13.60 $12.31 −$1.29
    Uncached input $0.23 $0.09 −$0.14
    Keep-warm pings — $0.73 +$0.73
    Total $35.35 $28.30 −$7.05

    Cache writes are three quarters of the difference. The extra spend on pings and one-hour entries comes to under a dollar, about a sixth of what the writes saved.

    4.3 The cache did most of the work

    Keep-warm, per run Average Range
    Pings sent 93 73–112
    Idle gaps over five minutes bridged 28 22–35
    Prefix tokens kept warm 0.96M 0.78M–1.20M
    Cost of the pings $0.73 $0.46–0.89
    Rewrites avoided, at $5 instead of $0.20 per million ~$4.6 ~$3.7–5.7

    Each bridged gap would otherwise have ended in a full rewrite of the prefix. That rewrite cost is what the uncompressed agent actually paid: 927K tokens per run. Net of the pings, keep-warm saved about $3.9 per run, roughly 11% of the uncompressed bill, on its own. In most runs no turn lost its cache at all.

    4.4 Reading the result honestly

    Agents do not take the same path twice. Four tasks account for the whole net difference: winning-avg-corewars, regex-chess, sam-cell-seg and crack-7z-hash. On the first three, the uncompressed agent took a much longer path: 45–72 steps against 19–26. Kompressor was cheaper on 44 of the 89 tasks.

    Splitting tasks by whether they wait on long-running tools separates the two effects:

    Tasks Tasks Cost Median cost per step
    With idle gaps over five minutes (training, builds, long simulations) 14 −34% −20%
    All others 72 −12% +2%

    The first row is the mechanism doing its job: where the cache would expire, keep-warm and adaptive TTL make each step cheaper. The second row is the honest limit. On a typical short task a step costs what it did before, and the rest of that cohort's saving comes from shorter paths, not cheaper steps. The mechanism-level estimate is what we expect to hold up across repeated measurements:

    • about 11% from keeping the cache alive;
    • plus a few percent from compressing the tail.

    4.5 What compression still does on Claude Code

    Per run, average
    Tokens removed from the uncached tail 348K (4.7–8.0% of what the engine saw)
    Messages protected inside the cached prefix ~1,800, about 2.2M tokens
    Elisions with a retrieval handle 126
    Retrievals the model made 0–2 per run, every one a hit

    The biggest contributor is cross-message de-duplication: tool output that repeats lines an earlier message already showed. Next come plain text, listings and config.

    Removal is far below the 62.8% we measured on Codex traffic, by design:

    • the engine only sees the uncached tail;
    • it skips elisions under 4,000 characters;
    • it leaves code bodies alone on this provider.

    The model almost never needs to retrieve anything, so compression adds no retrieval cost of its own.

    5. Conclusion

    On Claude Code the cache is the bill. Kompressor keeps it intact by never touching what the provider has cached, keeps it alive while tools run, and compresses only the new tail. Together that took 20% off the Anthropic API bill on Terminal-Bench 2.1 with no change in task success. The effect is largest on long-running work, where it removed a third of the cost.

    Try it:

    • Open source: run optscale-kompressor as a single binary.
    • In OptScale AI: turn on Kompressor in OptScale AI and every agent behind it starts saving from the next request, with no client changes.
    Repo: optscale-kompressor

    See what Kompressor saves on your Anthropic bill