The KV cache is the real context window
Long context is a memory budget, not a model feature. The arithmetic behind the cache, and why 128k on one card is usually a promise about someone else’s hardware.
Every few months a model card announces a bigger context window, and every few months someone tries to serve it on a card that cannot hold the cache. The weights get the attention; the cache decides what you can actually run.
In a decoder-only transformer, every generated token attends to the keys and values of all previous tokens in every layer. To avoid recomputing them, inference stores them. That is the KV cache. For one sequence, its size is:
cache_bytes = 2 × layers × kv_heads × head_dim × tokens × bytes_per_element
The 2 is for K and V. The batch dimension multiplies everything. It is a simple formula with an unpleasant result.
The arithmetic
The useful numbers are per token, for fp16:
| Model | Layers | KV heads | Head dim | Cache/token | 32k tokens |
|---|---|---|---|---|---|
| Llama 2 7B | 32 | 32 | 128 | 512 KiB | 16 GiB |
| Llama 3 8B | 32 | 8 | 128 | 128 KiB | 4 GiB |
| Llama 2 70B | 80 | 8 | 128 | 320 KiB | 10 GiB |
These are rough; alignment and allocator overhead push them up. The point is the scale. 512 KiB per token does not sound like much until you multiply by 128,000. That is 64 GiB for a single sequence, before weights, activations, or batching. Llama 2 7B’s weights in fp16 are about 13.5 GB; the cache for a 128k prompt is nearly five times the model.1
GQA helps. Llama 3 8B has 8 KV heads instead of 32, so the cache is 128 KiB per token. At 128k tokens that is 16 GiB — roughly the size of the weights, and still per sequence. GQA buys a factor, not a free 128k.
Prefill and decode are different problems
Prefill processes the prompt in parallel. It is usually compute-bound: there is a lot of matrix multiplication and the whole prompt is available at once. Decode produces one token at a time and is usually memory-bandwidth-bound: each step reads the weights and the cache, and produces one token. Batching raises throughput, but every sequence brings its own cache. A batch of eight at 32k tokens multiplies the table by eight.
This is why serving long context is less about FLOPs and more about memory allocation. The PagedAttention paper, which is on the reading list, treats the cache like virtual memory: fixed-size blocks, a page table, no contiguous allocation per sequence. It reduces fragmentation and makes prefix sharing practical. It does not make the cache smaller. A unique 100k-token document still needs the blocks.
Prefix caching is the other useful trick. If many requests share the same system prompt, the cache for that prefix can be reused. If every request is a different long document, it does nothing. The workload decides which trick matters.
What to check before believing a context number
- Compute the cache per token, multiply by context length and batch size, then add weights and 20–30% overhead. Compare that to VRAM, not to the model card.
- Read the config: layer count, KV heads, head dimension, and whether the attention is full or sliding-window. The context length on the card is an upper bound, not a default.
- Check the serving stack’s
max_model_lenand maximum batched tokens. A 128k model often runs at 8k or 16k by default because that is what fits with a reasonable batch. - Remember that the cache is per sequence. Four concurrent users at 32k tokens cost four times one user at 32k tokens.
A 24 GB card with a 7B fp16 model has roughly 8–9 GB left after weights and overhead. At 512 KiB per token that is about 16k tokens total across the batch, not 128k. Quantize the weights and you get more room; quantize the cache and you trade quality.2 Either way, do the arithmetic first.
Context length is a property of the model. The context you can serve is a property of the machine and the scheduler. Keep the two numbers separate.
- Sliding-window and hybrid attention change the sum: some layers keep only a window, and MLA compresses the stored keys and values. The budget question stays the same. ↩
- Cache quantization changes the constant, not the trend. Measure the quality cost on your own task; a benchmark average will not tell you whether your retrieval or your long-document question still works. ↩