Sub topic 8
Continuous Batching for LLM-Style Workloads
Assume you understand model-serving endpoints, p50 and p99 latency, and basic batching. This chapter focuses on autoregressive large language models (LLMs), which generate output one token at a time.
Why ordinary batching wastes capacity
A static batch contains requests that start and finish together. This works well when every input has a similar processing cost. LLM requests rarely behave that way. One prompt may produce 20 tokens, while another produces 1,000.
With ordinary dynamic batching, the server may wait for the whole batch to finish before starting another batch. Short requests then leave unused space while long requests continue. New requests must wait, even when some batch slots are free.
Continuous batching changes the scheduling unit from a complete request to a generation step. After each step, the server removes completed sequences and inserts waiting sequences into the available slots. The batch can therefore change while generation is in progress.
Prefill and decode
LLM inference usually has two phases:
- Prefill processes all prompt tokens. It builds the model's internal state for the request.
- Decode generates one new token per iteration. Each iteration uses the existing state and appends a token.
That state is commonly stored in a key-value (KV) cache. The cache avoids recomputing earlier tokens, but it consumes accelerator memory. A scheduler must limit both the number of active requests and the total number of cached tokens.
Continuous batching is most valuable during decode. A scheduler can run one decode step for all active sequences, remove completed sequences, and admit new ones before the next step.
A simplified scheduler
The following example models the scheduling idea. It is not an LLM server because it does not allocate a KV cache or execute a neural network.
from collections import deque
waiting = deque([
{"id": "A", "remaining": 3},
{"id": "B", "remaining": 1},
{"id": "C", "remaining": 2},
{"id": "D", "remaining": 4},
])
active = []
max_active = 2
while waiting or active:
while waiting and len(active) < max_active:
active.append(waiting.popleft())
for request in active:
request["remaining"] -= 1
print(f"generated a token for {request['id']}")
active = [request for request in active if request["remaining"] > 0]Request B finishes after one step. Request C can enter the next step without waiting for request A to finish.
Scheduling trade-offs
Continuous batching improves accelerator utilization and often increases throughput. It also introduces scheduling decisions:
- Admit too many sequences, and KV-cache memory can be exhausted.
- Admit long prompts without limits, and prefill can delay decode steps.
- Always favor short requests, and long requests may starve.
- Give every sequence equal treatment, and expensive sequences can reduce throughput.
Useful controls include maximum active sequences, maximum total tokens, maximum prompt length, output-token limits, and a fairness policy. Some serving systems separate prefill and decode work or use token budgets to prevent one iteration from consuming excessive compute.
> callout tip > Measure time to first token separately from time per output token. Continuous batching can improve total throughput while increasing the waiting time before a new request receives its first token.
When to use it
Use continuous batching for streaming, variable-length generation workloads where requests overlap in time. Use simpler static or request-level dynamic batching for short, uniform predictions. Select a serving system that exposes queue, active-sequence, KV-cache, time-to-first-token, and per-token latency metrics.
Exercise
Why can continuous batching admit a new request before every request in the current batch finishes?
Show answer
It reschedules after each decode iteration, so completed sequences release slots that waiting sequences can use.