---
name: How an LLM is built
description: >-
  Teach an Edge FDE to walk seven public Karpathy repos in Hao Hoang's
  recommended order, so they can explain how an LLM learns, reads text,
  trains, and gets cheaper, instead of only calling an API.
---
# How an LLM is built

Use this course when someone can call an LLM API but cannot explain how one is built. Follow Hao Hoang's ordering rule: each stop answers the question the one before it leaves open. Cite [Hao Hoang's LinkedIn post](https://www.linkedin.com/feed/update/urn:li:activity:7531134561951567872) once on the course home, with the date 24 November 2026 at 23:20 Europe/London (UTC). Do not repeat the full post in every lesson. Credit Andrej Karpathy's repos as the primary code and lecture sources.

## End-to-end agent recipe

1. Start with [lesson 01](01-how-a-network-learns.llms.md). Set up Python 3.11+ with `uv venv`, `uv add torch pytest`, and a device check for MPS, CUDA, or CPU. Teach a tiny scalar autograd `Value`, topological backward, and the micrograd README numbers 24.7041, 138.8338, and 645.5773. Point at [micrograd](https://github.com/karpathy/micrograd). The README says about 100 lines of engine and 50 lines of neural net. Current files are 94 and 60 lines. The readiness gate: rebuild the engine without looking.
2. Use [lesson 02](02-eight-lectures-to-gpt.llms.md) as a map of the eight Zero to Hero lectures. Official syllabus: [karpathy.ai/zero-to-hero.html](https://karpathy.ai/zero-to-hero.html). Repo: [nn-zero-to-hero](https://github.com/karpathy/nn-zero-to-hero). Watch lecture 1 with lesson 01. Do not try to replace the videos. Link each lecture to its official video and notebook. Use progressive diagrams, not one mega diagram as the only picture. Hao's gate still holds: ready for minbpe when the engine can be rebuilt without looking.
3. Use [lesson 03](03-what-the-model-reads.llms.md) for Byte Pair Encoding. Run the Wikipedia three-merge example on `aaabdaaabac` and get `[258, 100, 258, 97, 99]`. Cover BasicTokenizer, RegexTokenizer, and GPT4Tokenizer. Cite the README's tiktoken `cl100k_base` ID match, [Sennrich et al. 2015](https://arxiv.org/abs/1508.07909), the [GPT-2 paper](https://cdn.openai.com/better-language-models/language_models_are_unsupervised_multitask_learners.pdf), and [tiktoken](https://github.com/openai/tiktoken). Warn about `allowed_special`.
4. Use [lesson 04](04-a-full-training-loop.llms.md) to read nanoGPT. README: roughly 300-line `train.py` and 300-line `model.py` (current counts 336 and 330). Teach the loop: batch, forward, loss, backward, optimizer step. State the November 2025 deprecation and the pointer to nanochat. Laptop path: Shakespeare character example with `--compile=False` if needed. Do not start a new large training run in this repo.
5. Use [lesson 05](05-what-pytorch-hides.llms.md) for llm.c. CPU reference `train_gpt2.c` is described as about 1,000 lines in one file (current total 1,182). Tests match a PyTorch debug state. CPU demo is enough. CUDA is optional. Use the LayerNorm tutorial as a way into the C.
6. Use [lesson 06](06-what-the-pipeline-costs.llms.md) for nanochat. Explain $48 as about 2 hours on 8×H100 at about $3/GPU/hour, and $43,000 as Karpathy's 32 × 168 × $8 estimate from [Discussion 481](https://github.com/karpathy/nanochat/discussions/481) and [LEADERBOARD.md](https://github.com/karpathy/nanochat/blob/master/dev/LEADERBOARD.md), not an OpenAI invoice. Target is DCLM CORE 0.256525 from [arXiv:2406.11794](https://arxiv.org/abs/2406.11794). Current record 1.65 hours on 14 March 2026. Official install is `uv sync --extra cpu` or `--extra gpu`. Do not tell the reader they must rent 8×H100.
7. Use [lesson 07](07-when-an-agent-runs-the-loop.llms.md) for autoresearch. Correct the post: the agent iterates a simplified nanochat trainer, not nanoGPT. Metric is `val_bpb`, not raw validation loss. Five-minute budget, about 12 runs per hour, about 100 overnight. Human edits `program.md`. Agent edits `train.py`. Mention the overfitting risk on one validation set, NVIDIA as the official platform, listed forks, and leaderboard rounds 1 and 2.
8. Use the human HTML pages for the reader. Use the `.llms.md` briefs for agent steps and acceptance checks.

## Constraints

- Plain spoken English for a non-native reader.
- Literal sentences. No flourish, no quizzes.
- Do not use em dashes.
- Brand Edge FDE only. Do not name a personal course author.
- Credit Hao Hoang and link the inspiring post on the course home.
- Do not invent sources, WWDC sessions, or post text.
- Teach setup with `uv` and Python 3.11+. Install PyTorch with the Apple Silicon MPS-or-CPU path. Every HTML `<pre><code>` block needs a language class.
- Mark cost figures with their sources. Mark the $43,000 figure as Karpathy's estimate.
- Correct the post where the primary repo disagrees: autoresearch is based on nanochat, and its keep metric is `val_bpb`.

## Key concepts

- Calling an LLM and knowing how one is built are different skills. The second one is learnable from small, public code.
- Each stop answers the question the previous stop leaves open: learning, the path to GPT, tokens, the training loop, the kernels under PyTorch, today's cost, then agents that run the loop.
- Autograd builds a graph on the forward pass and walks it backward to get gradients.
- A tokenizer is a separate trained stage. It maps strings to token IDs and back.
- A training loop is batch, forward, loss, backward, and an optimizer step, repeated.
- nanoGPT is deprecated in favour of nanochat. Read nanoGPT for clarity. Train with nanochat if you want the current pipeline.
- The $48 and $43,000 figures are explained in the nanochat README and in Karpathy's write-up of the estimate. The current speedrun record is 1.65 hours on 8×H100, as of 14 March 2026.
- Autoresearch keeps a change only when validation bits per byte improves after a fixed five-minute training budget.

## Takeaways

- You can rebuild a tiny autograd engine and check it against published numbers.
- You have a map of the eight Zero to Hero lectures and a clear gate before you move to the tokenizer repo.
- You can train a small BPE tokenizer and say how a GPT-4 wrapper matches tiktoken.
- You can point at the two files that make a readable GPT trainer, and you know which repo to use if you want to train today.
- You can explain what remains when the trainer is written in C and CUDA.
- You can quote the cost figures with their sources, and you know a laptop is enough to learn the pipeline even if you never rent 8×H100.
- You can describe the keep-if-improved agent loop, including the metric it actually uses.

## Acceptance checks

- Every human lesson HTML has **Key concepts** and **Takeaways** sections before lesson-nav. The course home has the same two headings for the whole course.
- Seven human lessons exist with the basenames in this skill.
- Seven matching `.llms.md` briefs exist and link to their human HTML pages.
- Every human lesson has its Agent brief link.
- The course index lists all seven lessons, the course SKILL.md, and all seven briefs.
- The course home credits Hao Hoang, dates the post, and links it.
- There is no em dash, no quiz, no invented WWDC session, and no personal author name.
- Environment setup with `uv` and the MPS-or-CPU PyTorch check appears before the first runnable example.
- Cost figures and the autoresearch metric are sourced and corrected where needed.

## Environment setup

Lesson 01 needs Python 3.11 or newer, `uv`, `torch`, and `pytest`. Check `torch.backends.mps.is_available()` and `torch.cuda.is_available()`, and fall back to CPU. Later lessons reuse that environment. minbpe may add `tiktoken`. nanoGPT may add `numpy`, `transformers`, `datasets`, `tiktoken`, `wandb`, and `tqdm`. nanochat official install is `uv sync --extra cpu` or `uv sync --extra gpu`. autoresearch official install is `uv sync` on an NVIDIA GPU.
