Introduction

By Ludovico Bessi, Senior ML Engineer at Google ยท Updated

Why serving large language models is a systems problem, and how this book is organized.

Inference Is Where the Money Goes

Training a frontier model is a large, one-time cost. Serving it is a cost you pay on every request, forever. Once a model is popular, cumulative inference compute overtakes training compute, and every percent of serving efficiency turns directly into margin, latency, or the ability to serve a bigger model on the same hardware.

That is why most of the engineering effort around LLMs in production goes into inference: fitting models into GPU memory, keeping GPUs busy, cutting the latency users feel, and doing all of it without changing what the model outputs.

What Makes LLM Inference Different

A classic deep learning model โ€” an image classifier or a ranking model โ€” does one forward pass per request. An LLM does one forward pass per generated token, and each pass depends on the previous one. Three properties follow from this:

  • It is sequential. You cannot compute token 50 before token 49. Generation of a 500-token answer means 500 dependent forward passes.
  • It is memory-bound. Generating one token for one request reads every model weight from GPU memory and does very little math with each byte. The bottleneck is memory bandwidth, not FLOPs.
  • It carries state. Each request accumulates a KV cache that grows with every token and must stay in GPU memory until the request finishes. That state, not the weights, often decides how many requests a GPU can serve at once.

Almost every technique in this book attacks one of these three properties.

The Two Phases of a Request

Every LLM request has two phases with very different performance profiles:

Prefill Decode
What happens Process the whole prompt in one pass, build the KV cache Generate output tokens one at a time
Parallelism All prompt tokens at once One token per request per step
Bottleneck Compute (large matrix multiplications) Memory bandwidth (reading weights and KV cache)
User-facing metric Time to first token (TTFT) Time per output token (TPOT), a.k.a. inter-token latency

Good serving systems treat these phases differently, and the most advanced ones run them on different machines (see Batching Techniques).

The Metrics That Matter

  • Time to first token (TTFT): how long the user waits before anything appears. Dominated by queueing plus prefill.
  • Time per output token (TPOT): how fast text streams once it starts. Dominated by decode.
  • Throughput: tokens per second across all requests on a GPU. This drives cost per token.
  • Goodput: throughput counting only requests that met their latency targets. It is the honest version of throughput.

These pull against each other. Bigger batches raise throughput but slow every individual request. The art of serving is choosing the point on that curve your product needs.

A Mental Model: The Roofline

A GPU has a peak compute rate (FLOPs per second) and a peak memory bandwidth (bytes per second). The ratio between them is the arithmetic intensity a workload needs to be compute-bound. An H100 SXM offers roughly 989 TFLOPS of dense BF16 compute and 3.35 TB/s of HBM bandwidth, so a kernel needs on the order of 300 FLOPs per byte loaded to use all of its compute.

Decoding one token for a single request does about 2 FLOPs per parameter and loads 2 bytes per parameter (in FP16/BF16): an arithmetic intensity of about 1. The GPU spends almost all its time waiting for memory. This one number explains why batching, quantization and KV cache tricks are so effective: they either reuse each loaded byte for more work, or load fewer bytes.

A quick consequence: a 7B model in BF16 is about 14 GB of weights. At 3.35 TB/s, just reading them once takes about 4 ms, so a single request can never decode faster than roughly 240 tokens per second on one H100, no matter how fast the math is.

How This Book Is Organized

Each chapter explains the problem first, then the techniques, then when to use them. The goal is that you can look at a serving bottleneck and know which lever to pull.

Key Takeaways

  1. LLM inference is sequential, memory-bandwidth bound, and stateful. Most optimizations target one of these.
  2. Prefill is compute-bound and drives time to first token; decode is memory-bound and drives streaming speed.
  3. Throughput and per-request latency trade off; measure goodput against your latency targets.
  4. The roofline model โ€” FLOPs per byte โ€” explains why batching and quantization work.