Email me

Explainer · 6 min read

Filed under LLMs AI industry

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.

Card: What is the KV cache? The model's memory of your prompt. Bytes per token equal 2 times layers times KV heads times head size times 2 bytes. Qwen2.5-7B: 56 KiB per token, 1.75 GiB at 32K tokens. Qwen2.5-72B: 320 KiB per token, 10 GiB at 32K tokens. Cloudflare advertised 1,048,576 tokens on a 681,280-token pool.
One formula tells you how much GPU memory a long prompt eats. Card by Tensorust from Hugging Face model configs and Cloudflare's Oct 26 2026 write-up.

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
  1. Key numbers
  2. What the model is actually caching
  3. The formula you can run on any model
  4. Why most models now share KV heads
  5. Where it goes wrong
  6. Common misconceptions
  7. What you can do now
  8. Related
  9. FAQ
  10. Sources

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.

Bars: KV cache per token in bf16. Qwen2.5-7B 56 KiB, Mistral-7B v0.1 128 KiB, Qwen3-8B 144 KiB, Qwen2.5-72B 320 KiB. 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.

Bars: Mistral-7B at 32,768 tokens. With 8 KV heads (GQA, as shipped) the cache is 4 GiB; with 32 KV heads (no GQA) it would be 16 GiB. 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

Myth

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

Myth

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

Myth

"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

  1. Open the config.json of 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.
  2. If you self-host on SGLang, run pip show sglang and upgrade to 0.5.22 or later, the version Cloudflare points to.
  3. 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.
  4. 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.

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.

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

Back to The Manual →   All articles →

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.

Up next

The article this answer comes fromCloudflare's AI leak: 155 replies carried strangers' promptsCloudflare's GLM-5.3 leaked other users' prompts in 155 replies. The SGLang bug sat public 62 days, and only 0.8% of SGLang downloads run the fix.Read it next →

Art: Nicholas Roerich, Depsang Plains (1925-26). Public domain, via Wikimedia Commons.

Get the receipts 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.

Did this answer your question?

Popular in the shop

All 124 tools →

Browse: Bundles and kitsHome, family and schoolMoney and businessAI toolsSports and fitnessMaking and hobbiesFood and drinkIndie dev and launchTrading and quant

Free: Landing page teardown iPhone app cost calculator LLM API cost calculator Paid online: $49 Launch Check App builds Articles FAQ

Disagree, or have a number to add? Discuss this on The Floor →

What are people saying about this? Read and post on The Floor →