I find it really weird that Anthropic uses only a five minute cache on the API. Even in the course of active work, many commands—compiling, running tests, etc—take longer than five minutes to complete.
I of course don't know what it costs Anthropic to keep the cache warm—but since their subscription plans use one hour, Anthropic must have concluded a longer cache is more economical for most users. And there's a big difference between one hour and five minutes.
> I find it really weird that Anthropic uses only a five minute cache on the API.
You can set 5 minute or 1 hour with API requests. It's the user's choice, not something they force on you. You get the cheapest option (5 minutes) if you don't specify the time.
The API is used for a lot more than agentic coding. I would not be surprised if 5 minute was preferred on average across all types of usage.
If Anthropic set it to the more expensive option by default, we'd be reading articles about people complaining how evil they were for defaulting to the more expensive option.
yeah, not sure why you're downvoted, but the API is clearly for using Claude in your products that you serve to your users. It's also why it's priced higher since it's assumed to be close to where you make money. It's the same with cloud computing, where clouds could charge 1000x the involved compute for some per-access DB, but you wouldn't run a nuclear reactor simulation on Firebase.
It's also less vram and hence bandwidth hungry so much friendlier to hold for longer and swapping in and out to storage. If kv cache is large enough and slow enough, then prefill could turn out to be faster than cache restore... Although it depends on how the cluster and infra is setup, either way, Anthropic is subsidizing the subscriptions if all benefits and limits and etc. are included by at least 2 orders of magnitude aka ~100x compared to API which is rather insane..
It's surprising, and also not really surprising that they then charge enterprises and companies with over 100/150 employees full API prices with some bulk discounts.
I have seen a bill for a small 100+ person company for their tokens and it's insane when you compare it to how many tokens I burn in a week.
I waste more tokens on chasing useless stuff over a couple weeks, then their entire company spends in an entire month tbh that might include resets but this is madness, I am not sure if the music will stop but if it does I don't feel like I nor my current employer will be paying the full price for the tokens.
it takes claude 15-30 min to add a disk layer cache after system memory FYI... have this going on my 8x v100 server for all openweight models I'm running. currently preferring glm-5.3-flash. prefill is around 3k tok/s, decode is about 80 tok/s. have room for 3x 262k sessions with room to spare. and memory for another million. then a 6.4TB NVMe for quite a bit more.
deepseek-v4.1-flash per token kv is IIRC 600-700 bytes. this is much, much less than other models. and 500x less than deepseek v3.
Since the API is bare, the use cases will be broader than just agent sessions -- automated scripting and customer-served chatbotting are two obvious supplemental use-cases. Those are likely to have very different cache dynamics compared to a Claude Code session, so 5m expiry might be the hit-rate-optimizing point.
They are for subscription plans, where they pay by token and the user pays a fixed cost.
Subscription plans best align the user’s interest with the company’s interest. Presumably, the user’s goal is not token burn but some underlying task. The cheaper the company can accomplish that task, the more money the company keeps. If the user has to constantly retry because the model is bad, that’s more cost to the provider. If the model is big and slow and wastes tokens, that’s more cost to the provider. The more tasks that can be accomplished in the fixed quota, the better value perception by the user.
It seems like what would best align the interests of the customer and company would be if they’d charge by completed task instead of… tokens/time. (Why are these internal compute metrics being exposed to the customers?).
The amount Anthropic can charge per token is determined by how much value users get from those tokens. If users are getting more value, Anthropic can charge more and take a bigger profit.
Which means it is in Anthropic's interest to set cache lifetimes at a number that will let users get the most value from their tokens, relative to whatever it costs Anthropic to maintain the cache.
Does the average user even know what value they are getting from Anthropic? As though there is some kind of metric?
I don't think value is some immediately obvious and observable phenomenon. The average person doesn't even know what prompt caching is. It's completely feasible for them to set defaults that overcharge customers without them knowing.
> The amount Anthropic can charge per token is determined by how much value users get from those tokens. If users are getting more value, Anthropic can charge more and take a bigger profit.
I feel like statements like that, looked at most charitably, benefit from the confusion between some technical definition of "value" and a a more colloquial definition.
The easiest way to interpret that is: "what's good for you is good for Anthropic, so trust Anthropic," but that's almost never what's going on when you're dealing with a sophisticated business. In all market interactions, "value" is relative and everything is almost always obscured by intense information asymmetries. The business wants to maximize profit for itself, which often entails minimizing the value provided to customers (because more "value" usually costs something) and maximizing the price they pay for it.
The interest of any provider of proprietary inference is to make the most money per token.
Users don't have such a precise metric that would allow them to discriminate value per token reliably. Or have you never seen those comparison sites, with dozens of different scales?
If they can make you reload your cache statistically more often without you noticing (increasing their profits), their business model mandates them doing so.
There are many such open questions about "clandestine token-maxing" that get glossed over.
For any provider of proprietary inference, the targeted optimization of how (long) their models work according to their own goals is a central part of their business.
People believe those optimizations being about giving them the "best" inference. They fail to notice whose "best".
They probably do around the edges. But I bet they'd lose more if they raised the list price per token and included a longer cache time by default even if it the average bill works out the same.
Everything old is new again. Something similar was a time honored technique for getting an OS scheduler to keep a long-running task at interactive priority by writing a character to the console every so often, so it looked like an interactive process.
How to defeat these countermeasures used to be covered in os courses. I suspect the llm vendors will adopt similar techniques.
On a subscription plan (not API requests) Claude Code defaults to 1-hour cache duration for the main conversation and 5 minutes for subagents.
So if you're not paying API rates, you need not worry. You can force the main conversation or subagents to either 5 minute or 1 hour in the Claude settings file if you would like.
If you are paying API rates, paying close attention to caching is important. 1 hour is best for sessions where you have to think a lot, examine output, or wait for long builds because 5 minute interruptions are very common. The price premium for 1-hour TTL isn't that high over 5-minute, so I use it by default.
It's also good to get in the habit of doing /compact when you know you might be AFK for more than the cache TTL. Doing the /compact on a long context conversation after the cache has expired is much more expensive.
Ah, this explains so much, I need to adjust my settings. Lately I've been using bigger models to drive smaller models to do changes, sometimes they run for 45 minutes or more, so I definitely need to tweak my subagent cache time window. I'm not on API billing, but still.
Claude code actually compacts the main session just before the cache runs out automatically.
No idea when they added that, Its gray text that looks exactly like the auto summary line, so easy to miss (as it only shows up when you're idle for 55 minutes or so, and have a session that's compact worthy - again no idea what threshold they use)
Is there a pi plugin for resetting the keepalive? Sounds simple enough. From the article:
What is a keep-alive ping for prompt caching?
-
You send the last request again with max_tokens set to 0 about every 4.5 minutes. The API reads the cached prefix, returns no output tokens and restarts the cache timer. In our replay, keep-alive pings capped by session depth cut a Claude coding bill by about 8 to 9% net.
If I'm not mistaken, on subscription plans your main agent defaults to 1 hour cache with subagents spawned by the main session on 5 minute cache timer.
Best ensure your subagents avoid getting blocked if there's any wait or command that takes more than 5 minute.
Yeah, this looks like a discussion of API traffic, not claude code subscription traffic. I'd be shocked to find out that my claude code sessions, which auto-compact "before the cache expires", after 54 minutes of idle time, due to claude code's own idleCompaction setting, was actually already expired at that point.
Welp, doing this immediately. I had to build a Rust build queue and a client lease system for a game I have been building entirely with agents (intentionally with a lot of subagents and multiple top-level sessions) just to avoid consuming all available memory and compute on a server I have laying around. I have a lot of subagents that end up having to wait for a long time for their turn to build and run the game to verify behavior.
It's not Claude Code so much as provider. If you're on a subscription plan this article doesn't apply to you. If you're using Vertex API or similar it defaults to 5m.
I guess that refers to cache pricing? (Surely after some time the tokens are no longer cached, but you mean they still charge you as though they were?) Either way, that's surprising.
Several studies have found that performance degrades sharply after 100k tokens or so, even in models with much larger contexts. (The further out you get the lossier the attention gets from what I understood.)
(Which is funny, because you pay a premium on many providers for the worse attention!)
The last study I found was from a little while ago though, and there's been a lot of development in attention mechanisms since then. So it might be less bad now.
This is obvious if you just put cache timeout in your statusline. If the nature of your work is such that every time you return to the window the 5m cache is expired, bump it up.
This is a big contributor to long-running sessions. I built a small CLI that opens in a tmux popup and shows what my Cursor agents cost: https://github.com/rishabhBudhouliya/tend.
It uses a ridgeline chart to show how big each chat's context has grown and what it's spending.
Where can I find cache "duration" of different providers? I have never seen such information, despite it being a very big deal in cost estimation. Or maybe it's dynamic?
Not all providers publish it, and aren't always correct when they do. For some providers (e.g. DeepSeek, Antigravity Gemini) it is indeed dynamic/not guaranteed.
Bet bet is to measure it yourself by observing billing/credit usage or time-to-first-token.
It's not dynamic, in the sense pricing has rules, but, it is dynamic in the sense you won't always hit the cache.
Generally, by Googling "(provider) llm cache cost and duration" - can't recommend any source that collects them all.
Generally OpenAI and Anthropic are very serious about this and document it well.
Google is currently a shit show but getting better if you're on Interactions API.
The not-$1T crowd, i.e. other inference providers, can't and won't really guarantee anything.
OpenRouter shows data on their cache hit rates.
On not-$1T-crowd APIs, they'll want to set an arbitrary hint that they'll use for server affinity. (i.e. Fireworks needs to know this request wants to run on the same server/cluster/whatever as the last request so it can get a cache hit)
I've felt this long before and my agent's contexts are compacted very very often. Even though they work on a common bus, I try to keep them regularly pooled back to minimum.
As stated by others, it’s a precaution. It’s been shown that agents can get desperate and use whatever they can get their hands on to solve the problem at hand. I don’t want them snooping around the network for information, credentials or test data.
Edit: Not likely to happen currently but having this infra makes it easier to demonstrate compliance with your orgs rules.
It's a bit overkill for most users but depending on what you're asking it to work on it could theoretically traverse your local network to compromise devices/computers and escape the sandbox.
These AI written articles are exhaustingly long, and the #s included make it feel very scientific but are one offs for one person.
It's self-labelled as a 15m read and a human in the loop should recognize it's about 3m, tops. Here:
LLM API context caches, by default, only last for 5 minutes on Anthropic and 30m on OpenAI.
If you use Claude, there's an hour cache for 2x the cost of the 5 minute one.
If you use OpenAI, there's a 12 hour cache for 2.5x the cost of the 30m one.
FWIW, it's very Chicken Little / "the sky is falling!!111!": the idea is if you walked away for > 5m between every Claude request, re-caching would be 23% of your bill.
>FWIW, it's very Chicken Little / "the sky is falling!!111!": the idea is if you walked away for > 5m between every Claude request, re-caching would be 23% of your bill.
I did indeed run into this problem when I was steering 4 or 5 chats that would ask for feedback that took a few minutes to provide. Pretty soon you're juggling a bunch of 5 min timers if you don't want every single response to have to rebuild the cache.
Not even 3 min. Took like 3 seconds to skim the title and understand that it's about cache misses (and that Claude is probably presenting it in some inane incomprehensible technobabble so someone who isn't familiar with prompt caching's budget impact already is going to read the whole thing and still have zero idea wtf is going on)
> FWIW, it's very Chicken Little / "the sky is falling!!111!": the idea is if you walked away for > 5m between every Claude request, re-caching would be 23% of your bill.
On subscription (so 1h cache) I run into this being even higher, reaching 50%, before I noticed problem.
Requisite smarmy comment about how this is not an issue with local models.
There's even the ability to persist your kv cache to disk, so future session resumptions load back into context instantly without needing to do a full prefill ingestion by the LLM. (CachyLLaMA, iirc, not sure where the project is now)
Up until a few weeks ago, I wouldn't say it (at least for my use case).
But now, with Bonsai 2.5 27B, this is the first time that I don't need Haiku (or a similar cheap model) for tasks in their scope. I think local models are close to an inflection point in usefulness.
Note: I don't have anything local remotely close to reasoning capabilities of SOTA models such as Opus/Neon/Astra.
Neon really cooked with tamping down on the inane incomprehensible Claude slop huh? It's so damn annoying when they spew this poetic babbling gibberish.
TDLR prompts are cached. Prompt cache hit == $. Cache miss == $$$$$$$. Claude ttl is 5 min. Codex ttl is 30 min. If u take longer than 5 min between turns then $$$$
Edwin from Anthropic here. Thanks for the thorough writeup. I will point out two things:
1. This looks like API-billed traffic only. Claude Code defaults to a 1 hour cache, not 5 min! So if you're within your plan's usage, this doesn't really apply [1].
2. In the API, we have 5 min and 1 hour TTL options. The "23%" here for the 5 min option includes breaks over 30 min, where both Claude's and GPT's default caches have expired and the loss is the ~same (7.8% vs. 8.5%). If you run long sessions with breaks, and are using an API key, you could set "ttl": "1h". There will be a bit higher write price, though [2].
I of course don't know what it costs Anthropic to keep the cache warm—but since their subscription plans use one hour, Anthropic must have concluded a longer cache is more economical for most users. And there's a big difference between one hour and five minutes.
You can set 5 minute or 1 hour with API requests. It's the user's choice, not something they force on you. You get the cheapest option (5 minutes) if you don't specify the time.
https://platform.claude.com/docs/en/build-with-claude/prompt...
Okay, but isn't it weird that the default is a giant footgun?
If Anthropic set it to the more expensive option by default, we'd be reading articles about people complaining how evil they were for defaulting to the more expensive option.
I mean... you say I wouldn't, but im all up for trying.
Making you spend tokens without noticing it is a free meal to them.
There are various other possibilities to clandestinely extract tokens from you. People just haven't thought about it yet.
What a load-bearing observation. Good instinct!
It's surprising, and also not really surprising that they then charge enterprises and companies with over 100/150 employees full API prices with some bulk discounts. I have seen a bill for a small 100+ person company for their tokens and it's insane when you compare it to how many tokens I burn in a week.
I waste more tokens on chasing useless stuff over a couple weeks, then their entire company spends in an entire month tbh that might include resets but this is madness, I am not sure if the music will stop but if it does I don't feel like I nor my current employer will be paying the full price for the tokens.
deepseek-v4.1-flash per token kv is IIRC 600-700 bytes. this is much, much less than other models. and 500x less than deepseek v3.
Subscription plans best align the user’s interest with the company’s interest. Presumably, the user’s goal is not token burn but some underlying task. The cheaper the company can accomplish that task, the more money the company keeps. If the user has to constantly retry because the model is bad, that’s more cost to the provider. If the model is big and slow and wastes tokens, that’s more cost to the provider. The more tasks that can be accomplished in the fixed quota, the better value perception by the user.
It's a useful metric, especially for those who know what it means.
It's rather an equilibrium based on what they can get away with.
It's safe to say, that equilibrium is a safe distance from what's in the users interest.
Which means it is in Anthropic's interest to set cache lifetimes at a number that will let users get the most value from their tokens, relative to whatever it costs Anthropic to maintain the cache.
I don't think value is some immediately obvious and observable phenomenon. The average person doesn't even know what prompt caching is. It's completely feasible for them to set defaults that overcharge customers without them knowing.
I feel like statements like that, looked at most charitably, benefit from the confusion between some technical definition of "value" and a a more colloquial definition.
The easiest way to interpret that is: "what's good for you is good for Anthropic, so trust Anthropic," but that's almost never what's going on when you're dealing with a sophisticated business. In all market interactions, "value" is relative and everything is almost always obscured by intense information asymmetries. The business wants to maximize profit for itself, which often entails minimizing the value provided to customers (because more "value" usually costs something) and maximizing the price they pay for it.
Users don't have such a precise metric that would allow them to discriminate value per token reliably. Or have you never seen those comparison sites, with dozens of different scales?
If they can make you reload your cache statistically more often without you noticing (increasing their profits), their business model mandates them doing so.
I’m also not running a multi billion or trillion dollar company, and probably never will if I keep doing it.
I’m okay with that.
For any provider of proprietary inference, the targeted optimization of how (long) their models work according to their own goals is a central part of their business.
People believe those optimizations being about giving them the "best" inference. They fail to notice whose "best".
How to defeat these countermeasures used to be covered in os courses. I suspect the llm vendors will adopt similar techniques.
https://algomaster.io/learn/operating-systems/multilevel-que...
So if you're not paying API rates, you need not worry. You can force the main conversation or subagents to either 5 minute or 1 hour in the Claude settings file if you would like.
If you are paying API rates, paying close attention to caching is important. 1 hour is best for sessions where you have to think a lot, examine output, or wait for long builds because 5 minute interruptions are very common. The price premium for 1-hour TTL isn't that high over 5-minute, so I use it by default.
It's also good to get in the habit of doing /compact when you know you might be AFK for more than the cache TTL. Doing the /compact on a long context conversation after the cache has expired is much more expensive.
Ah, this explains so much, I need to adjust my settings. Lately I've been using bigger models to drive smaller models to do changes, sometimes they run for 45 minutes or more, so I definitely need to tweak my subagent cache time window. I'm not on API billing, but still.
No idea when they added that, Its gray text that looks exactly like the auto summary line, so easy to miss (as it only shows up when you're idle for 55 minutes or so, and have a session that's compact worthy - again no idea what threshold they use)
What is a keep-alive ping for prompt caching? - You send the last request again with max_tokens set to 0 about every 4.5 minutes. The API reads the cached prefix, returns no output tokens and restarts the cache timer. In our replay, keep-alive pings capped by session depth cut a Claude coding bill by about 8 to 9% net.
lol if this works there is no way they are gonna let you keep getting away with it
Best ensure your subagents avoid getting blocked if there's any wait or command that takes more than 5 minute.
Ask your agent to look at its own usage metrics to see if you would have saved money by doing so. If your test suite is >5 mins then you likely will.
API is all 5m but apparently keeping it warm every 4.5 mins on API is fine.
This is what i heard.
(Which is funny, because you pay a premium on many providers for the worse attention!)
The last study I found was from a little while ago though, and there's been a lot of development in attention mechanisms since then. So it might be less bad now.
It uses a ridgeline chart to show how big each chat's context has grown and what it's spending.
Bet bet is to measure it yourself by observing billing/credit usage or time-to-first-token.
Claude Code is here: https://code.claude.com/docs/en/prompt-caching
If you're using OpenRouter they publish cache hit rates, which can be used as a sort of proxy, e.g.: https://openrouter.ai/deepseek/deepseek-v4.1-flash-1017#pric...
Generally, by Googling "(provider) llm cache cost and duration" - can't recommend any source that collects them all.
Generally OpenAI and Anthropic are very serious about this and document it well.
Google is currently a shit show but getting better if you're on Interactions API.
The not-$1T crowd, i.e. other inference providers, can't and won't really guarantee anything.
OpenRouter shows data on their cache hit rates.
On not-$1T-crowd APIs, they'll want to set an arbitrary hint that they'll use for server affinity. (i.e. Fireworks needs to know this request wants to run on the same server/cluster/whatever as the last request so it can get a cache hit)
And start a new blank session with its own branch (and have the agent update the project readme) so you can restart without context.
Edit: Not likely to happen currently but having this infra makes it easier to demonstrate compliance with your orgs rules.
It's a bit overkill for most users but depending on what you're asking it to work on it could theoretically traverse your local network to compromise devices/computers and escape the sandbox.
It's self-labelled as a 15m read and a human in the loop should recognize it's about 3m, tops. Here:
LLM API context caches, by default, only last for 5 minutes on Anthropic and 30m on OpenAI.
If you use Claude, there's an hour cache for 2x the cost of the 5 minute one.
If you use OpenAI, there's a 12 hour cache for 2.5x the cost of the 30m one.
FWIW, it's very Chicken Little / "the sky is falling!!111!": the idea is if you walked away for > 5m between every Claude request, re-caching would be 23% of your bill.
I did indeed run into this problem when I was steering 4 or 5 chats that would ask for feedback that took a few minutes to provide. Pretty soon you're juggling a bunch of 5 min timers if you don't want every single response to have to rebuild the cache.
I could tell that this was AI-written just from the turn of phrase in the title before I even clicked :\
On subscription (so 1h cache) I run into this being even higher, reaching 50%, before I noticed problem.
10-100x cheaper is not something to sneeze at
Are they offering Opus 5.5 quality and quantity of 20$ Claude subscription at and 2$/month? Or 0.2$/month?
There's even the ability to persist your kv cache to disk, so future session resumptions load back into context instantly without needing to do a full prefill ingestion by the LLM. (CachyLLaMA, iirc, not sure where the project is now)
But now, with Bonsai 2.5 27B, this is the first time that I don't need Haiku (or a similar cheap model) for tasks in their scope. I think local models are close to an inflection point in usefulness.
Note: I don't have anything local remotely close to reasoning capabilities of SOTA models such as Opus/Neon/Astra.
TDLR prompts are cached. Prompt cache hit == $. Cache miss == $$$$$$$. Claude ttl is 5 min. Codex ttl is 30 min. If u take longer than 5 min between turns then $$$$
1. This looks like API-billed traffic only. Claude Code defaults to a 1 hour cache, not 5 min! So if you're within your plan's usage, this doesn't really apply [1].
2. In the API, we have 5 min and 1 hour TTL options. The "23%" here for the 5 min option includes breaks over 30 min, where both Claude's and GPT's default caches have expired and the loss is the ~same (7.8% vs. 8.5%). If you run long sessions with breaks, and are using an API key, you could set "ttl": "1h". There will be a bit higher write price, though [2].
[1] https://code.claude.com/docs/en/prompt-caching
[2] https://platform.claude.com/docs/en/build-with-claude/prompt...
for +i; send prompt=prompt+"."