Prefix caching lets an inference server reuse key-value (KV) states for the beginning of a prompt it has already processed. A cache hit can reduce prefill work and shorten time to first token. The reusable KV blocks still occupy slots in the GPU cache pool, and each active request needs room for its own prompt suffix and generated tokens. Size the pool for the active blocks you may have to hold during a cold burst, then measure how much prefill your warm traffic actually skips.
What prefix caching reuses
A shared prefix is an identical sequence of token IDs from the beginning of the rendered prompt. Count tokens after applying the chat template and tokenizing the request. A dynamic field near the beginning of a prompt can split the cache even when most of the text later in the prompt matches. In a document assistant, a useful layout is a stable system message and document followed by the per-request question, where that ordering is compatible with the application.
The vLLM v0.15.1 prefix caching design describes how vLLM V1 hashes complete blocks using their tokens and the preceding block hash. Cache identity may also include adapter or multimodal inputs and a cache salt. The shared part ends at a complete matching block. With 16-token blocks and 4,099 matching prompt tokens, the first 4,096 are eligible under this block rule. The remaining three must be accounted for in each request’s private tail. Confirm actual matches in your engine because a block can also be missing or belong to a different cache namespace.
On a hit, the engine can reuse the matching prefix’s KV states and prefill the remaining prompt. The new suffix still attends to the earlier context, and the engine still decodes the output. Cached-token counts describe the scope of reuse. They cannot predict time to first token or billing on their own. The vLLM feature guide describes the distinction between prefill and decoding.
Budget resident KV blocks separately from prefill
Start with the KV payload per token. For a full-attention decoder with a uniform KV layout, multiply the number of layers by KV heads per layer, head dimension, bytes per element, and two for keys plus values. Use KV heads here. Query heads can overstate memory for models with grouped-query attention.
The example uses one GPU, 32 full-attention layers, eight KV heads, a head dimension of 128, and a two-byte KV cache dtype. The payload is 128 KiB per token (32 × 8 × 128 × 2 × 2 = 131,072 bytes). At 16 tokens per block, it is 2 MiB per block. The arithmetic estimates KV payload slots. Separately allow for model weights, runtime overhead, block metadata, and any engine-specific reservations. Hybrid attention, distributed KV layouts, and quantized caches need the actual per-layer or per-device allocation. The PagedAttention paper explains block allocation and sharing. ModelFit’s Paged Attention field note shows how to calculate uneven tails.
For each active request, count its private prompt tail plus its planned output, then round up to the next block. Add each distinct physical shared block once. Measure reclaimable cached blocks separately from active blocks. In vLLM V1, when no active request references a cached block, the engine can put it back in the free queue while retaining the ability to hit it until it is overwritten. Eviction releases the slot for reuse within the preallocated pool. The pool itself can remain allocated on the GPU.
Worked example with three requests
Assume three requests share a 4,096-token prefix. They each have a different private prompt suffix of 256, 512, or 768 tokens. Reserve 128 output tokens per request. With the example block size, the three private tails are 384, 640, and 896 tokens, or 24 + 40 + 56 = 120 blocks. The shared prefix takes 256 blocks. Each length happens to divide evenly by the block size.
| Cache state | Prefill tokens | Active KV |
|---|---|---|
| Cold burst | 13,824 | 888 blocks (1,776 MiB) |
| Staged cold | 5,632 | 376 blocks (752 MiB) |
| Warm | 1,536 | 376 blocks (752 MiB) |
The cold independent case is a capacity scenario for a burst in which none of the requests sees a completed reusable copy. Prefill is 3 × 4,096 + 1,536, and active capacity uses three prefix copies plus 120 private blocks. In the staged cold case, the first request populates the shared prefix before the other two look it up. Prefill is 4,096 + 1,536. In the warm case, all three requests can reuse the shared prefix. Both sharing cases use 256 + 120 blocks once the requests are active at their planned output lengths. This is a worked estimate with explicit assumptions. Actual scheduling can produce partial hits and change peak overlap.
When all three requests finish, they hold zero active blocks. If the common prefix remains indexed, its 256 blocks, or 512 MiB of slots, are potentially reusable inside the pool. Other completed blocks may be reclaimable too. If the whole prefix is evicted, the next matching request has to prefill it again, along with its own suffix. For a partial hit, count the cached initial blocks actually reused and recompute the remainder. A warm hit therefore changes work for the next arrival even when the total GPU allocation reported by the runtime stays steady. For example, request A needs to prefill 256 tokens with a full hit and 4,352 tokens after full eviction. In both cases its completed working set is 280 blocks (560 MiB) when running alone at the planned output length.
Measure reuse before increasing concurrency
- Record the model, runtime version, KV dtype, block size, chat template, and cache pool size. Use the longest relevant output budget and realistic request overlap to estimate the active working set.
- On the same engine configuration, send a cold request, repeat the stable prefix with a distinct suffix, then test a representative cold burst and a burst after cache churn. Keep the output budget and concurrency comparable. Check a restarted or freshly isolated test instance for a clean cold baseline.
- Track cached prompt tokens, time to first token, inter-token latency, and cache pressure. Note whether repeated traffic reaches the same worker and whether new requests queue or preempt existing ones during the cold burst.
- Set concurrency against the cold and mixed cases you observe in your deployment. For an independent-request baseline, use the KV cache calculator with your real model and a representative prompt plus output length. Then apply the block ledger above using the number of physical shared copies you can verify. See how context length and concurrency use VRAM for the rest of the memory budget.
Long stable prefixes with repeated traffic give prefix caching more prefill work to reuse. If traffic is mostly unique, arrives at different workers, or repeatedly evicts shared blocks, use the lower-hit capacity case until production measurements support a different assumption.
ModelFit Team · Calculation methodology
