Inference Engineering

Lesson 6 of 14

Continuous batching

A language model serves many chats at once, and their answers finish at very different lengths. If the server waits for the longest answer before taking anyone new, finished requests keep their seats doing no useful work and short answers sit finished but undelivered. This lesson shows the fix, refilling every free seat after each round, and how much it buys.

One sushi counter, two ways to seat it

One shot, three stages. Stages 1 and 2 seat the same 21 diners for 14 rounds on 6 seats, and price every round at its best-case time for Llama 3.1 8B on one NVIDIA H100, each diner 600 tokens into the conversation. It plays by itself; press a stage to replay it, or drag to look around. The shelf in stage 3 is to scale along its length.

01 · One round

Every seat rides the same trip

A chat server works like a sushi counter. The chef is the GPU, the chip that runs the model. Each diner is a request, and each piece is a token (a token is a piece of a word, about three-quarters of one on average). In a round, one pass through the model, the chef serves one piece to every seated diner. The diners served together are the batch.

Each round starts with a trip to the fridge for the model’s weights, its billions of numbers (lesson 1). The trip costs the same for one diner or 8. Each diner adds only a little: reading their own cards, the KV cache from lesson 4. So an empty seat is a token the chef could have made almost for free.

One round, as the seats fill

Llama 3.1 8B on one H100. Plays by itself; move the slider to take over.

  • Fetching the weights: once per round, shared by every seat
  • Reading each diner's own cards (KV cache)
  • A round with one seat

The best case for one round of Llama 3.1 8B on one NVIDIA H100, each diner 600 tokens into the conversation. The slider doubles the seats at each step.

02 · Two hosts

Wait for the group, or refill every round

Diners order wildly different amounts, and nobody knows how much until they stop. The simplest host seats a group and seats nobody new until the whole group has finished. This is static batching. A diner who wanted two pieces keeps her seat, the chef keeps serving her, and her bill comes with everyone else’s.

That is why a one-line answer waited for an essay. With continuous batching the host looks along the counter after every round: anyone finished leaves at once, and the next person in line sits down before the next round starts.

Ten diners, four seats, one round at a time

Wait for the whole batch static batching

In line
Answered

Refill after every round continuous batching

In line
Answered

  • Token written (colour = request)
  • Token still to write
  • Finished, still holding its seat (it flashes: the GPU still works on it, for nothing)
  • E · 3 tokIn line: request, tokens it needs
  • B · 12Answered: request, rounds from arriving to answer

A toy: 4 seats, 10 requests, every round counted as one tick of time. Request A is the essay and B the one-line answer. The empty boxes show each answer's length so you can follow along; a real server only finds out when the model writes its last token.

03 · At real scale

One line grows, the other drains

The lesson’s simulator serves 100 requests of random length, arriving about 5 a second, on 8 seats. Under static batching, finished requests sat in their seats for 49% of all seat-time, so the server fell behind and the line kept growing. Continuous batching kept up with the same arrivals.

Short answers felt it most. Up to 50 tokens, they took 10.2 s on average under static batching, nearly all of it spent in line, and 141 ms under continuous batching.

The line at the door, second by second

Llama 3.1 8B on one NVIDIA H100 with 8 seats; 100 requests with 512-token prompts and answers of random length, the same arrivals for both. Every round is priced at its best-case time.

04 · The price

Newcomers and shelf space

Refilling is not free. A newcomer’s whole question, the prompt, is read in the round they join, so that round runs longer for everyone seated.

Each seat also needs shelf space for its diner’s cards, which grow by one card a round until the diner leaves, at a moment nobody can predict. The shelf is GPU memory, so it caps the seats: 52 conversations of 8,192 tokens, as the opening scene’s last stage shows. How to give out that space without wasting it is the next lesson.

Rounds, drawn to scale in time

  • A round for the seated diners: one token each
  • Extra time to read a newcomer's prompt in the same round

7 diners seated, 600 tokens into their conversations; a newcomer with a 512-token prompt joins every fifth round.

05 · Playground

Run the counter yourself

Set the arrival rate and the spread of answer lengths, then press Play. Try a spread of 0, where every answer has the same length, and then a rate of 12.

100 requests, 8 seats, both hosts

generating tokensfinished, slot held until the batch endsrequest arrives

StaticContinuous
Throughput (tok/s)
Mean latency
p99 latency
Slot utilization

Hover or tap a bar for that request's details.

Llama 3.1 8B on one H100, 8 seats, 100 requests with 512-token prompts. Each row is a seat and each bar a request; colours only tell neighbours apart. Hover or tap a bar for its details. Throughput counts output tokens only.

06 · Glossary

The vocabulary, in one place

Token
The unit a model reads and writes: a piece of a word, about three-quarters of one on average. One piece of sushi.
GPU
The chip that runs the model. The chef.
Weights
The model’s billions of numbers. Every round starts by reading all of them from GPU memory: the trip to the fridge.
Round
One pass through the whole model (a decode step, or iteration). It makes one token for every seated request: about 4.50 ms for one seat, 4.67 ms for 8, on one H100.
Batch and seat
The requests served together in a round. A seat (a batch slot) is one place in it.
KV cache
Each request’s notes on every token so far, one card per token per layer (lesson 4). They sit on the shelf: GPU memory.
Static batching
Seat a group, and seat nobody new until its longest answer is finished. Finished requests keep their seats.
Continuous batching
Rebuild the batch after every round: a finished request leaves at once and the next in line takes its seat. Also called iteration-level scheduling.
Prompt
The request’s whole input. It is read in the round the request joins, which makes that round longer.
Throughput
Tokens per second, all requests together.
Latency
Time from a request arriving to its answer coming back. The p99 latency is the one that 99% of requests beat.
Slot utilization
The share of seat-time spent making a token someone asked for.

You can now

  • Explain why a one-line answer waits behind an essay under static batching, and how rebuilding the batch after every forward pass lets it leave as soon as it is done.
  • Compute the share of slot-steps a static batch wastes from its requests' output lengths, and estimate from the roofline floor how little a decode step slows down as more seats fill.
  • Decide when continuous batching will raise throughput and when it only cuts latency, from the load, the spread of output lengths, and whether the static server keeps up with arrivals.

Open the workbook: the formulas, the exact numbers, graded exercises and the sources behind this page.