Chunked Prefill: Steadier LLM Token Streams

Chunked prefill slices a long prompt into pieces so decode steps never stall behind it. How vLLM's default scheduler keeps token streams smooth under load.

A few weeks ago I wrote about prefill/decode disaggregation and ended it by naming a cheaper lever you should try first: chunked prefill. This post is that lever, on its own. It is for engineers who already serve an open model behind an endpoint, with vLLM or something like it, and who have noticed their token stream stutter when a long prompt shows up. Disaggregation fixes that by buying a second pool of GPUs. Chunked prefill fixes a lot of it by changing how one GPU schedules its work, and it costs you nothing but a config value.

The symptom is the same one I keep coming back to. You are streaming tokens to a dozen users, the cadence is smooth, and then a request with a 3,000-token context lands. For one beat the stream to everyone else freezes, then resumes. That freeze is not random. It is the shape of how most engines used to schedule the two phases of inference, and it is exactly what chunked prefill is built to remove.

Why a long prompt stalls everyone else

An autoregressive model answers in two phases with different hardware profiles, which I broke down in prefill vs decode. Prefill reads the whole prompt in one forward pass and produces the first token. It is compute-bound, and its cost grows with prompt length. Decode then generates the rest of the answer one token at a time, reading the KV cache and the weights out of memory on every step. It is memory-bound, and a single decode step is cheap.

Continuous batching keeps the GPU busy by running many requests in one batch and refilling finished slots every step. The question it does not answer on its own is what to do when a new request arrives mid-stream and needs a big prefill. The simplest policy, and the one vLLM shipped for a long time, is to run that prefill first: admit the request, do its full prompt pass, and only then get back to decoding everyone else. On a 3,000-token prompt that is one very expensive step, and every request that was mid-answer waits behind it. The person watching their screen sees one token take several times longer than the last. That is the stall.

A streaming request's inter-token latency drawn step by step. In the prefill-first case a wide block interrupts the row where a 3,000-token prefill runs, leaving a visible gap; in the chunked-prefill case the steps stay evenly spaced because a small prefill chunk rides along each one.

What chunked prefill actually does

Chunked prefill makes two changes to that policy. First, it flips the priority: decode requests go into the batch before any prefill, so the users already receiving tokens keep receiving them. Second, instead of running a long prompt’s prefill as one giant step, it slices the prompt into pieces and feeds one piece per step, fitting each piece into whatever room is left after the decodes are placed.

The mechanism hangs on a single number, the per-step token budget, which vLLM calls max_num_batched_tokens. Every scheduler step may process at most that many tokens. The scheduler fills the budget with the running decode requests first (one token each, one per request), then spends whatever budget remains on a slice of a pending prompt. If the prompt does not fit in the leftover room, it is chunked, and the rest waits for the next step.

Four scheduler steps shown as token-budget bars of fixed width. In each step a small green block of decode tokens is placed first, then a prefill chunk fills the rest of the budget. A 1,500-token prompt is consumed across the steps, and the final step leaves free budget where the next prompt can begin.

So the chunk size is not a separate knob. It falls out of the budget: chunk size is roughly the budget minus however many decode tokens are in flight. A long prompt is simply carried across however many steps it takes, and on every one of those steps the decodes still run. The big compute-bound prefill work and the cheap memory-bound decode work ride in the same batch, which is also why it helps throughput on paper: the GPU’s math units are busy with the prefill chunk while the decodes, which would otherwise leave those units idle, come along almost for free. This piggybacking is the original insight, and I will get to where it came from.

The practical effect is the one in the hero diagram. The long prefill no longer appears as a single wide stall. It shows up as a small, constant tax spread across several steps, so inter-token latency stays close to flat. Tokens arrive on a steady beat. The total time to finish that long prompt’s prefill goes up a little, because you are doing it in slices with decode work in between, but nobody’s stream freezes.

The one knob: maxnumbatched_tokens

In current vLLM (the V1 engine), chunked prefill is enabled by default, so the thing you actually tune is the budget. It is worth understanding which way to turn it, because the default is not right for every workload.

A smaller max_num_batched_tokens means smaller prefill chunks per step, so a long prompt disturbs the ongoing decodes less and inter-token latency stays smoother. A larger budget lets more prefill into each step, which gets a new request to its first token faster and raises raw throughput, at the cost of more jitter for the requests already decoding. The vLLM optimization docs put the tradeoff plainly: smaller values favor inter-token latency, larger values favor time to first token. The default sits around 2,048 tokens in recent releases (older versions used 512), and the docs note it is tuned for latency rather than peak throughput, so check the value for the version you are running rather than trusting a number from a blog post, including this one.

# Chunked prefill is on by default in vLLM V1; this sets the per-step budget.
# Lower it for steadier inter-token latency, raise it for faster TTFT / throughput.
vllm serve <model> \
  --max-num-batched-tokens 2048

Two related settings matter once prompts get very long. long_prefill_token_threshold marks a prompt as “long,” and max_long_partial_prefills caps how many long prompts can be prefilled at once, which lets a short prompt jump ahead of a giant one instead of waiting for it to finish chunking. If your traffic mixes quick questions with occasional huge contexts, those are the levers that keep the quick ones quick. As always, the flag names drift between releases, so confirm them against the docs for your version.

Where the idea comes from

This is not a vLLM invention, and it is worth reading the source because the papers explain the tradeoff better than any release note. The piggybacking idea comes from SARATHI (Agrawal et al., Microsoft Research India, 2023). Their observation was that a decode-only batch wastes GPU compute, because generating one token per request barely touches the math units, while a prefill saturates them. Build each batch around one prefill chunk and fill the rest with decodes, and the decodes cost up to an order of magnitude less than they would in a batch of their own.

The follow-up, Sarathi-Serve (Agrawal et al., 2024), is the one that frames the online-serving tradeoff directly. It calls the technique stall-free batching: new requests join a running batch without pausing the decodes, by throttling the number of prefill tokens admitted per iteration. The paper states the dilemma that the budget knob resolves, that prioritizing prefills helps throughput but hurts tail time-between-tokens, while prioritizing decodes does the reverse. Chunked prefill is how you stop choosing. Databricks’ inference performance guide is a good companion if you want the hardware-level framing of why the two phases behave so differently in the first place.

Tradeoffs and failure modes

Chunked prefill is close to a default-on win, but it is not free, and a few things are worth knowing before you lean on it.

It can lower peak throughput slightly. The vLLM docs are honest that the latency-tuned default may not hit the tokens-per-second a prefill-prioritizing scheduler would. If you are running an offline batch job where nobody is watching a stream and all you want is throughput, a larger budget, or less aggressive chunking, can be the better call. The smooth-stream default is built for interactive serving.

Very long prompts still cost what they cost. Chunking spreads the prefill out; it does not make it cheaper. A 100,000-token context is still a large amount of compute, now split across many steps, and the first token for that request will not be fast. Chunked prefill protects the other users from it, which is the point, but it is not a context-length optimization. For that you want prefix caching, which skips prefill entirely for prompt prefixes you have seen before and removes the work rather than rescheduling it. The two stack well: cache what you can, chunk what is left.

And chunked prefill does not add capacity. If your GPUs are genuinely saturated, slicing the work differently will not create headroom, it only changes who waits. This is the line where the cheaper lever stops being enough and disaggregation, or simply more hardware, becomes the real answer.

What I reach for first

The order I actually use is cheapest-first. Confirm continuous batching is on. Turn on prefix caching so repeated prefixes skip prefill. Make sure chunked prefill is on, which on current vLLM it is, and then tune max_num_batched_tokens toward smoother inter-token latency if the stream feels jumpy, or toward throughput if it does not and you are chasing tokens per second. Only when prompts are long, varied, and heavy enough that one shared pool genuinely cannot hold the tail steady do I reach for disaggregation.

That spread of prompt lengths is exactly the profile behind Archi, the retrieval copilot I worked on for CMS operations at CERN. A one-line question would sit next to a request that pulled in several retrieved documents and ran to many thousands of tokens, all hitting the same endpoint. Chunked prefill is the setting that keeps the short questions responsive while the long ones are being read, without a second GPU pool or a line of application code. It is the quiet default that earns its place, and the one I check before touching anything more expensive.


Image credit: diagrams by Muhammad Hassan Ahmed, released under CC0. No third-party images were used.