Continuous batching, also known as in-flight batching, allows a language model server to change, between two computation steps, the set of requests processed simultaneously. Rather than waiting for an entire group to finish, the scheduler can admit new requests and remove completed ones at each iteration. This introductory tutorial explains how this mechanism works and what constraints it imposes.
Requests neither arrive nor finish at the same time. With static batching, the server forms a group, then waits for each of its members to finish. The shortest requests then leave empty slots while the longest ones continue, and the GPU (graphics processing unit that performs the computation) may have available capacity without the frozen batch being able to accommodate new work.
In the example presented, three requests, A, B and C, arrive at different times. When A finishes, C can occupy the freed slot while B is still in progress. The tutorial specifies that the scheduler generally does not modify a GPU computation mid-iteration: the useful boundary is the model’s next step.
Each iteration can combine two types of work. The prefill phase, which processes the prompt, operates on many positions in parallel, while the decoding phase, which produces the response, generally processes one position per active sequence. Mixing the two can improve GPU occupancy, but a long prefill can delay the next token of a stream already in progress.
An iteration-level scheduler
The tutorial proposes a simulation using only Python’s standard library, which does not run a model and simply orders work in tokens. It sets three limits: a maximum of two active sequences (max_num_seqs = 2), a budget of six tokens per iteration (max_num_batched_tokens = 6) and a capacity of twenty tokens for the KV cache, the memory that holds intermediate computations for ongoing requests.
The scheduler reserves the maximum length of the prompt and output upon admission, which the tutorial describes as prudent. It first serves ready decodings, at one token each, then uses the remaining budget for prefill chunks. The first_token_at field indicates an iteration number and not a real duration: an iteration of six prefill tokens may take a different amount of time than an iteration of two decoding tokens.
Token budgets and prefill policy
Each iteration must respect both a token budget, a limit on active sequences, and the available capacity of the KV cache. Good values depend on the model, available GPU memory, expected prompt length, and latency objectives.
The tutorial also gives the equivalents in vLLM, using the facebook/opt-125m model as an example. The parameter enable_chunked_prefill=True allows a prompt too large for the remaining budget to be split and a portion of this prefill to be mixed with decoding. According to the tutorial, this setting changes the granularity of scheduling without altering the model’s context limit or reducing the total prefill computation.
Each choice involves trade-offs. A FIFO admission is predictable, but a large request at the head of the queue can block smaller ones. Fully reserving the cache avoids overload, at the cost of unused memory when requests stop early. The tutorial reminds us that the chosen policy depends on what the service prioritizes: wait time, streaming latency, throughput, fairness, or memory usage. Settings should therefore be compared on the same mix of requests, measuring wait time, TTFT (time to first token), time per subsequent token, throughput, and KV cache usage.
