LLM Inference Performance Optimization: Engineering Practices for High-Throughput, Low-Latency Serving
This article breaks down the engineering side of large language model (LLM) inference: why serving speed is now a first-class concern, how to define and measure the right metrics, where the bottlenecks actually sit, and which optimization techniques move the needle. It covers latency vs. throughput, prefill and decode profiling, continuous batching, KV cache management, quantization, speculative decoding, and parallelism strategies, then closes with a practical rollout order and a validation checklist for teams that need to push inference throughput and latency to their limits without changing model capability.
1. Background: Why Speed Has Become a First-Class Engineering Concern
A common rule of thumb in the industry has been simple: if you want speed, switch to a smaller model. Lightweight models have fewer parameters and lower compute requirements, so they naturally achieve higher throughput. Flagship models generate higher-quality output but usually come with higher latency. This “trade capability for speed” compromise has forced many interactive products to cut corners on user experience.
As inference engineering has matured, that trade-off is being broken. The key insight is that model capability (parameter count and training quality) and inference speed (system efficiency on the serving side) are fundamentally two different dimensions. The former determines how well the model writes; the latter determines how fast it writes. Almost all of the room for engineering optimization sits in the second dimension.
So this article does not discuss how to train a stronger model. Instead, it focuses on a general engineering problem: with model capability held constant, how do you push the throughput and latency of an inference service to the limit?
2. Getting the Metrics Right: Latency and Throughput Are Not the Same Thing
The first step in performance optimization is aligning on metric definitions. Otherwise every later discussion loses focus.
- Time to first token (TTFT): The time from when a request is sent to when the first output token is received. It determines how strong the user’s sense of waiting is, and it is the key factor in interactive experience.
- Decode throughput (tok/s): After the first token, the number of tokens generated per unit of time. It determines how fast long-form and code-oriented tasks complete.
- Aggregate throughput: On the serving side, under concurrency, the total number of tokens produced per second across all requests combined. It measures the system’s overall resource efficiency and capacity.
A common mistake is to fixate only on aggregate throughput. For a single-user interaction, no matter how high aggregate throughput is, if per-request decode speed is slow the experience is still bad. Conversely, if you optimize only single-request speed and ignore batch scheduling, the system gets saturated under high concurrency. Both classes of metrics must be optimized at the same time, and they need to be load-tested separately.
3. Locating the Bottleneck: Where Is the Slowness Actually Coming From?
Inference can be split into two phases, and the causes of bottlenecks are completely different.
- Prefill phase: Processes all input tokens at once. It is compute-intensive and mainly affected by compute capacity and parallelism strategy. The longer the input, the higher the TTFT.
- Decode phase: Generates tokens one by one via autoregression. It is memory-access-intensive and mainly affected by GPU memory bandwidth and batching efficiency.
Suggested approach for locating the bottleneck:
- Record TTFT and decode rate separately to determine which phase the bottleneck falls in;
- Observe whether aggregate throughput grows linearly as concurrency rises; non-linear growth indicates that scheduling or memory has become the bottleneck;
- Pay attention to the proportion of long-context requests, since long inputs significantly raise prefill cost.
4. Key Optimization Paths
4.1 Continuous Batching and Dynamic Scheduling
Traditional static batching requires all requests in a batch to enter and exit in sync, so short requests must wait for long ones, causing large amounts of compute to sit idle. Continuous batching allows requests to join or leave the batch at any point during decoding, significantly improving GPU utilization. This is the most basic and most effective first step for raising aggregate throughput.
4.2 KV Cache Management
Autoregressive generation needs to cache historical key-value pairs. A memory management approach that appends continuously and allocates on demand can greatly reduce fragmentation and copy overhead, thereby supporting higher concurrency and longer contexts. Cache hit rate and memory footprint are the core observation points at this layer.
4.3 Quantization and Low-Precision Compute
Within an acceptable range of quality loss, using lower-precision weight and activation representations reduces both memory footprint and memory-access cost, with especially clear gains for the memory-access-intensive decode phase. In practice, you need to validate the magnitude of quality regression against an offline evaluation set to avoid “sacrificing usability for speed.”
4.4 Speculative Decoding
A lightweight draft model first guesses several tokens, then the main model verifies them in parallel; accepted tokens are adopted directly. It does not change the output distribution, but it compresses multiple serial decode steps into a single parallel verification, making it an effective way to improve single-request decode speed.
4.5 Parallelism Strategy and Resource Isolation
Splitting a very large model across multiple GPUs via tensor parallelism or pipeline parallelism, combined with request priority queues and resource isolation, prevents long tasks from blocking short ones and keeps interactive requests at low latency.
Illustrative scheduling pseudocode:
while serving:batch = scheduler.form_batch(max_tokens=256)outputs = engine.decode(batch) # continuous batching decodefor req, tok in outputs:stream(req, tok) # stream first token and increments immediatelyscheduler.recycle(finished_requests) # exit on completion, free memory
5. Scenario Validation: Once Speed Crosses a Threshold, the Product Form Changes
The value of optimization ultimately shows up in interaction patterns. Four general scenarios can be used for validation:
- Side-by-side generation comparison: On the same task, the high-throughput version produces a complete page within tens of seconds while the low-throughput version takes several times longer, making the difference in decode rate directly visible;
- Real-time shader creation: Natural-language instructions and rendered output take effect in sync, with output rate holding steady in the range of hundreds of tokens per second, enabling “edit as you speak”;
- Sketch to page: Input a wireframe sketch and get structurally complete interface code and interaction logic in a short time;
- High-concurrency aggregation: Dozens of requests generating simultaneously, with aggregate throughput reaching the order of a thousand tokens per second, validating system capacity.
The conclusion is clear: only when latency drops below a certain threshold do real-time collaboration and instant-preview patterns—previously impossible because of waiting—actually become viable.
6. Rollout Recommendations and Validation Checklist
- Establish a metric baseline first, recording TTFT, single-request decode rate, and aggregate throughput separately;
- Introduce optimizations step by step in the order “continuous batching → cache management → quantization → speculative decoding → parallel splitting,” running A/B load tests at each step;
- After each optimization, run a fixed evaluation set to confirm there is no obvious regression in generation quality;
- In production, pair with logging services and monitoring alerts, and set threshold alerts on latency percentiles (P95/P99);
- Rely on container platforms and message queues for elastic scaling and peak shaving to avoid sudden traffic spikes overwhelming the service.
In related practice at Baidu AI Cloud, the above capabilities are typically delivered by combining general product categories such as inference services, container platforms, logging services, and monitoring alerts. Enterprises can orchestrate them flexibly according to their own traffic characteristics to achieve stable high throughput and low latency on top of flagship-level model capability.
