frontpage.
newsnewestaskshowjobs

Open Source @Github

fp.

Open in hackernews

Claude Code's API billing depends on how often the cache expires

2•MaxBozhenko•2h ago
Hello, HN! I’m Max, CTO at Hystax. We use Claude a lot in our development work, and we’re also trying to make coding agents cheaper to run. We built the open-source Kompressor for compressing identical tool outputs that agents constantly resend. In our Codex tests, we noticed a real cost reduction of 22.7%. But testing it with Claude Code, we found something completely different. In Claude Code, the cache plays a more crucial role than compression.

Claude Code marks its messages with caching points. As a result, almost every request consists of a long cached prefix and a new, truncated final part. Based on practical experience with Opus 5.5, reading from the cache appears to cost $0.20 per million tokens, accounting for about 92–95% of the tokens charged for prompts. Meanwhile, a five-minute cache entry costs $5, which is 25 times more expensive than reading from the cache. When an agent needs to run a build or a longer training cycle, they face the problem of the cache expiring. The next request then has to write the prefix to the cache again. We saw this 33 times in a cycle of 89-task runs without compression. As a result, 927,000 tokens were rewritten, which is approximately one-third of the volume billed in full.

Until now, we’ve deliberately left the Claude Code text uncompressed because overwriting any byte within a cached prefix would very likely cause it to become invalid. But here’s what we came up with instead:

– Compress only the uncached part. On the first transmission, only the new part is compressed. Everything already cached stays byte-for-byte unchanged. Fragments shorter than 4,000 characters are sent in full, since compressing each fragment would be much more expensive than sending the entire dialogue as is.

– Keep the cache active while the tools are running; don’t let it expire. While the agent waits for a tool response (model building/training), Kompressor resends the last request every 4.5 minutes with max_tokens: 1, so Anthropic re-reads the cached dialogue, and the five-minute timer restarts. Pings stop on the agent’s next request, after ~50 min, on the first failed attempt, or if a ping finds nothing in the cache.

– Switch to the one-hour cache only when necessary. After the first pause longer than 5 min, Kompressor requests a one-hour cache for that conversation.

We ran Claude Code on the Opus 5.5 model through the Terminal-Bench 2.1 benchmark (89 tasks) and calculated the cost based on Anthropic's actual billing, including pings. The cost per run decreased from $35.35 to $28.30 (-20%). The percentage of tasks solved by the agent without compression was 89.9%, and 90.6% when using Kompressor. The number of tokens lost due to cache expiration decreased from 927,000 to 13,000.

But there’s a caveat: the agent never solves the same task twice in exactly the same way, and the entire difference came down to just 4 out of the 89 tasks. We expect these savings to persist from run to run: about 11% from keeping the cache up to date, and a few more percentage points from compression. This effect is most noticeable on tasks where the agent waits a long time – more than five minutes – for a command to execute: model training, compilation, and long simulations. In those cases, costs decreased by 34%. To try Kompressor, see the GitHub repo below. Setup is in the README. The default configuration already includes compression of new text only and a switch to an hourly cache on long pauses; “keep-warm” is off (the only request that the proxy sends on its own). To enable, add to your config:

     [proxy.providers.anthropic] 
     cache_keep_warm = true
and then run:

    kompressor wrap claude 
The benchmark above was run with all these settings enabled.

Here are cost calculations: https://optscale.ai/blog/claude-code-prompt-caching If you run long tasks in Claude Code, how often does the cache expire while a tool is still running? Have you seen this with other providers too?

Tell HN: Cast('abc' AS INTEGER) returns 0 in SQLite

2•prmph•52m ago•3 comments

Tell HN: I'm working on original approaches to tag-based file organization

2•prmph•1h ago•1 comments

Claude Code's API billing depends on how often the cache expires

2•MaxBozhenko•2h ago•0 comments

Ask HN: What is holding back robots from being deployed everywhere?

4•mjs06•3h ago•11 comments

Ask HN: What are you working on? (November 2026)

298•david927•3d ago•1060 comments

ChatGPT Desktop forwards your SSH agent to remote machines

219•DenGorbachev•3d ago•79 comments

Ask HN: Do you still enjoy coding in C++?

60•hasley•3d ago•88 comments

Ask HN: Personal "Tears in the Rain" Moments

13•HoldOnAMinute•18h ago•11 comments

Any good ways on how to get into kernel dev?

7•ihms•1d ago•5 comments

Ask HN: What do your technical interviews look like today? (Nov 2026)

6•Heap4112•14h ago•3 comments

Ask HN: Is lack of human imagination now the bottleneck instead of intelligence?

8•amichail•1d ago•12 comments

How do you keep up quality when you vibe-code?

37•nkcodes•1d ago•37 comments

What are your favorite tools?

13•d675•1d ago•9 comments

Do 9/10 new AI SaaS websites seem like they should be an editor command to you?

4•Kevin_Flynn•22h ago•2 comments

Ask HN: "No AI" apps/companies/resources?

7•BrunoBernardino•1d ago•8 comments

Ask HN: How did you convince family and friends to join your XMPP/Matrix server?

102•mtlynch•5d ago•97 comments

Ask HN: Did anybody used F# in a production?

5•vad_n__•22h ago•3 comments

Ask HN: Software engineer with broken arm, any tips?

6•spapas82•22h ago•8 comments

Ask HN: How do you evaluate the skills your AI agents use?

23•enesalatas•3d ago•6 comments

Ask HN: Anybody using their own AI harness as well?

5•madprops•1d ago•4 comments

Tell HN: Another GitHub Major Outage

17•sergiotapia•2d ago•7 comments

Tell HN: A flood of reports of Opus 5.5 being nerfed

31•b--l•3d ago•17 comments

CoderPad Reports a Security Breach

9•werrett•2d ago•1 comments

Why does Reddit want me to use the iOS app on my desktop?

9•corroclaro•1d ago•5 comments

uBlock Origin Can't Block Facebook Ads

13•anidsiam•3d ago•5 comments

Superwall (iOS Paywall Provider) Hacked – customers asked to rotate keys

4•epsteingpt•1d ago•0 comments

Tell HN: Notion AI chat sessions have been exposed via Fireworks AI hack

18•RamblingCTO•2d ago•1 comments

Ask HN: Is misaligned AI less dangerous than AI controlled by a bad actor

2•InfiniteRand•2d ago•5 comments

You've reached the end!