What is continuous batching in LLM serving?
Continuous batching is a scheduling method for LLM inference that adds new requests to a running batch and removes finished ones at every decode step, instead of waiting for a whole batch to finish. A server decodes many sequences together because one read of the model weights then serves all of them. With continuous batching, a slot freed by a short answer is refilled on the next step, so the GPU rarely sits idle while a long answer finishes.
Every major LLM serving engine uses it, including vLLM, Hugging Face Text Generation Inference, TensorRT-LLM (where it is called in-flight batching), and SGLang. It usually comes paired with a paged KV cache, because the number of sequences a GPU can run at once is limited by cache memory. Together these two ideas raise serving throughput several times over request-level batching, and they shape the latency figures users see: time to first token and time per output token.
Key ideas
- Decoding is memory-bound, so batching sequences shares one read of the weights and raises throughput.
- Static batching holds every slot until the longest request in the batch finishes, which leaves slots idle.
- Continuous batching admits and retires requests at every decode step, keeping slots full.
- The KV cache, not compute, usually limits how many sequences fit in a batch.
- PagedAttention stores the cache in fixed-size blocks, cutting waste to at most one partial block per request.
- Larger batches raise throughput but slow each token, and chunked prefill limits the stalls new prompts cause.
Common questions
How is continuous batching different from dynamic batching?
Dynamic batching collects requests that arrive within a short time window and runs them as one batch from start to finish. It works well for fixed-size work such as image classification. Continuous batching changes the batch at every token step, which suits generation, where output lengths vary widely and are unknown in advance.
Does continuous batching change the model's output?
No. Each sequence attends only to its own tokens, so other requests in the batch do not affect its logits. Small numerical differences can appear because kernels sum in a different order for different batch shapes. With greedy decoding this can occasionally flip a near-tie between two tokens.
What happens when the KV cache fills up?
The scheduler stops admitting new requests, and if running sequences still need more blocks, it preempts some of them. vLLM either swaps the preempted blocks to CPU memory or frees them and recomputes the cache later from the tokens already generated. Preempted requests resume once memory frees up.
Why do serving benchmarks report both throughput and latency?
A server can raise total tokens per second by packing more sequences into each pass, while each user sees slower tokens and a longer wait for the first one. Throughput measures cost per token, and latency measures the experience of one user. Serving limits are usually set as a latency target, with throughput maximized under it.
Related explainers
- KV Cache — Why LLM generation caches keys and values, and what it costs
- Decoder-Only Transformers — The architecture behind GPT and modern LLMs
- Quantization — Shrinking model weights to 8 and 4 bits
- Mixture of Experts — Routing each token to a few experts in a much larger model
- Sampling: Temperature, Top-k & Top-p — How a language model picks its next token
- How Self-Attention Works — Query, key, value — the core of self-attention
References
- Yu et al. (2022), "Orca: A Distributed Serving System for Transformer-Based Generative Models", OSDI '22
- Kwon et al. (2023), "Efficient Memory Management for Large Language Model Serving with PagedAttention"
- Agrawal et al. (2024), "Taming Throughput-Latency Tradeoff in LLM Inference with Sarathi-Serve"
- Pope et al. (2022), "Efficiently Scaling Transformer Inference"
Last updated