Concepts · Level 3 · Note 11
Prompt caching and the KV cache: the discount that can leak
KV cache and prompt caching explained in plain words, then the timing side channel: who can tell what you asked, what the research showed, and what to check.
Short answer
A KV cache lets a model keep its notes on the text it has already read, so each new word costs less work. Prompt caching (prefix caching) reuses those notes across requests that start the same way, and vendors now discount cached input by up to 90 to 95%. The catch is that a cache hit answers faster, and speed is visible to outsiders. A 2025 Stanford audit of 17 AI providers found prompt caches shared across all users at 7 of them at the time, and at least five have since changed that. Check who shares your cache: your provider's customers, your own users, or nobody.
Prompt caching makes an AI model cheaper and faster by reusing work it has already done. That reuse is also a leak if the wrong people share the cache, because a reused prompt answers faster than a new one and anyone can time a response.
The KV cache, in plain words
Imagine you’re answering a long letter by hand, one word at a time. Before writing each word you reread the whole letter, plus everything you’ve written so far. That works, but it gets slower with every word.
Now imagine you keep notes in the margin the first time through. For each later word you glance at your notes and read only the newest word. Those margin notes are the KV cache. The model keeps them for text it has already processed.
Animated: how a KV cache saves work
The text so far
Notes kept in the cache (one key and one value per word)
- The model reads the prompt once. For each word it writes a note: a key and a value.
- To choose the next word, it looks back at every note.
- With a cache, the old notes are kept. Writing "the" needs just one new note.
- Without a cache, the model would throw the notes away and rewrite all of them for every new word.
- Next word, "mat". With the cache: one more note. Without it: the whole pile again.
- With a cache, each new word needs one new note plus a look back at the old ones, so the cost grows slowly. Without one, the whole pile is rewritten every time and the cost grows much faster. The price is memory to hold the notes.
The KV cache, technically
A language model writes one token (a piece of a word) at a time. To choose the next token, each attention layer compares the newest token against every earlier one. For that comparison each token is turned into three vectors: a query, a key and a value. This happens in every layer of the model. The Hugging Face documentation describes them as “projections from the input embeddings”.
The keys and values of earlier tokens don’t change as the text grows. Without a cache the model recomputes them at every step anyway. Hugging Face puts it this way: “This is a lot of matrix multiplications a model has to compute over and over for each token!” With a cache, the model stores those key and value pairs and at each step will “only compute current K and V”.
The same documentation gives the effect on cost. Without a cache, attention cost per step is quadratic with sequence length, so twice the text means four times the work. With one it is linear. The price is memory, which “grows linearly” as the text gets longer. A second Hugging Face page warns that the cache “can occupy a significant portion of memory and become a bottleneck for long-context generation.”
This is also why the first word of an answer takes longest. On the first pass nothing is cached, so the model has to read the whole prompt. After that it reuses its notes.
Prompt caching, in plain words
A busy kitchen doesn’t make stock from scratch for every order. It makes a big pot in the morning and starts each dish from it.
Most requests to an AI app start the same way: the same long system prompt, the same tool list, the same reference document. Prompt caching (also called prefix caching) keeps the model’s notes for that shared opening and starts each new request from them. Only the part that differs is processed fresh.
Animated: how two requests share a cached opening
Request A
Request B, a minute later
- Request A arrives. Nothing is cached, so the model reads all of it.
- The notes for the opening part are saved in the cache for a few minutes.
- Request B arrives. It starts with exactly the same opening.
- The model reuses the saved notes and reads only the new part, B's question.
- B comes back faster, and the reused part is billed at a discount that vendors put at up to 90 to 95%.
- The catch: a fast reply tells anyone sharing that cache that the same opening was sent recently.
Prompt caching, technically
The KV cache lives inside one request. Prefix caching keeps it between requests. OpenAI says so directly: “The prompt cache stores key-value (KV) tensors rather than the original prompt text.”
Three rules are the same at every vendor we checked on 23 November 2026:
- The match is exact and starts at the beginning. Anthropic: “Cache hits require 100 percent identical prompt segments”. OpenAI: “Cache reuse requires the entire rendered prefix to match.” One changed character early in the prompt means no hit.
- Entries expire. Anthropic’s default is a 5-minute lifetime, with a 1-hour option at extra cost, and the timer restarts each time the entry is used. OpenAI’s default for GPT-5.6 and later is 30 minutes, and earlier models keep entries for around 5 to 10 minutes of inactivity, up to one hour.
- Short prompts aren’t cached. OpenAI’s minimum is 1,024 tokens for GPT-5.6 and later.
The discount is the draw. On Anthropic you opt in. On OpenAI and Google caching is on by default, so you can be using it without having chosen to. You’ll see “save 80%” quoted on social media. The vendors’ own pages now say more than that.
Stated discount on cached input tokens
Where the leak comes from
A cache hit is faster than a miss. That is the whole point of caching, and it can’t be hidden without giving up the speed. What you can control is who shares the cache.
If two people share a cache, one of them can learn something about the other. Send a prompt and time the answer. A fast answer means someone sent that same opening recently, and a slow one means nobody did. On servers that match token by token, repeated guesses can rebuild a prompt one piece at a time. On the large APIs the realistic result is narrower: confirming that a prompt or document you can already guess in full was sent recently. Security people call this a timing side channel, because the secret leaks through how long something takes and never through the data itself.
How a shared cache turns timing into a leak
What the research showed
Five Stanford researchers tested 17 API providers for this in a paper first posted in February 2025. They detected prompt caching at 8 of the 17, and “global cache sharing in 7 providers”, meaning the cache was shared across all users of the API. Their warning: “an attacker can potentially learn information about other users’ prompts by identifying cached prompts from timing data.”
Two qualifiers belong next to that. The researchers say they “were unable to execute practical prompt extraction attacks”. And they disclosed the results in October 2024, gave providers 60 days, and report that “at least five providers made changes”, such as disabling global cache sharing across organizations.
The Stanford audit in three numbers
Timing also told the same team something about a model’s design. Prefix caching only works in one family of model architecture, so seeing it on OpenAI’s text-embedding-3-small “indicates that text-embedding-3-small is a decoder-only Transformer”. The paper says the cache sharing behind this was patched after disclosure.
On self-hosted servers, a paper at NDSS 2025 called the attack PromptPeek and ran it against SGLang, an open source model server, with Llama2-13B on one GPU. The signal there was the order in which answers came back, since the server’s default scheduler serves requests with a longer cached match first. In that local test the attack fully or partially reversed prompts with a 99% average success rate, and 95% when the attacker had no background knowledge. It was a lab experiment, and it took dozens to hundreds of requests per token.
A third paper, “The Early Bird Catches the Leak”, built the most practical case. The researchers set up a summarization app that sent every user’s text to the real DeepSeek API through a single account, which they call “typical in enterprise LLM deployments”. All of that app’s users sat inside one account, so they shared a cache. The researchers could tell whether a given document had been uploaded by another user, with 89% average accuracy.
What to check
That last case is the one I’d act on, because the fix sits with whoever builds the app. The checks below are in the order I’d do them.
- Read your provider’s isolation sentence. Anthropic: “Different organizations never share caches, even if they use identical prompts.” OpenAI: “Caches are not shared across organizations”. Google Cloud: “cached content is never shared across projects”. All three stop at the customer’s organization, workspace or project. None promises a wall between your own end users by default.
- Count who sits inside your account. If your app serves many people through one account (organization, workspace or project), however many API keys you issue, they share whatever your provider caches for you. Putting the shared instructions first is not enough, because the cache keeps the longest matching opening, a user’s private text included. Stop the cache at the end of the shared part where the vendor lets you set that point, or give each user a separate cache key.
- Separate users where the vendor lets you. OpenAI documents separate cache keys that “help prevent cache-hit probing across users.” Google’s guidance for agents is to “scope cached prompt data and outputs to each user session.” The Stanford paper is blunter: “only per-user caching should be allowed” if you want no leakage at all.
- Check your own model servers. If you host models with SGLang, vLLM or similar and serve more than one tenant (separate customers or teams), find out whether prefix caches are shared between them. If they are, turn that off or give each tenant its own namespace.
- Treat semantic caches the same way. These reuse a stored answer when a new question merely resembles an old one. The Early Bird paper could tell when another user had asked about a chosen subject in GPTCache, with 95.4% accuracy after 5 tries. A semantic cache can also hand one user another user’s stored answer outright, with no timing needed, so scope it per user.
- Keep secrets out of system prompts. On a local test server, the same paper recovered system prompt text through timing with 89.0% average accuracy. A system prompt is configuration. Passwords, keys and customer data don’t belong in it.
Nobody has shown a full prompt stolen from a major provider. This still matters, because the weak spot has moved toward you. At least five providers changed their caching after the Stanford disclosure. The cross-user hole inside one customer’s app is yours to close. It’s the same pattern we describe in LLM security: the risks, the fixes and where to start, where the fix is to enforce access where the data lives, in code you control.
Sources (all checked 23 November 2026)
- Hugging Face Transformers documentation, “How caching works”: huggingface.co and “Cache strategies”: huggingface.co
- Anthropic, “Prompt caching”: platform.claude.com
- OpenAI, “Prompt caching”: developers.openai.com
- Google Cloud, context caching overview: docs.cloud.google.com
- Gu, Li, Kuditipudi, Liang, Hashimoto (Stanford University), “Auditing Prompt Caching in Language Model APIs”, 2025: arxiv.org/abs/2502.07776
- Wu et al. (Southern University of Science and Technology, ByteDance), “I Know What You Asked: Prompt Leakage via KV-Cache Sharing in Multi-Tenant LLM Serving”, NDSS 2025: ndss-symposium.org
- Song et al., “The Early Bird Catches the Leak: Unveiling Timing Side Channels in LLM Serving Systems”, 2024: arxiv.org/abs/2409.20002