07 / 16

Inference, Serving & Cost

Latency, throughput and the bill: running models in production.

0/17 studied
01Latency metrics: TTFT, TPOT, throughput

Split LLM latency into TTFT (wait for the first token: queueing + prefill) and TPOT (gap between later tokens: decode), track p95/p99, and size capacity by goodput, the throughput that still meets both SLOs.

Core◇ Diagram</> Code
02Prefill vs decode

Prefill reads the whole prompt in one parallel, compute-bound pass and sets TTFT; decode then emits one token per step, bound by memory bandwidth, and sets TPOT. Most serving optimizations target one phase.

Core◇ Diagram</> Code
03GPU memory math

Weights take parameters × bytes (70B in FP16 ≈ 140 GB), the KV cache gets what's left and that leftover sets concurrency; batch-1 decode speed is capped at memory bandwidth ÷ weight bytes.

Core◇ Diagram</> Code
04Continuous batching

Re-form the batch at every decode step: finished sequences leave and queued requests take their slots at once, instead of everyone waiting for the longest member. It's the main reason engines get several-fold more throughput.

Advanced◇ Diagram</> Code
05PagedAttention & vLLM

Store the KV cache in small fixed-size blocks allocated on demand, like OS memory pages, so almost no GPU memory is wasted; the freed memory becomes bigger batches and 2–4× throughput.

Advanced◇ Diagram</> Code
06FlashAttention

A GPU kernel that computes exact attention block by block in fast on-chip memory, never writing the n×n score matrix out, so memory grows linearly with length and attention runs ~2–4× faster.

Advanced◇ Diagram</> Code
07Quantization (INT8, INT4, FP8)

Store weights (and optionally activations and KV cache) in 8 or 4 bits instead of 16: 2–4× less memory and faster decode; nearly free at 8-bit, a quality cost to measure at 4-bit.

Core◇ Diagram</> Code
08Speculative decoding

A cheap draft guesses the next few tokens and the big model verifies them all in one forward pass, keeping those it agrees with: ~2–3× lower latency with exactly the target's output distribution.

Advanced◇ Diagram</> Code
09Self-hosted serving stack

An inference engine (vLLM, SGLang, TensorRT-LLM) does the batching and KV-cache work on the GPUs; a platform layer (Kubernetes, KServe, Ray Serve, Dynamo) routes, scales on queue depth and hides minutes-long cold starts.

Advanced◇ Diagram</> Code
10API vs self-hosting

Start with a managed API for speed and frontier quality; self-host open weights only when data control, custom weights or steady high utilization justify owning GPUs and on-call.

Core◇ Diagram</> Code
11Token economics & cost estimation

Cost = input tokens × input price + output tokens × output price, summed over every call. Estimate per request, multiply by volume, then pull the levers: caching, shorter prompts, smaller models, batch.

Core◇ Diagram</> Code
12Model routing & cascades

Send each request to the cheapest model that can handle it, by rules, a learned classifier or a cascade that escalates on a failed check, and fall back to another provider on outages.

Advanced◇ Diagram</> Code
13Semantic caching

Reuse a stored answer when a new query's embedding is close enough to an earlier one: it skips a full generation, but every false hit serves a confident wrong answer.

Advanced◇ Diagram</> Code
14LLM gateway

One proxy in front of every model provider: apps call a single API, and the gateway handles keys, per-team token quotas, retries and fallbacks, caching, redaction, logging and cost attribution in one place.

Advanced◇ Diagram</> Code
15Streaming responses (SSE)

Send tokens to the user as they are generated, over Server-Sent Events, so they start reading at time-to-first-token instead of waiting for the whole answer; total time is unchanged.

Core◇ Diagram</> Code
16Batch inference

When nobody waits on a single answer, submit the whole job offline: provider batch APIs give ~50% off with results within 24 h, and self-hosted runs push GPUs to peak throughput.

Core◇ Diagram</> Code
17LLM observability & tracing

Record every request as a trace with one span per step (retrieval, model call, tool call), carrying prompt version, tokens, latency, cost and feedback, so you can explain, price and replay any answer.

Core◇ Diagram</> Code