0%
LLM Inference and Serving: GPUs, Batching, and Cost
How Inference Works
Hardware and Memory
Serving Systems
Continuous Batching
Batching is the single most important technique for making LLM serving affordable. Because decode is memory bound, a GPU can process many sequences in one step for nearly the cost of one, so serving many users together multiplies throughput. But LLM requests arrive at different times and finish at different lengths, which makes batching them well surprisingly hard. This lesson explains why simple batching fails for LLMs, how continuous batching solves it, how schedulers decide what runs each step, and how to tune batching for latency and throughput. This first section revisits why batching matters so much.
The economics of a decode step
A decode step for a 16 gigabyte model on an H100 reads the weights once, about 5 milliseconds, regardless of whether it generates a token for one sequence or for sixty four. The arithmetic for sixty four sequences finishes long before the weights arrive. So the step costs nearly the same whether it serves one user or sixty four, and the cost per token falls almost in proportion to the batch size until cache reads and compute start to bind. A single user decoding alone uses well under one percent of the compute of the GPU.
This is why serving providers can sell tokens cheaply. A GPU that rents for a few dollars an hour can produce thousands of tokens per second when batching well, but only dozens when serving one request at a time. The difference between good and bad batching is often a factor of ten or more in cost per token, larger than the effect of any other single optimization.
Batch size and latency
Batching is not free for each user. Every additional sequence adds its KV cache reads to each step, and eventually adds compute, so step time rises with batch size. Each user in a batch of 128 waits slightly longer for each token than a user alone. The design goal is to batch as many sequences as possible while keeping time per output token within the latency objective. Most of this lesson is about how to achieve that goal under real traffic.
Why LLM batching is different
Batching is standard for other neural networks: an image classifier collects a batch of images, runs one forward pass and returns all results. LLM generation does not fit this pattern because each request is a loop of many steps, and the number of steps differs per request. One user asks for a one word answer; another asks for a 2,000 token essay. Requests also arrive continuously rather than in groups. A batching approach must cope with sequences joining and leaving at arbitrary times, without making short requests wait for long ones.
Two phases in one batch
Each request also has two phases with different costs. Prefill processes the whole prompt at once and is compute heavy; decode generates one token at a time and is memory bound. A new request entering a running batch needs prefill, while existing requests need decode. How the scheduler mixes these in each step decides the trade off between TTFT for new requests and TPOT for running ones, the subject of the third section and of lesson thirteen.
Measuring batching effectiveness
The effectiveness of batching shows in a few metrics: the average number of running sequences per step, the tokens generated per step, GPU utilization and throughput in tokens per second. A replica under heavy load with only a handful of running sequences is batching poorly, perhaps because of an engine setting, memory limits or an inefficient scheduler. Watching running sequences alongside throughput and latency gives an immediate sense of whether batching is working.
What limits the batch
Three limits cap the batch at any moment: the number of requests available to run, the KV cache memory to hold their contexts, and the latency objective. At low traffic, there simply are not enough requests, and the GPU is underused whatever the scheduler does. At high traffic, memory or latency binds. Lesson six covered the memory limit; this lesson focuses on how the scheduler uses whatever batch the limits allow.
Offline and online batching
Offline workloads, such as processing a million documents overnight, have all requests available at once and no per request latency objective. They can run enormous batches limited only by memory, and achieve the highest throughput the hardware allows. Online workloads, such as chat, must respond quickly to requests arriving one at a time. The same engine serves both, but with different settings: offline jobs maximize batch size, while online serving caps it to protect latency. Many providers offer cheaper batch APIs precisely because offline batching is so much more efficient.
Batching across users and within a user
Batching usually means combining requests from different users. A single user can also be batched with themselves: parallel sampling generates several candidates for one prompt together, and agent frameworks may issue several independent model calls at once. These patterns share the same mechanics, and they all benefit from the scheduler being able to combine sequences of different lengths and stages into one step.
A worked example of batching economics
Consider an 8 billion parameter model in 16 bit precision on one H100 rented at about three dollars an hour. A single decode step reads 16 gigabytes of weights at roughly 3.3 terabytes per second, which takes about 5 milliseconds. Serving one user at a time, the GPU produces about 200 tokens per second, or 720,000 tokens per hour, so each million tokens costs roughly four dollars. Serving sixty four users together, the step grows to perhaps 7 milliseconds because of cache reads, but each step now yields sixty four tokens, about 9,000 tokens per second. That is over 32 million tokens per hour, and the cost per million tokens falls to about ten cents. The hardware, the model and the price of the GPU are identical; only the batch changed.
This arithmetic explains why public price lists for open models are so low, and why a team self hosting the same model with poor batching can pay thirty times more per token than a provider. It also explains why idle capacity is so expensive. A replica that runs with an average batch of four during the night costs the same per hour as one running a batch of one hundred during the day, so the effective cost per token at night can be many times higher.
Where the free ride ends
The free ride lasts only while the weight read dominates. Each sequence adds its own cache reads, which grow with its context length, and at some batch size the arithmetic itself starts to bind. For short contexts on a large GPU, that point may be hundreds of sequences away; for long contexts, the cache reads catch up with the weight read after only a few dozen sequences, and step time starts to climb in proportion to the batch. Lesson five described this transition with the roofline. In practice, engineers measure step time at increasing batch sizes for their real context lengths and find where it begins to rise steeply, which marks the practical ceiling for that workload.
Throughput and latency are not opposites everywhere
It is common to say that batching trades latency for throughput, but in the memory bound region the trade is very mild. Going from one to thirty two sequences might raise time per output token by twenty percent while raising throughput by twenty five times. Only near the ceiling does each extra sequence cost a noticeable amount of latency. This is why well run services keep batches large: the latency cost of the first few dozen sequences is small, and the savings are enormous. The latency objective matters mainly near the top of the range, where the scheduler must stop admitting work.
Why the request queue matters
A batch can only include requests that are waiting or running. If clients send one request and wait for its full response before sending the next, the server sees far fewer concurrent requests than the number of users would suggest. Streaming, asynchronous clients and agent frameworks that issue calls in parallel all raise the concurrency the server sees. When designing a system that calls an LLM many times, such as a document pipeline, issuing requests concurrently rather than sequentially is often the difference between minutes and hours of runtime on the same hardware.
Because decode reads the weights once per step regardless of batch size, the number of sequences sharing each step largely determines cost per token. Good batching is often a ten times difference in cost.