1:1 mentoring with Big Tech AI engineers
System Design

Inference Optimization

The single-GPU arithmetic of LLM serving: prefill and decode as two clocks, the memory-bandwidth ceiling, KV cache sizing, batching, quantization and speculative decoding, and how to match a lever to the bottleneck.

Last updated

Production12 min readFirst readHow LLMs Are BuiltRate Limiting & Cost

After this section you can

  • Decompose a latency complaint into time to first token and per-token decode.
  • Size the KV cache for a concurrency and context target.
  • Choose the lever that matches the bottleneck, and say what it costs.
19

Inference Optimization

Read a slow reply as two clocks, size the KV cache for a concurrency target, and pick the lever that matches the bottleneck.

Key idea

Decode reads every weight for every output token, so one user's speed is memory bandwidth divided by the bytes moved per token. Prefill sets time to first token; decode sets the rest. Each lever either moves fewer bytes per token or shares one weight read across more tokens.

One decode step re-reads all 70 GB of weights to make one token
ONE DECODE STEP, ONE USER 3.35 TB/s ÷ 70 GB ≈ 48 tok/s Weights in HBM 70 GB at FP8 re-read in full The model about 2 FLOPs per weight One token out about 48 a second every token one token KV cache one row per token reads, appends

Blue input, amber model, green output, purple KV state. Batch 1: attention reads the whole KV cache each step. Pans sideways on a phone.

Related

More in System Design

Get full access to all 149 sections with code examples, diagrams, and interactive animations.

Unlock Premium