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.
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 inbook/. - Served on your own machine. Run
python3 tools/serve.pyfrom the repository root and openhttp://127.0.0.1:8765/. Progress tracking then works in every browser, and with your own Anthropic key Ask AI can answer inside the page. 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. 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.
Build now
Start with working code and read the concepts each lab points to.
- Lab 0: set up your workbench: Python, a virtual environment, and a backend (fake, Ollama or Claude).
- Lab 1: your first model call, with tokens and sampling and prompt engineering.
- Lab 2: a prompt test bench, with prompt engineering and evaluating LLM output.
- Lab 3: RAG from scratch, with embeddings and retrieval and RAG.
- Lab 4: measure your retrieval, with vector search internals and evaluating RAG.
- Lab 5: tools and an agent loop, with tools and function calling and agents.
Foundations
How the machinery works inside, in order. Do every Try it; they are short.
- Part I, Maths: chapters 2 to 9, the maths everything later needs.
- Part II, ML foundations: chapters 10 to 14, then train your own neural network (chapter 15).
- 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.
- Chapters 33 to 37: harness engineering, guardrails, security, hallucination and memory.
- Chapters 38 to 43: frameworks, quantisation, fine-tuning, production and regulated domains.
- Chapters 44 and 45: evaluating output with judges, and observability, tracing and cost.
- 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
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
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.