Unit 5 · Evaluation, Safety and Deployment
Lesson 20 · Available to read
Serving LLMs at Scale and the Road Ahead
Making models fast and affordable — KV caches, quantization, batching — plus multimodality and the open frontiers.
A trained model is only useful if people can run it quickly and affordably — often for millions of requests a day. This final lesson covers how LLMs are served at scale, how they're extending beyond text, and the open frontiers that connect everything in this course.
Two phases of a request
Every request runs in two phases with very different characteristics:
- Prefill — the whole prompt is processed in one parallel pass, building up the KV cache. It's compute-bound (big matrix multiplications) and determines the time to first token (TTFT).
- Decode — the response is generated one token at a time. Each step does little arithmetic but must read all the model's weights and the KV cache from memory, so it's memory-bandwidth-bound. It determines tokens per second.
A rough upper bound for single-user decode speed: tokens/second ≈ memory bandwidth ÷ bytes read per token. A 70B model in 16-bit (≈140 GB) on hardware with 3 TB/s of bandwidth tops out around 3,000 ÷ 140 ≈ 21 tokens per second for one user — which is why batching and smaller weights matter so much.
Key serving metrics: TTFT, tokens per second, throughput (tokens per second across all users), and cost per million tokens.
The KV cache
During decoding, every new token attends to all previous tokens' keys and values (Lesson 6). Recomputing them at each step would be wasteful, so servers store them in a KV cache and compute keys and values only for the new token.
The catch is memory: the cache grows with context length, layers and heads. With 32 layers, 8 key-value heads of dimension 128 and 16-bit numbers, it's about 128 KB per token — around 4 GB for a single 32K-token conversation. Techniques to tame it include grouped-query attention (fewer KV heads, Lesson 7), paged memory management that allocates cache in small blocks like an operating system, quantizing the cache, and prompt caching — reusing the cache for a shared prefix (such as a long system prompt or document) across many requests, which cuts both latency and price.
Batching
A GPU decoding for one user leaves most of its compute idle while it waits on memory. Serving many users at once — batching — reads the weights once per step and uses them for every sequence in the batch, multiplying throughput. Continuous batching adds and removes requests from the running batch at every step instead of waiting for a whole batch to finish. The trade-off: larger batches raise throughput but can increase each user's latency.
Making models smaller and faster
- Quantization stores weights (and sometimes activations and the KV cache) in fewer bits — 8 or 4 instead of 16 — cutting memory and speeding up the memory-bound decode phase, usually with a small quality loss.
- Distillation trains a smaller "student" model to imitate a larger "teacher", keeping much of the quality at a fraction of the cost.
- Mixture of experts (MoE) replaces each MLP with many expert MLPs plus a router that sends each token to only a few of them. The model can have a very large total parameter count while each token uses only a small active fraction, so compute per token stays modest (memory still has to hold all the experts).
- Speculative decoding uses a small, fast draft model to propose several tokens; the large model checks them all in one parallel pass and keeps the ones it agrees with. With a correct acceptance rule, the output follows the same distribution as the large model alone — just faster.
Multimodality
The transformer isn't limited to text. To process images, a model splits each image into small patches, turns each patch into an embedding (as in vision transformers), and feeds those embeddings into the same sequence as the text tokens. Audio can similarly be converted into token-like embeddings. Once everything is a sequence of vectors, attention can relate a word in the question to a region of a chart or a moment in a recording. Many current models read images and documents natively; some also produce audio or images, often with specialised generation components alongside the language model.
The road ahead
Each lesson in this course is a pillar that today's systems stand on. The open frontiers draw on all of them:
- Longer-horizon agents — completing tasks that take hours or days, which demands far higher per-step reliability (Lesson 16), better memory and context management (Lesson 7) and robust evaluation (Lesson 17).
- Efficiency — better architectures, data (Lesson 10) and serving techniques that make capable models cheaper, faster and greener.
- Reasoning and test-time compute — models that think longer on harder problems (Lessons 8 and 14).
- Reliability and honesty — fewer hallucinations, better calibration and grounding (Lessons 15 and 18).
- Understanding and alignment — interpretability that can verify what models are doing, and alignment methods that hold up as capability grows (Lessons 12 and 19).
If you've worked through the tree, you now have the vocabulary and the mental models to follow — and contribute to — that frontier. Head back to the tech tree and mark this final node as researched.
The takeaway
Serving is its own engineering discipline: prefill and decode have different bottlenecks, the KV cache trades memory for speed, quantization, batching, mixture-of-experts and speculative decoding cut cost, and the same transformer machinery now extends to images and audio — while longer-horizon agents, efficiency, reliability and alignment define the road ahead.
Practice
0 of 7 attempted
- 1Recall
Name the two phases of an LLM request, what bottleneck each has, and which latency metric each determines.
Saved in this browser
- 2Calculate
Estimate the KV cache size per token for a model with 48 layers, 8 KV heads, head dimension 128 and 16-bit values. How much memory does a 100,000-token context need?
Saved in this browser
- 3Calculate
A 13B-parameter model is stored in 4-bit precision. Roughly how much memory do its weights need? If the hardware has 1 TB/s of memory bandwidth, what is the approximate upper bound on single-user decode speed?
Saved in this browser
- 4Multiple choice
Why does batching increase throughput during decoding?
- 5Explain in your own words
Explain how speculative decoding speeds up generation without changing the output distribution.
Saved in this browser
- 6Explain in your own words
What is the difference between total and active parameters in a mixture-of-experts model, and why does it matter?
Saved in this browser
- 7Apply
Capstone: write a one-paragraph explanation, for a non-technical friend, of how a chatbot answers a question — from tokenization to the final word. Try to touch at least eight concepts from this course.
Saved in this browser