Skip to content

Automatic Prefix Caching

Introduction

Automatic Prefix Caching (APC in short) caches the KV cache of existing queries, so that a new query can directly reuse the KV cache if it shares the same prefix with one of the existing queries, allowing the new query to skip the computation of the shared part.

Note

Technical details on how vLLM implements APC can be found here.

Enabling APC in vLLM

Set enable_prefix_caching=True in vLLM engine to enable APC. Here is an example:

examples/features/automatic_prefix_caching/automatic_prefix_caching_offline.py

Hybrid Mamba models

With prefix caching enabled, Mamba state is stored only on the Mamba block grid, so a prefix-cache hit can resume only at a block boundary. --enable-mamba-shared-prefix-checkpoint also stores a checkpoint at the shared-prefix junction, the point where an earlier request with the same prefix stopped. Requests whose shared prefix ends inside a block can then reuse it.

This helps when many requests share a long system prompt and then diverge. It is off by default, and takes effect only when all of the following hold:

  • prefix caching enabled
  • --prefix-match-unit smaller than the Mamba block size
vllm serve <hybrid-model> \
    --prefix-match-unit 64 \
    --enable-mamba-shared-prefix-checkpoint

--prefix-match-unit is required. It sets the granularity at which prefix-cache keys are computed. When unset it defaults to the greatest common divisor of the prefix-cacheable KV cache group block sizes. For hybrid Mamba models that is the block size itself, so no sub-block boundary exists and the flag has no effect.

Choose a value that divides the block size of every prefix-cacheable KV cache group, matches the block size of any group that requires block-aligned lookups (such as sliding-window attention), and is a multiple of the per-state compression ratio for models that use one, such as sparse MLA. vLLM validates these at startup and fails if an explicit value would disable partial cache hits. Read the served block size from the startup log. 64 is a reasonable starting point.

FlashInfer ReplaySSM

FlashInfer ReplaySSM caches recent inputs to reduce full-state writes during Mamba2 decoding. It is disabled by default. Both Model Runners V1 and V2 support prefix caching with ReplaySSM, including MTP speculative decoding.

This example enables ReplaySSM and prefix caching with three MTP draft tokens:

vllm serve nvidia/NVIDIA-Nemotron-3.5-Lightning-30B-A3B-NVFP4 \
    --tensor-parallel-size 2 \
    --max-model-len 12288 \
    --dtype bfloat16 \
    --mamba-backend flashinfer \
    --mamba-ssm-cache-dtype float16 \
    --enable-mamba-cache-stochastic-rounding \
    --mamba-cache-philox-rounds 5 \
    --use-replayssm \
    --replayssm-buffer-len 16 \
    --enable-prefix-caching \
    --speculative-config '{"method":"mtp","num_speculative_tokens":3}'

--enable-prefix-caching also caches Mamba states at block boundaries. To use ReplaySSM without prefix caching, replace this flag with --no-enable-prefix-caching. To disable speculative decoding, remove --speculative-config.

FlashInfer ReplaySSM has these limits:

  • Prefix caching requires BF16 model inputs for its replay rings. The SSM state dtype is independent. The example uses FP16 state with stochastic rounding.
  • The logical replay buffer cannot exceed 16 tokens. For speculative decoding, it must contain at least 1 + num_speculative_tokens tokens.
  • Pipeline parallelism, microbatching, and KV connectors are not supported.

With --mamba-backend triton (for example on ROCm), ReplaySSM also supports speculative decoding with or without prefix caching. It has the same speculative-decoding buffer requirement and parallelism limits, has no 16-token buffer cap, and does not support stochastic rounding.

Prefix reuse still requires a shared prefix with a saved Mamba checkpoint. The matching rules for hybrid Mamba models above also apply.

Gated DeltaNet ReplaySSM

Qwen3.5 and Qwen4Exp models (Gated DeltaNet hybrids) support ReplaySSM for standard decoding with --mamba-backend triton, with or without prefix caching:

vllm serve nvidia/Qwen3.5-122B-A10B-NVFP4 \
    --mamba-backend triton \
    --use-replayssm \
    --enable-prefix-caching

Speculative decoding, --enable-mamba-cache-stochastic-rounding, and a --replayssm-buffer-len above 32 are not supported. ReplaySSM pays off at serving batch sizes. On one GB300, Qwen3.5-122B-A10B-NVFP4 decodes 1.1–1.3x faster at 64–512 concurrent requests, and Qwen3.8-Flash-Next-NVFP4 1.1–1.2x faster at 256–512; with a single request both are 3–7% slower.

The replay rings add 8–9% of an FP32 SSM state to each request's state with the default 16-token buffer. Where the existing padding of the Mamba page cannot hold them, the attention block grows and the KV cache holds up to 7% fewer tokens. With BF16 models the rings keep the cached inputs in FP16, which is more precise than BF16 but has a smaller range.

With --prefix-match-unit, a request can resume inside a Mamba block, so ReplaySSM also flushes its exact state at every prefix match unit. A unit that is a multiple of the buffer length adds no flushes.

Example workloads

We describe two example workloads, where APC can provide huge performance benefit:

  • Long document query, where the user repeatedly queries the same long document (e.g. software manual or annual report) with different queries. In this case, instead of processing the long document again and again, APC allows vLLM to process this long document only once, and all future requests can avoid recomputing this long document by reusing its KV cache. This allows vLLM to serve future requests with much higher throughput and much lower latency.
  • Multi-round conversation, where the user may chat with the application multiple times in the same chatting session. In this case, instead of processing the whole chatting history again and again, APC allows vLLM to reuse the processing results of the chat history across all future rounds of conversation, allowing vLLM to serve future requests with much higher throughput and much lower latency.

Limits

APC in general does not reduce the performance of vLLM. With that being said, APC only reduces the time of processing the queries (the prefilling phase) and does not reduce the time of generating new tokens (the decoding phase). So APC does not bring performance gain when vLLM spends most of the time generating answers to the queries (e.g. when the length of the answer is long), or new queries do not share the same prefix with any of existing queries (so that the computation cannot be reused).