If your app sends the same long system prompt with every request, you pay to process those same tokens again and again. Learning how llm prompt caching works can cut that repeat bill, because the provider stores the processed form of a stable prompt prefix and reuses it instead of running the full computation from scratch on each call.
The idea is simple, but the details decide whether you save money or just add complexity. A cache hit depends on an exact prefix match, a time limit, and a minimum size. A cache miss means you pay the full price again. This guide walks through the mechanism, the hit and miss rules, and a cost check you can run on your own traffic before you turn caching on.
Why repeat prompts cost so much
Most LLM calls start with a long, fixed block: system instructions, tool definitions, a style guide, and a few examples. That block can run to thousands of tokens, while the new user question at the end is often short. Without caching, the model processes the whole block on every call, so the fixed part dominates the bill even though it never changes.
This is where llm prompt caching earns its place. If fifty users ask different questions against the same system prompt, the provider only needs to process that shared block once, then reuse the stored result for the other forty-nine calls. You still pay a smaller fee to read the cached tokens, but you skip the larger fee to process them again.
What the cache actually stores
The cache does not store your text. It stores the key-value states the model computed while reading that text, often called the KV cache. When a new request arrives with the same prefix, the model loads those stored states and starts from the end of the prefix, instead of recomputing each token in the fixed block.
That stored state is tied to the exact model and the exact token sequence. Change one word near the start, and the stored states after that point no longer match. Providers handle this by caching only the matching prefix, up to the first difference, and recomputing the rest. For the memory side of this, see our explainer on how the KV cache works in LLM inference.
How LLM prompt caching works in a real request
Here is the sequence for a typical cached call. First, your app sends a request with a stable prefix followed by new input. The provider checks the cache for that prefix. If it finds a match, it marks the request as a cache hit, loads the stored states, and processes only the new tokens. Your bill shows a lower read price for the cached part.
If no match is found, the request is a cache miss. The provider processes the full prompt, stores the prefix states for later use, and bills the full input price. The first call after a change always costs full price. The saving arrives on the calls after it, as long as the cache entry is still alive.
Most providers also set a minimum prefix size, often in the hundreds or low thousands of tokens, and a time to live, often a few minutes to an hour. A prefix below the minimum is not cached at all. An entry past its time to live is dropped, and the next call pays full price again. Check official pricing and cache rules for your provider before you rely on a saving, because limits and read prices differ by vendor.
When a cache hits and when it misses
A hit needs three things to line up: the same model, the same prefix tokens in the same order, and a live cache entry. That sounds strict, and it is. Small changes break it in ways that surprise teams the first time.
Common miss causes include:
- A timestamp, user ID, or random value placed at the top of the prompt, which shifts every token after it.
- A user message or tool result inserted before the stable instructions, instead of after them.
- A different model version, even a minor one, which uses a separate cache.
- A prefix that is too short to meet the provider minimum.
- A gap between calls longer than the time to live, so the entry is gone before the next hit.
- High traffic that pushes your entry out when the cache fills up.
The fix is prompt order. Put the stable block first: system instructions, tool schemas, and examples that rarely change. Put the variable parts last: the user question, the retrieved context, and the current date. That order keeps the shared prefix intact across calls, and it is the single change that turns most misses into hits.
Our earlier piece on prompt caching for repeated LLM calls looks at the same feature from the cost side, with examples of where teams tend to place the cache boundary. This article stays on the mechanism: what is stored, what must match, and why order matters more than prompt length alone.
How to work out the saving
Run the numbers on your own traffic, not on a vendor example. Take one week of logs and count how many calls share the same stable prefix, how long that prefix is in tokens, and how often each prefix repeats within the time to live. A prefix that repeats twice a day will not pay for the extra code. A prefix that repeats two hundred times an hour will.
A simple estimate looks like this. Say your stable prefix is 4,000 tokens, your full input price is 3 dollars per million tokens, and the cached read price is 0.30 dollars per million. Without caching, one hundred calls cost 100 times 4,000 tokens at full price, plus the new user tokens. With caching, the first call pays full price, and the other ninety-nine pay the read price for the same 4,000 tokens. That is prompt cache cost savings in plain form: ninety-nine calls at one tenth of the input price for the shared part.
Work the same sum with your real prices, your real prefix length, and your real hit rate. If your hit rate is 40 percent, only forty of those hundred calls save money. If your prefix is 400 tokens, the saving per call is small even at a 90 percent hit rate. Keep the estimate honest by counting the new user tokens and the output tokens too, because caching never touches those.
When caching helps little
Caching helps little when each prompt is mostly new. Long retrieved documents, fresh tool results, and user-uploaded files change the prefix on every call, so there is nothing stable to reuse. In that case, shorten the fixed block first, or move the variable content to the end so at least the system instructions stay cached.
Do not confuse prompt caching with semantic caching llms setups. Prompt caching needs an exact token match on the prefix. Semantic caching stores answers for questions that mean the same thing, even when the words differ, and it returns a stored reply without calling the model at all. The two can sit side by side: semantic caching for repeat questions, prompt caching for repeat instructions. Our guide to semantic caching for LLMs explains the second pattern in detail.
How to set it up without breaking answers
Start with one stable prefix and one endpoint. Mark the boundary in your code so the stable part is easy to find and hard to edit by accident. Log the cache status on each call: hit, miss, or not cached. If your provider returns that status, store it next to the cost for the same call, so you can see the saving per endpoint instead of guessing from the monthly bill.
Then watch for stale content. A cached prefix is a snapshot. If you update the system instructions, the old cache entry may still serve for a few minutes until it expires. During a rollout, that means some users get the old instructions and some get the new ones. Plan for that window, and test the new prompt with caching off first, then turn it back on once the text is final.
Finally, keep monitoring after launch. Cache hit rates drift as traffic patterns change, prompts get edited, and providers adjust time to live rules. A dashboard that tracks hit rate, cached tokens, and cost per call will catch a drop early. Our guide to monitoring LLM apps in production shows which metrics to track and how to set alerts when a cost or quality number moves the wrong way.