Skip to content
Inference Academy

Continuous batching explained: how LLM servers serve many users at once

Why batching makes LLM decode dramatically cheaper, why static batching wastes most of the GPU (with a worked example), how continuous (in-flight) batching adds and removes requests every step, how chunked prefill and preemption fit in, the settings that control it in vLLM and SGLang, and how to find the right batch size for your latency target.

Key takeaways

  • Batching is nearly free for decode: the weights are read once per step and used by every sequence in the batch.
  • Static batching wastes the GPU because short requests wait for long ones and new requests wait for the whole batch.
  • In a worked example with mixed answer lengths, static batching leaves about two-thirds of batch slots idle; continuous batching keeps them full.
  • Chunked prefill caps tokens per step so long prompts don't stall other users' streams.
  • Settings such as max-num-seqs trade throughput against per-user latency; let your latency targets decide.

By

7 min read

Share
On this page

The single biggest reason a hosted LLM costs a fraction of a cent per request is batching. And the reason modern servers batch so well is a scheduling idea called continuous batching. If you run models for more than one user, it's the first thing to understand.

This guide explains why batching works so well for LLMs, what goes wrong with the obvious way of doing it, how continuous batching fixes it, and how to tune it.

#Why batching is nearly free for decode

During decode, every step reads all of the model's weights from GPU memory to produce one token (prefill vs decode). For an 8B model in BF16, that's 16 GB per step. The arithmetic for one token is tiny by comparison, so the GPU's compute sits mostly idle.

Now process 32 users in the same step. The weights are still read once, but each weight is used 32 times. The step gets a little slower (more KV cache to read, a bit more compute), but you get 32 tokens instead of 1.

As a rough model, step time is the weight-reading time plus a small cost per sequence:

Batch size Step time (illustrative) Tokens per step Total tokens per second
1 5.0 ms 1 200
8 5.4 ms 8 about 1,480
32 6.6 ms 32 about 4,850
128 11.4 ms 128 about 11,200

These numbers use 5 ms to read the weights plus 0.05 ms per sequence for its KV cache and compute. They're a model, not a measurement, but the shape is what real servers show: total throughput climbs steeply while each user's speed falls slowly. Eventually the batch becomes compute bound or runs out of KV cache memory, and the curve flattens.

Two limits stop the batch growing forever:

  • Memory. Every sequence in the batch needs its KV cache in GPU memory. On a single GPU, that's usually the first limit (KV cache explained).
  • Compute. Each sequence adds about 2 FLOPs per weight per step. Once the batch is large enough (a few hundred tokens per step on a data-centre GPU), the step becomes compute-bound and stops getting cheaper per token.

#The problem with static batching

The naive approach collects a batch of requests, runs them together until all of them finish, then starts the next batch. That wastes a lot:

  • Short requests wait for long ones. A 20-token answer in the same batch as a 2,000-token answer occupies its slot for all 2,000 steps, doing nothing useful for 1,980 of them.
  • New requests wait for the whole batch. A request arriving one step after the batch started waits for the slowest request in it to finish.
  • Padding. Prompts of different lengths are padded to the longest, wasting prefill compute.

#A worked example

A batch of 8 requests with answer lengths of 50, 80, 120, 200, 300, 400, 600 and 1,000 tokens. Under static batching the batch runs for 1,000 steps, the length of the longest answer:

  • Slot-steps available: 8×1,000=8,0008 \times 1{,}000 = 8{,}000.
  • Slot-steps doing useful work: 50+80+120+200+300+400+600+1,000=2,75050 + 80 + 120 + 200 + 300 + 400 + 600 + 1{,}000 = 2{,}750.
  • Utilisation: about 34%. Two-thirds of the batch slots sit idle, and every new request waits up to 1,000 steps to start.

With continuous batching, each slot is refilled as soon as its request finishes, so utilisation stays near 100% as long as requests are waiting, and new requests start within a step or two.

#Continuous batching

Continuous batching (also called in-flight or iteration-level batching) makes scheduling decisions every step instead of every batch:

  1. At each step, the scheduler looks at running requests and the waiting queue.
  2. Requests that just produced their final token leave immediately, and their KV memory is freed.
  3. Waiting requests join as soon as there's memory and batch space, with their prefill run as part of the step.
  4. The step runs; repeat.
Static vs continuous batching
Animated
Static batchingstep 0
Continuous batchingstep 0

Same hardware, same requests

Gaps are wasted GPU time. Continuous batching (used by llama.cpp server, vLLM and others) finishes sooner and keeps the GPU busier, which is how one machine serves many users.

Six requests (lengths 4, 9, 3, 6, 5, 2 steps = A to F) share 3 slots. Static batching waits for the slowest request in each batch. Continuous batching refills a slot the moment it frees up.

No request waits for an unrelated one to finish, and the batch stays full as long as there's work. Combined with paged KV cache allocation (PagedAttention explained), this is what made vLLM's throughput a step change when it appeared, and it's now standard in every serious engine. The idea was first described in the Orca paper (2022) as iteration-level scheduling.

#What a step looks like inside

In a single step, the engine might run:

  • 60 decode tokens, one for each running request,
  • the first 1,000 tokens of a new request's 6,000-token prompt (a prefill chunk),
  • the whole 300-token prompt of another new request.

All of these are packed into one forward pass. The attention kernels know which tokens belong to which request and how long each request's context is, so mixing them is safe. This "ragged" batching, with no padding, is what lets the engine use every slot.

#Mixing prefill and decode

A new request needs a prefill, which is far more work per step than a decode token. If a step includes a 10,000-token prefill, every decoding user waits for it, and their streams stutter.

Chunked prefill fixes this by capping how many tokens a step can process (the "token budget"). A long prompt is split into chunks and spread across several steps, each mixed with the ongoing decodes. Streams stay smooth; the new request's time to first token goes up a little.

The budget is a trade-off:

  • A large budget (say 8,192 tokens) finishes prefills quickly, so time to first token is low, but decode steps that include a big chunk are slow.
  • A small budget (say 512) keeps decode steps steady, but long prompts take many steps to finish.

At very large scale, the next step is to run prefill and decode on separate GPUs altogether (disaggregation explained).

#The settings that matter

In vLLM and SGLang, a handful of settings decide how batching behaves:

What it controls vLLM name Effect
Max requests in a batch --max-num-seqs Higher means more throughput, slower per-user speed
Max tokens processed per step --max-num-batched-tokens The chunked-prefill budget; bigger means faster TTFT, bumpier streams
Memory for weights plus KV cache --gpu-memory-utilization More KV memory means more concurrent requests
Maximum context length --max-model-len Lower lets the engine plan for more concurrent requests

SGLang has equivalent options (such as --max-running-requests and --chunked-prefill-size). Names change between versions, so check your engine's documentation.

The trade-off you're tuning is always the same: throughput versus per-user latency. A batch of 256 serves more tokens per second per GPU but each user sees slower streaming. Your latency targets decide where to stop.

#Finding the right batch size

Using the illustrative model above, suppose your target is at least 100 tokens per second per user, which means a step time of 10 ms or less:

5+0.05B≤10⇒B≤1005 + 0.05B \le 10 \quad \Rightarrow \quad B \le 100

So you'd cap the batch at about 100 sequences, giving about 10,000 tokens per second per GPU. Relax the target to 50 tokens per second per user (20 ms steps) and the cap rises to 300, if memory allows. The same calculation with real hardware numbers is how capacity plans are built (how many GPUs do you need).

In practice, find the cap by measurement: run an open-loop load test at increasing request rates and record P95 time per output token. The highest rate that still meets your target is your capacity (how to benchmark an LLM server).

#Preemption: when memory runs out

The batch can only grow while there's KV cache memory. When a burst of long requests fills it, the scheduler must preempt: pause some requests, free their cache (recomputing it later) or swap it to CPU memory. Frequent preemption is a sign you've set concurrency higher than memory can support; it shows up as latency spikes in load tests.

#Common mistakes

  • Benchmarking at concurrency 1 and concluding a GPU is slow. Batch is where serving efficiency comes from.
  • Maximising throughput without a latency target. The highest-throughput setting is usually one users would hate.
  • Setting max-model-len to the model's full context when your traffic uses a fraction of it, which can reduce the concurrency the engine plans for.
  • Ignoring preemption counters in engine metrics.
  • A huge prefill budget with strict streaming targets, causing visible stutters whenever a long prompt arrives.

#FAQ

Does continuous batching change the model's outputs? It shouldn't change them in any meaningful way, but because the batch composition affects the order of floating-point operations, outputs aren't always bit-for-bit identical across runs. Engines offer batch-invariant modes when exact reproducibility matters.

Does llama.cpp do continuous batching? Yes, across its parallel slots, which works well for a handful of users (llama.cpp vs vLLM).

Is bigger batch always cheaper per token? Until the step becomes compute-bound or memory runs out. After that, per-token cost stops falling and latency keeps rising.

#Go deeper

The scheduling, chunked prefill and preemption lesson covers how engines decide what runs each step, and the first benchmarking lab has you tune these settings on a real engine. Both are part of the Inference Engineering course.

batchingservingthroughputvllminference-engineering

Found this useful?

Send it to someone who is learning the same thing.

Share this article

Next: learn it step by step, free

Capacity planning and cost

Turn traffic and latency targets into a GPU count and a monthly bill. A free lesson with step-through diagrams and a quiz.

Get new articles by email

One short email when a new guide is out. Unsubscribe any time.