LLM Inference: P/D Separation and KVCache Tiering
The cost problems of LLM inference often only surface after deployment. As model parameters grow and context windows lengthen, GPU memory and compute both become constrained. The core challenge of inference system design is improving throughput under low-latency requirements while keeping per-GPU costs under control. This article explains two key architectural strategies—Prefill/Decode separation and tiered KVCache storage—covering design principles, scheduling policies, and a step-by-step cost calculation method, so engineers can size their own systems rather than copying others' numbers.
LLM Inference: P/D Separation and KVCache Tiering
The cost problems of LLM inference often only surface after a system goes live. As parameter counts grow and contexts get longer, both GPU memory and compute become tight. How to increase throughput under low-latency constraints while controlling per-GPU cost is the central question in inference system design. This article does not target any specific product; it covers general architectural principles and calculation methods only. All example figures are illustrative and should not be used as a basis for procurement or capacity commitments.
1. Why Separate Prefill and Decode
Inference consists of two phases. The Prefill phase processes the entire input prompt in one pass. It is compute-intensive and primarily consumes FLOPs. The Decode phase generates output tokens one at a time; each step reads a large amount of KVCache, so it is mainly bound by memory bandwidth. The two phases have very different resource profiles, and co-locating them on the same pool of GPUs causes mutual interference. P/D separation splits the two phases into separate resource pools, each configured for its own workload characteristics.
If inputs include multimodal content such as images or audio, an additional Encode phase appears in the architecture, forming E/P/D separation. The Encode phase converts raw input into embedding vectors and can usually be scaled independently. Whether to dedicate a separate resource pool to it depends on the share of multimodal requests.
Common resource configuration guidelines:
- Preprocessing: lightweight operations that do not depend on parallelism; a single GPU is sufficient, avoiding occupation of large clusters.
- Prefill pool: compute-intensive; typically uses a smaller expert parallelism degree (e.g., EP of 2 to 8), prioritizing lower time-to-first-token.
- Decode pool: memory-access-intensive; can use larger-scale Wide-EP (wide expert parallelism) to reduce per-step decoding latency.
One point deserves clarification. A certain public case study uses Wide-EP on the decode side without tensor parallelism (TP) or context parallelism (CP). This does not mean TP and CP should never be used. Whether to adopt them depends on model architecture, inter-node interconnect bandwidth, and latency targets (SLAs). When interconnect bandwidth is ample and the number of experts is large, EP communication overhead can be well hidden. In bandwidth-constrained environments, TP or CP may still be more appropriate. You should benchmark with your own model rather than copying others’ conclusions.
2. KVCache Tiering: First Calculate Bytes per Token
KVCache stores previously computed Keys and Values to avoid redundant computation. Its capacity directly determines how many concurrent requests a single node can support, so tiered storage design must start with capacity estimation.
The KV footprint per token can be estimated with this formula:
Bytes per token = 2 × number of layers × number of KV heads × head dimension × bytes per element
The factor of 2 accounts for K and V. Bytes per element depend on precision: 2 bytes for BF16, 1 byte for FP8. The number of KV heads depends on the attention architecture. With structures such as GQA or MLA, the number of KV heads is significantly smaller than the number of query heads, and the KV footprint drops accordingly.
An example: suppose a model has 60 layers, 8 KV heads, a head dimension of 128, and uses BF16. Then the KV per token is roughly 2 × 60 × 8 × 128 × 2 = 245,760 bytes, about 240 KB. At that rate, a 128K-token context requires about 31 GB of KVCache. This shows that HBM alone cannot sustain high concurrency with long contexts—tiered storage becomes necessary. These parameters are used only to demonstrate the calculation and do not represent any real model configuration.
Tiered storage typically has three levels:
- HBM (GPU memory): holds only the hot data currently involved in computation, guaranteeing the highest access bandwidth.
- Host DRAM: holds the recent data required by sliding window attention (SWA). SWA only attends to the most recent segment of the context; data outside the window no longer participates in computation, so this portion has a well-defined size bound, and DRAM read/write speeds are sufficient for decoding.
- Distributed file system: stores complete KVCache blocks. It offers large capacity but high access latency and requires a high-throughput network.
SWA data is generally not written to the distributed file system. Frequent small-block reads and writes would saturate the network and degrade overall performance.
When a KVCache block misses locally, the system has two options: fetch it remotely or recompute it locally. A simple rule governs the trade-off: if remote fetching takes longer than recomputation, recompute; otherwise, fetch. Recomputation consumes compute, while fetching consumes network bandwidth—whichever resource is scarcer should be avoided.
Bounded Replay is a compromise. It replays only the most recent tokens to quickly restore context, rather than recomputing the full history. One public case used replay lengths of 128 and 2,560 tokens, but the corresponding model, precision, and error tolerance were not disclosed. Readers should determine an appropriate replay length based on their own accuracy requirements and latency budgets.
3. Scheduling: Time-Budgeted Prefill Chunking, Dual-Dimension Decode Balancing
The scheduler decides when and on which node each request executes. Prefill and Decode have different scheduling goals and should be designed separately.
In the Prefill phase, the key metric is time-to-first-token (TTFT). One approach is to assign each request a time budget first, then work backward to determine how large each input chunk should be. When the budget is tight, chunks are cut finer and processed in a pipelined fashion. When the budget is loose, the entire input can be processed at once to improve compute utilization. The core principle is that a single chunk’s compute time must not exceed the remaining budget.
In the Decode phase, the biggest challenge is that output length is unpredictable. The scheduler can consider two dimensions simultaneously: each request’s compute load and its memory footprint. Looking only at compute load lets long-output requests hold GPU memory for extended periods, squeezing out other requests.
The case study also mentions EPLB (expert-level load balancing). Its principle can be summarized as “push down the max, not just the mean.” Traditional load balancing focuses on lowering average load, while EPLB prioritizes reducing the load on the most heavily loaded node in the cluster, even if the average load rises slightly. This reduces tail latency and suits services sensitive to P99 latency. The trade-off is potentially lower average utilization, so benchmarking is needed to find the right balance.
Before going live, it is advisable to build a checklist of the following parameters and fix each value:
- TTFT budget and its acceptable fluctuation range
- Upper and lower bounds on Prefill chunk size
- Concurrency limit per node
- Threshold for identifying tail latency
- GPU memory watermark that triggers migration or throttling when exceeded
4. Cost Accounting: Calculate Step by Step, Don’t Reuse Others’ Numbers
Per-GPU cost accounting can be done in three steps:
- Divide daily token throughput by 86,400 to get average throughput per second (tokens/s).
- Divide average throughput per second by the number of GPUs to get per-GPU throughput.
- Divide the per-GPU hourly cost by (per-GPU throughput × 3,600) to get the cost per token.
A hypothetical example: suppose the system processes 100 million tokens per day using 100 GPUs, each costing $2 per hour. Average throughput is about 1,157 tokens/s, per-GPU throughput is about 11.6 tokens/s, and the cost per token is about $0.000048—roughly $48 per million tokens. These numbers illustrate the calculation process only and do not represent any real system.
Common pitfalls in cost accounting:
- Count input and output separately. Prefill token throughput is typically far higher than Decode throughput; mixing them makes per-GPU generation speed look inflated.
- Averages hide peaks. Capacity should be sized for peak load with headroom reserved, not for average load.
