7 Inference and Serving
  1. In this chapter
  2. Your first inference service
  3. Decoding and Sampling
  4. KV Cache and Inference Performance
  5. Prefix Caching and PD Disaggregation
  6. Model Quantization
  7. LLM Inference Serving
  8. Continuous Batching and PagedAttention
  9. Speculative Decoding
All chapters →

Part 3 · Deploy and apply Chapter 7

Inference and Serving

Decoding and sampling, the KV cache, quantization, inference serving, continuous batching and speculative decoding: how to run LLMs fast and cheaply, and how to see the trade-offs between latency, throughput and memory.

8 pages about 3 h Chapter labs on GitHub ↗ Beginner → Intermediate Updated

After this chapter you will be able to

  • Say which part of the distribution temperature, top-k and top-p each change, and choose decoding parameters for different tasks.
  • Compute the KV-cache memory of a model and explain why it decides how many requests can run at once.
  • Compare INT8, INT4, GPTQ and AWQ, and read where the error comes from in a quantization experiment.
  • Describe a serving system with TTFT, TPOT and throughput, and explain how continuous batching and paged attention raise hardware utilization.
  • Explain why speculative decoding is lossless and what the speedup depends on.

If model endpoints are new to you, run Your first inference service to follow one request on your own machine.

Train once, run inference millions of times. In real businesses the cost of inference is often far higher than the cost of training, so the content of this chapter largely decides whether an LLM application can be profitable.

The chapter follows the path of a request: first what the decoding step actually does, then the cache that makes it faster, then quantization that makes the model smaller, and finally the serving systems and speculative decoding that put many requests onto the same hardware. Between cache and serving stands prefix caching and PD disaggregation: the former makes repeated compute across requests happen only once, the latter gives the two differently-shaped stages what each needs. The roofline and bandwidth ideas from Compute and Systems are the key to every optimization here: decoding is bandwidth-bound, so almost every optimization reduces the bytes that must be moved for each token.

Related paper explainers: vLLM and PagedAttention, FlashAttention and Speculative Decoding.

Chapter roadmap

  1. 7.1

    Your first inference service: put a model function behind HTTP

    Run a local inference endpoint, send JSON, and interpret status codes. Follow the model function, runtime, server, and client before learning streaming and deployment.

    Beginner 9 min read

    Prerequisites4.1LLM hardware basics

  2. 7.2

    Decoding and Sampling: Temperature, Top-k, Top-p and Beam Search

    Decoding turns the model's output distribution into tokens: greedy picks the most likely one, temperature sharpens or flattens the distribution, top-k and top-p cut off the unlikely tail, and beam search keeps several candidates. Together they set how deterministic and diverse the output is.

    Beginner 14 min read

    Prerequisites2.2From Language Modeling to Next-Token Prediction

  3. 7.3

    KV Cache and Inference Performance

    Understand how the KV cache removes repeated computation from autoregressive decoding, and master the trade-offs between latency, throughput, memory and request scheduling.

    Intermediate 18 min read

    Prerequisites3.2Attention Basics

  4. 7.4

    Prefix Caching and PD Disaggregation: Pay for Repeated Compute Once

    Prefix caching reuses the KV cache of system prompts, shared documents, and chat history across requests (RadixAttention, CacheBlend, LMCache). PD disaggregation places compute-bound prefill and bandwidth-bound decoding on different hardware; DistServe, Splitwise, and Mooncake show when it pays off.

    Intermediate 12 min read

    Prerequisites7.3KV Cache and Inference Performance

  5. 7.5

    Model Quantization: INT8, INT4, GPTQ and AWQ Explained and Measured

    Quantization stores weights, activations or the KV cache in fewer bits. This page starts from the memory budget, builds up from scales and groups, measures INT8 and INT4 on Qwen2.5-0.5B, implements SmoothQuant, AWQ-style scaling, GPTQ and KV-cache quantization, and ends with a selection guide.

    Intermediate 55 min read

    Prerequisites7.3KV Cache and Inference Performance

  6. 7.6

    LLM Inference Serving: Latency Metrics, Measuring, and Choosing an Engine

    A serving system has to balance time to first token, time per output token and total throughput. This page measures where one request's time goes with llama.cpp, shows why laptop benchmarks need a warm-up, and compares llama.cpp, Ollama, vLLM and SGLang.

    Intermediate 31 min read

    Prerequisites7.3KV Cache and Inference Performance7.1Your first inference service

  7. 7.7

    Continuous Batching and PagedAttention: How a Server Handles Many Requests at Once

    Every decode step reads all the weights, so putting several requests in one batch raises throughput almost for free. This page measures concurrency, continuous batching, paged KV caches and prefix caching with llama.cpp to show how one GPU serves many requests.

    Intermediate 35 min read

    Prerequisites7.3KV Cache and Inference Performance7.6LLM Inference Serving

  8. 7.8

    Speculative Decoding: Draft with a Small Model, Verify with a Large One

    Speculative decoding lets a small, fast draft model guess the next few tokens and the large model verify them in one parallel forward pass, with an accept-and-correct rule that keeps the output distribution unchanged. This page derives the expected speedup and lets you tune it interactively.

    Intermediate 15 min read

    Prerequisites7.2Decoding and Sampling7.3KV Cache and Inference Performance