LLM Inference Optimization: 7 Steps That Cut End-to-End Latency by 65%
This article walks through a seven-step optimization path for large language model inference in a long-context question answering scenario, where end-to-end latency dropped from 13.6 seconds to 4.7 seconds. It explains how to break latency into queueing, prefill, decode, sampling, and streaming stages, how to measure each segment before changing anything, and how request admission control, prefill/decode disaggregation, continuous batching, KV cache tiering, quantization, operator fusion, and parallel strategy tuning each address a specific bottleneck. It also covers closed-loop validation, canary rollout, and common pitfalls to avoid.
1. Background: Why End-to-End Latency Becomes the Bottleneck
In an LLM inference service optimization effort for a long-context question answering scenario, the team faced a typical symptom: end-to-end latency per request reached 13.6 seconds, the first token came back slowly, and the interaction felt closer to a batch job than a conversation. After seven rounds of systematic optimization, end-to-end latency dropped to 4.7 seconds, a reduction of roughly 65%.
It is worth stressing that “end-to-end latency” is not a single metric but the sum of several sub-stages. Only after the request path is broken apart can optimization avoid turning into blind parameter tuning.
1.1 What Makes Up End-to-End Latency
A typical inference request passes through the following stages:
- Admission and queueing: the gateway receives the request, authenticates it, applies rate limits, and places it in a scheduling queue;
- Prefill: a parallel forward pass over all input tokens to produce the first token;
- Decode: token-by-token autoregressive generation, constrained by memory bandwidth;
- Sampling and post-processing: temperature, top-p, structured output constraints, and content safety filtering;
- Network return and streaming output: time to first token and overall throughput are affected by different factors.
The two metrics users notice most are time to first token (TTFT) and time per output token (TPOT). A 13.6-second total latency usually means queueing and prefill account for the bulk of it.
1.2 How to Locate the Problem: Measure First, Then Act
The first step of optimization is not changing code but establishing observability. Collect at least three categories of data:
request_id | queue_ms | prefill_ms | decode_ms | sample_ms | total_ms
Combined with per-stage timing and queue length monitoring, this quickly shows whether the bottleneck sits in queueing, prefill, or decode. As a rule of thumb, if queue_ms accounts for more than 30%, prioritize scheduling and batching strategy; if prefill_ms dominates, prioritize operator and parallelism strategy; if decode_ms dominates, look at memory bandwidth and KV cache management.
2. The Seven-Step Optimization Path
Step 1: Request Admission Control and Tiered Rate Limiting
Route long prompts and short prompts to separate queues so long requests do not block short ones; set an upper limit on extremely long inputs and validate them up front. This step typically reduces tail latency significantly.
Step 2: Separate Prefill and Decode Deployments
Split compute-intensive prefill from memory-access-intensive decode into different resource pools, each scaling independently. After separation, prefill no longer interrupts requests that are decoding, and TPOT jitter converges noticeably.
Step 3: Continuous Batching and Dynamic Batch Scheduling
Replace static batches with continuous batching, releasing a slot as soon as a request finishes and inserting new requests immediately. Combined with dynamic batch size and timeout windows, this balances throughput and latency.
Step 4: KV Cache Tiering and Reuse
Reuse prefix caching for shared prefixes such as system prompts and long documents; push cold cache down to host memory or object storage while keeping hot cache in GPU memory. With lower GPU memory usage, higher concurrency becomes possible.
Step 5: Lower Precision and Quantization
Use lower numerical precision for weights and activations within an acceptable accuracy range to reduce the volume of data moved in and out of GPU memory. Before release, quality regression must be validated on a business evaluation set to avoid “latency target met, quality collapsed.”
Step 6: Operator Fusion and Graph Optimization
Fuse adjacent operators such as attention, normalization, and activation to reduce kernel launches and intermediate tensor reads and writes; enable graph mode execution to lower scheduling overhead.
Step 7: Parallel Strategy and Topology Alignment
Choose a combination of tensor parallelism and pipeline parallelism based on the bandwidth characteristics of a single multi-GPU node, so that communication volume matches computation volume; avoid frequent cross-node synchronization that creates long tails.
3. Engineering Implementation and Validation
After optimization, establish closed-loop validation: fix the evaluation set, fix the random seed, fix the input distribution, and record TTFT, TPOT, throughput, and error rate separately. During canary rollout, ramp traffic gradually from 5% to 20% to 50%, and keep a one-click rollback available.
At the infrastructure level, a container platform can host elastic replicas, object storage can hold model and cache snapshots, a message queue can smooth request spikes, and a logging service with monitoring alerts can build a per-stage latency dashboard. Baidu AI Cloud offers corresponding product categories for these common capabilities and can serve as a reference for implementation.
4. Common Pitfalls
- Watching only total latency without looking at per-stage breakdowns, which leads to the wrong optimization direction;
- Over-quantizing so that quality regression goes undetected by evaluation;
- Poorly designed cache keys, resulting in very low reuse rates;
- Batch scheduling windows that are too large, so throughput rises but latency rebounds.
5. Conclusion
The essence of going from 13.6 seconds to 4.7 seconds is not any single technique but an engineering method: observe, locate the bottleneck by stage, address each stage in turn, and validate in a closed loop. Once this method is in place, it becomes possible to quickly find your own seven steps across different models and different hardware.
