CHAPTER 29

LLM Fundamentals and Inference Systems

What a system designer must know about transformers, tokens, GPUs and serving

Source: PDF pp. 77–79

Core PDF content

PDF page 77The mental model p. 77

  • Tokens, not words. Text is split into sub-word tokens (BPE / SentencePiece). In English ~4 characters or ~0.75 words per token; code, numbers and many non-English languages use more tokens per word. Cost, latency and context limits are all counted in tokens.
  • Decoder-only transformers generate autoregressively: each new token requires a forward pass that attends to all previous tokens, then is appended and the process repeats until a stop condition.
  • Attention lets each token weigh every earlier token (queries, keys, values). Compute grows ~quadratically with sequence length; memory for cached keys/values grows linearly.
  • Context window is the maximum prompt + output tokens. Long contexts are expensive (prefill compute, KV-cache memory) and models use middle-of-context information less reliably than the start and end.
  • Mixture-of-Experts (MoE) models route each token to a few "expert" sub-networks: many total parameters, fewer active parameters per token (cheaper compute), but all experts must still sit in GPU memory.
  • How models are built: pre-training (next-token prediction on huge corpora) then post-training: supervised fine-tuning on demonstrations, preference optimisation (RLHF / DPO), and for reasoning models, reinforcement learning with verifiable rewards. Reasoning models spend extra "thinking" tokens, trading latency and cost for accuracy.

Controlling generation p. 77

ParameterEffectGuidance
temperatureScales randomness of token choice (0 = most deterministic)0-0.3 for extraction, classification, code; 0.7-1 for creative text
top_p / top_kRestrict sampling to the most likely tokens (nucleus / k best)Tune one of temperature or top_p, not both
max_tokens, stopBound output length and stop at delimitersAlways set; protects cost and latency
Structured outputJSON-schema or grammar-constrained decoding guarantees parseable outputUse for any machine-consumed result
Tool / function callingModel emits a structured call that your code executesFoundation of agents (Ch. 31)
seedBest-effort reproducibilityNot a guarantee across hardware or model updates

Latency anatomy: prefill and decode p. 77

Compute-bound: big matrix-matrix work,                                          Memory-bandwidth-bound: every step re-reads all weights
        GPU cores saturated.                                                         and the whole KV cache for one new token.

KV cache prompt K/V stored

Total latency = TTFT + (output tokens - 1) x TPOT. The KV cache grows by one entry per token per layer.

Figure 53. Prefill is parallel and compute-bound; decode is sequential and memory-bound. They need different optimisations. · PDF p. 77
MetricMeaningWhat improves it
TTFTTime to first token (queueing + prefill)Shorter prompts, prefix caching, chunked prefill, more GPUs, routing to idle replicas
TPOT / ITLTime between streamed tokens (~30-100 ms feels fluent)Smaller/quantised model, speculative decoding, less batching contention, faster memory
ThroughputTotal tokens/s across all requestsBigger batches, continuous batching, paged KV cache, quantisation
Cost per 1M tokensOutput tokens usually cost several times input tokensSmaller models, caching, shorter outputs, batch APIs

Memory arithmetic for inference p. 78

Model sizeWeights fp16/bf16 (2 B/param)8-bit4-bit
7-8 B~14-16 GB~8 GB~4-5 GB
13 B~26 GB~13 GB~7 GB
70 B~140 GB (2-4 x 80 GB GPUs)~70 GB~35-40 GB
400 B+ (MoE)~800 GB (multi-node / 8+ GPUs)~400 GB~200 GB

KV cache per token = 2 (K and V) x layers x KV-heads x head-dim x bytes. A 70 B model with grouped-query attention (80 layers, 8 KV heads, head-dim 128, fp16) needs 2 x 80 x 8 x 128 x 2 B = ~0.31 MB per token, so one 8,000-token conversation holds ~2.5 GB, and 100 concurrent such conversations need ~250 GB on top of the weights. Memory, not compute, usually limits concurrency. A useful bound for decode speed: tokens/s per sequence <= memory bandwidth / bytes read per token. A 16 GB (8 B fp16) model on a ~3 TB/s GPU is capped near ~200 tokens/s per stream; batching serves many streams per weight read.

Inside an inference engine p. 78

Figure 54. An LLM inference replica: a scheduler batches many requests per step; the KV cache is allocated in small blocks. · PDF p. 78
TechniqueIdeaBenefit / caveat
Continuous (in-flight) batchingAdmit new requests and retire finished ones at every decode step instead of waiting for a whole batchSeveral-fold throughput vs static batching; the default in modern engines
PagedAttentionKV cache stored in fixed-size blocks mapped like virtual memoryNear-zero waste, more concurrent sequences, shared prefixes
Prefix (prompt) cachingReuse KV for an identical leading prompt (system prompt, document, few-shot examples)Cuts TTFT and cost; put static content first, variable content last
Speculative decodingA small draft model proposes several tokens; the large model verifies them in one passOften 2-3x faster decode with identical output distribution
Quantisation (AWQ, GPTQ, FP8, INT4)Lower-precision weights (and KV cache)2-4x less memory, faster decode; evaluate quality on your task
TechniqueIdeaBenefit / caveat
FlashAttention, GQA / MQAIO-efficient exact attention; fewer KV headsFaster attention, much smaller KV cache
Tensor / pipeline / expert parallelismSplit a model that does not fit on one GPU across GPUsTensor parallel needs NVLink inside a node; pipeline across nodes; experts for MoE
Disaggregated prefill / decodeSeparate GPU pools for compute-bound prefill and memory-bound decodeProtects TPOT from long prompts; more complex
Multi-LoRA servingOne base model, many small adapters swapped per requestCheap per-tenant customisation

Engines: vLLM, TensorRT-LLM, SGLang, Hugging Face TGI, and llama.cpp / MLC for local and edge. Autoscaling GPUs is slow (loading tens of GB of weights takes minutes), so keep a warm pool, scale on queue depth and KV-cache utilisation rather than CPU, and use fast model-loading paths (local NVMe, weight snapshots).

API or self-hosted? p. 79

Hosted model APISelf-hosted open-weights model
QualityFrontier capability, fast upgradesStrong but usually a step behind; excellent for narrow tasks after fine-tuning
Cost shapePay per token; zero idle cost; price per token falls over timeFixed GPU cost; cheaper per token only at high, steady utilisation
Latency / controlProvider-controlled; shared capacity, rate limitsFull control: batching, caching, co-location, custom kernels
Privacy /Contractual (zero retention, regional endpoints)Data never leaves your VPC / on-prem

residency

Operations            Minimal                                                      GPU fleet, engine upgrades, monitoring, capacity planning

Review questions for this chapter have not been written and reviewed yet. The chapter text above is the full source content.

Try it

References

System Design & AI System Design: The Complete Guide — 2026 Edition, pp. 77–79; figures 53, 54.

Tip: use ← and → keys to move between chapters.