Notes · Capacity planning

How much KV cache does a 128K agent session need?

A model can accept 131,072 tokens and still run out of cache when a second user connects. How much memory should you reserve for each conversation?

Short answer

For one 128K-token sequence, budget 16 GiB of BF16 KV for Llama 3.1 8B, or about 2.70 GiB for Gemma 4 26B-A4B with rolling local windows. DeepSeek-V4.1-Flash reports 111.25 MiB for the global-history component at that length. Local state and serving buffers need their own budget.

Count the positions each layer retains

When a transformer processes a token, an attention layer can keep its key and value vectors so later tokens can attend to that position. For BF16 keys and values of equal width, one position costs 2 vectors × 2 bytes × KV heads × head dimension. Use the number of KV heads here. Several query heads can share a KV head.

For Llama 3.1 8B, that gives 32 layers × 8 KV heads × 128 × 4 bytes = 128 KiB/token. At 131,072 tokens, the total is 16 GiB. For a sliding-window layer, multiply by the smaller of the history length and the retained window. For linear-attention layers, budget the recurrent and convolution state separately.

Four ways to store the same length of history

ModelOne 128K sequenceWhat the number covers
Llama 3.1 8B16 GiBBF16 K and V at every layer.
Gemma 4 26B-A4B2.70 GiB2.50 GiB for 5 global layers plus 200 MiB for 25 local layers retaining 1,024 positions each. Assumes separate BF16 K and V.
Qwen3.6-35B-A3B2.50 GiB + stateBF16 KV for 10 full-attention layers. Add recurrent and convolution state for the other 30 layers.
DeepSeek-V4.1-Flash111.25 MiB + local statePublisher’s 890 bytes/token for global history, including cross-layer reuse and FP4 main KV. Local windows and runtime buffers are additional.

The first three rows are architecture estimates. The last scales DeepSeek’s published figure to the same length. None measures whole-device memory. For example, allocating Gemma’s local layers at full sequence length would raise its estimate to 27.5 GiB. Derivations, checkpoint configs and more models · DeepSeek model card

Verify residency and reuse separately

Fix the model, cache dtype and concurrency. At 8K, 32K and 128K tokens, record occupied KV pages, page size and peak memory per device. Once local windows fill, divide the change in occupied bytes by the change in token count. A preallocated pool can keep process memory flat through all three runs.

Then send a tool result back on the same conversation. In a synthetic two-call replay on one DGX Spark, Gemma’s median follow-up contained 32,096 prompt tokens. All three trials reused 30,976 tokens, leaving a median of 1,120 uncached, including the new tool response. The preceding lookup call already contained the ticket ID; the tool supplied the queue ID. The complete next tool call took 1.66 seconds, compared with 5.89 seconds for the initial lookup (medians of three pairs). These are measured request durations with different work at each step. They do not establish a universal cache speedup. Replay protocol and results

For a fleet, sum the histories that are resident at the same time. Four independent 128K Gemma sessions need about 10.78 GiB of BF16 KV. Deduct a shared prefix only when the engine actually shares its physical pages. Reserve weights, workspace and communication buffers on top.

FAQ

Does a 4-bit weight file imply a 4-bit KV cache?

Weight precision and cache precision are separate runtime choices. Check the cache dtype, scale storage and attention backend before budgeting memory.

Related notes

Tracing a prefix-cache miss in Gemma 4 with MTP · Field notes from the tool-call replay · Request access