Interview's MateAI Engineering
Interview's Mate

AI Engineering

LLMs, RAG, agents & evals

Visualizer

DSA Visualizer
All 32 lessons
Custom Playground
Try your own examples

Learning

System Design
31 architecture topics
SQL Learning
160 foundations → production topics
AI Engineering
160 LLM → backend topics
Data Science
160 Python → MLOps topics
Data Engineer
160 SQL → streaming topics
Docker
160 Docker learning topics
Kubernetes
160 K8s fundamentals → cloud
Java LearnSoon
Coming soon
Python Learn
Interview Q&A & internals
JavaScript Learn
Closures, async & DOM Q&A
Tech Learn
Search, practise & track Q&A
AI DSA Practice
Solve coding problems
Theme
Home
AI Engineering topics
Lesson 88/ 160
05. AI System Design

Throughput vs Latency

Batching and concurrency knobs that trade tokens/sec against per-user delay.

IntermediateBatch Size · Concurrency3 quiz questions
Practice Generative AI Q&A
In plain wordsDiagramTheoryStep by stepWhen to use itInterview cluesMistakes to avoidHands-onQuiz (3)Practice

In plain words

Serving many people at once makes the computer more efficient and cheaper per answer, but each person's answer gets a bit slower, so you choose a balance.

Think of it like…

A bus: one passenger per trip is fast for that passenger but absurdly costly, a full bus is cheap per rider, and a packed bus that stops constantly makes everyone's journey slow. The service level you promise sets how full to run it.

Where you’ve seen it

Illustrative measurements for one GPU serving a large model: batch 1 has a decode step time of 20 ms, so a single user gets 50 tokens per second and the GPU's total is 50. Batch 8 takes 24 ms per step (41.7 tokens/s per user, 333 total); batch 32 takes 35 ms (28.6 per user, 914 total); batch 64 takes 55 ms (18.2 per user, 1,164 total); batch 128 takes 100 ms (10 per user, 1,280 total). With a policy of at least 25 tokens per second per user for a smooth stream, batch 32 is the best point: 914 total tokens per second. At an illustrative $3 per GPU-hour ($0.000833 per second), cost per million output tokens is $16.67 at batch 1 (20,000 s), $0.91 at batch 32 (1,094 s) and $0.65 at batch 128 (781 s), so the SLO costs about $0.26 per million tokens relative to the cheapest throughput-only setting, a price worth paying for smooth streaming. Offline summarisation jobs run on a separate pool at batch 128.

Words you’ll hear

Throughput
Total tokens per second the system generates across all concurrent requests.
Per-user speed
Tokens per second experienced by one request: 1 ÷ step time.
Batch size / max concurrent sequences
How many sequences are decoded together in each step.
Pareto frontier
Operating points where improving one of throughput or latency requires giving up the other.

Visual diagram

Loading interactive diagram

Theory & deep dive

Why batching raises throughput

LLM inference has two phases. Prefill processes the prompt in parallel and is compute-bound. Decode generates one token per sequence per step and is memory-bandwidth-bound: each step streams the model weights (tens of gigabytes) through the GPU, and doing that for 1 sequence or for 32 costs almost the same time, because the arithmetic per weight is tiny. Batching sequences therefore multiplies tokens produced per step with only a small increase in step time, which is why throughput can grow 10 to 20 times from batch 1 to batch 32. Eventually the extra per-sequence work (attention over each sequence's KV cache, the larger matrix multiplications) makes the step compute- or memory-limited and step time rises roughly in proportion, so throughput flattens while per-user speed keeps dropping.

The same logic sets the KV cache constraint: every sequence in the batch holds its own KV cache, so the maximum batch is also limited by GPU memory (see PagedAttention). The result is a curve like the one in the example: throughput = batch ÷ step time, per-user speed = 1 ÷ step time. At batch 32 with 35 ms steps, throughput is 32 ÷ 0.035 = 914 tokens per second and each user sees 28.6 tokens per second, more than 5 times typical reading speed (about 5 tokens per second), so the stream stays smooth.

Reading the table

Batch 1: 1/0.020 = 50 tok/s per user, total 50. Batch 32: per user 1/0.035 = 28.6, total 32/0.035 = 914. Batch 128: per user 10, total 1,280. Going from 32 to 128 adds only 40% throughput while cutting per-user speed by 65%.

Key takeaway: Decode is memory-bound, so batching multiplies throughput until step time starts to grow; per-user speed is 1 ÷ step time.

Choosing and operating the point

Start from the user-facing SLO: a minimum streaming speed (for example 25 tokens per second per user), TTFT and inter-token latency targets. Benchmark with realistic prompt and output lengths and a load test that sweeps concurrency, plotting throughput against p50 and p95 latency to find the knee of the curve. Set the engine's max concurrent sequences, max batched tokens per step (which controls how much prefill is mixed with decode, trading TTFT of new requests against ITL of running ones) and the admission queue length. Then size replicas for peak demand: GPUs needed = peak tokens per second ÷ per-GPU throughput at the chosen batch.

Cost follows from throughput: cost per million tokens = (GPU $ per second) × (1,000,000 ÷ tokens per second). At $3 per GPU-hour, batch 32 gives about $0.91 and batch 1 gives $16.67. Operational tactics: separate pools for interactive and offline traffic (offline jobs run at maximum batch), priority scheduling so chat preempts bulk work, autoscaling on queue depth and p95 latency, and load shedding or degraded modes when saturated. Watch for the cliff: past the knee, small load increases cause large latency jumps, so keep a headroom margin (for example target 60% to 70% of the knee throughput). Techniques that shift the whole curve include quantisation, speculative decoding, prefix caching and better kernels.

Sizing replicas

Peak demand is 40,000 output tokens per second. At batch 32 (914 tokens/s per GPU) you need 40,000 / 914 ≈ 44 GPUs, or about 63 with a 70% utilisation headroom target. At batch 128 you'd need 32 GPUs but would break the 25 tokens/s per-user SLO.

Key takeaway: Benchmark the curve, set concurrency from the SLO, separate pools, keep headroom and size replicas from per-GPU throughput.

How it works, step by step

Step 1 of 4
Product + engineering

1. Define latency SLOs

Set minimum per-user speed, TTFT and inter-token latency targets for each traffic class.

≥ 25 tokens/s per user
Key numbers at this step
Reading speed
~5 tok/s

When to use it & trade-offs

What it is

In LLM serving, throughput (total tokens generated per second across all users) and latency (how long each user waits per token or per response) pull against each other. During decoding, each step reads the model weights from memory once regardless of how many sequences are in the batch, so bigger batches produce far more tokens per second in total at only a slightly higher step time, until the GPU becomes compute-bound or memory-limited and step time grows. The knobs that move you along this trade-off are batch size (max concurrent sequences), concurrency limits, chunked prefill budgets and the number of replicas. The result is a Pareto frontier: you pick an operating point that meets a latency SLO at the lowest cost per token.

Why it matters: A GPU running one request at a time delivers the best per-user speed but wastes most of the hardware: cost per token can be 20 times higher than at high batch sizes. Packing in too many requests drives per-user speed down and the queue up until the product feels sluggish. Understanding the curve lets you set capacity and concurrency by numbers, decide when to add GPUs instead of increasing batch size and separate latency-sensitive traffic from batch jobs.

When to reach for it

  • Setting concurrency and capacity for self-hosted models
  • Splitting interactive and batch traffic
  • Estimating GPU cost per million tokens

When to avoid or be careful

  • Running one request at a time to maximise speed
  • Pushing batch size to the maximum for chat workloads
  • Mixing offline jobs into the interactive pool

Operating points

Low batch (latency-first)
Best for: Premium interactive or voice use cases

Strengths: Fast per-user streaming.

Watch out: Very high cost per token.

Medium batch at the knee
Best for: Most chat products

Strengths: Good per-user speed and strong cost efficiency.

Watch out: Needs benchmarking and headroom.

High batch (throughput-first)
Best for: Offline and batch workloads

Strengths: Lowest cost per token.

Watch out: Slow per-user speed; unsuitable for chat.

Interview signals & numbers to know

When this topic comes up

  • “GPU utilisation is low but costs are high”
  • “Latency jumps when traffic increases”
  • Memory-bandwidth-bound decode
  • Pareto frontier
  • Cost per million tokens

Benchmarks & rules of thumb

50 → 914 tok/s -
GPU throughput from batch 1 to batch 32 in the example
$16.67 → $0.91 -
cost per million tokens at batch 1 vs 32
25 tok/s -
example per-user speed SLO

Common mistakes & follow-ups

Common interview mistakes

  • Benchmarking with short synthetic prompts and then serving long ones.
  • Choosing the throughput-optimal batch and breaking streaming smoothness.
  • Not keeping headroom, so a modest traffic increase tips past the knee.

Likely interviewer follow-ups

  • Why does batching increase throughput so much for LLM decoding?
  • How would you choose the batch size for a chat service?
  • How do you split interactive and batch workloads?
Interview tip: Quote throughput = batch ÷ step time and per-user speed = 1 ÷ step time. Two formulas explain the whole trade-off.
Loading hands-on exercise

Knowledge check

Loading quiz

Put it into practice

Practice related interview Q&A

Apply Throughput vs Latency with generative AI interview Q&A and hands-on practice.

Open Generative AI Q&A →Tech Learn Q&A
Previous topic
Time to First Token
Next topic
Inference Serving with vLLM