Batching & Serving

Layer 2 · Mechanics

Batching & Serving

The mechanics of continuous batching, paged KV cache memory, and the prefill/decode split.

8 min read70 XP

  1. Prefill

    process the whole prompt at once

  2. Populate KV cache

    one entry per prompt token

  3. Decode loop

    generate one token per step

  4. Append new KV entries

    cache grows by one per step

Two distinct phases with very different compute profiles.

Serving splits into two phases with opposite bottlenecks. Prefill processes the entire prompt in one parallel forward pass — it's compute-bound, since all prompt tokens' matmuls can run together. Decode generates one new token at a time, each step only computing for a single new token per request — it's memory-bound, since the whole KV cache and weight set must be read for very little new arithmetic.

Go deeper: L3 Code