KV Cache Capacity Planner
Compute live-token capacity for an LLM deployment and turn VRAM into max concurrency under different prompt and output lengths.
- Lesson
- KV cache math
- Module
- Serving an LLM
- Objective
- Use KV-cache math to estimate concurrency and explain why long context changes serving capacity.
Prompt
You are deploying a model with:
layers: 32
kv_heads: 8
head_dim: 128
KV dtype: bf16
GPU memory: 48 GiB
weights and runtime overhead: 22 GiB
reserve for fragmentation/workspace: 4 GiB
Product wants to support:
- Chat: 2K prompt, 512 output.
- Long document QA: 16K prompt, 1K output.
- Agent traces: 8K prompt, 4K output.
Deliverable
Create a capacity plan:
| workload | tokens/request | KV GiB/request | rough max concurrent requests |
|---|
Then answer:
- Which workload is most dangerous for concurrency?
- What is the max live-token budget after weights, runtime overhead, and reserve?
- What admission control rule would you add before launch?
- What metric should alert before OOM?
Rules
- Use the KV formula from the lesson.
- Treat 1 GiB as
1024 ** 3bytes. - Do not use all theoretical capacity. Leave the stated reserve untouched.
Acceptance criteria
A good answer turns memory into product behavior:
- It does not say "the model fits" and stop.
- It explains why long document QA and agent traces need separate limits.
- It proposes a live-token or KV-memory admission rule.
Stretch
Recompute the table for a model with 4 KV heads instead of 8. Explain the product impact without using the phrase "it is faster" unless you can justify the mechanism.
Debrief
Serving capacity is live tokens, not vibes. Once you can compute the cache, you can argue about context windows like an engineer instead of a fortune teller.
Part B — Show the arithmetic
Bytes/token = 32 × 2 × 8 × 128 × 2 = 131072 = 128 KiB/token.
Free pool = 48 − 22 − 4 = 22 GiB = 22 × 2³⁰ bytes.
Live-token budget ≈ 22 × 2³⁰ / 131072 ≈ 180,224 tokens.
Fill the table with that budget (not 22 GiB / request_tokens guessed):
| workload | live tokens | KV/request | max seqs |
|---|---|---|---|
| Chat 2560 | 2560 | 2560 × 128 KiB ≈ 0.31 GiB | floor(180224/2560) ≈ 70 |
| Doc QA 17408 | 17408 | ≈ 2.12 GiB | ≈ 10 |
| Agent 12288 | 12288 | ≈ 1.50 GiB | ≈ 14 |
If your Part A numbers are not within ~10% of this, redo the bytes/token line before you invent admission rules.
Part C — Mixed load
The GPU does not run one product. It runs 40 chats (2560 tok) plus some Doc QA. How many Doc QA jobs fit in the remaining tokens? Write the inequality the scheduler should evaluate on every admission.