Request lifecycle, metrics and the roofline
- Production
- 40 min
- Lesson 1 of 7 in Inference engineering
- Last verified
Select text in the lesson first to keep it with your note. With the keyboard, move focus here and press the button: the note goes with the section you are reading.
In this module
On this page
Before you start
- Completed:LLM inference and servingProduction: cost, serving and observability
- Completed:How LLMs workLLM foundations
You will be able to
- Trace a request through tokenization, queueing, prefill, decode and streaming, say which phase sets time to first token and which sets inter-token latency, and compute inter-token or end-to-end latency from the other quantities.
- Define TTFT, inter-token latency, end-to-end latency, throughput, goodput and cost per million tokens, and explain why a throughput number without a load and a latency is not a result.
- Derive that decode intensity is about 2B divided by bytes per weight, compute a ridge point from a datasheet, and classify decode and prefill as memory-bound or compute-bound.
- Compute an upper bound on batch-1 decode speed from weight bytes and memory bandwidth, and say what that bound leaves out.
- Map the four families of serving techniques (less work, fewer bytes, better hardware use, reliability) to the roofline fact each one exploits.
The team is about to run the model on its own servers. The hardware sheet is on the table, the dashboards are still blank, and a queue of customers is waiting.
Someone says, “Just start it and see how fast it goes.” Ananya puts her pen down. “Not yet. How fast could it possibly go?”
Nobody has written that number down.
Concept
The story’s setup is a serving cluster: the machine is a GPU, the model answers customers, and the queue holds requests. Inference engineering is the work of making a trained model fast, cheap and reliable to run. This module goes deeper than LLM inference and serving, which gave you the overview (prefill, decode, batching, the KV-cache, a self-host break-even). Here you learn to predict the answer first and then measure.
The habit this lesson builds is Ananya’s: before you run anything, work out the fastest the hardware could possibly go. Two numbers decide it, how many arithmetic operations the model needs and how many bytes it must move, and one comparison tells you which of them is the limit. That comparison is the rooflineRooflineA model of attainable speed against arithmetic intensity: below the ridge point a workload is limited by memory bandwidth, above it by peak compute. The ridge point is peak FLOP/s divided by peak bandwidth.Open the glossary entry.
Why It Matters
Without a prediction, a benchmark is a number with no meaning. If a service delivers 40 tokens per second and the hardware ceiling is 48, you are done tuning at this precision and GPU count. If the ceiling is 210, something else is wrong, and you know to look for it. The roofline also explains most of the techniques later lessons cover: batching, quantization, speculative decoding, prefill and decode disaggregation. They are not a list to memorise. Each one follows from the same two facts.
In interviews, “why is our LLM slow or expensive?” and “how would you self-host a 70B model for ten thousand users?” are answered best by someone who starts from bytes and FLOPs rather than from a tool name.
Mental Model
Think of the engine as a workshop with a fast machinist and a narrow door to the stockroom. The machinist can do enormous amounts of work per minute, but every part must come through the door. If each trip fetches only a little work’s worth of parts, the machinist waits. If each trip fetches a lot, the machinist is the limit. Which one limits you is a ratio: work per part carried. The roofline is that ratio, and its crossing point is the ridge.
What happens to one request
Every request takes the same path.
- Tokenize the prompt into tokens. This runs on the CPU.
- Queue. The scheduler holds the request until it can be admitted to a batch.
- PrefillPrefillThe first phase of a request, in which the whole prompt is processed in parallel. It sets time to first token and is usually limited by compute.Open the glossary entry. One forward pass over all n prompt tokens at once builds the KV-cacheKV cacheStored key and value vectors of earlier tokens, reused at each decode step so they are not recomputed. It saves compute but its memory grows with context length and concurrent requests. It is not a response cache.Open the glossary entry and produces the logits for the first new token.
- DecodeDecodeThe phase after prefill where one token is generated per step. Each step has to read the weights and the KV cache, so it is usually limited by memory bandwidth.Open the glossary entry. Each step feeds the newest token through the model, reuses the cache, samples one token and appends it. The loop repeats until an end-of-sequence token or a length limit.
- Detokenize and stream the tokens back to the client.
Prefill and decode feel different to a user. The wait before anything appears is queueing plus prefill. The pace at which words then arrive is the decode step time. They have different causes and different fixes, so measure them separately.
Metrics, defined precisely
NVIDIA’s benchmarking documentation (accessed 2026-10-21) gives the definitions most tools follow. It also warns that tools differ in how they compute them, so compare results only when the definitions match.
| Metric | Definition | What dominates it |
|---|---|---|
| TTFTTTFTShort for time to first token: the delay from sending a request until the first output token arrives.Open the glossary entry | From query submission to the first received token with content. It generally includes queueing, prefill and network time. | queue length and prompt length |
| ITLInter-token latencyThe gap between successive output tokens (also called time per output token, TPOT), which sets how smooth streaming feels. It reflects decode speed and batch pressure.Open the glossary entry (also TPOT) | : the average gap between tokens after the first | decode step time: bandwidth, batch size, context length |
| End-to-end latency | From request sent to the final token received: | both |
| Throughput (TPS) | Output tokens across all concurrent requests, divided by the measurement window | batching and memory |
| TPS per user | One request’s output length divided by its end-to-end latency; it approaches as outputs get long | decode step time |
| GoodputGoodputThe rate of requests that meet the latency targets. Throughput can look high while many users get an unacceptable experience. DistServe uses a stricter version: the highest request rate at which a target share of requests meets the limits.Open the glossary entry | Requests per second that meet the latency targets you promised | the metric closest to business value |
| Cost per million tokens | throughput and utilization |
Two habits follow.
- State which tokens. Input tokens and output tokens cost the hardware different amounts. “2,000 tokens per second” is incomplete without saying which, at what concurrency, and at what latency.
- Quote throughput and latency together. Bigger batches raise throughput and can lengthen each user’s wait. A throughput figure alone says nothing about whether users were happy.
A worked case. A request has TTFT of 400 ms and produces 200 tokens, and its decode phase takes 4.975 s. Then , , and , below the ceiling of 40 because the first-token wait is included.
| Quantity | Calculation | Value |
|---|---|---|
| Time to first token | given | 0.400 s |
| Decode phase | given | 4.975 s |
| Inter-token latency | gaps | 25 ms |
| End-to-end latency | 5.375 s | |
| Tokens per second per user | about 37 | |
| Ceiling of | 40 |
FLOPs and bytes per token
For a dense decoder with parameters, one token’s forward pass does about floating-point operations: one multiply and one add for each weight (kipply’s “Transformer Inference Arithmetic”, 2022, makes the same argument). Attention adds terms that grow with the context length, which this lesson sets aside and the next one returns to.
The weights occupy in memory: 2 bytes in BF16 or FP16, 1 in FP8 or INT8, 0.5 in INT4. Quantized formats also store scaling factors, so real files are a little larger than that.
During decode the GPU must read every weight from memory for every step, because each step passes a token through all of them. NVIDIA’s inference blog (2023-11-17) says the same in words: decode is a memory-bound operation, and data transfer dominates its latency rather than the arithmetic.
The roofline and the ridge point
A chip can deliver two things per second at most: floating-point operations (peak FLOP/s) and bytes from memory (peak bandwidth). Which you hit first depends on the work done per byte moved, the arithmetic intensityArithmetic intensityFloating-point operations done per byte moved from memory. For decode with BF16 weights it is about the batch size; for prefill, about the number of prompt tokens (also with BF16 weights).Open the glossary entry, in FLOP per byte. The roofline model (Williams, Waterman and Patterson, UC Berkeley technical report, 2008) draws attainable performance against intensity: a rising slope limited by bandwidth, then a flat ceiling limited by peak compute. The crossing is the ridge pointRidge pointThe arithmetic intensity at which a chip stops being limited by memory bandwidth and starts being limited by compute: peak FLOP/s divided by peak bandwidth. About 295 FLOP per byte for BF16 on an H100 using the dense peak.Open the glossary entry:
Below the ridge the workload is memory-boundMemory-bandwidth-boundLimited by how fast data can be read from memory rather than by arithmetic. Decode is typically this.Open the glossary entry; above it, compute-bound.
Datasheet figures need care. NVIDIA’s product pages (accessed 2026-10-21) list tensor-core numbers marked “with sparsity”, which assume a structured-sparsity feature. The dense figure is half of the sparse one. The A100 page prints both figures (312 dense, 624 with sparsity), while the H100 page prints only the sparse one, so the H100 dense number below is the listed one halved, an estimate that matches secondary summaries of the datasheet footnote. Check the datasheet of your part.
| GPU | Listed BF16 TFLOPS (with sparsity) | Dense BF16 (listed ) | Memory bandwidth | Ridge, FLOP per byte |
|---|---|---|---|---|
| H100 SXM, 80 GB | 1,979 | about 990 | 3.35 TB/s | about 295 |
| A100 80 GB SXM | 624 | 312 | 2,039 GB/s | about 153 |
These are single-model examples, not the whole product range: other variants (PCIe cards, 40 GB parts, newer generations) have other numbers, and kipply’s post uses 1.5 TB/s and gets a ridge of about 208. Always take the numbers from the datasheet of the exact part you run.
Decode and prefill on the roofline
Decode, batch . The step reads the weights once and shares them across the batch. FLOPs per step are about . Bytes read are about , where is bytes per weight. So:
For BF16 weights () the intensity is simply . The batch size at which decode reaches the ridge is : about 295 on an H100 with BF16 weights, and about 148 if the weights are stored in 8 bits but the arithmetic still runs in BF16. At batch 1 to 64, decode sits far to the left of the ridge. The arithmetic units idle and the memory system is the limit. This single fact is why decode is slow per user and why batching is almost free in throughput terms.
Decode on an H100 with BF16 weights, where the ridge is about 295 FLOP per byte:
| Batch size B | Intensity (about B) | Position against the ridge | Limit |
|---|---|---|---|
| 1 | 1 | far left | Memory bandwidth |
| 8 | 8 | far left | Memory bandwidth |
| 64 | 64 | left | Memory bandwidth |
| 295 | 295 | at the ridge | Both |
| 512 | 512 | right | Compute |
Prefill, n prompt tokens. All n tokens share one read of the weights, so intensity is about n for BF16. A 4,000-token prompt is far above the ridge and is compute-bound. A 64-token prompt is not: it is below the ridge on both GPUs above, so even prefill can be memory-bound when the prompt is short. Do not memorise “prefill is compute-bound”; compare n with the ridge.
Prefill FLOPs are about plus attention terms that grow with . For short prompts the first term dominates. For tens of thousands of tokens the attention term takes over, TTFT grows faster than linearly, and the KV-cache grows linearly with . Floor on prefill for an 8B model on an H100 at 2,000 tokens, assuming peak dense BF16 and no other cost: . Real engines do not reach peak, so treat this as a floor.
Worked bounds: tokens per second at batch 1
A decode step cannot be faster than the bytes it must read divided by the bandwidth: .
| Case | Weight bytes | Aggregate bandwidth | Step time at least | Tokens per second at most |
|---|---|---|---|---|
| 8B, BF16, one H100 | 16 GB | 3.35 TB/s | 4.8 ms | about 209 |
| 70B, BF16, two H100 (tensor parallel) | 140 GB | 6.7 TB/s | 20.9 ms | about 48 |
| 70B, INT4, one H100 | about 35 GB | 3.35 TB/s | 10.4 ms | about 96 |
The 70B BF16 case does not fit on one 80 GB GPU, so it needs at least two. Quantizing to INT4 halves the step time compared with two GPUs and removes the need for the second GPU, which is why quantization appears in almost every serving plan.
What the bound ignores, and so overstates:
- the KV-cache read, which adds bytes to every step and grows with context length and batch size;
- communication between GPUs when the model is split;
- scaling-factor overhead in quantized formats;
- real kernels reaching only part of peak bandwidth.
Real engines land below these ceilings. Use the ceiling to decide whether a measurement is plausible or whether something specific is wrong.
Why batching helps, and what stops it
At batch 32 the same 16 GB weight read serves 32 tokens. Step time stays near 4.8 ms, so aggregate output rises to about tokens per second in the ideal case, while each user’s pace barely changes. That is why serving engines fight for bigger batches (the PagedAttention paper, Kwon et al. 2023, is motivated by exactly this: batching needs memory, and wasted KV-cache memory limits the batch).
| Batch | Weight bytes read per step | Step time (about) | Aggregate tokens per second (ideal) | One user’s pace |
|---|---|---|---|---|
| 1 | 16 GB | 4.8 ms | about 209 | about 209 tokens/s |
| 32 | 16 GB | 4.8 ms | about 6,700 | barely changes |
Three things stop it before the ridge at is reached:
- KV-cache memory. Each request holds a cache; the pool fills (lesson 2).
- Attention cost. Reading each request’s cache adds bytes per request that sharing the weights cannot hide.
- The latency target. A bigger batch means each step does more, and users feel the step time.
What the roofline leaves out
Real step time also includes kernel launch overhead, sampling and logits processing, CPU scheduling and tokenization, inter-GPU communication, and framework overhead. At small batch on a fast GPU the CPU and launch costs can matter as much as bandwidth. The roofline gives you a ceiling to compare with, not a prediction of the measurement. Profile before you theorise.
The optimization map
Cost per token falls when you do less work, move fewer bytes, use the hardware better, or keep the service running. The rest of the module follows this map.
Each family answers a roofline question. Decode is bandwidth-bound, so shrinking bytes (family 2) speeds it directly and batching (family 3) raises intensity. Prefill is compute-bound at length, so doing less of it (family 1) and not stalling decode behind it (family 3) matter. Family 4 is what stops a good design from failing in production.
A calculator you can write live
def decode_step_ms(params_b, bytes_per_param, bw_tb_s, n_gpus=1):
"""Lower bound on a decode step in ms: weight bytes over aggregate bandwidth."""
weight_bytes = params_b * 1e9 * bytes_per_param
return weight_bytes / (bw_tb_s * 1e12 * n_gpus) * 1e3
def ridge(dense_tflops, bw_tb_s):
"""FLOP per byte at which the chip stops being memory-bound."""
return dense_tflops / bw_tb_s
def cost_per_m_tokens(gpu_usd_hr, tokens_per_s):
return gpu_usd_hr / (tokens_per_s * 3600) * 1e6
print(round(decode_step_ms(8, 2, 3.35), 1)) # 4.8
print(round(decode_step_ms(70, 2, 3.35, 2), 1)) # 20.9
print(round(ridge(990, 3.35))) # 295
print(round(cost_per_m_tokens(2.5, 2500), 2)) # 0.28The last line uses an invented price of 2.50 dollars per GPU hour and an invented throughput of 2,500 tokens per second. Real prices and throughputs vary widely, and cost per token falls as utilization rises, so never treat it as a constant.
Real-World Application
When a service is slow, write the prediction before you open a profiler. Take the model’s parameter count and precision, the GPU’s bandwidth and the number of GPUs, and compute the batch-1 ceiling. Compare it with the measured inter-token latency at low load. If the measurement is within a factor of two of the ceiling, the lever is bytes or batching. If it is many times worse, look for overhead: launch costs, a CPU-bound scheduler, a slow interconnect. Then measure TTFT and ITL separately under load and change one lever at a time.
Interview Perspective
Expect: “Why is decode memory-bound and prefill compute-bound?”, “Derive a tokens-per-second ceiling for a 70B model on two GPUs”, “Why does batching raise throughput?”, “Why can a short prompt be memory-bound?”. A strong answer states bytes divided by bandwidth, intensity of about B, the ridge point, and then names KV-cache memory, attention cost and the latency target as the limits. State your assumptions aloud: precision, GPU, batch, and that the ceiling ignores the KV-cache and communication.
Common mistakes: saying decode is compute-heavy; quoting one tokens-per-second figure with no batch, load or token type; treating datasheet “with sparsity” numbers as dense; presenting an invented price or throughput as a fact.
Provenance: Common industry pattern and Generated practice. These are the questions this topic tends to draw in ML-infrastructure and GenAI-depth interviews; none is verified for a named company. Hardware numbers move quickly, so check the datasheet of the part you are asked about.
Practice Questions
- Basic: Why is decode memory-bandwidth-bound while prefill at length is compute-bound?
- Intermediate: For a 13B model in BF16 on a GPU with 2 TB/s of bandwidth, what is the batch-1 decode lower bound? What if the weights are INT8?
- Advanced: At what batch size does decode reach the ridge on an H100 with BF16 weights, and what stops you in practice?
- Product-Company: A teammate says “our 8B model gives 90 tokens per second on one H100, so we are bandwidth-limited”. Is the claim supported? What would you check?
Activity
Pick a model you might serve and a GPU you might serve it on. Find the parameter count and the GPU’s datasheet bandwidth and dense BF16 figure. Compute the weight bytes, the batch-1 ceiling, the ridge, and the batch size at which decode would reach it. Write which assumptions you made and which costs your ceiling ignores. If a number is not published, say so instead of guessing.
Homework
Write a one-page prediction memo for a serving plan: the model, the precision, the GPU and count, the batch-1 ceiling, the ridge, the expected effect of batch 32, and the three costs your numbers leave out. Then list the two measurements you would take first and the result that would tell you the prediction was wrong.
Quick Revision
- Request path: tokenize, queue, prefill, decode loop, stream. TTFT is queueing plus prefill; ITL is the decode step.
- End-to-end latency is TTFT plus the generation time. .
- Quote throughput with the load and a latency. Goodput counts only requests that meet the targets.
- One token costs about FLOPs and needs bytes read per decode step.
- Decode intensity is about ; . Compare or with the ridge.
- Batch-1 ceiling: . It ignores KV reads, communication and overhead.
- Use dense datasheet numbers, not “with sparsity”.
Check yourself
- Walk-through: Without notes, derive a tokens-per-second ceiling for a 34B INT8 model on one GPU with 2 TB/s of bandwidth, then say why batching helps and what limits it.
- Variation: Repeat for the same model in BF16 on two such GPUs, and say what the ceiling now ignores.
- Common misconception: “More GPU FLOPs makes decode faster.” At low batch, bandwidth and bytes set the speed, not peak compute.
Related
Resources & Further Reading
Checked sources to go deeper. The essential ones come first in each group.
Official documentation
Production Metrics (opens in a new tab)
Shows which latency and throughput metrics a production engine exposes, to compare with the definitions here.
Further reading
LLM Inference Handbook (opens in a new tab)
A broad, current tour of inference topics that places this lesson's roofline view in a wider map.
Explains why keeping the batch full raises throughput, the family-three lever on the optimization map.
Research papers
Builds a serving design around goodput under latency targets, the metric defined in this lesson.
FlashAttention: Fast and Memory-Efficient Exact Attention with IO-Awareness (opens in a new tab)
A worked example of cutting memory traffic, the move-fewer-bytes family.
More for this module: Inference engineering resources
Practice
Test what you just read. Quizzes and cards are grouped by module: Inference engineering.
Check yourself15 questions on what this lesson set out to teach
Checking answers needs JavaScript. You can still read every question and option below.
Keyboard: arrow keys move between options. Space or Enter selects the focused option.