AI Systems, From First Principles
A first-principles guide to how AI systems run, and how to make them efficient. We build understanding in dependency order — each topic only uses ideas already covered. For every technique, keep asking one question: what does this cost — in latency, memory, and dollars — and how does it make that cheaper? That single thread runs through the whole book.
The spine is inference and serving: how a token is actually produced, why it costs what it costs, and the techniques that cut the bill — KV caches, batching, quantization, and the serving stacks that tie them together. The capstone then turns the lens around: you build a real app on the Claude API and cut its cost with the levers you control from outside the model. Read in order, and after each page answer the Check your understanding questions in your own words. Depth over speed.
Part 0 · Foundations — Why Efficiency Matters
- Overview — Why AI Efficiency Matters
- The Cost Problem: Why a Token Costs Money
- What a Model Actually Is
- Training vs Inference: Two Cost Regimes
- Where the Time and Money Actually Go
- The Efficiency Mindset
Part 1 · The Hardware Reality
- Overview
- GPUs From First Principles
- Memory Bandwidth vs Compute: The Roofline
- Why LLM Inference Is Memory-Bound
- Numeric Precision: FP32 to INT4
- Interconnect & Why Big Models Need Many GPUs
Part 2 · The Transformer, Mechanically
- Overview
- Tokenization & Embeddings
- Attention From Scratch
- The FFN: Where the Parameters Live
- Prefill vs Decode: The Two Phases
- Why the KV Cache Exists
Part 3 · Inference Efficiency Core
- Overview
- KV Cache Math: Sizing the Dominant Cost
- Continuous Batching
- PagedAttention & vLLM
- FlashAttention
- Speculative Decoding
- Prefix & Prompt Caching
Part 4 · Model Compression
- Overview
- Quantization Fundamentals
- The Quantization Zoo: GPTQ, AWQ, GGUF
- Distillation
- Pruning & Sparsity
- Mixture of Experts
Part 5 · Serving Systems
- Overview
- The Serving Stack: vLLM, TGI, TensorRT-LLM, SGLang
- Request Scheduling & Queueing
- Tensor & Pipeline Parallelism
- Multi-GPU & Multi-Node Serving
- Autoscaling & Cold Starts
Part 6 · Application-Layer Efficiency
- Overview
- Prompt & Context Economy
- Caching: Prefix, Semantic, Response
- RAG Efficiency
- Model Routing & Cascades
- Structured Output & Constrained Decoding
Part 7 · Training & Fine-Tuning Efficiency
- Overview
- Why Training Is Its Own Beast
- Parallelism: Data, Tensor, Pipeline, FSDP
- LoRA, QLoRA & PEFT
- Fine-tune vs RAG vs Prompt
Part 8 · Measuring & Operating (LLMOps)
- Overview
- The Metrics That Matter: TTFT, TPOT, Goodput
- Load Testing & Benchmarking
- Observability for AI Systems
- Evals vs Efficiency
- Cost Modeling: Build vs Buy
Part 9 · Advanced & Rare Concepts
The senior-level track — the frontier techniques and failure modes that show up once you operate AI systems at scale.
- Overview — Advanced & Rare
- Disaggregated Prefill & Decode
- Long-Context Efficiency
- Multi-LoRA Serving
- Chunked Prefill
- On-Device & Edge Inference
- Beyond GPUs: TPUs, LPUs, Custom Silicon
Part 10 · Hands-On Capstone (Claude API)
The capstone — build a real RAG/agent app on the Claude API, measure its cost and latency, then cut both with the levers you control without owning a GPU: caching, model routing, context trimming, and streaming.
- Overview — The Capstone
- The Plan: Build, Measure, Optimize
- The Baseline: A Naive RAG App
- Lever 1: Prompt Caching
- Lever 2: Model Routing & Cascades
- Lever 3: Context Trimming & Streaming
- The Cost & Latency Dashboard
Part 11 · Frontier
The efficiency frontier as of 2024–2025 — where the unit of cost stops being one call: agentic systems, reasoning/test-time compute, multimodal & diffusion, embeddings & vector DBs, small language models, the DeepSeek moment, and the 2025 serving frontier.