Batch Inference with LLMs: A Throughput Optimization Guide for Engineers
Most LLM deployments leave GPU capacity on the table. A single request at a time keeps the accelerator busy only during short bursts of math, and the rest of the time it waits on memory reads. Batch inference is the fix for that waste: you serve many requests together so one pass through the model does work for all of them at once. Done well, it raises tokens per second per GPU without buying new hardware. Done carelessly, it tanks response times and fills your KV cache until the server errors out. This guide covers how batching actually works in modern serving engines, where the real limits are, and how to tune them step by step.
What batch inference really does
An LLM generates text one token at a time. Each step (a “forward pass”) reads the model weights from GPU memory and produces one new token per sequence. Serving one user request alone means every pass loads the full weight matrix to produce a single token. That is the definition of memory-bandwidth-bound. Add a second, third, or thirtieth request to the same pass and the weight read is shared across all of them: the cost of reading weights stays roughly the same while the useful output multiplies. That amortization is the entire point of batching.
There are two phases in every request. Prefill processes the whole prompt at once and is compute-heavy. Decode generates tokens one by one and is memory-heavy. A request spends most of its life in decode, so throughput optimization is mostly about packing decode steps efficiently.
Static batching vs. continuous batching
The old approach, static batching, groups requests into fixed-size batches: wait until N requests arrive, run them all to completion, then start the next group. It works, but it has a painful flaw: if one request in the batch generates 2,000 tokens and the others generate 50, the short ones finish early and the GPU keeps grinding through the long one while the finished slots sit idle. Engineers call this the straggler problem, and in practice it eats a large share of the gains.
Continuous batching (also called iteration-level scheduling, popularized by vLLM) removes the fixed batch entirely. Instead of running one batch to completion, the engine re-evaluates the schedule at every single forward pass: a finished request’s slot is immediately handed to a waiting request, and new prompts can join mid-generation. There is no padding to the longest sequence and no waiting for stragglers. In real deployments this single change is usually the biggest throughput win available, especially under mixed-length traffic.
If your serving stack still uses static batching, switching to a continuous-batching engine is step zero. Everything below assumes you are on one.
The KV cache is the real bottleneck
Every token an LLM generates needs context about all previous tokens. The model stores that context in the KV cache, a per-request, per-layer data structure that grows with sequence length. Doubling your batch size means doubling your KV cache footprint, and GPU memory is finite. This is why batch inference usually hits a memory wall long before it hits a compute wall.
The cache size per token depends on the model: more layers and larger hidden dimensions mean more bytes per token. Long-context models and long outputs multiply the cost. When the engine runs out of KV cache space, it has to pause or evict requests, which shows up as latency spikes or outright failures. Two practical consequences follow:
- Short sequences batch far better than long ones. A workload of 200-token chat replies can run a much larger batch than a workload of 8k-token document summaries on the same GPU.
- Precision matters. KV cache quantization (storing cache in 8-bit instead of 16-bit) roughly halves the memory per token, which roughly doubles the number of concurrent sequences you can hold. It is one of the cheapest throughput wins for offline or latency-tolerant workloads, though you should verify output quality on your own evals before enabling it in production.
vLLM’s PagedAttention, which manages the KV cache in fixed-size blocks instead of contiguous memory, exists precisely to reduce fragmentation here. If you are running vLLM or a similar engine, this is already working for you.
Tuning knobs that actually move throughput
Most serving engines expose a handful of scheduling parameters. They have different names across projects (vLLM, TensorRT-LLM, SGLang), but the concepts map one-to-one.
1. Max concurrent sequences
This caps how many requests can be in flight at once. Raise it and throughput climbs, until the KV cache fills and requests start getting paused. The right value is the largest number your GPU memory can sustain for your typical sequence lengths, not the largest number the config file allows. If you see pause-and-resume behavior in your logs, you have set it too high.
2. Max tokens per forward pass
This caps the total tokens processed in one iteration across all sequences (sometimes called max_num_batched_tokens in vLLM). Prefill chunks are the reason it matters: a 4,000-token prompt arriving in one pass can block decode steps for everything else. Chunked prefill splits that prompt across multiple passes so decodes keep flowing. Keep this cap high enough that prefills do not stall, but low enough that a single large prompt cannot starve the queue.
3. Prefill-to-decode ratio
Some engines let you control how aggressively prefills are prioritized over decodes. Prioritizing prefill reduces time-to-first-token but can delay already-running generations. For throughput-focused offline workloads, let decodes drain; for chat workloads where users watch the first token appear, bias toward prefill. There is no universal answer; it depends on which latency your users notice.
4. Queue and timeout behavior
When requests arrive faster than the engine can drain them, the queue grows. Decide what happens at saturation: reject with a clear error, shed load, or let latency degrade gracefully. An explicit policy beats silent degradation every time, because silent degradation is how you discover the problem from your customers.
Latency vs. throughput: choose deliberately
Throughput and latency pull in opposite directions. Maximum throughput comes from packing as many sequences as possible into each pass. The catch: every sequence added to a pass makes that pass take slightly longer, so each individual request gets slower. An interactive chatbot needs low time-to-first-token and steady token streaming, which argues for smaller batches. An offline job (classifying a million documents overnight) does not care about per-request latency at all, which argues for the largest batch memory allows.
The common mistake is optimizing one metric while measuring the other. Pick your target first: if you are serving users in real time, optimize p99 latency under your SLO and accept whatever throughput that gives you. If you are running batch jobs, maximize tokens per second per GPU and let latency be whatever it is. Write the target down before you touch a config.
A practical tuning workflow
Do not guess. The tuning loop is short and every step is observable:
- Baseline with your real traffic. Replay a representative sample: same prompt lengths, same output lengths, same arrival pattern. Synthetic uniform traffic lies about throughput because it does not reproduce the stragglers and bursts your engine actually faces.
- Raise max concurrent sequences in steps. Increase by roughly 25% at a time, measure tokens per second and p99 latency at each step, and stop when latency degrades past your target or you see cache evictions in the logs.
- Enable chunked prefill and KV cache quantization (if your quality bar allows), one change at a time, re-measuring after each.
- Stress the edges. Test with a burst of long prompts, a burst of short prompts, and a mix. The configuration that survives all three is the one you ship.
- Monitor in production. Track tokens per second, KV cache utilization, queue depth, and p99 latency on a dashboard. Traffic patterns drift; retune when the mix changes.
One change at a time is the whole discipline. Two simultaneous changes that improve throughput leave you unable to say which one did the work, and you will need that knowledge the next time traffic shifts.
Putting it together
Batch inference is how LLM serving is supposed to work. Continuous batching fixes the straggler waste of static batches, the KV cache sets your real ceiling, and a handful of scheduling knobs trade latency against throughput. Measure with your own traffic, tune one knob at a time, and keep an eye on cache utilization in production. The GPU you already have is probably capable of more than it is doing right now.