Zenaique

Walk through how a prefix cache hit changes the work that chunked prefill has to do

Short answer·Medium·4.0 · 0·~3 min·Asked atFlowiseKrutrimShopify·Relevant atMicrosoftNVIDIASglang
Attempt it

A continuous batching scheduler (vLLM / SGLang) supports both prefix caching and chunked prefill. Walk through 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.

Free · 2 AI evals / day
TL;DR

Prefix cache reuses 5K tokens of existing KV, that prefill is skipped entirely.

Memory aid
Sign in to see the mnemonic that makes this stick.
Easy to grasp

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 just 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.

The composition of prefix caching and chunked prefill is one of the cleanest examples of why modern LLM serving stacks have converged on continuous batching as the central scheduling abstraction. Each optimization targets a different production pain point, redundant prefix recomputation, and head-of-line blocking from large prefills, and they compose without conflict because they operate on different dimensions of the work. The first reduces how much prefill work exists. The second controls how whatever prefill work remains is scheduled relative to other in-flight requests.

The 5K/7K/1024 scenario in the question is deliberately concrete because the choreography is what matters. A new request arrives with a 7,000-token prompt. The scheduler hashes its prefix against the radix tree of cached KV blocks and finds a 5,000-token hit, those blocks already exist in HBM from prior requests. Reference-counted, attached, done. 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 rides along with other requests' decode steps in a single batched forward pass. Three iterations later the new request is in pure decode mode, producing one token per iteration like everyone else.

This deep dive walks the data path through both optimizations, explains the implementation patterns (radix tree, reference counting, attention-mask handling for fused prefill+decode), names the failure modes when chunk size or eviction policy is tuned poorly, and connects the recipe to the production serving stacks (vLLM, SGLang, TensorRT-LLM) that ship it in 2026.

Prefix caching: how the cache hit happens

The prefix cache is typically a radix tree (SGLang's RadixAttention is the canonical implementation) whose nodes are KV blocks and whose edges are token sequences. When a new request arrives, the scheduler walks the tree along the request's prompt tokens, matching as far as it can. Each matched node corresponds to a KV block that 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. Total compute: a hash lookup per block, plus pointer updates. In wall-clock terms this is sub-millisecond regardless of the size of the hit.

The critical property is that the cached blocks contain the exact K and V tensors that would have been computed by running prefill on those tokens fresh. They are not approximations, not summaries, they are the real KV state. The model can attend over them during subsequent decoding without any quality degradation.

Eviction is the dual problem. When HBM pressure rises, the cache must release some blocks. Reference counts protect in-flight requests; among unreferenced blocks, LRU or a fanout-weighted policy picks victims. Eviction policies that overprioritize raw recency can thrash on common prefixes; ones that overweight fanout can starve niche but valuable prefixes. Production stacks tune this empirically per deployment.

Chunked prefill: how the tail gets scheduled
Why the two compose without interference
Tuning and failure modes
Sign in to unlock the full deep dive.

Situations where this technique stops working.

Sign in to see when this approach fails.

2–4 min · Everything important, quickly.

Sign in to see the quick scan of the deep dive.

Real products, models, and research that use this idea.

  • SGLang's RadixAttention combines prefix caching with continuous batching and is the canonical 2026 reference for this composition.
  • vLLM 0.6+ supports automatic prefix caching with `--enable-prefix-caching` and chunked prefill via `--enable-chunked-prefill`; teams often run both together by default.
Sign in to see more production examples.

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?
A

Reference-counting + LRU on unreferenced blocks. Blocks attached to in-flight requests can't be evicted. Among unreferenced blocks, least-recently-used go first. Some implementations bias toward keeping high-fanout prefix tree nodes (system prompts that many requests hit) over deep tail nodes.

2 more follow-ups an interviewer would ask next. Sign in to reveal them.

Red flags & common mistakes

The phrases that signal junior thinking. Click to expand.

Most common mistake

Believing the cache hit just speeds up prefill, it skips it entirely for the hit region. Or believing chunked prefill helps the cached portion, it doesn't; it only shapes the remaining work.

Sign in to see all red flags and common mistakes.

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)

Sign in to unlock the revision sheet.

Primary sources. Browse if you want the original framing.

Similar questions

Same topic, related formats. Practice these next.

4 curated
Next question
What is the KV cache in transformer inference?
Flashcard·Easy