A continuous batching scheduler (vLLM / SGLang) supports both prefix caching and chunked prefill. What happens to a new request whose first 5,000 tokens hit the prefix cache, when the total prompt is 7,000 tokens and the chunk size is 1,024? Explain how the two optimizations stack within one scheduler iteration.
The prefix cache hit skips prefill for the first 5,000 tokens. Chunked prefill then splits the 2,000-token tail into 1,024 and 976 token chunks, each batched with other requests' decode steps.
Picture a teacher who needs to read a 7-page essay before grading. With prefix caching, she realizes she already read the first 5 pages last week and has notes. She attaches those notes and starts fresh on page 6. Now she has 2 pages to read. Chunked prefill is her policy of never sitting down for a long uninterrupted read. She reads roughly a page (1,024 tokens), then briefly checks in with five other students' work in progress, then reads the rest of the second page, then checks in again. Every student gets a steady stream of attention instead of waiting an hour while she finishes one essay.
Concept explanation~2 min read
Everything you need to truly understand this topic: intuition, mechanics, step by step explanation, code, formulas, and worked example. Click to expand.
Concept explanation~2 min read
Everything you need to truly understand this topic: intuition, mechanics, step by step explanation, code, formulas, and worked example. Click to expand.
Prefix caching and chunked prefill show why serving stacks schedule around continuous batching. Each targets a different problem (redundant prefix recomputation, and head-of-line blocking from large prefills), and they combine without conflict because they act on different parts of the work. The first reduces how much prefill work exists. The second controls how the remaining prefill work is scheduled relative to other in-flight requests.
In this scenario, a new request arrives with a 7,000-token prompt. The scheduler matches its prefix against the cached KV blocks and finds a 5,000-token hit; those blocks already exist in HBM from prior requests. The scheduler reference-counts and attaches them. The remaining 2,000-token tail enters chunked prefill, which under a 1,024-token chunk size splits it into chunks of 1,024 and 976. Each chunk runs with other requests' decode steps in a single batched forward pass. From the third iteration on, the new request decodes one token per iteration like everyone else.
Prefix caching: how the cache hit happens
In SGLang, the prefix cache is a radix tree whose edges hold runs of tokens and point to their KV tensors (RadixAttention); matching works at token granularity. vLLM instead keys each full 16-token KV block by a hash of its parent block's hash and its tokens, and caches only full blocks. When a new request arrives, the scheduler matches the request's prompt tokens as far as it can. Each matched block already lives in HBM from prior requests.
The match length in this scenario is 5,000 tokens. The scheduler reference-counts the matched blocks (so they cannot be evicted while this request needs them) and attaches them as the prefix of the new request's KV cache. The cost is hashing or tree lookup plus pointer updates, small next to the prefill it skips.
The important property is that the cached blocks contain the exact K and V tensors that running prefill on those tokens would have computed. They are not approximations or summaries; they are the real KV state. The model can attend over them during later decoding without quality loss.
Eviction is the other half. When HBM fills, the cache must release blocks. Reference counts protect in-flight requests. Among unreferenced blocks, vLLM evicts in LRU order from a free queue, and SGLang evicts radix-tree leaves first in LRU order, so a shared prefix outlives the unique tails that branch from it.
Situations where this technique stops working.
2–4 min · Everything important, quickly.
Real products, models, and research that use this idea.
- SGLang's RadixAttention (Zheng et al., NeurIPS 2024) combines prefix caching with continuous batching and is a widely used reference design for this combination.
- vLLM's V1 engine enables both automatic prefix caching and chunked prefill by default.
What an interviewer would ask next. Try answering before peeking at the approach.
QHow does the scheduler decide which prefix-cache blocks to evict when HBM pressure rises?
Reference counting plus LRU on unreferenced blocks. Blocks attached to in-flight requests can't be evicted. vLLM keeps unreferenced blocks in an LRU free queue; SGLang evicts radix-tree leaves first in LRU order, so shared prefixes near the root (such as system prompts) stay longest.
Red flags & common mistakes
The phrases that signal junior thinking. Click to expand.
Red flags & common mistakes
The phrases that signal junior thinking. Click to expand.
Believing the cache hit only speeds up prefill for the hit region; it skips that prefill entirely. Or believing chunked prefill helps the cached portion; it only shapes the remaining work.
60 second bullets to scan on the way to the call.
How prefix caching attaches existing KV blocks instead of recomputing prefill
Why chunked prefill exists (head of line blocking from large prefills)
Primary sources. Browse if you want the original framing.
Same topic, related formats. Practice these next.