Why your AI agent rereads the same prompt 400 times a day
A large language model has no memory. Every API call starts from zero.
So if you send a 5,000-token system prompt with every request, the model processes those 5,000 tokens (chunks of words) every time. A hundred requests a day means half a million tokens of compute for a text that hasn't changed by a comma.
Prompt caching fixes this. It is probably the best effort-to-payoff optimization in the whole LLM stack.
What happens when you send a prompt
The work happens in two steps.
First, the prefill. The model reads your whole prompt and computes an internal representation for each token. These representations are called the KV cache. It's a lot of compute, but it runs in parallel, so it's fast on a GPU.
Then the decode. The model writes the answer one token at a time, using the KV cache built in the first step. This part is slow and sequential.
Prompt caching keeps the KV cache from the prefill and reuses it on the next call. The model only processes what is new.
The book analogy
Picture a consultant you ask to review an 80-page contract. They read the 80 pages, then answer your question.
You come back five minutes later with a second question about the same contract. Without a cache, they read the 80 pages again. With a cache, they kept their reading notes and answer right away.
The KV cache is the reading notes.
The catch: it only works from the start
This is where most implementations fail.
The cache only works on a prefix: a continuous run of text that starts at the very first token. Not the middle, not the end. The start.
The reason is how a transformer works. The internal representation of each token depends on every token before it. Change word number 5, and the representations of words 6 to 5,000 are no longer valid, even if those words are exactly the same as in the previous request.
In practice, the order of your prompt sets your bill.
What never changes goes first: tool definitions, system prompt, reference documents, examples. What changes with every request goes last: the user's question, the date, session settings.
[tools][system prompt][document][history] | [new question]
<---------- reused from cache ---------> <- recomputed
Swap two blocks and you lose all of it.
The timestamp trap
The most common failure is silent.
You put today's date in your system prompt so the model knows what day it is. Good intention. But that string changes every minute, and it sits at the very top of the prompt. The prefix never matches. The cache never kicks in.
No error, no warning. Just a bill that stays at full price.
Other usual suspects: a session ID, a random greeting, a tool list built from a dictionary whose order isn't fixed.
Check the cache hit rate in your usage metrics. Don't assume it works.
What it costs, what it saves
Writing to the cache costs a bit more than a normal input token. Reading from it costs about ten times less.
It pays off from the second request that shares the same prefix. On an agent running in a loop with a stable context, the cut on input tokens is around 90%.
Latency drops in the same proportion, since most of the prefill is skipped. On long contexts, that's several seconds saved on every turn.
The cache doesn't last. At Anthropic, it lives five minutes by default, or one hour as a paid option. If your traffic is uneven, say one request every seven minutes, you keep paying for the write and never get a read. That's when the longer duration is worth it.
Automatic or explicit
It depends on the provider.
OpenAI, DeepSeek and Gemini handle it on the server without you asking. The system finds the longest stable prefix. Simple, but you get no control.
Anthropic makes you declare it. Either one global setting that places the cache point for you, or explicit markers on the blocks you choose. More verbose, but you can cache a retrieved document separately from the system prompt, which is a real advantage for RAG workloads (where the model answers from documents fetched at query time).
If you host your own models, vLLM and SGLang do prefix caching automatically, shared across all requests on the instance.
When it doesn't help
Two cases.
One-off, varied requests. If every call has a different context, there is no shared prefix to reuse.
Output-heavy workloads. A 50-token prompt that generates a 5,000-token answer: the cost is elsewhere, and the cache changes nothing.
In practice
- Pull your input token volumes for one week.
- Find your three most expensive workloads.
- For each one, measure the length of the stable prefix.
- Above two thousand tokens, turn on caching.
It's one of the rare cases where the code change fits in three lines and the change on the bill is counted in thousands.