Skip to content
AI.info

Generative AI

Serving LLMs: Prefill, Decode, KV Cache, and Batching

Explain the main phases and resource constraints of LLM inference and connect them to batching, caching, scheduling, and optimization decisions.

By the end you can

Visual

One request has several performance phases

An LLM request is not one workload with one bottleneck. Optimising the wrong part of it can leave user-visible latency exactly where it was. Production traffic says so. Pratyush Patel and six coauthors measured real inference traffic and opened their abstract with the finding: “Based on our extensive characterization, we find that there are two main phases during an LLM inference request: a compute-intensive prompt computation, and a memory-intensive token generation, each with distinct latency, throughput, memory, and power characteristics.” Two phases, four ways of differing. That is the whole design problem.

The consequence was measured, not asserted. Token generation underutilises compute so badly that the authors moved it onto separate, cheaper, power-capped machines. They called the system Splitwise, and published it in 2024. Against a Baseline-H100 cluster it delivered 1.4x higher throughput at 20 percent lower cost, or 2.35x more throughput at the same cost and power budgets. Microsoft's own evaluation ran on a DGX-H100 variant with all GPUs power-capped to half the maximum power, on Llama-2-70B and BLOOM-176B code and conversation traces.

A request therefore passes through phases with different bottlenecks. Admission and queueing come first: scheduling, quotas, routing and available accelerator capacity decide when work starts. Then prefill, the compute-intensive prompt computation that builds the attention state. Then first-token emission, when the client sees something. Then the decode loop, the memory-intensive phase that generates tokens sequentially while reusing that state. Tool or retrieval pauses interrupt generation and create further prefill and decode phases. Completion and cleanup release or persist caches, slots, traces and billing records. If prompt computation and token generation differ in latency, throughput, memory and power, a fix aimed at one of them is not a fix for the other.

FigureProcess · 6 steps
  1. 1

    Admission and queue

    Scheduling, quotas, routing, and available accelerator capacity determine when work starts.

  2. 2

    Prefill

    The model processes the input context and creates attention state for generation.

  3. 3

    First-token emission

    The system returns an initial token or structured result to the client.

  4. 4

    Decode loop

    Tokens are generated sequentially while previous attention state is reused.

  5. 5

    Tool or retrieval pauses

    External work can interrupt generation and create additional prefill or decode phases.

  6. 6

    Completion and cleanup

    Caches, slots, traces, and billing records are released or persisted.

Comparison

Latency and throughput metrics answer different questions

A service can improve one metric while making another worse, and one paper puts both numbers on the same model. Reiner Pope and nine coauthors closed their abstract with the pair: “Finally, we achieve a low-batch-size latency of 29ms per token during generation (using int8 weight quantization) and a 76% MFU during large-batch-size processing of input tokens, while supporting a long 2048-token context length on the PaLM 540B parameter model.” Read that as two settings, not one. The 29 ms per token needs a low batch size; the 76% model FLOPS utilization needs a large one. Both were measured on PaLM 540B running on TPU v4 slices, and the work appeared at MLSys in 2023. The same paper showed that multiquery attention's lower KV memory enables up to 32x larger context lengths, which is a memory result, not a latency one. Latency and utilisation sit on a frontier. There is no single configuration that wins both.

The thresholds a serving team is held to are equally specific and equally workload-dependent. MLCommons tightened them in MLPerf Inference v5.0, with results published on 2 April 2025. Thomas Atta-Fosu and four coauthors explained the new Llama 2 Chat 70B Interactive bar for MLCommons: “To prioritize reliability under peak demand, MLPerf adopts a stricter 99th percentile threshold of 25 tokens/second (TPOT of 40ms), ensuring consistent responsiveness even during high-load scenarios. Additionally, we set a 99th percentile TTFT limit of 450ms to minimize initial latency, aligning with user expectations for near-instantaneous query response.” The original Llama 2 70B server scenario, introduced in v4.0, allowed 2 s TTFT and 200 ms TPOT. NVIDIA's blog described the new constraints as “a 5x shorter TPOT and 4.4x lower TTFT”. The 40 ms figure came from an analysis of user data from platforms including ChatGPT and Perplexity AI in late 2024. The bar was set by what people will sit through.

The same two metrics carry different bars for different traffic. The newly added Llama 3.1 405B benchmark has mean input and output lengths of 9,400 and 680 tokens. It is allowed a 99th-percentile TTFT of 6 seconds and a TPOT of 175 ms. Time to first token is set by queueing and prefill, and governs perceived responsiveness. Time per output token reflects decode, and shapes the reading experience. Throughput measures useful work completed per resource and time. It improves with batching and utilisation, sometimes at the cost of queue delay. All three are measured at tail percentiles, because that is where the benchmark's promise lives.

FigureComparison · 3 columns

Time to first token

Delay before the user sees the beginning of a streamed response.

  • Affected by queue and prefill
  • Important for perceived responsiveness
  • May improve without faster full completion
  • Measure tail percentiles

Inter-token latency

Delay between generated tokens after streaming begins.

  • Reflects decode performance
  • Shapes reading experience
  • Can vary under batching
  • Not equal to total request latency

Throughput

Tokens, requests, or successful tasks completed per resource and time.

  • Improves with batching and utilization
  • May increase queue delay
  • Needs quality and SLO context
  • Measure useful work, not tokens alone

The KV cache trades memory for avoided recomputation

During autoregressive decoding, attention needs information derived from previous tokens. Caching key and value tensors avoids recomputing that history at every step.

Cache size grows with sequence length, batch occupancy, layers, heads and representation precision. Long contexts and many concurrent requests can therefore exhaust memory even when model weights fit comfortably. The obvious remedy is to throw old entries away. Guangxuan Xiao and four coauthors showed at ICLR in 2024 that it fails: naive window attention, which keeps only the most recent KV entries, collapses once the text exceeds the cache size. Retaining the KV of a few initial "attention sink" tokens restores stable decoding. That is enough to let Llama-2, MPT, Falcon and Pythia stream up to 4 million tokens with no fine-tuning.

The alternative to truncation is recomputation, and that has a price too. The closing sentence of their abstract reports it: “In streaming settings, StreamingLLM outperforms the sliding window recomputation baseline by up to 22.2x speedup.” So the cache is not an optimisation bolted onto decoding. It is the thing decoding runs on. Both ways of shrinking it, dropping entries and recomputing them, cost either correctness or a factor of up to 22.2x.

Serving capacity depends on model weights plus per-request state, not parameter count alone.

Example

Variable requests make scheduling a systems problem

A production queue mixes prompt lengths, output lengths, priorities and cancellation behavior. One such queue has been described by the people who run it. Ruoyu Qin and eight coauthors, at Moonshot AI and Tsinghua University, documented the scheduler behind Moonshot AI's Kimi chatbot at the USENIX FAST conference in February 2025. The system is called Mooncake. It separates prefill and decoding clusters, pools idle CPU, DRAM, SSD and NIC capacity into a disaggregated KVCache, and adds a prediction-based early-rejection policy for overload. Their abstract states the payoff: “In tests using real traces, Mooncake increases the effective request capacity by 59%~498% when compared to baseline methods, all while complying with SLOs.” The deployment spans thousands of nodes and processes over 100 billion tokens daily. There, it let Kimi serve 115% and 107% more requests on NVIDIA A800 and H800 clusters respectively. The preprint version reports 75% more requests under real workloads, and up to a 525% throughput increase in simulation.

  • Short request behind long prefill: A small interactive prompt waits while a large document consumes the batch. That pressure is why Mooncake runs prefill and decoding as separate clusters rather than one pool.
  • Memory fragmentation: Free memory exists but cannot accommodate the required cache layout efficiently. Hence the disaggregated KVCache pooling idle CPU, DRAM, SSD and NIC capacity, instead of treating accelerator memory as the only place state can live.
  • Straggler decode: One long generation holds a batch slot after shorter requests finish. The memory-intensive token-generation phase, not the prompt, sets when capacity is returned.
  • Priority inversion: Offline work delays a high-value interactive request. Under overload Mooncake does not admit everything and hope; it applies a prediction-based early-rejection policy, and that policy is part of what buys 59%~498% more effective request capacity at the same SLOs.
  • Cancellation waste: The user disconnects after compute has already been spent. Nothing in the throughput figure distinguishes that work from work someone read.
  • Tool interruption: A request leaves and later re-enters the scheduler with new context. It arrives as a fresh prompt computation, not a resumed decode.

Key idea

Every serving optimization changes an assumption

Quantization changes numeric representation. Speculative decoding adds a draft model and a step that decides whether to accept what it drafts. Prefix caching assumes reusable context. Batching alters scheduling. Parallelism makes the machines talk to each other. Measure target quality, tail latency, memory, throughput, energy and failure recovery together, because a speedup in an isolated kernel can disappear in the complete workflow.

The optimizations that survive are the ones that change memory traffic. Tri Dao and four coauthors published FlashAttention at NeurIPS in 2022, an IO-aware exact attention algorithm that “uses tiling to reduce the number of memory reads/writes between GPU high bandwidth memory (HBM) and GPU on-chip SRAM”. The result stays exact rather than approximate. Joshua Ainslie and five coauthors attacked the decode side at EMNLP in 2023 with grouped-query attention. They give a recipe for uptraining an existing multi-head checkpoint “using 5% of original pre-training compute”, and report that “uptrained GQA achieves quality close to multi-head attention with comparable speed to MQA”.

Prefix caching has a name and a ceiling. Lianmin Zheng and eleven coauthors introduced RadixAttention, automatic KV-cache reuse across requests sharing a prefix, in the SGLang paper at NeurIPS in 2024. Their abstract states: “Experiments show that SGLang achieves up to 6.4x higher throughput compared to state-of-the-art inference systems on various large language and multi-modal models on tasks including agent control, logical reasoning, few-shot learning benchmarks, JSON decoding, retrieval-augmented generation pipelines, and multi-turn chat.” Read the task list, not just the multiple. Those are exactly the workloads in which a shared prefix exists.

Quantization has a cost line as well as a benefit line. Elias Frantar and three coauthors showed with GPTQ that a 175-billion-parameter GPT model can be quantized to 3 or 4 bits per weight in approximately four GPU hours. The accuracy degradation relative to the uncompressed baseline was negligible. A 175B model then runs inside a single GPU for generative inference. The work appeared as an ICLR 2023 poster, retitled OPTQ. On the payoff they write: “We show experimentally that these improvements can be leveraged for end-to-end inference speedups over FP16, of around 3.25x when using high-end GPUs (NVIDIA A100) and 4.5x when using more cost-effective ones (NVIDIA A6000).” One technique, two numbers, two cards. That is this section's own warning about isolated kernels, stated by the authors themselves.

Serving optimizations are system changes that require quality and reliability regression tests.

Steps

Optimize serving from measurements

Start with the user-visible SLO and the dominant resource constraint. Every result in this lesson was earned in that order. The Splitwise team characterised the two phases before splitting them across machines, and Mooncake's 59%~498% capacity gain was measured on real traces against SLOs, not on a microbenchmark.

First, instrument the request lifecycle: queue, prefill, first token, decode, tools, cancellation, completion. Then a change can be attributed to a phase. Second, segment workloads. Interactive, long-context, batch, multimodal and tool-heavy traffic do not share a bar. MLPerf Inference v5.0 holds Llama 2 70B Interactive to a 99th-percentile 450 ms TTFT and 40 ms TPOT; it allows the Llama 3.1 405B benchmark 6 seconds and 175 ms. Third, profile compute and memory: utilisation, cache occupancy, bandwidth, fragmentation and communication. Remember that the memory-intensive token-generation phase can underutilise compute while saturating something else.

Fourth, test one intervention under matched load. Quantization is the clearest case for going one at a time. The same GPTQ conversion delivered around 3.25x on an NVIDIA A100 and 4.5x on an NVIDIA A6000. Prefix caching's up-to-6.4x applies to workloads that actually share a prefix. Fifth, re-run quality gates on rare tasks, structured output, long context and safety behavior. Reduced precision was reported as negligible accuracy degradation against the uncompressed baseline, and that is a claim your own tasks either reproduce or do not. Sixth, canary under real traffic and watch tail latency, errors, cost and capacity. The numbers that decided Splitwise were throughput at a given cost and power budget. The numbers that decided Mooncake were served requests at an unchanged SLO.

FigureProcess · 6 steps
  1. 1. Instrument the request lifecycle

    Record queue, prefill, first token, decode, tools, cancellation, and completion.

  2. 2. Segment workloads

    Separate interactive, long-context, batch, multimodal, and tool-heavy traffic.

  3. 3. Profile compute and memory

    Measure utilization, cache occupancy, bandwidth, fragmentation, and communication.

  4. 4. Test one intervention

    Evaluate batching, caching, quantization, speculation, or parallelism under matched load.

  5. 5. Re-run quality gates

    Check rare tasks, structured output, long context, and safety behavior.

  6. 6. Canary under real traffic

    Monitor tail latency, errors, cost, and capacity before broad rollout.

Runtime architecture determines the usable capability envelope

Strong offline numbers can become impractical once long prompts, large outputs, concurrency and review latency are included. Serving design is what connects model capability to user experience and operating cost. In every measured system here, the winning move was structural rather than local. Splitwise separated a compute-intensive prompt computation from a memory-intensive token generation. Mooncake pooled idle CPU, DRAM, SSD and NIC capacity into a disaggregated KVCache. RadixAttention reused a prefix that is genuinely shared. StreamingLLM kept a few initial tokens so a window would not collapse. The reported gains belong to those decisions, on those traces, at those SLOs: 1.4x throughput at 20 percent lower cost, 59%~498% more effective request capacity, up to 6.4x, up to 22.2x.

The next lesson covers monitoring and change management. In production, prompts, sources, tools, traffic and model versions evolve together, so a healthy endpoint can still deliver degraded outcomes.

Key takeaways