Local LLM Inference Speed: Benchmarking and Optimization Guide
This guide explains how to benchmark local large language model (LLM) inference correctly and where the real bottlenecks are. Instead of reporting a single tokens/s number, it shows why speed must be measured as a triple: prefill and decode throughput under a specific quantization scheme, context length, and hardware/runtime configuration. It walks through the core variables that determine throughput—quantization format and precision, context length and KV cache growth, memory generation versus capacity and bandwidth, and inference engine launch parameters—then explains why coding tasks show two distinct speed phases. A reproducible measurement method and recording template are included, along with common pitfalls and a practical tuning path: secure enough capacity first, pick a quantization scheme, fix the context strategy, tune runtime parameters one by one, and build your own performance baseline.
1. Asking the Right Question: Don’t Stop at “tokens/s”
When deploying a large model locally, the most common question is “what speed do you get, in tokens/s?” That question carries too little information on its own: an isolated number says nothing about the test conditions and gives nobody a way to reproduce the result. A more meaningful answer is a triple: under a specific quantization format, a specific context length, and a specific hardware and inference configuration, what are the prefill and decode throughput figures?
Two metrics are frequently conflated and need to be separated:
- Prefill throughput: the speed at which the input prompt is processed; it determines time to first token.
- Decode throughput: the speed of token-by-token generation; it determines how fast the output reads.
The “fast” that users subjectively perceive comes from decode throughput and time to first token together. In long-document and large-codebase scenarios, however, what actually drags down the experience is usually the prefill stage.
2. Core Variables That Determine Speed
2.1 Quantization Format and Precision
Weight precision directly determines how many bytes must be moved per inference pass. In memory-bandwidth-bound environments, the decode stage is essentially “moving bytes” rather than “doing math,” so lower-bit quantization usually improves decode throughput significantly. A common community practice is to treat 4-bit-class quantization as the default tier, balancing quality against speed; precision-sensitive scenarios can move to higher bit widths, while speed-sensitive, fault-tolerant tasks can move lower.
What needs emphasis is that quantization is not a single “bit width” dimension. Within the same 4-bit tier, differences in mixed-precision strategy, group granularity, and the policy for preserving key layers produce considerable differences in both speed and quality. Any speed comparison must therefore state the specific quantization scheme, not just “4-bit.”
Illustrative quantization file naming (format explanation only):
model-id-parameters-quantization-scheme-context-length.Q4K_XL
2.2 Context Length
The context window is one of the biggest variables affecting speed, and its impact is non-linear:
- The KV cache grows linearly with context and consumes a large amount of memory.
- Attention computation grows super-linearly with sequence length, which is especially visible in the prefill stage.
- When the context is maxed out (for example, in the hundreds-of-thousands-of-tokens range), decode throughput may drop to roughly half of what it is with short contexts.
Peak speed and long-context speed must therefore be reported separately. A responsible way to state results is to give the peak decode throughput at short context together with the measured value at the target context length.
2.3 Memory Generation, Capacity, and Bandwidth
The bottleneck in local inference is usually memory bandwidth rather than compute. There is a counterintuitive phenomenon that is easy to overlook: stepping down a memory generation to gain more capacity can improve the overall experience.
The chain of reasoning:
- Model weights must reside in memory in full; insufficient capacity triggers paging or layered loading, and throughput falls off a cliff.
- More capacity allows a more generous KV cache allocation, supporting longer contexts without overflow.
- Although older-generation memory has lower per-channel bandwidth, the gain from “it fits and nothing is paged out” usually outweighs the bandwidth loss.
Practical conclusion: capacity is the first constraint, bandwidth is the second optimization item. Better to have ample capacity than to sit at the critical value and repeatedly trigger paging.
2.4 Inference Engine and Launch Parameters
With the same model and the same hardware, throughput differences between inference configurations can reach several times. The key tunable items typically include:
- The context length limit and the KV cache allocation strategy.
- Batch size and concurrency level.
- Tensor parallelism/sharding mode and thread-count binding.
- Whether warmup and memory locking are enabled.
- The effect of sampling parameters (temperature, top-p, repetition penalty) on generated length.
A common mistake is to adopt a “one-click script” of unknown origin. A safer approach is to have an AI coding tool generate a launch configuration from your actual hardware parameters, then verify the meaning of each item one by one, producing a configuration file you can reproduce yourself.
Illustrative configuration snippet (pseudocode, structure only):
runtime:context_length: 524288 # target context limitkv_cache: auto # allocate based on available memorythreads: physical_cores # bind to physical cores, avoid hyperthread contentionwarmup: truesampling:temperature: 0.7top_p: 0.95
3. Why Coding Tasks Show “Two Speeds”
In code generation and debugging scenarios, speed often appears as two clearly different ranges: the thinking (reasoning) phase is slower, and the output (final answer) phase is faster. This is not an engine anomaly but a consequence of behavioral characteristics:
- The thinking phase usually involves longer implicit reasoning chains and more frequent context look-backs, so the effective sequence is longer and attention overhead is larger.
- The output phase is relatively linear token-by-token generation with stable cache hits, so throughput is closer to peak.
- If the thinking content is kept in full in the context, the prefill cost of subsequent turns keeps accumulating.
Optimization options include: setting an explicit cap on the thinking budget, summarizing and compressing prior reasoning content, and putting large blocks of code in externally retrievable storage instead of stuffing all of it into the context.
4. A Reproducible Measurement Method
No measurement, no optimization. Fix the following conditions and then record data:
- Fix the quantization file and the context length.
- Fix the input prompts (a set of short prompts plus a set of long-document prompts is recommended).
- Record time to first token, prefill throughput, and decode throughput separately.
- Run multiple rounds at each tier and take the steady state rather than the first round (the first round includes warmup and a cold cache).
- Record peak memory usage and whether paging occurred.
Example recording template:
Quantization: 4-bit classContext: 512K / measured at 32KMemory: 128GB capacity, older generationTime to first token: x sPrefill: x tokens/sDecode (short context): x tokens/sDecode (full context): x tokens/sPeak memory: x GB / paging: none
5. Common Pitfalls and Conclusions
- Reporting only the peak without the conditions makes the data unreproducible.
- Treating peak speed as the everyday experience and ignoring long-context degradation.
- Chasing memory generation and bandwidth blindly while overlooking capacity as a hard constraint.
- Applying an external script directly without understanding what the parameters mean.
- Ignoring the effect of sampling parameters on generated length and perceived speed.
The overall practical path is: first ensure ample capacity and full model residency, then choose a suitable quantization scheme, then fix the context strategy and tune runtime parameters one by one, and finally build your own performance baseline with a unified template. If you need elastic scaling or multi-user sharing, you can containerize the inference service and run it on a container platform, using object storage to distribute model weights and logging services plus monitoring and alerting to observe throughput and latency metrics—these are general industry capability categories, and the specific implementation can be chosen for your own environment.
In related work at Baidu AI Cloud, this “build a baseline first, then tune” approach applies as well: write the measurement conditions clearly, understand what each parameter means, and only then do the speed numbers carry real reference value.
