Build AI handbook

Build AI, from first principles to working code

A free, open-source, hands-on book about how AI works and how to build with it. It starts from school algebra, explains how neural networks and language models learn, covers the ideas behind real AI applications, and has you build them yourself in eleven labs: a first model call, a prompt test bench, retrieval with citations, an agent, an MCP server, guardrails, a judge, tracing and a small assistant.

Who this is for

Anyone who wants hands-on experience building AI. You need no background in AI or advanced maths: Part I starts from equations you met at school and works every idea with real numbers before any symbols. For the labs you need to be able to run a Python script; Lab 0 sets up everything else. Each lab runs without an account or a key on a built-in fake model, on a free local model through Ollama, or on Claude with your own key.

How to use this book

The book is free and needs no account. You can read it in three ways, and the pages are the same in each:

  • Online at handbook.aggarwal.dev, on a computer or a phone. The site sets no cookies, runs no analytics and loads nothing from other sites.
  • Offline, from a copy. Clone the repository, or download it as a ZIP, and double-click book/index.html. There is no build step and no network access: every page, diagram and widget is plain HTML, CSS and JavaScript in book/.
  • Served on your own machine. Run python3 tools/serve.py from the repository root and open http://127.0.0.1:8765/. Progress tracking then works in every browser. Chapter 1 says what the server does and refuses.

Each of the three keeps its own progress; chapter 1 compares them and says how to move your progress from one to another.

The book is open source. Its pages, labs and tools are at github.com/aggarwaldev/build-ai-handbook. The text is under CC BY 4.0 and the code under the MIT License, so you may share and adapt both with credit (how to credit it). Corrections and contributions are welcome, from a typo to a new lab: Help improve the book says where to start.

The labs run on your own computer once Lab 0 has set it up, or in the browser with nothing installed: every lab page has buttons to open it in Colab, Binder or Codespaces.

Inside the book:

  • Navigation. The contents are on the left (behind the menu button on a phone). Every page ends with previous and next links. Press / to search chapter titles, headings and glossary terms.
  • Dotted words are glossary terms, like token. Hover or focus one to see a short definition; click it for the full entry.
  • Boxes. A green Why it matters box says what problem an idea solves. An amber Careful box marks a common mistake. A blue Note is a side remark. A teal Build it box sends you to the lab where you build the idea yourself.
  • Interactive pieces carry a Try it label. Move the sliders and type into the boxes; nothing is sent anywhere.
  • Self-checks close every chapter. Answer in your head, then click to open the answer.
  • Ask AI. Select any passage and press Ask AI to ask Claude about it, in a new tab, or in the page with your own Anthropic API key. Chapter 1 explains how.

Reading paths

Read straight through if you like a firm foundation. Or pick a path: each one links back to the chapters it needs, so you can fill gaps as you meet them.

Foundations

How the machinery works inside, in order. Do every Try it; they are short.

  1. Part I, Maths: chapters 2 to 9, the maths everything later needs.
  2. Part II, ML foundations: chapters 10 to 14, then train your own neural network (chapter 15).
  3. Part III, How LLMs work: chapters 16 to 19, then train a tiny language model (chapter 20).
Shipping

What it takes to run an AI system other people rely on.

  1. Chapters 33 to 37: harness engineering, guardrails, security, hallucination and memory.
  2. Chapters 38 to 43: frameworks, quantisation, fine-tuning, production and regulated domains.
  3. Chapters 44 and 45: evaluating output with judges, and observability, tracing and cost.
  4. Lab 7, Lab 8 and Lab 9: guardrails, a judge and tracing you can measure. Then the capstone.

Your progress

The book keeps track of what you have read and built, in this browser only: no account, no cookies, nothing sent anywhere.

  • Every chapter and lab has a Mark this chapter done box and a My notes box above its previous and next links. Notes save as you type.
  • Each lab ends its steps with a checklist. Tick the steps as you finish them; when every step is ticked, the lab is marked done.
  • The sidebar shows a tick beside pages you have finished, a dot beside pages in progress, and how many pages of each part are done.
  • Your progress shows the whole picture, where to continue, your notes and recent activity, and lets you export your progress to a file, import it on another device, or reset it.

Contents

Start here

Part I: Maths

A self-contained maths track. Worked numbers first, then the symbols, then an exercise with its answer.

Part II: ML foundations

Part III: How LLMs work and are trained

Part IV: Applied concepts

Chapter 21Prompt engineeringRoles, instructions, examples, delimiters, templates, cache-friendly order and injection-resistant prompts. Chapter 22Context engineering and choosing modelsWhat goes in the window and in what order, token budgets, and picking a model on cost, speed and quality. Chapter 23Embeddings and similarityFrom token vectors to one text vector, cosine similarity, and choosing an embedding model. Chapter 24Retrieval and RAGChunking, putting passages in the prompt, citations, and how RAG fails. Chapter 25Vector search internalsExact search cost, HNSW, IVF, BM25 worked by hand, hybrid search and re-ranking. Chapter 26Evaluating RAGPrecision@k, recall@k, MRR, nDCG, groundedness and golden sets. Chapter 27The knowledge base and maintaining itDocuments, official texts, figures and news: provenance, licences and keeping them current. Chapter 28Tools and function callingSchemas, the tool loop and its cost, structured output, and calculators that keep the numbers in code. Chapter 29Agents and agentic flowReAct, planning, graphs versus free agents, stopping conditions. Chapter 30MCP, basic and advancedThe Model Context Protocol from handshake to security, with a tiny server you can run. Chapter 31SkillsA folder with SKILL.md that an agent loads on demand, with a worked example. Chapter 32SubagentsDelegating to workers with their own context, and writing a good brief. Chapter 33Harness engineeringEverything around a model that turns it into a working agent or app. Chapter 34GuardrailsInput and output guards, required wording owned by code, and measuring a guard's precision and recall. Chapter 35LLM securityInjection, jailbreaks, leakage, excessive agency, cost abuse, supply chain. Chapter 36HallucinationWhy it happens, how to measure it with intervals, and how to reduce it. Chapter 37MemoryConversation history and its cost, long-term facts, recall, and giving people control. Chapter 38Frameworks and model providersThe layers, gateways and routing, graphs for multi-step flows, and running open models yourself. Chapter 39QuantisationNumber formats, scale and zero point, GPTQ, AWQ, GGUF and QLoRA. Chapter 40Fine-tuning and distillationFull versus LoRA, the W + BA maths, soft targets and the KL loss. Chapter 41 · ProjectsLoRA fine-tune and distillationHands-on with small open models, with hardware notes. Chapter 42Running AI in productionLatency, cost per answer, prompt caching, retries, rate limits and budgets that cannot be overrun. Chapter 43AI in a regulated domainTraceable answers, people in charge of actions, data handling and change control. Chapter 44Evaluating LLM output: judges and agreementTest sets, deterministic checks, a model as judge, Cohen's kappa against people, and judge biases. Chapter 45Observability, tracing and costWhat to record per call, traces and spans, latency percentiles, cost from tokens, and alerts.

Part V: Build it

Runnable labs in labs/. Each one says what it costs on Claude; Ollama and the built-in fake model cost nothing.

Reference

A note on accuracy

Model names, prices and APIs change quickly. Every number the labs print comes from running their code, and each lab says which model and prices it assumed. If something here has gone out of date, the code in labs/ and the provider's own documentation win. Chapter 1 says how to report it or fix it.