TL;DR
Continuous batching is a way to serve a language model to many users at once. The server runs a batch of requests together, and at every generation step it can remove the requests that have finished and add waiting ones into the freed slots. Without it, a batch must wait for its longest request, so short requests sit idle in their slots. Many of the serving engines that answer chat traffic use this method.
Builds on: KV Cache, Autoregressive Model · Leads to: DistServe
Where it's used
Chat services. Thousands of users send messages at once to a language model. Replies vary from a single word to several pages. Serving them one at a time leaves the GPU mostly idle, so the server groups them into a batch. Continuous batching keeps that batch full even when the replies end at different times.
Open-source serving engines. vLLM and TensorRT-LLM are two widely used engines for running language models on GPUs. Both schedule requests so that they join and leave the running batch at each step. TensorRT-LLM calls this in-flight batching.
Offline document processing. A company may run a model over millions of documents to summarise them overnight. Each step is mostly matrix multiplication on the GPU, so the work can be batched heavily, and the same scheduling keeps throughput high even when documents differ in length.
How it works
Take a server with two slots: at most two requests can run in the same batch at each step. Each request needs a number of output tokens, and each step produces one new token for every request in a slot. Four requests are waiting in a Queue:
- R1 needs 2 tokens.
- R2 needs 4 tokens.
- R3 needs 1 token.
- R4 needs 2 tokens.
Compare two schedulers. Static batching starts a batch and runs it until every request in it is finished. Continuous batching refills a slot as soon as its request finishes.
Static batching
- Load R1 and R2 into the two slots. Run steps 1 to 4, because R2 needs four steps. Check: R1 finishes at step 2, but its slot stays empty for steps 3 and 4.
- Load R3 and R4 once the whole batch has finished. R3 needs one step and R4 needs two steps. Check: in steps 5 and 6, R3 runs only in step 5 and R4 runs in both steps, so R3's slot is empty in step 6.
- Total: six steps. The slots hold 2 times 6 equals 12 slot-steps, and only 9 of them produce a token (2 + 4 + 1 + 2). Utilisation is 9 out of 12, or 75 percent.
Continuous batching
- Step 1: R1 and R2 run. Each produces a token. Check: both slots are in use.
- Step 2: R1 produces its second and last token and leaves. Its slot is free, so R3 joins before step 3. Check: R2 is still running. Nobody waited for it.
- Step 3: R2 and R3 run. R3 produces its only token and leaves, so R4 joins before step 4. Check: R2 has now produced three tokens.
- Step 4: R2 produces its fourth and last token and leaves. R4 produces its first token. Check: R4 still needs one more token.
- Step 5: R4 produces its second token and leaves. Only one slot was in use this step. Check: total of five steps. Slot-steps are 2 times 5 equals 10, of which 9 are used. Utilisation is 9 out of 10, or 90 percent.
The flowchart is the scheduler that runs at every step:
flowchart TD
A[Start a step] --> B[Remove finished requests and free their memory]
B --> C{Free slot and memory for a waiting request?}
C -- yes --> D[Admit it and process its prompt]
D --> C
C -- no --> E[Run one generation step for every running request]
E --> F[Emit one token per request]
F --> A
The step count fell from six to five, and the utilisation rose from 75 to 90 percent. The gain comes from never leaving a slot empty while a request is waiting.
The math
Definition
Let be the number of slots in the batch, and let request need output tokens. A scheduler is continuous if, at every step, each slot holds either a running request or nothing, and a request leaves the batch as soon as its -th token is produced, which frees its slot for the next waiting request in the same step.
Work and utilisation (steps 3 and 8 of the walkthrough)
Each step produces one token for each running request. So the total useful work is the sum of the output lengths,
In the example, . The batch has slots, so over steps (the number of steps the run takes) there are slot-steps. Utilisation , the share of slot-steps that produce a token, is
Any schedule needs at least steps, because each step produces at most tokens. Here . The continuous schedule reaches this bound.
The static schedule (steps 1 to 3 of the walkthrough)
A static batch of requests with lengths runs until its longest request finishes. It takes steps and uses of its slot-steps. For the two static batches in the example:
The total is steps, which matches step 3 of "How it works".
Why the static schedule wastes slots
The waste is slot-steps for each batch. The gap is large when lengths vary. Continuous batching removes most of the gap, because a short request's slot is reused immediately instead of being held for the longest request. In the walkthrough's continuous schedule (steps 4 to 8), the same count gives wasted slot-step.
Worked examples
Two slots. Requests R1, R2 and R3 each need 1 token. All three arrive together.
- Static batching: batch 1 is R1 and R2, which takes 1 step. Batch 2 is R3, which takes 1 step. Total 2 steps.
- Continuous batching: step 1 runs R1 and R2, and both finish. Step 2 runs R3. Total 2 steps.
- Utilisation under both schedules: , , so .
Interpretation: when every request has the same length, static and continuous batching take the same number of steps, because no slot is held longer than it needs to be. The gain depends on variation in lengths.
Use the four requests from "How it works": R1 needs 2 tokens, R2 needs 4, R3 needs 1, R4 needs 2. Two slots.
- Work: .
- Static: 6 steps, from item 3 of "How it works". Utilisation .
- Continuous: R3 joins when R1 finishes at step 2. R4 joins when R3 finishes at step 3. The schedule is R1 and R2, then R2 and R3, then R2 and R4, then R4 alone.
- Continuous: 5 steps. Utilisation .
- The lower bound was , so this schedule is optimal for this workload.
(The four-request trace was already worked out in "How it works", so this example checks the same numbers in the symbols of "The math".)
Interpretation: the saving here is one step and fifteen points of utilisation. It is modest because the workload is tiny. The next example shows where the gain is large.
A chat server has 4 slots. Eight requests arrive together. One is a long reply of 400 tokens (R0). Seven are short replies of 20 tokens each (S1 to S7). Each step produces one token per running request.
- Work: tokens.
- Static batching, batch 1: R0, S1, S2 and S3. The batch lasts steps. S1 to S3 need 20 steps but hold their slots until step 400. Their slots are idle from step 20 onwards, and S4 cannot start until step 400.
- Static batching, batch 2: S4 to S7, which last 20 steps. They finish at step . The whole run takes 420 steps, and utilisation is .
- Continuous batching: R0 runs in one slot for 400 steps. The other three slots take S1 to S3 at once. They finish at step 20, and S4 to S6 take those three slots and finish at step 40. S7 takes one of the slots freed at step 40 and finishes at step 60.
- Continuous batching: the whole run is 400 steps, because R0 must produce 400 tokens one step at a time. Utilisation is .
- Latency for the short requests: under static batching, S4 to S7 finish at step 420. Under continuous batching, S1 to S3 finish at step 20, S4 to S6 at step 40, and S7 at step 60.
Interpretation: the total time barely changes, since the long reply sets the minimum. The benefit is for the users with short replies, whose last reply arrives at step 60 instead of step 420. That is the difference a chat user feels.
Try it: Suppose R0 is 800 tokens instead of 400. With continuous batching, at which step does S7 finish? Answer: step 60. The short requests run in the free slots while R0 runs in its own slot, so their finish times do not depend on R0's length.
Common mistakes and tradeoffs
It is tempting to think that continuous batching makes each token faster. In fact one step takes roughly as long as before, and the server simply spends less time with empty slots. A single request still generates at the same rate.
It is tempting to think that a bigger batch is always better. In fact each running request holds its KV Cache in GPU memory, and the cache grows with every token. Admitting too many requests exhausts memory, so a scheduler must check free memory as well as free slots before admitting a request.
It is tempting to think that a newly admitted request starts generating at once. In fact its prompt must be processed first, which produces the cache entries for the prompt. That work takes time in the same step as the running requests, so a long prompt can slow the others down. Schedulers therefore limit how much prompt work is done in one step.
When to use it: online serving with many requests of varying output lengths, where short requests should not wait behind long ones.
When not to use it: a small, fixed job where every request has the same length, or a setting with a single user, where the scheduler adds complexity and there is nothing to share. Static batching is simpler and can be enough for offline work with similar lengths.
Alternatives: static batching, which is simpler but wastes slots; separating the prompt processing and the token generation onto different workers, as DistServe does, which avoids the slowdown from long prompts; and limiting prompt work per step.
History
Batch serving was built first for fixed-size inputs, where every request in a batch takes about the same time. Language models generate text one token at a time, and their replies vary greatly in length, so static batching wastes more slots. Scheduling at each generation step, rather than per batch, is the change that fixes this. The PagedAttention paper describes a serving system that combines this step-level scheduling with a memory manager that allocates the KV Cache in small blocks as requests grow, much as an operating system pages virtual memory.
Recap
- A static batch runs until its longest request finishes, so short requests hold empty slots.
- Continuous batching checks at every step for finished requests and waiting requests in the Queue, and fills freed slots immediately.
- Utilisation is the total number of tokens divided by slots times steps. In the four-request trace, continuous batching reaches the lower bound of total tokens over slots; in general it is close to that bound when requests are short and plentiful.
- The batch is limited by GPU memory for the KV Cache as well as by the number of slots.
- Short requests gain the most. The total time of a run is still set by its longest request.
What to read next: DistServe separates prompt processing from token generation, which addresses the slowdown from long prompts mentioned above.
Check yourself
QuestionWhy does a static batch waste GPU capacity when its requests have different lengths?Show answer
A static batch runs until its longest request finishes. Requests that finish earlier leave their slots empty for the rest of the run, so those slots do no useful work.
QuestionTwo slots. Requests need 3, 1, 1 and 2 tokens, in that order, and all arrive together. Using continuous batching, how many steps does the run take?Show answer
Work is tokens. Step 1 runs the first two requests (3 and 1). The request needing 1 token finishes at step 1, so the next waiting request (1 token) joins. Step 2 runs the 3-token request and the new 1-token request, which finishes at step 2. The 2-token request joins in step 3. Step 3 gives the 3-token request its third and last token, so it finishes there. Step 4 gives the 2-token request its second and last token. The total is 4 steps. The lower bound is , so the schedule is optimal.
QuestionWhy can admitting too many requests make a server fail even when a slot is free?Show answer
Each running request holds its key-and-value cache in GPU memory, and the cache grows with every token. If there is not enough memory for the new request's cache as it grows, the server must evict requests or run out of memory, even though the number of slots is not the limit.
QuestionA scheduler admits a request with a very long prompt into a running batch. Why might the other requests slow down in that step?Show answer
Processing a prompt means computing its cache entries, which takes work in the same step as generating tokens for the running requests. A long prompt adds a lot of that work, so the step takes longer for everyone in the batch.
QuestionA batch has 4 slots. The total work is 540 tokens, and one of the requests needs 400 tokens. What is the smallest number of steps any schedule needs?Show answer
Two bounds apply. The work bound is steps, since each step produces at most 4 tokens. The long request alone needs 400 steps, because it produces one token per step. The smallest possible number of steps is the larger of the two, . The chat example reaches this bound with continuous batching.