Skip to content
ML to LLMs
Cheat sheets

LLM inference

KV-cache and decode-speed formulas, inference optimizations, serving techniques, latency metrics, and cost per token on one page.

KV cache: 2⋅L⋅nkv⋅dhead⋅b⋅T⋅B2 \cdot L \cdot n_{kv} \cdot d_{\text{head}} \cdot b \cdot T \cdot B bytes (K and V, every layer, every KV head, every token, every sequence). Uses nkvn_{kv}, never nhn_h.

Weights: P⋅bwP \cdot b_w. 8B parameters: 16 GB BF16, 8 GB FP8/INT8, 4 GB INT4 (plus scales).

Decode speed limit: steps/s ≤BW/(weight bytes+all KV bytes)\le \text{BW} / (\text{weight bytes} + \text{all KV bytes}); aggregate tokens/s =B×= B \times steps/s.

Arithmetic intensity: decode at batch BB ≈ BB FLOP/byte (BF16 weights); prefill of NN tokens ≈ NN. Compute-bound only above the ridge point F/BWF / \text{BW} (a few hundred on current accelerators).

Worked numbers (synthetic 8B-style GQA: L=32L = 32, nkv=8n_{kv} = 8, dhead=128d_{\text{head}} = 128, BF16; 80 GB, 3.0 TB/s):

QuantityValue
KV per token2⋅32⋅8⋅128⋅2=131,0722 \cdot 32 \cdot 8 \cdot 128 \cdot 2 = 131{,}072 B (128 KiB)
KV, 8k context, batch 1617.18 GB (MHA: 68.72; MQA: 2.15; FP8 KV: 8.59)
Max batch at 8k⌊(80−16)/1.074⌋=59\lfloor (80 - 16)/1.074 \rfloor = 59
Decode bound, batch 13000/16=187.53000 / 16 = 187.5 tokens/s
Decode bound, batch 16 at 8k90.4 tokens/s per user, 1,447 total
TechniqueSavesCosts or caveat
GQA / MQAKV memory by nh/nkvn_h / n_{kv}set at training; MQA can lose quality
Weight quantization (FP8, INT8, INT4)memory and decode timeaccuracy loss, larger at 4 bits; re-run evals
KV-cache quantization (FP8, 4-bit)KV memory, KV readslong-context recall most sensitive
FlashAttentionattention memory traffic, O(T2)→O(T)O(T^2) \to O(T) memoryexact; does not shrink the KV cache
Speculative decodingsequential target passesextra compute; helps at low batch and high acceptance
Mixture-of-expertscompute per token (active params)memory holds all experts
Continuous batchingidle slotsscheduler complexity
Paged KV (block tables)reserved-but-unused KV memoryunder one block of waste per sequence
Prefix cachingprefill for shared prefixesstable content must come first in the prompt
Chunked prefill / disaggregationdecode stalls behind long promptsslightly higher TTFT for the long prompt; KV transfer

Speculative decoding: E[tokens per target pass]=(1−αγ+1)/(1−α)E[\text{tokens per target pass}] = (1 - \alpha^{\gamma+1})/(1 - \alpha); speedup =E/(1+γc)= E / (1 + \gamma c). α=0.8\alpha = 0.8, γ=4\gamma = 4, c=0.05c = 0.05: 3.36/1.2=2.80×3.36 / 1.2 = 2.80\times.

Latency: TTFT = queue + prefill. TPOT (ITL) = mean gap between tokens. End-to-end = TTFT + TPOT ⋅(nout−1)\cdot (n_{\text{out}} - 1). Report p50 and p95, separately.

Little's law: requests in flight = arrival rate × time in system. Use it to size batch and KV memory.

Parallelism: replicas for throughput · tensor parallel when it doesn't fit or latency is too high (fast intra-node links) · pipeline parallel across nodes · expert parallel for MoE.

Cost: cost per million tokens = hourly GPU price ÷ (tokens/s × 3600 ÷ 1,000,000) ÷ utilization. $4/h at 1,500 tokens/s: $0.741 at 100%, $2.47 at 30%.

Lessons: LLM inference · Serving LLMs