STANFORD-CS229 COURSE GUIDE

Building Large Language Models (LLMs)

A chapter-by-chapter readback of the free course by Yann D on Stanford Online.

Length1:44:31
Chapters17
Modules5
Plain Markdown with clickable timestamps — drops into Obsidian, Notion or GitHub as-is.

This lecture covers the full LLM pipeline in one sitting, with an industry-first emphasis: data, evaluation, and systems matter more than architecture or training algorithms.

Who it's for
Read this guide if
  • You want a single overview of the full LLM lifecycle, from tokenization through RLHF
  • You want to understand scaling laws, Chinchilla-optimal ratios, and training costs
  • You want practical details like perplexity limits and GPU utilization that tutorials skip
Skip ahead if
  • You want transformer architecture internals, explicitly deferred to other lectures
  • You need hands-on code rather than conceptual explanations
  • You are looking for coverage of multimodal models or inference optimizations
In brief
  • 1h45 lecture (6271 seconds) with 11 chapters across 6 modules
  • Covers pre-training (language modeling) and post-training (SFT, RLHF, DPO)
  • Cites real models and projects: GPT-3/4, Llama 2/3, Alpaca, Chinchilla, Chatbot Arena, AlpacaEval
  • Includes concrete numbers: Llama 3 trained on 15.6T tokens for ~$75M on 16,000 H100s
  • Intermediate level; assumes basic ML knowledge and references CS224N, CS324, CS336 for depth

The walkthrough

Each chapter below is our original written explanation of what happens at that moment in the video — a paraphrase, not a transcript. Click any timestamp to jump the player.

Foundations of Language Modeling

0:00

Introduction

Introduces LLMs behind chatbots like ChatGPT, Claude, Gemini, and Llama, and lays out five components of training them: architecture, training loss, data, evaluation, and systems. The speaker argues industry rightly emphasizes data, evaluation, and systems over the architecture focus common in academia.

3:44

Autoregressive Language Models

Explains language models as probability distributions over token sequences, factorized via the chain rule into next-token predictions. Covers the tokenize-predict-sample workflow, why sequential generation is slow, and cross-entropy training as a classification task.

10:04

Tokenization

Motivates subword tokens over words (typos, languages without spaces) and characters (quadratic complexity), then walks through Byte Pair Encoding training and its practical quirks like pre-tokenizers and picking the largest match. Highlights drawbacks such as poor arithmetic generalization and GPT-4's code tokenization fix.

19:05

Evaluation and Perplexity

Defines perplexity as 2 to the average per-token loss, its range from 1 to vocabulary size, and why it depends on the tokenizer, so it is no longer used for benchmarking. Introduces academic benchmarks like HELM, the Open LLM Leaderboard, and MMLU, plus issues like inconsistent evaluation and train-test contamination.

Pre-training Data and Scaling

29:15

The Data Pipeline

Describes starting from Common Crawl (~250 billion pages, ~1 petabyte) and the filtering pipeline: HTML extraction, removing NSFW/PII content, deduplication, heuristics, and Wikipedia-like model-based filtering. Notes domain reweighting (upweighting code improves reasoning), final annealing on high-quality data, and that ~15 people on Llama's team worked on data.

40:03

Scaling Laws

Presents the empirical finding that loss decreases linearly (on log-log plots) with compute, data, and parameters, letting future performance be predicted years ahead. Explains how to fit a scaling recipe on small models before training the big one, and how data quality strongly affects scaling.

49:49

Chinchilla, Compute, and Cost

Covers optimal token-per-parameter ratios (Chinchilla's 20 for training compute, ~150 when factoring inference cost), the Bitter Lesson, and Llama 3 405B as a case study: 15.6T tokens on 16,000 H100s, roughly $75M total cost, ~4,400 tons CO2, and flops kept deliberately below a regulatory threshold.

Post-Training: SFT

59:59

From Language Model to Assistant

Motivates post-training: pure language models mimic internet formats instead of answering, so alignment teaches them to follow instructions and refuse harmful requests. Since desired (question, answer) pairs are rare online, the solution is fine-tuning on human-provided data, which is what turned GPT-3 into ChatGPT.

1:03:04

Supervised Fine-Tuning and Synthetic Data

Explains SFT as the same language-modeling loss applied to human answers, illustrated by the Alpaca project (52k LM-generated examples from 175 seeds). Notes that scaling SFT data barely helps, since SFT mainly teaches formatting over knowledge already in the model, and advocates human-in-the-loop editing over purely synthetic data.

Post-Training: RLHF

1:09:31

Limitations of SFT

Identifies three SFT problems: behavior cloning is bounded by humans' generation ability, it can teach hallucination when fine-tuning on facts the model never saw, and writing ideal answers for every instruction is expensive. These motivate maximizing human preference instead of cloning behavior.

1:12:04

Reward Models and PPO

Walks through the RLHF pipeline: generate two answers per instruction, collect human preferences, train a reward model (Bradley-Terry with softmax over pairs), then optimize the LLM with PPO plus regularization against over-optimization. Stresses that PPO is messy in practice, and that the resulting model is a policy whose likelihoods are no longer meaningful.

Introduces DPO, a Stanford-proposed simplification that skips the reward model and RL entirely, directly maximizing preferred and minimizing dispreferred response likelihoods. Its global optima match PPO under assumptions, it performs about as well, and it has become standard in open source; PPO's head start is attributed to OpenAI's RL-heavy team.

1:21:27

Problems with Preference Data

Surveys difficulties of human preference collection: complicated guidelines, annotators optimizing superficial features like length, slow and expensive labeling, and only ~66% human inter-annotator agreement. LLM annotators are cheaper and agree with the human consensus more reliably, now a standard ~50x-cheaper practice.

1:22:23

Evaluating Post-Training

Explains why perplexity is useless after alignment (PPO-trained models are near-deterministic policies) and introduces evaluation via pairwise comparison: Chatbot Arena with human users, and LLM-as-judge methods like AlpacaEval, which correlates 98% with Arena in under three minutes and ten dollars.

1:30:51

LLM-as-Judge Bias and Fine-Tuning Details

Shows the verbosity bias of LLM judges (GPT-4's win rate swings from 64.4% to 20% just by prompting style) and how regression analysis can control for length. Covers practical fine-tuning facts: full weight updates in industry vs LoRA in open source, ~5k-50k SFT vs ~1M RLHF examples, and the view of pre-training as mere weight initialization.

Systems and Efficiency

1:33:42

GPU Fundamentals

Explains why compute is the bottleneck: GPUs are throughput machines optimized for matrix multiplication, and compute has outpaced memory and communication so naive code leaves GPUs idle. Introduces the memory hierarchy and model FLOPs utilization, noting even LLaMA only reached ~45%.

1:38:45

Optimizations

Covers low-precision training (mixed precision: 16-bit compute with 32-bit weight storage) and operator fusion, where torch.compile rewrites code to fuse operations for roughly 2x speedup. Mentions further topics (tiling, mixture of experts), uncovered areas, and recommended Stanford courses CS224N, CS324, and CS336.

Moment by moment

TimeWhy it matters
2:54The speaker's contrarian framing: data, evaluation, and systems matter more than architecture and training algorithms
42:25The full pre-training data pipeline, from 1 petabyte of Common Crawl to ~15 trillion curated tokens
41:25Scaling laws explained: why loss is predictable and how hyperparameters are tuned on small models first
52:46Chinchilla and the Bitter Lesson: optimal token-per-parameter ratios and what they imply for real models
1:33:06Chatbot Arena and AlpacaEval: how post-trained models are actually evaluated in the field

Do this next

  1. Reimplement Byte Pair Encoding on a small corpus to internalize how tokenizers are trained and applied
  2. Read the Chinchilla paper and compute the optimal model size and token count for a hypothetical compute budget
  3. Compare a pre-trained and an instruction-tuned model on the same prompt to see SFT/RLHF differences firsthand, then explore the AlpacaEval leaderboard

Common questions

Why does the lecture skip transformer architecture?

The speaker explicitly defers architecture, saying it was covered in a prior lecture and is well documented online; his focus is on data, evaluation, and systems, which he argues industry values most.

Why is perplexity no longer used to compare LLMs?

Perplexity depends on the tokenizer and evaluation data, so it is not comparable across models with different vocabularies. It remains useful during development, but benchmarks like MMLU, Chatbot Arena, and AlpacaEval replaced it.

What is the difference between SFT and RLHF?

SFT clones human-written answers with the same language-modeling loss; RLHF instead trains a reward model on human preferences and optimizes the model against it (with PPO or DPO), letting the model exceed what humans can generate directly.

How much does it cost to train a frontier LLM?

The lecture estimates Llama 3 405B at roughly $75M: about $52M in GPU rental (26M H100 hours) plus ~$25M in salaries, trained on 16,000 H100s over roughly 70 days.

Spot a mistake?

Every summary on this page is hand-checked against the video, but even careful editing can miss things. If you spot an error in a timestamp, summary, or fact, please email us and we will correct it.

More course guides