What is the KV cache in an LLM, in plain English?
Short answer
The KV cache is the GPU memory a large language model uses to remember the prompt it has already read. For every token, each attention layer stores a "key" and a "value" vector, so the model only does new work for the newest token. It grows with every token, it is usually what limits how many users one GPU can serve, and when the engine managing it has a bug, one user can read another's data.

Part of the article Cloudflare's AI leak: 155 replies carried strangers' prompts. This page answers one question from it in depth.
On this page · 10 sections
TL;DR: Bytes per token = 2 x layers x KV heads x head size x 2 bytes (bf16). From the published configs: Qwen2.5-7B needs 56 KiB per token (1.75 GiB at 32,768 tokens), Mistral-7B 128 KiB, Qwen3-8B 144 KiB, Qwen2.5-72B 320 KiB (10 GiB at 32K). Cloudflare's GLM-5.3 deployment advertised 1,048,576 tokens of context on a KV pool that held 681,280, and 155 replies leaked other customers' text.
Every "1M-token context" on a spec sheet is really a promise about GPU memory. Once you can do the multiplication yourself, you can check that promise, size your own hardware, and stop paying for context you never use. We dug into this for Cloudflare's KV cache leak, and the math below is the tool you need.
Key numbers
| What | Number | Source (checked Oct 27 2026) |
|---|---|---|
| Qwen2.5-7B KV cache per token, bf16 | 56 KiB (28 layers, 4 KV heads, 128 dims) | config.json |
| Mistral-7B v0.1 per token | 128 KiB (32 layers, 8 KV heads) | config.json |
| Qwen3-8B per token | 144 KiB (36 layers, 8 KV heads) | config.json |
| Qwen2.5-72B per token / at 32,768 tokens | 320 KiB / 10 GiB | config.json |
| Cloudflare GLM-5.3: advertised context vs real KV pool | 1,048,576 vs 681,280 tokens | Cloudflare blog, Oct 26 2026 |
| Replies that carried other customers' data | 155 (Aug 27 to Sep 9 2026) | Cloudflare blog, Oct 26 2026 |
What the model is actually caching
A transformer reads text as tokens. In each attention layer, every token is turned into three vectors: a query (what am I looking for), a key (what do I contain) and a value (what I pass on). To write token 1,001, the model compares its query against the keys of tokens 1 to 1,000 and blends their values.
Keys and values for old tokens never change. So instead of recomputing them every step, the engine stores them once. That store is the KV cache. It is the reason generation is fast after the first token, and the reason the first token of a long prompt (the "prefill") takes the longest: that is when the whole cache gets built.
The formula you can run on any model
KV bytes per token = 2 (K and V) x layers x KV heads x head size x bytes per number
Pull num_hidden_layers, num_key_value_heads and the head size (head_dim, or hidden_size / num_attention_heads) out of any model's config.json. bf16 or fp16 is 2 bytes; an 8-bit cache is 1. Multiply by your context length and by how many requests run at once.
Computed by Tensorust from each model's Hugging Face config.json, Oct 27 2026.
Worked example: Qwen2.5-72B serving 8 users at 32,768 tokens each needs 8 x 10 GiB = 80 GiB of cache, on top of roughly 145 GB of bf16 weights. That is why long context is priced the way it is. The weights are a fixed cost; the cache is the per-user meter.
Why most models now share KV heads
Older models gave every attention head its own keys and values. Grouped-query attention (GQA) lets several query heads share one KV head. Mistral-7B has 32 query heads and 8 KV heads, so its cache is a quarter the size it would be otherwise.
Same model, same context: GQA cuts the cache 4x. Tensorust calculation from the published config.
The industry's answer to "long context is too expensive" has not been to give up on long context. It has been GQA, 8-bit and 4-bit caches, paged memory (vLLM's PagedAttention) and prefix reuse. Every one of those is an engineering win that made more context cheaper for everyone.
Where it goes wrong
Serving engines like vLLM and SGLang carve GPU memory into pages and hand them out across requests. Pages are not zeroed between customers because zeroing costs speed. That is fine as long as the bookkeeping is right.
At Cloudflare it was not. The prefill pool held 681,280 tokens; the product promised 1,048,576. Prompts over the line got swapped for a placeholder instead of an error, the decode side read pages that still held someone else's session, and the customer got an HTTP 200. The SGLang bug was public as issue #30233 from July 6 2026; the fixed release (0.5.20) shipped September 18. The full timeline and our PyPI version data are here.
Common misconceptions
"The context window is a model property."
The recordThe trained maximum is. What you actually get is whatever fits in the KV pool the operator allocated, which is computed at startup from leftover GPU memory.
"The KV cache is the same as prompt caching."
The recordThe KV cache lives for one request. Prompt caching keeps a repeated prefix's cache around between requests, which is why providers bill cached input tokens cheaper. See what prompt caching is.
"More context is always better."
The recordYou pay for every cached token in memory and latency. If your app never needs 128K tokens, capping it frees memory for more concurrent users.
What you can do now
- Open the
config.jsonof the model you run and compute bytes per token with the formula above. Multiply by your max context and your batch size, and compare it to the KV pool size your engine logs at startup. - If you self-host on SGLang, run
pip show sglangand upgrade to 0.5.22 or later, the version Cloudflare points to. - Set your server's max context to what the pool actually holds, and add a test that sends a prompt just over the limit and expects an error, not a 200.
- Paying a provider for long context? Read why 429 errors happen next. Want to know what your LLM bill should be? The AI Cost Audit prices it against your real usage.
Related
- Cloudflare's KV cache leak: 155 replies, 62 days of a public bug
- Rust is the future of AI
- The real AI risk is a quiet cascade
- What is prompt caching?
FAQ
What is the KV cache in simple terms?
It is the model's scratchpad of everything it has already read. For each token, every attention layer stores a key and a value vector so the model does not recompute them when it generates the next token. It sits in GPU memory and grows with every token.
How much memory does the KV cache use?
Bytes per token = 2 x layers x KV heads x head size x bytes per number. Qwen2.5-7B in bf16 uses 56 KiB per token, so 1.75 GiB for a full 32,768-token context. Qwen2.5-72B uses 320 KiB per token, 10 GiB at 32K.
Why does a longer context cost more?
The cache is linear in tokens and it must stay in fast GPU memory for the whole request. Memory, not math, usually caps how many users a GPU can serve at once.
Can the KV cache leak data between users?
It can if the serving engine has a bug. Servers pool cache pages across requests and do not wipe them for speed. Cloudflare reported 155 replies that carried other customers' data from Aug 27 to Sep 9 2026, traced to a pool smaller than the advertised context plus an SGLang bug.
What is the difference between the KV cache and prompt caching?
The KV cache lives for one request. Prompt caching keeps the KV cache of a repeated prefix around between requests so the provider can skip recomputing it, which is why cached input tokens are billed cheaper.
Sources
All checked October 27, 2026.
- Cloudflare, KV cache exposure remediation, Oct 26 2026: 155 affected replies, dates, 1,048,576 vs 681,280 tokens, SGLang issue #30233, fix and recommended version.
- Hugging Face model configs: Qwen2.5-7B-Instruct, Qwen2.5-72B-Instruct, Mistral-7B-v0.1, Qwen3-8B.
- Ainslie et al., GQA: Training Generalized Multi-Query Transformer Models, 2023.
- Kwon et al., Efficient Memory Management for LLM Serving with PagedAttention, 2023.
- SGLang release history on PyPI.
- Images: cards and charts by Tensorust.
Next question
Every number on this page links to its source and was checked on Oct 27, 2026. Spot an error or a newer figure? Email matt@tensorust.com and it gets fixed.
Read next
- Article · Oct 27, 2026Cloudflare's AI leak: 155 replies carried strangers' prompts
- Article · Oct 24, 2026Rust is the language AI will be written in. 13,243 crates agree
- Article · Oct 24, 2026The Real AI Risk Isn't Skynet: 7 Quiet Failures Already Running
- Guide · Oct 27, 2026How to search USAspending.gov for any company's federal contracts (and catch the resellers)
- Explainer · Oct 26, 2026What is an IPO lock-up expiration, and why do stocks drop around it?
Get the next one by email.
What my AI agents tried this week, with the real numbers: what worked, what flopped, what made money. One email when there is something worth reading.
No spam, never sold. Every email has a one-click unsubscribe.
