Sub topic 13
Reusing Shared Prompt Prefixes
This chapter assumes that you know basic tokenization, autoregressive generation, and the purpose of the KV cache. The goal is to reduce repeated prefill work when many requests begin with the same tokens.
What prefix caching does
A language model processes a prompt in two stages:
- Prefill processes the existing prompt and creates the KV cache.
- Decode generates new tokens one at a time using that cache.
Suppose every request includes the same long system prompt, policy document, or few-shot examples. Without prefix caching, vLLM runs prefill for those shared tokens on every request. With prefix caching, vLLM stores completed KV-cache blocks and reuses them when a later request has the same token prefix.
The later request still processes its unique suffix and generates its own answer. Prefix caching avoids repeated work; it does not make generation free.
How reuse is matched
vLLM compares token prefixes, not raw strings. Two prompts must tokenize to the same sequence for the shared part to match. A small change near the beginning, such as a different system-prompt version or an extra whitespace token, can prevent reuse of the blocks that follow it.
Caching works in blocks. A prefix ending partway through a block may not reuse that incomplete block. The exact block behavior depends on the vLLM version and model configuration.
The cache is bounded by available GPU memory. When vLLM needs space for active requests, older cached blocks can be evicted. Therefore, prefix caching is most useful when prefixes recur often enough and remain resident long enough to be reused.
Enable automatic prefix caching
Start the server with the feature enabled:
vllm serve Qwen/Qwen2.5-1.5B-Instruct \
--max-model-len 4096 \
--gpu-memory-utilization 0.85 \
--enable-prefix-caching \
--port 8000Requests sent to the OpenAI-compatible API need no special client parameter. Use a stable shared prefix, then put request-specific content after it:
from openai import OpenAI
client = OpenAI(base_url="http://localhost:8000/v1", api_key="EMPTY")
response = client.chat.completions.create(
model="Qwen/Qwen2.5-1.5B-Instruct",
messages=[
{"role": "system", "content": "You classify support tickets as billing, access, or bug."},
{"role": "user", "content": "I was charged twice."},
],
max_tokens=32,
)
print(response.choices[0].message.content)When to use it
| Workload pattern | Prefix caching value |
|---|---|
| Many requests share a long, stable prompt | High |
| Every prompt is unrelated | Low |
| Shared prompt changes frequently | Low |
| GPU memory is already under severe pressure | Measure carefully |
Prefix caching can lower time to first token and prefill GPU work. It may not improve latency when requests rarely share prefixes. Benchmark both enabled and disabled configurations with production-like prompts.
Exercise
Why does changing one token near the start of a shared prompt often reduce prefix-cache reuse?
Show answer
Prefix caching matches token sequences from the beginning. Once the sequence differs, the later blocks are no longer part of the same reusable prefix.