0%
LLM Inference and Serving: GPUs, Batching, and Cost
How Inference Works
Hardware and Memory
Serving Systems
Chunked Prefill and Disaggregation
Prefill and decode are two very different kinds of work sharing the same GPUs. Prefill processes a whole prompt at once and is compute bound; decode produces one token per sequence per step and is memory bound. When they run together, each gets in the way of the other. This lesson explains the conflict, how chunked prefill smooths it within one engine, how disaggregated serving separates the phases onto different machines, how the KV cache moves between them, and how to choose between these designs. This first section explains why the conflict exists.
Two phases, two bottlenecks
Prefill takes a prompt of hundreds or thousands of tokens and runs them through the model in one pass, computing keys and values for every position. Because many tokens share each weight read, prefill does a lot of arithmetic per byte and keeps the tensor cores busy. Decode takes the next token of each running sequence, a single position per sequence, so each weight read serves only a batch of tokens and the step is limited by memory bandwidth. The two phases want different things from the hardware: prefill wants compute, decode wants bandwidth and low per step latency.
What happens when they collide
With continuous batching, new requests arrive while others are decoding. A simple engine runs a new prompt prefill as soon as it arrives, in its own step or alongside decode. A prompt of 8,000 tokens might take 400 milliseconds to prefill on a given GPU. If decode for the running sequences pauses during that prefill, every user streaming a response sees a 400 millisecond stall in their token stream. If prefill and decode share the step, the step takes about as long as the prefill, and decode tokens arrive late. Either way, long prompts damage the time per output token of everyone else.
The metrics in tension
Time to first token, TTFT, depends on how quickly a new request prefill runs. Time per output token, TPOT, depends on how regularly decode steps run for active sequences. An engine that prioritizes new prefills gives good TTFT but spiky TPOT for everyone else. An engine that prioritizes decode keeps TPOT smooth but makes new requests wait in a queue, raising TTFT. With one shared pool of GPUs and a naive scheduler, improving one metric hurts the other. The rest of this lesson is about breaking that tradeoff.
Why it got worse with longer contexts
When prompts were a few hundred tokens, prefill was short and the conflict was minor. Retrieval augmented generation, long system prompts, agent tool histories and document analysis have pushed typical prompts to many thousands of tokens, and some to hundreds of thousands. Prefill time grows at least linearly with prompt length, and attention makes it grow faster for very long prompts. Long prompts turned prefill from a brief interruption into a major source of latency spikes, which motivated both chunked prefill and disaggregation.
Utilization in each phase
The conflict also wastes hardware. During pure decode at moderate batch, tensor cores sit mostly idle waiting on memory. During a large prefill, memory bandwidth is underused while compute saturates. A GPU that alternates between them never uses both resources fully at once. Mixing the right amount of prefill work into each decode step can fill the idle compute with useful prefill arithmetic, raising overall throughput. This observation is the foundation of chunked prefill.
A concrete picture
Imagine a GPU serving 32 active chat sessions with 25 millisecond decode steps, giving each user 40 tokens per second. A request arrives with a 16,000 token document. Prefilling it in one pass might take 800 milliseconds. Without any mitigation, the 32 users see their streams freeze for most of a second, and the user with the document waits 800 milliseconds plus queueing for the first token. Product teams notice these freezes immediately, because they are visible as text that stops and starts. The techniques in this lesson exist to prevent exactly this experience.
Measuring the conflict in your own service
The conflict is easy to see in data once you know where to look. Plot the time between consecutive tokens for streaming requests, not just the average TPOT, and overlay the arrival times of requests with long prompts. In a service suffering from prefill interference, the inter token gaps show sharp spikes that line up with those arrivals, while the median stays low. Users perceive these spikes as text that freezes and then jumps forward. The p99 inter token latency is therefore a better health metric than average TPOT, because it captures exactly the stalls that averages hide.
A second signal is the step time distribution in the engine. With a healthy configuration, step times cluster tightly around one value. With unchunked prefill, the distribution has a long tail of slow steps, each corresponding to a large prompt. Engines export per step timing and the number of prefill and decode tokens in each step, which makes this analysis straightforward.
The cost of queueing prompts instead
One way to protect decode is to hold new prompts in a queue until there is a lull. This keeps streams smooth but shifts the pain onto new users, whose first token is delayed. For interactive products, a long wait before anything appears is often worse than an occasional stall mid response, since users may assume the product is broken. Queueing also wastes capacity: while prompts wait, the GPU decodes at a batch size that leaves compute idle, compute that could have been used to make progress on the waiting prompts. Neither prioritizing prefill nor prioritizing decode is satisfying, which is why the field moved to designs that let both progress together.
Goodput as the target
Throughput alone is a misleading target for this problem, because a system can push high token counts while violating latency objectives for many requests. Goodput counts only the requests that meet both their TTFT and TPOT objectives, per second or per GPU. It captures the real goal: serve as many users as possible within the experience the product promises. Comparing scheduling and architecture options by goodput, rather than raw throughput, is the standard method in recent serving research, and it is a good habit for production evaluation as well.
Short prompts are not immune
Even services with mostly short prompts see the conflict at scale. A batch of 100 new requests arriving together, each with 1,000 tokens, adds up to 100,000 prompt tokens, as much as one very long document. Bursty traffic, such as a notification that sends many users to a product at once, produces exactly this pattern. Any design for the conflict must handle bursts of many medium prompts, not just occasional huge ones.
Why averages mislead here
Dashboards that show only average TPOT can look healthy while users suffer. If one step in fifty is a 400 millisecond prefill stall and the rest are 25 milliseconds, the average step is about 33 milliseconds, which looks fine against a 50 millisecond objective. Yet every streaming user hits a visible freeze roughly every second or two. Tracking the p95 and p99 of inter token gaps, alongside the share of steps above a threshold, reveals the problem that averages hide. It is also worth segmenting by prompt length, since the users who submit long prompts and the users who are stalled by them are usually different people, and the product impact falls on the second group.
What comes next
The next two sections present the two main answers to the conflict. Chunked prefill keeps both phases on the same GPUs but slices prefill so it never dominates a step. Disaggregation moves the phases onto different GPUs entirely. Both aim to raise goodput, and many real systems combine elements of each.
Prefill is compute bound and bursty; decode is memory bound and steady. Treating them as the same kind of work is the root of most latency spikes in LLM serving.