High-Speed LLM Inference in Production: Architecture, Stress Testing, and Cost Control
High-speed LLM inference requires balancing time-to-first-token (TTFT), throughput, and tail latency under given hardware constraints. This article explains how to identify inference bottlenecks (prefill-bound, decode-bound, or scheduling-bound) and covers four key optimization techniques: continuous batching, PagedAttention-based KV cache management, speculative decoding, and low-precision inference with compilation optimizations. For each technique, it provides practical parameter settings, memory estimation formulas, acceptance-rate thresholds, and regression evaluation strategies to help teams deploy large models efficiently without losing output quality.
1. Identify the Bottleneck First
High-speed inference means satisfying three goals on given hardware at the same time: acceptable time to first token (TTFT), sufficient throughput, and controlled tail latency. These three goals often conflict with each other. Before optimizing, use monitoring to determine which type of bottleneck you are facing.
- Prefill-bound: With long inputs, the model must process the entire prompt first. TTFT rises and GPU compute units are heavily occupied.
- Decode-bound: Generating each new token requires reading large amounts of weights and KV cache, so the per-token generation speed (TPOT) of a single sequence is limited by memory bandwidth.
- Scheduling-bound: GPU utilization is low, but requests are queued up. This usually means the batching strategy or concurrency limits are misconfigured.
The diagnostic method is straightforward. Fix one short input and one long input, and measure TTFT and TPOT under single concurrency. If TTFT for the long input grows roughly linearly with input length, the problem is most likely in prefill. If TPOT degrades noticeably as concurrency increases, the problem is most likely memory bandwidth or an overly large batch.
2. Four Key Techniques and Their Parameters
2.1 Continuous Batching
Traditional static batching waits until every request in a batch finishes before releasing resources. Continuous batching checks the queue after each decoding step: finished sequences exit immediately and new requests join immediately. For workloads where output lengths vary widely, this significantly improves GPU utilization.
Three parameters matter in practice:
- max_num_seqs (maximum concurrent sequences): Limited by GPU memory. Beyond this limit, requests queue due to insufficient KV cache, or get evicted and recomputed.
- max_num_batched_tokens (maximum tokens per step): Controls the compute per step. A larger value increases throughput but lengthens each step, slowing down all in-flight requests.
- Waiting window: Only used when doing dynamic batching at the gateway layer; typical values are 5–20 ms. A longer window produces larger batches, but under low load the added wait time is a pure loss.
2.2 KV Cache Management (PagedAttention)
The KV cache stores the key and value vectors of every generated token to avoid redundant computation. Its memory footprint can be estimated as:
Memory per token = 2 × number of layers × number of KV heads × head dimension × bytes per element
For example, a model with 32 layers, 8 KV heads, head dimension 128, and FP16 precision: 2 × 32 × 8 × 128 × 2 = 131,072 bytes, roughly 128 KB. A 4096-token sequence needs about 512 MB of KV cache. If an 80 GB GPU uses about 16 GB for weights, dividing the remaining memory available for cache by 512 MB gives a rough concurrency ceiling. This is only an estimate—activations and framework overhead must also be deducted, so keep a 10–20% margin.
The traditional approach pre-allocates contiguous memory of the maximum length for each sequence, which wastes a lot of memory on short responses. PagedAttention splits the cache into fixed-size blocks allocated on demand, similar to paging in operating systems. With the same memory, it typically supports more concurrency. A common failure case: setting max_model_len far beyond the actual business sequence length causes the scheduler to reserve resources for long sequences that never occur, needlessly lowering concurrency.
2.3 Speculative Decoding
Speculative decoding uses a small draft model to propose several candidate tokens at once, which the large model then verifies in parallel; accepted tokens are committed in one pass. This does not change the output distribution and is theoretically equivalent to the original model, but the benefit depends on the acceptance rate.
When the acceptance rate is low, the draft model’s own overhead can slow the system down. As a rule of thumb, if the acceptance rate stays below roughly 0.5, disable the feature or switch to a different draft model. The exact threshold should be confirmed with your own business data. It works best for predictable text such as code completion and templated responses. For high-temperature sampling and creative writing, the gains are usually small.
2.4 Low-Precision Inference and Compilation Optimizations
Lower precision reduces the memory footprint of weights and KV cache while improving compute utilization. Common options:
- BF16: Dynamic range close to FP32; typically used as the baseline precision and requires almost no extra calibration.
- INT8 weight quantization: Roughly halves memory usage; accuracy loss is acceptable for most tasks, but a calibration dataset is required.
- FP8: Used on newer hardware that supports the format, balancing speed and accuracy. Verify framework and driver versions first.
- INT4: Lowest memory footprint, but carries the highest accuracy risk for mathematical reasoning and long code generation—it must be validated on an evaluation set first.
Regression evaluation is mandatory before shipping lower precision. Split the evaluation set by task type, covering at least general Q&A, structured JSON output, code pass rate, and math accuracy. Set acceptable degradation thresholds for each category—for example, the format-compliance rate of structured output may drop by no more than 1 percentage point. If a threshold is exceeded, roll back to the previous precision. Common failures include the model starting to repeat itself in long outputs after quantization, or a declining rate of properly closed JSON brackets.
