07 / 16
Inference, Serving & Cost
Latency, throughput and the bill: running models in production.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.