How does prefix caching (RadixAttention) speed up multi-turn and agentic LLM workloads?
Written by the mlprep team · Updated
In multi-turn chat, RAG, and agentic workflows, requests repeatedly share long prompt prefixes. How does automatic prefix caching (such as vLLM's APC or SGLang's RadixAttention) work under the hood, and what breaks cache hit rates in production?
Why prefix caching matters so much#
In real production workloads, a huge fraction of prompt tokens are repeated across requests:
- Multi-turn chat: Turn 10 resends turns 1–9 plus one new user message.
- Agentic loops: Every tool-call step resends the full system prompt, tool schemas, and prior trajectory.
- Few-shot / RAG: Thousands of users hit the same 3,000-token instructions or shared knowledge-base documents.
Without prefix caching, every single request recomputes prefill for those identical tokens from scratch — burning GPU FLOPs and inflating Time to First Token (TTFT).
Why only prefixes can be cached (causal attention)#
Because decoder-only transformers use causal masking, the KV tensors at position t depend only on tokens 0..t.
- If two requests share an exact token prefix
0..k, their KV tensors for positions0..kare bit-for-bit identical and can be reused directly. - If even one token at position 3 differs (e.g., injecting a per-request timestamp or
request_idat the top of the system prompt), every KV tensor from position 3 onward changes and the cache is completely invalidated.
How engines implement it#
- Block-hash prefix caching (vLLM): Each fixed-size KV block (e.g., 16 tokens) is keyed by a chained hash:
hash(parent_block_hash, block_token_ids). When a new request arrives, the engine hashes its prompt block-by-block, attaches matching cached physical blocks with zero prefill compute, and only runs prefill on the uncached suffix. - RadixAttention (SGLang): Stores cached KV blocks in a radix tree (compressed prefix trie) keyed by token sequences. Matching a prefix is a fast tree walk, and eviction naturally uses LRU on leaf nodes so deeply shared roots (like global system prompts) stay pinned in GPU HBM while stale conversation tails are evicted first.
What destroys cache hit rates in production#
- Dynamic fields near the start of the prompt: Putting
Current time: 14:32:05, user IDs, or randomized few-shot order at the top of the prompt breaks every downstream block hash. - Cache-unaware load balancing: Round-robin routing across 8 GPU replicas means turn 2 of a chat lands on a different replica than turn 1, dropping hit rates by ~8×.
- Tiny KV headroom: If the KV pool is 98% occupied by active requests, idle cached prefixes get evicted immediately before the next turn arrives.
How would you design routing and memory tiering to keep prefix cache hit rates high across a multi-replica serving cluster?
Prefix-aware routing with load-aware fallback#
Instead of round-robin load balancing, use consistent hashing / prefix-tree-aware routing at the gateway:
- Hash the first
Ntokens (or session/tenant ID) to map requests with shared prefixes to the same replica (or small replica subset). - Guard against hot-spotting: Track each replica's live queue depth and KV cache utilization. If the preferred replica is overloaded (e.g., queue > threshold), spill over to the least-loaded replica (or replicate hot prefixes across multiple workers).
Hierarchical KV cache tiering (HBM -> CPU RAM -> NVMe)#
When GPU HBM is full, instead of discarding cold-but-expensive prefixes (e.g., a 32k-token document), offload evicted KV blocks asynchronously to host CPU DRAM (often 1–2 TB per GPU node) or shared distributed cache (e.g., LMCache / Mooncake). Loading a 16k-token KV cache over PCIe Gen5 (~50 GB/s) can still beat recomputing 16k tokens of prefill on saturated GPUs.
Senior/staff considerations#
- Prompt engineering as a systems optimization: Always structure prompts from most static to most dynamic:
[Global system prompt] -> [Tool definitions] -> [Shared context/docs] -> [Conversation history] -> [Dynamic timestamp / current user turn]. - In multi-tenant serving, never share KV blocks across tenants unless the prefix is explicitly public/global, or you risk subtle timing side-channel leaks.
Interview red flags#
- Suggesting you can cache arbitrary substrings in the middle of a prompt (causal attention breaks if anything before the substring changes).
- Forgetting load balancer routing — local GPU prefix caching barely helps if round-robin scatters multi-turn sessions across 20 replicas.
- Ignoring the memory-vs-recompute trade-off when offloading KV caches to CPU/disk (for very short prefixes, recomputing on GPU is faster than fetching over PCIe).
Go deeper: How KV caching works covers the per-request cache that prefix caching shares across requests.
The key takeaway unlocks after you reveal an expert answer.
Key Takeaway
Automatic prefix caching reuses KV tensors for identical leading token sequences using chained block hashes or a radix tree, slashing TTFT and prefill compute on multi-turn and agentic workloads. High production hit rates require putting static prompt sections first, using prefix-aware routing with load-skew protection, and leaving enough KV cache headroom (or CPU tiering) to prevent premature eviction.
Follow-up Questions to Test Yourself
Interviewers often pivot here once you cover the core concept.
- Why does inserting a live timestamp at line 1 of a system prompt break prefix caching, and where should timestamps go instead?
- When is recomputing a prefix on the GPU faster than fetching its KV cache from CPU RAM or network storage?
- How do you isolate prefix caches across tenants to avoid timing side-channel attacks?
How confident do you feel explaining this?
Track your understanding or flag it to revisit before your next loop.
Related Questions
Explain PagedAttention and how vLLM eliminates KV cache memory fragmentation
Walk me through post-training quantization for LLM serving: INT8, INT4 (AWQ/GPTQ), and FP8 trade-offs
How do you measure and optimize TTFT, TPOT, and goodput under production SLA constraints?
How would you design an LLM serving system for millions of users?