AI Infra Interviews logo
← Napkin Math, Cost & Capacity / 02
easy★ EssentialOpenAIAnthropicBaseten

How big is the KV cache for Llama 3.1 70B at a 128k context?

Calculate Llama 3.1 70B KV cache memory at 128k context using eight KV heads. Worked BF16 and FP8 payloads, GB versus GiB, and per-GPU capacity checks.

Updated Sep 2026 · Learn how AI infrastructure works through explanations, worked examples and diagrams, then practise applying it to interview questions.

TL;DR: With a BF16 cache and 131,072 retained positions, Llama 3.1 70B stores 2 × 80 × 8 × 128 × 2 × 131,072 = 42,949,672,960 payload bytes: 40 GiB, or about 42.95 GB. Use its eight KV heads, not its 64 query heads. Physical allocation and placement still need checking before turning that number into a user limit.

How to approach it

Clarify what 128k means and which precision the cache uses. State the KV Cache Sizing formula with each variable named, calculate bytes per position, then multiply by retained positions. Finish with the distinction between this one sequence's payload and what each GPU must allocate.

A strong answer

Imagine a team has already loaded the model and now wants to enable long conversations. The weight footprint did not grow, but each conversation brings more attention history. That history can consume tens of GiB before the answer has finished.

The KV Cache holds keys and values for consumed token positions. For this model's uniform full-attention layers, each position stores two vectors per KV head per layer. Grouped-query attention shares those KV heads across query heads; the cache does not store a separate copy for every query head.

NVIDIA's published Llama 3.1 70B configuration supplies the architecture dimensions. BF16 cache storage is our explicit runtime assumption, independent of that checkpoint's weight format.

InputValue
Layers80
KV heads per layer8
Elements per K or V head128
Bytes per cached value, BF162
Retained positions, our definition of 128k131,072
per position = 2 (K and V) × 80 × 8 × 128 × 2 bytes
             = 327,680 bytes
             = 320 KiB

one sequence = 327,680 × 131,072
             = 42,949,672,960 bytes
             = 40 GiB exactly
             ≈ 42.95 GB decimal

Sanity check: 131,072 is sixteen times 8,192. The same configuration at 8,192 positions uses 2.5 GiB, and 16 × 2.5 = 40 GiB. The 128k count includes retained prompt and generated-token state; it is not an extra output allowance on top of a full prompt. The configured context limit and any needed output space are separate admission constraints.

8,192 positions2.5 GiB of BF16 cache 131,072 positions40 GiB of BF16 cache 16× the positions → 16× payloadSame scale; weights excluded.

Query heads read shared state; they do not each require a duplicate stored pair. The boxes show the eight stored head groups, not eight GPUs until the stated sharding assumption is applied.

Eight stored KV heads, 64 queriesOne KV head serves 8 query headsStore its K and V once per position.KV 0KV 1KV 2KV 3KV 4KV 5KV 6KV 7Repeat across 80 layers and131,072 retained positions.BF16 payload: 40 GiB in total.Eight equal unreplicated shards:5 GiB per rank per sequence.

At one byte per cached value, the corresponding FP8 value payload is 20 GiB. Scales and other storage are additional. Changing only the weights to FP8 leaves this BF16 cache calculation unchanged. vLLM's quantized-cache documentation also makes clear that scaling and backend support matter; measure task quality rather than treating reduced precision as a free capacity switch.

To turn the model-wide payload into a capacity estimate, suppose each of eight ranks has 48 GiB left for KV after other allocations and headroom. Also assume the engine places one of the eight KV heads on each rank, with no duplication. Each full-length sequence then needs 40 / 8 = 5 GiB on every rank. Nine fit within each 48 GiB budget; ten would need 50 GiB. Dividing the total is valid here because the storage layout and equal rank budgets were stated explicitly.

That is a hypothetical budget, not an benchmark. Confirm the cache layout and the most constrained rank. If one rank has only 43 GiB, the ceiling falls to eight even if the fleet's total free memory looks sufficient. Available cache blocks, preemptions and per-rank allocation reports are more useful evidence than a fixed reserve percentage.

PagedAttention changes how much unused space is reserved, not the value payload at a fully retained length. Compatible shared prefixes can reduce unique physical storage; block padding or replicated state can increase it. For an unshared sequence at 131,072 positions, 16-position blocks have no partial-tail waste because the length is an exact multiple of 16.

I would consider a lower-precision cache if cache capacity is the binding constraint and quality checks pass. I would reverse that choice if latency or quality becomes the limiting factor. A cache that fits does not prove a 128k prompt can be processed or decoded within the product's time budget.

What interviewers probe next

  • “What happens if you use 64 heads?” The estimate rises eightfold to 320 GiB. Those are query heads, so they do not belong in this model's KV-head term.
  • “What if 128k means 128,000?” Compute it explicitly: 327,680 × 128,000 = 41,943,040,000 bytes = 39.0625 GiB. State the interpretation rather than silently rounding.
  • “Can I divide the result by any GPU count?” Only if the actual layout shards the retained state evenly without replication. Check the backend, tensor-parallel degree and layer placement.
  • “Does the same formula apply to MLA?” Not to a compressed latent cache. Count its stored representation; KV Cache Sizing shows the alternative calculation.

Common mistakes

  • Saying 320 KB when the exact result is 320 KiB, or comparing unconverted units.
  • Reading cache precision from the weight-format label.
  • Reserving the whole supported context for every short request without checking allocation behavior.
  • Treating payload capacity as a throughput or latency guarantee.

Key takeaways

  • The stated BF16 configuration needs 320 KiB per retained position and 40 GiB at 131,072 positions.
  • Eight KV heads matter; substituting 64 query heads creates an eightfold error.
  • An evenly sharded eight-rank layout needs 5 GiB per rank per full-length sequence before extras.
  • Derive the payload, then verify precision, allocation, placement and the workload's latency target.
You can read the first 10 answers per topic without an account. Signing in increases that to 20, includes lessons marked Plus, and saves your progress so you can return to topics you want to review.no card · Google sign-in · nothing to cancel
HOW DID IT GO?
0
READING SIGNED OUT

Signing in doubles your free answers, from 10 to 20 per topic, and the site starts remembering you: mastery per topic, bookmarks, and a next-focus recommendation. Free, no card.

Sign in free

Work through the numbers

Change the assumptions in a free calculator, then explain what the result establishes and what still needs measurement.

UP NEXT ON YOUR JOURNEY
FEDITOR'S NOTE

Name the cache format, retained length and KV-head count; check units and the actual per-rank layout before predicting concurrency.

DISCUSSION · 0

No comments yet — be the first to share your approach.