Optimizing Local LLM Inference Speed: A Practical Guide to Tokens per Second

    This guide covers the main factors that set tokens per second when running large language models locally, including quantization, context length, speculative decoding, and multimodal overhead. It also describes a consistent way to benchmark generation speed, offers tuning advice for common workloads, and briefly compares cloud deployment as an alternative.

    A Practical Guide to Optimizing Local LLM Inference Speed

    1. Why Tokens per Second Matters

    In local LLM communities, “What is your speed in tokens/s?” is one of the most frequently asked questions. This metric shapes two parts of the user experience. First, interactive smoothness: when generation is slower than human reading speed, users feel a clear wait. Second, usability for long chain-of-thought reasoning: models with deep reasoning often produce thousands or even tens of thousands of tokens of thinking. If decoding runs at only single-digit tokens/s, one complete answer can take tens of minutes, which is unacceptable in practice.

    Take a mid-sized open-source model with about 27 billion parameters on a single high-end consumer GPU. With proper quantization and inference framework tuning, generating more than 100 tokens per second is a realistic goal. This article breaks down the key variables that affect speed and presents a systematic optimization path.

    2. Core Factors Affecting Inference Speed

    2.1 Balancing Quantization Precision and Memory Usage

    Quantization is the first hurdle in local deployment. Common options include 8-bit, 4-bit, and lower-bit mixed-precision formats. Quantization has two benefits. It reduces VRAM usage, so the large model fits fully on one card. It also reduces memory bandwidth pressure. During decoding, the bottleneck is mainly memory bandwidth rather than compute.

    Keep two points in mind:

    • Reliability of the quantized weights. Prefer officially released quantized weights, or quantize the model yourself with a mature tool that follows the official calibration process. Unverified community quantizations may show noticeable accuracy loss or even abnormal outputs.
    • Trade-off between accuracy and speed. 4-bit quantization typically shrinks the model to about one quarter of its original size, and the quality loss is manageable for most tasks. If your workload is sensitive to numerical precision, such as code generation with exact symbols, compare evaluation results before deciding.

    2.2 The Hidden Impact of Context Length

    Many developers overlook how context length affects speed. As the number of cached tokens grows, the key-value cache (KV Cache) that attention uses at each decoding step also grows. VRAM usage rises, per-step computation increases, and speed gradually drops. When you set the context window to 100K or even 180K tokens, check the following:

    • Whether the KV Cache also uses low-precision storage. Otherwise, very long contexts can exhaust VRAM.
    • Whether your application really needs the full context, or whether summarization, retrieval augmentation, or similar methods can control the effective input length.
    • Whether the time to first token (TTFT) for long contexts is acceptable, since prefill cost scales roughly linearly with input length.

    Speculative decoding and its variants have become important ways to improve decoding speed. The basic idea is simple. A lightweight draft module predicts several candidate tokens at once, and the main model verifies them in parallel. Tokens that pass verification are output together. When the draft hit rate is high, this gives a significant effective speedup, and the output distribution remains mathematically consistent with the original sampling.

    The effect depends heavily on the task type. Code and templated text are highly predictable, so their hit rates are high and the gains are clear. Highly random creative writing sees limited gains. Also, enabling the draft module uses extra VRAM and compute, so balance this in your configuration.

    2.4 Overhead of Multimodal Capabilities

    When a model also enables multimodal features such as vision, the image encoder adds VRAM usage and preprocessing time. This cost falls mainly on the input stage and does not change the decoding speed itself. In VRAM-constrained environments, however, it can squeeze the space available for the KV Cache, which indirectly reduces context capacity or batching capability. Load multimodal modules only when your application needs them, rather than enabling them all by default.

    3. A Sound Benchmarking Method

    When you compare or report speed data, use a consistent test protocol. Otherwise, the numbers have little meaning.

    1. Separate prefill and decoding speed. Record the time to first token and the steady-state decoding tokens/s separately.
    2. Fix the generation length. For example, generate 512 or 1024 tokens and average the results. This avoids errors caused by speed that drops over the course of a generation.
    3. Control the input length. State the context size used in testing. The same model can decode at noticeably different speeds with a 2K context and a 100K context.
    4. Average over multiple runs. Exclude compilation and warm-up overhead from the first run.
    5. Record the full configuration. Include the quantization format, parallelism strategy, sampling parameters, and other settings so that results can be reproduced.

    An illustrative benchmarking script skeleton:

    1. import time
    2. def measure_speed(generate_fn, prompt, gen_tokens=512, repeat=5):
    3. speeds = []
    4. for _ in range(repeat):
    5. start = time.perf_counter()
    6. output = generate_fn(prompt, max_new_tokens=gen_tokens)
    7. elapsed = time.perf_counter() - start
    8. speeds.append(gen_tokens / elapsed)
    9. return sum(speeds) / len(speeds)

    4. Optimization Recommendations for Common Scenarios

    • Long chain-of-thought reasoning. These workloads produce large outputs, so decoding speed is the top optimization target. Combine 4-bit quantization with speculative decoding, and lower the sampling temperature somewhat to improve the draft hit rate.
    • Multi-turn conversations. The priority is controlling context growth. Use session summarization, sliding windows, or similar strategies to keep the KV Cache at a manageable size and avoid speed decay over successive turns.
    • Batch offline processing. Throughput matters more than per-request latency. Increase the batch size where possible to use compute fully, rather than being limited mainly by memory bandwidth.
    • VRAM-constrained environments. First compress the KV Cache precision, then consider more aggressive weight quantization. If necessary, disable non-essential multimodal modules.

    5. Cloud Deployment as an Alternative

    Teams with limited local hardware, or those that need to handle concurrent requests elastically, can also move inference to the cloud. Baidu AI Cloud, for example, offers general-purpose cloud products such as GPU compute instances, container platforms, object storage, log services, and monitoring and alerting. Together, these can support the full pipeline from model hosting and automatic scaling to observability.

    The cloud advantage is avoiding a one-time upfront hardware investment and adjusting resources with the load. The local advantage is that data stays on-premises and there are no recurring usage fees. Developers should decide based on data compliance requirements, call volume, and budget.

    6. Conclusion

    “Tokens per second” may look like a simple number, but it depends on quantization strategy, context management, decoding acceleration, multimodal overhead, and benchmarking methodology. On a high-end consumer GPU, a mid-sized open-source model can reach more than 100 tokens per second with reasonable tuning. That requires understanding what each setting does and verifying the results with a reproducible method. We hope this framework helps developers avoid wasted effort in local inference and spend more of their time on the value of their applications.