New Tool Radar

AI Concepts

How to Estimate KV Cache Memory for a Long-Context Request

Work out how much GPU memory one long-context request needs for its KV cache, before it crowds out the model itself.

Schematic illustration with three step boxes: find the per-token cost, multiply by the context, and count the users.

The KV cache is the model’s scratchpad of everything it has already read: every layer stores a key and a value vector for every token, so the cache grows with context length. For Llama 3.1 8B it costs 128 KiB per token, which means one 128,000-token request needs 16 GiB, more than the 15 GiB model weights. When you self-host a long-context model, KV cache memory, not model weights, caps your concurrency.

Before you start

The model’s config file with layer count, KV heads and head size, or a published per-token figure, plus the context length you must serve.

Find the per-token cost

Work it out from the config: 2 times layers times KV heads times head size times bytes per number. The factor of 2 is for one key and one value vector, and FP16 uses 2 bytes per number.

Or use a published figure: Llama 3.1 8B costs 128 KiB per token, and grouped-query attention shrinks the cache 4 times on the 8B and 8 times on the 70B. Without it, one 128,000-token user would need 64 GiB.

Multiply by the context length

Multiply the per-token cost by the longest context you must serve. At 128,000 tokens, Llama 3.1 8B needs 16 GiB for one user; at 4,000 tokens it needs about 0.5 GiB.

Do the same for the output: the cache grows as the model generates, so the longest total length, prompt plus output, is the number that matters.

Count the concurrent users

Divide the free VRAM by the per-request cache to get the maximum concurrent users. On one 80 GB card, the published arithmetic fits 119 users at 4,000 tokens and only 3 at 128,000 tokens.

If the count is too low, shorten the context, quantize the cache, or add a card. Do not discover the limit in production.

Practice check: fictional Maple Corner server, Llama 3.1 8B, 80 GB card

The table below is fictional practice material, worked out by hand from the stated per-token cost of 128 KiB. It is the expected result for the practice server, not a run of any GPU.

Context per user KV cache per user Max users on 80 GB Verdict
4,000 tokens 0.5 GiB 119 Pass
32,000 tokens 4.0 GiB 16 Pass
128,000 tokens 16.0 GiB 3 Pass, only 3 users

One 128,000-token user costs 16 GiB, more than the model weights, and only 3 such users fit on the card. The cache, not the weights, sets the limit here.

Expected result. UNTESTED: the figures in the practice table are worked out by hand from the stated per-token cost, not measured by running any GPU. Work out the per-token cost from your own model’s config, multiply by your longest context, and count the users before you deploy.

Limits

  • The formula gives the cache only: weights, activations and fragmentation all need memory too.
  • Grouped-query attention changes the per-token cost a lot, so use the figure for your exact model, not for another size.

Sources

See all