Capacity Planning for LLM Inference: Separating Prefill and Decode Costs
This article explains why LLM inference costs should be split into prefill and decode phases for capacity planning. It defines key metrics such as TTFT and tokens/s, provides a per-request measurement checklist, compares the compute and memory-bandwidth profiles of the two phases, and reviews the trade-offs of prefix caching and paged KV cache.
1. Background: Why Analyze the Two Phases Separately
LLM serving cost is often measured as a single figure, such as tokens generated per second or total time per request. This works for short conversations. In long-context and agent workloads, it can lead to wrong conclusions.
An agent call usually reads conversation history, tool results, and retrieved document snippets, then outputs a short decision or instruction. Long input and short output place very different demands on resources at each end. Total latency alone does not show whether the bottleneck is prefill or decode, or which one to optimize first.
When analyzing inference cost, split each request into two phases: prefill and decode. Each phase has its own bottleneck, metrics, and optimization methods.
2. Metric Definitions and Measurement Checklist
The same numbers can lead to opposite conclusions under different definitions. This article uses the following definitions.
- TTFT (time to first token): The time from when the client sends a request to when it receives the first output token. It includes queueing time, network time, and prefill time. Report queueing time separately. Otherwise, you cannot tell whether latency comes from computation or from waiting.
- tokens/s: This term has two meanings, which should not be mixed. Per-request decode speed equals output tokens divided by (completion time minus time to first token). System throughput equals the sum of output tokens from all requests per unit of time.
- Memory usage: Peak GPU memory after the model is loaded, including weights, KV cache, and framework overhead. Report it in GiB.
- Cache hit rate: Cached input tokens divided by total input tokens. State whether the rate is counted by request or by token, because the two often differ greatly.
During measurement, keep the same model, precision, hardware, and batching policy. Collect data after warm-up, from a stable period. Record the distribution of input lengths, including the median, P90, and maximum. Data from different environments cannot be compared directly.
Measurement checklist: Record the following fields for each request.
- Arrival time and queueing time.
- TTFT and completion time.
- Input token count and output token count.
- Number of cached tokens, and the resulting hit rate by request and by token.
- Per-request tokens/s.
- Batch size during measurement and peak memory (GiB).
3. Compute Characteristics of Prefill and Decode
Prefill receives all input tokens at once. Within a layer, the computation for all input positions runs in parallel, while layers still run in sequence. After computation, the Key and Value of each position at each layer are written to the KV cache. The compute cost of this phase grows roughly in proportion to input length, and the attention part grows faster as the input gets longer. Prefill is compute-intensive and usually makes good use of accelerator compute.
Decode generates output tokens one at a time. For each new token, the model reads the KV cache for all prior context. The compute per step is small, but the memory access is large, so decode is often limited by memory bandwidth.
| Dimension | Prefill | Decode |
|---|---|---|
| Processing method | All input tokens in a layer are processed in parallel. Layers run in sequence. | Tokens are generated one at a time, serially. |
| Main bottleneck | Compute throughput | Memory bandwidth and cache reads |
| Relation to input length | Cost grows with input length, and attention grows faster. | Longer context means more cache reads per step, so each step is slower. |
| Relation to output length | Essentially none | Proportional to the number of output tokens |
| Common metric | Time to first token (TTFT) | Tokens per second (tokens/s) |
This leads to a general rule: prefill is usually limited by compute, and decode is usually limited by memory bandwidth. This is a common pattern, not an absolute rule. Batching, quantization, model architecture (such as GQA), and hardware differences can all change which phase dominates. To identify the bottleneck of a given service, rely on measured GPU utilization and memory access data.
This framework also explains a common observation. Two requests may have the same total token count, but one has long input and short output, while the other has short input and long output. Their costs can differ several times over. The first request spends most of its time in prefill, and the second spends nearly all of its time in decode. Pricing or sizing capacity by total token count alone underestimates the cost of one of these request types.
4. Trade-offs of Cache Optimizations
The KV cache saves compute, but it also consumes memory. The table below compares common approaches.
| Method | Main benefit | Applicable conditions | Main costs and risks |
|---|---|---|---|
| Prefix Caching | Identical prefixes are prefilled once, and the TTFT of the cached part drops accordingly. | Request prefixes match token for token, and the cache has not been evicted. | Uses GPU memory. Any change in the prefix invalidates all cache after that point. In multi-tenant sharing, timing side channels may appear. |
| Paged KV Cache | Memory is allocated in blocks, which reduces waste and fragmentation from reserving space for the maximum length. | Benefits are clear when request lengths vary widely and concurrency is high. | Does not reduce the bytes per token. Block table management adds small overhead and requires kernel support. |
