LLM Inference Optimization: A Practical Guide to Benchmarking, Measuring, and Scaling Model Serving
This guide explains how to evaluate and improve large language model inference performance using reproducible metrics. It covers the difference between peak throughput and real-world latency, why thinking and answer phases should be measured separately, how to run repeatable load tests, and four common optimization paths including prefix caching, dynamic batching, quantization, and autoscaling. Readers will learn how to build a performance baseline before tuning and avoid false optimizations that are fast but incorrect.
1. The Real Engineering Problem: From Model Benchmarks to Inference Efficiency
Recent industry discussions around a new generation of Chinese large language models keep returning to metrics such as peak tokens per second, throughput during the thinking phase versus the answer phase, and median response time. Setting aside specific products, these numbers point to the same engineering problem: as model capabilities converge, inference serving efficiency is becoming the core variable that determines both user experience and cost per unit.
It is important to stress that no single number tells the whole story. Peak throughput is usually achieved under a specific input length, a specific concurrency level, and a specific batching strategy. Median response time describes only half of the distribution, while the long tail is what users actually perceive as stuttering. The right approach is to interpret these metrics inside a reproducible evaluation framework rather than comparing marketing claims side by side.
2. Breaking Down Performance: Throughput, Latency, and the Long Tail
Inference performance is typically described by three groups of metrics:
- Throughput (tokens/s): the number of tokens generated per unit of time, most affected by batch size, memory bandwidth, and kernel implementations.
- Latency (TTFT / TPOT): time to first token determines how long it takes to start speaking, while time per output token determines whether the response flows smoothly.
- Long tail (P95/P99): in real workloads, tail requests determine timeout rates and retry costs.
A common misreading is treating peak throughput as the everyday operating level. Peaks are usually reached with small batches, short contexts, or a heavily optimized decoding path, whereas production traffic mixes input lengths, and long contexts significantly increase memory usage and attention computation overhead. It is therefore advisable to record peak, median, and percentile values together to form a complete performance picture.
A sample structure for collecting these metrics:
metrics = {"throughput_peak": 0, # Peak throughput"ttft_ms": [], # Time-to-first-token samples"tpot_ms": [], # Per-output-token latency samples"latency_ms": [], # End-to-end latency samples}# Before launch, run a baseline with a fixed corpus and fixed concurrency,# and report median, P95, and P99
3. Why the Thinking Phase and the Answer Phase Must Be Measured Separately
Models that expose their reasoning process usually produce a response in two segments: internal reasoning (thinking) and the final answer. The two have different computational characteristics. The thinking phase produces longer, more continuous output, while the answer phase emphasizes first-token speed and stability. Measuring throughput and latency for each segment separately is what reveals whether the bottleneck is “thinking too long” or “writing too slowly.”
In practice, you can use a unified request identifier and phase markers to persist the elapsed time of each segment, then aggregate them through a logging service and monitoring alerts. This makes it possible to split resources by phase during capacity planning: thinking-heavy scenarios should prioritize sustained throughput, while interaction-heavy scenarios should prioritize low time to first token.
4. Load Testing Methodology: Making Numbers Reproducible
Performance numbers that cannot be reproduced have no engineering value. Four categories of variables should be fixed: corpus and length distribution, concurrency gradient, batching strategy, and hardware plus runtime version. Load tests should cover the full curve from low concurrency to the saturation point, locating the inflection point where throughput stops rising and latency deteriorates sharply. That point marks the capacity limit.
# Pseudocode: sweep concurrency levels and record percentile latencyfor concurrency in [1, 4, 16, 64, 256]:run_benchmark(dataset="mixed_len_v1", concurrency=concurrency)report(percentiles=[50, 95, 99])
5. Four General Paths to Lower Cost and Higher Efficiency
- Prefix caching: reuse the KV cache for shared prefixes such as system prompts and long documents, which significantly reduces repeated computation.
- Dynamic batching: merge requests within latency constraints to improve memory and compute utilization.
- Quantization and kernel optimization: reduce weight and cache precision within an acceptable accuracy range to improve bandwidth efficiency.
- Elastic autoscaling: combine container platforms with monitoring metrics to scale automatically based on queue length and P95 latency, avoiding paying for peak capacity all year round.
All of these capabilities can be delivered by common cloud components, such as object storage for models and corpora, message queues for peak shaving, container platforms for hosting inference instances, and logging and monitoring systems for observability. Baidu AI Cloud offers capabilities in these general categories that are consistent with the industry and can serve as a reference baseline for architecture selection.
6. Implementation Advice: Build a Baseline Before Optimizing
The problem for most teams is not that they do not know how to tune, but that they lack a baseline. The recommended first step is to establish a regression-capable performance baseline with a fixed corpus. The second step is to change only one variable at a time for controlled experiments. The third step is to turn the conclusions into a capacity model and alert thresholds. Code generation scenarios should additionally introduce metrics for functional correctness and runnability to avoid false optimizations that are fast but wrong.
7. Conclusion
Competition over inference speed is ultimately competition over engineering systems. Only by putting peak, median, and long-tail metrics on the same chart, measuring the thinking and answer phases separately, and backing traffic with reproducible load testing and an elastic architecture can you achieve stable and sustainable low-latency, high-throughput performance in real workloads.
