# 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.

- Published: 2026-11-23
- Topics: AI security, LLM security, Prompt caching
- Canonical page: https://thesecguy.com/notes/prompt-caching-kv-cache-timing-leaks/
- Publisher: The Sec Guy (https://thesecguy.com)

**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.

<figure class="fig">
<p class="fig-k">Work needed to write word 100 of an answer</p>
<div class="hbars">
<div class="hbar hot"><em>Without a cache: reprocess all 100 earlier words</em><div class="track"><i style="width:100%"></i></div><b>100</b></div>
<div class="hbar"><em>With a cache: write notes for the newest word only</em><div class="track"><i style="width:1%"></i></div><b>1</b></div>
</div>
<figcaption>Our illustration of the idea in the Hugging Face documentation: without a cache each step repeats the earlier work, with one it writes notes only for the new token. It still looks back at the saved notes.</figcaption>
</figure>

## 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](https://huggingface.co/docs/transformers/cache_explanation) 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.

<figure class="fig">
<p class="fig-k">Two requests that start the same way</p>
<div class="flow">
<div class="node"><b>Request A</b><span>Shared instructions, then A's question</span><div class="chips"><i class="chip">all processed fresh</i><i class="chip">notes saved</i></div></div>
<div class="node"><b>Request B</b><span>The same instructions, then B's question</span><div class="chips"><i class="chip">instructions reused</i><i class="chip">only B's part is fresh</i></div></div>
<div class="node"><b>Result</b><span>B answers faster and the reused part is billed at a discount</span><div class="chips"><i class="chip">cheaper</i><i class="chip">faster</i></div></div>
</div>
<figcaption>Our illustration of prefix caching as the vendor documentation describes it.</figcaption>
</figure>

## 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.

<figure class="fig">
<p class="fig-k">Stated discount on cached input tokens</p>
<div class="hbars">
<div class="hbar"><em>Anthropic (cache read = 0.1x base price)</em><div class="track"><i style="width:90%"></i></div><b>90%</b></div>
<div class="hbar"><em>Google Cloud (Gemini 2.5 or later)</em><div class="track"><i style="width:90%"></i></div><b>90%</b></div>
<div class="hbar hot"><em>OpenAI ("discounted up to 95%")</em><div class="track"><i style="width:95%"></i></div><b>≤95%</b></div>
</div>
<figcaption>Source: each vendor's prompt caching documentation, read 23 November 2026. Anthropic's 0.1x is its standard rate, with a few newer models priced lower still, and it charges 1.25 times the base price to write a 5-minute cache entry.</figcaption>
</figure>

## 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.

<figure class="fig">
<p class="fig-k">How a shared cache turns timing into a leak</p>
<div class="flow">
<div class="node"><b>1. Victim asks</b><span>Their prompt is processed and its notes go into the cache</span><div class="chips"><i class="chip">cache write</i></div></div>
<div class="node"><b>2. Attacker guesses</b><span>Sends a prompt that starts the way the victim's might</span><div class="chips"><i class="chip">same cache</i></div></div>
<div class="node"><b>3. Clock runs</b><span>A right guess is a cache hit and answers faster</span><div class="chips"><i class="chip">timing</i></div></div>
<div class="node"><b>4. Repeat</b><span>Each fast answer confirms a little more of the prompt</span><div class="chips"><i class="chip">many requests</i></div></div>
</div>
<figcaption>Our layout of the attack described by Gu et al. (Stanford) and Song et al.</figcaption>
</figure>

## What the research showed

Five Stanford researchers [tested 17 API providers](https://arxiv.org/abs/2502.07776) 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.

<figure class="fig">
<p class="fig-k">The Stanford audit in three numbers</p>
<div class="tiles">
<div class="tile"><b>17</b><span>API providers audited</span></div>
<div class="tile"><b>8</b><span>had prompt caching the test could detect</span></div>
<div class="tile hot"><b>7</b><span>shared one cache across all users</span><small>before disclosure</small></div>
</div>
<figcaption>Source: Gu, Li, Kuditipudi, Liang and Hashimoto, "Auditing Prompt Caching in Language Model APIs", Stanford University, 2025.</figcaption>
</figure>

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](https://www.ndss-symposium.org/ndss-paper/i-know-what-you-asked-prompt-leakage-via-kv-cache-sharing-in-multi-tenant-llm-serving/) 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"](https://arxiv.org/abs/2409.20002), 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.

1. **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.
2. **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.
3. **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.
4. **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.
5. **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.
6. **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](https://thesecguy.com/notes/llm-security-guide/), 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](https://huggingface.co/docs/transformers/cache_explanation) and "Cache strategies": [huggingface.co](https://huggingface.co/docs/transformers/kv_cache)
- Anthropic, "Prompt caching": [platform.claude.com](https://platform.claude.com/docs/en/build-with-claude/prompt-caching)
- OpenAI, "Prompt caching": [developers.openai.com](https://developers.openai.com/api/docs/guides/prompt-caching)
- Google Cloud, context caching overview: [docs.cloud.google.com](https://docs.cloud.google.com/vertex-ai/generative-ai/docs/context-cache/context-cache-overview)
- Gu, Li, Kuditipudi, Liang, Hashimoto (Stanford University), "Auditing Prompt Caching in Language Model APIs", 2025: [arxiv.org/abs/2502.07776](https://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](https://www.ndss-symposium.org/ndss-paper/i-know-what-you-asked-prompt-leakage-via-kv-cache-sharing-in-multi-tenant-llm-serving/)
- Song et al., "The Early Bird Catches the Leak: Unveiling Timing Side Channels in LLM Serving Systems", 2024: [arxiv.org/abs/2409.20002](https://arxiv.org/abs/2409.20002)
