Inside a Foundation Model

How a raw text predictor becomes an assistant, and why the same prompt can return a different answer, or a made-up one.

Lesson 2 · Previews Chapter 2 · about 15 min

Your win today Given a product need (“consistent grading,” “creative taglines,” “strict JSON”), you'll be able to pick sampling settings for it and explain inconsistency and hallucination to a teammate.

Warm-up · from Lesson 1

Recall these before reading on. Spacing your review across sessions is what moves an idea into long-term memory.

1 · What makes one model differ from another

You'll mostly use models, not build them. Still, Huyen traces most differences between models to three decisions: training data, architecture and size, and post-training. Ch2 intro

Napkin math you'll reuse Each parameter stored in 16 bits takes 2 bytes, so a 7B model needs at least 14 GB of GPU memory just to run inference. Ch2 · Model Size Chapter 7 builds on this arithmetic.

2 · From monster to assistant: the training pipeline

1. Pre-training 2. Supervised finetuning 3. Preference finetuning Self-supervised on web-scale text Learns to complete text ~98% of compute (InstructGPT) (prompt, response) demonstrations Learns to converse a.k.a. behavior cloning RLHF, DPO, RLAIF Learns human preferences steps 2+3 = post-training
After Fig. 2-10. Huyen compares the result to the “Shoggoth with a smiley face” meme: pre-training produces the monster, SFT makes it socially acceptable, and preference finetuning adds the smile.

Why SFT is needed. A pre-trained model only completes text. Give it “How to make pizza” and “for a family of six?” is a perfectly valid completion. SFT trains it on high-quality demonstration data, (prompt, response) pairs, often written by highly educated labelers at up to 30 minutes per pair. Ch2 · Supervised Finetuning

Why preference finetuning is needed. Demonstrations teach the model how to converse but not which conversations to have. In RLHF, you first train a reward model that scores responses, then optimize the language model to earn high scores. The reward model learns from comparison data, (prompt, winning response, losing response), because people agree far more on “A beats B” than on a 1–10 score. DPO is a simpler alternative; Meta switched to it for Llama 3. Ch2 · Preference Finetuning, Reward model

Pre-training [is like] reading to acquire knowledge, while post-training is like learning how to use that knowledge.Huyen, Ch. 2, “Post-Training”

3 · Sampling: how the model picks each token

For every next token, the model outputs one logit (a raw score) per token in its vocabulary. Softmax turns the logits into probabilities. Then a token is sampled. Huyen calls sampling “perhaps one of the most underrated concepts in AI.” Ch2 · Sampling

KnobWhat it doesTypical values
GreedyAlways takes the most likely token. Fine for classification, boring for text.= temperature 0
TemperatureDivides the logits before softmax. Lower values sharpen the distribution (consistent); higher values flatten it (creative, less coherent).Providers allow 0–2; 0.7 is common for creative work; 0 for consistency
Top-kKeeps only the k most likely tokens, which also cuts the softmax computation.50–500
Top-p (nucleus)Keeps the smallest set of top tokens whose probabilities add up to p, so the pool size adapts to the context.0.9–0.95
Stopping conditionA maximum token count or stop tokens. Saves latency and cost, but can cut off JSON mid-object.—

A worked example from the book: logits [1, 2] for tokens A and B. At temperature 1, B gets 73%. At temperature 0.5, B gets 88%. Ch2 · Temperature

Try it: do the challenges below with the knobs

Before each challenge, predict which knob you'll need, then check whether you were right.

Toy logits invented for this exercise; the context “My favorite color is” is from Fig. 2-14. Click “Sample 20 times” to see the randomness for yourself.

Test-time compute. Instead of a single response, generate several and pick the best (best-of-N), using a scorer, the highest overall probability, or the user's choice. It reliably helps, but two outputs cost about twice as much as one. Ch2 · Test Time Compute

4 · The probabilistic nature of AI

Anything with a non-zero probability can come out, “no matter how far-fetched or wrong.” Ch2 · The Probabilistic Nature of AI That causes two problems you'll fight for the rest of the book:

Inconsistency

The same input can give different outputs (Huyen got 3/5 and then 5/5 grading the same essay), and a slightly different input can give a very different output. Mitigations: cache answers; fix temperature, top-p, and top-k; fix the seed. Even then, identical outputs aren't guaranteed, because the hardware running the model can change results. Ch2 · Inconsistency

Hallucination

A response not grounded in facts. Two complementary hypotheses: Ch2 · Hallucination

  1. Self-delusion (DeepMind, 2021). The model can't tell the text it was given from the text it generated. After writing “Chip Huyen is an architect,” it treats that as fact and builds on it. Errors snowball.
  2. Knowledge mismatch (Leo Gao, John Schulman, OpenAI). In SFT the model imitates labelers' answers. When those answers use knowledge the model doesn't have, we're effectively teaching it to make things up.

Practical levers for now: ask for concise answers (fewer tokens means fewer chances to invent), tell it to say “I don't know,” and ground it with context (Chapters 5–6). Detecting hallucinations is covered in Chapter 4.

Check yourself

Primary source

Read next: Huyen, AI Engineering, Chapter 2: “Post-Training,” “Sampling” (through “Structured Outputs”), and “The Probabilistic Nature of AI.” These are the parts this lesson previews. You can skim “Modeling” (the transformer internals and scaling laws) on a first pass; Huyen herself says to skip any concept you're confident about.

Optional, about 30 min: the first half of Andrej Karpathy's “Intro to Large Language Models” covers the same pre-training → finetuning pipeline from a model builder's view.

Stuck or curious? Ask me anything, e.g. “what's the actual difference between RLHF and DPO?” or “what temperature should a support bot use?”. When you're done, tell me your warm-up and quiz scores, and whether all three sampler challenges went green.

Next: Lesson 3, Evaluation Methodology →

Related ideas