Lecture 2. How LLMs work
Question 1
The crawl of September 2026, CC-MAIN-2026-39, contains 2.17 billion pages, 361.4 TiB of uncompressed content. It is distributed in three file formats:
| Format | Content of the files | Compressed size |
|---|---|---|
| WARC | raw HTTP responses, including the HTML of each page | 82.79 TiB |
| WAT | metadata: HTTP headers and links | 14.16 TiB |
| WET | plain text extracted from the HTML | 5.89 TiB |
Llama 3 405B was trained on \(15.6\cdot10^{12}\) tokens of text with knowledge up to the end of 2023.
| Category | Share | Tokens (computed) |
|---|---|---|
| General knowledge | 50% | \(\approx7.8\cdot10^{12}\) |
| Mathematics and reasoning | 25% | \(\approx3.9\cdot10^{12}\) |
| Code | 17% | \(\approx2.7\cdot10^{12}\) |
| Multilingual | 8% | \(\approx1.2\cdot10^{12}\) |
Olmo 3 32B was pretrained on \(5.5\cdot10^{12}\) tokens.
| Source | Share | Tokens |
|---|---|---|
| Web pages (Common Crawl) | 76.1% | \(4.51\cdot10^{12}\) |
| Academic PDFs, converted to text | 13.6% | \(0.81\cdot10^{12}\) |
| Code (GitHub, Stack-Edu) | 6.9% | \(0.41\cdot10^{12}\) |
| Mathematics web pages (FineMath) | 2.6% | \(0.15\cdot10^{12}\) |
| arXiv papers with LaTeX | 0.9% | \(0.05\cdot10^{12}\) |
| Wikipedia and Wikibooks | 0.04% | \(0.003\cdot10^{12}\) |
Microsoft, 35B active of 1T parameters: \(30\cdot10^{12}\) tokens.
| Source family | Share | Unique tokens | Training tokens | Epochs |
|---|---|---|---|---|
| Code (GitHub) | 54.6% | \(7.4\cdot10^{12}\) | \(16.4\cdot10^{12}\) | 2.22 |
| STEM | 15.8% | \(2.2\cdot10^{12}\) | \(4.7\cdot10^{12}\) | 2.17 |
| Web text | 14.9% | \(8.1\cdot10^{12}\) | \(4.5\cdot10^{12}\) | 0.55 |
| Mathematics | 5.4% | \(0.3\cdot10^{12}\) | \(1.6\cdot10^{12}\) | 5.28 |
| PDFs | 4.7% | \(2.7\cdot10^{12}\) | \(1.4\cdot10^{12}\) | 0.53 |
| Books and journals | 3.1% | \(0.6\cdot10^{12}\) | \(0.9\cdot10^{12}\) | 1.65 |
| Multilingual (other) | 1.6% | \(8.1\cdot10^{12}\) | \(0.5\cdot10^{12}\) | 0.06 |
Question 2
Any problems with the crawled data?
Olmo 3: of 252.6 billion web documents, 9.7 billion (about 4%) remain after filtering.
| Stage | What it does |
|---|---|
| URL filtering | drops pages from blocklisted domains: adult content, spam, malware |
| Text extraction | turns raw HTML into plain text |
| Language identification | classifies language on pages |
| Model-based quality filters | a classifier trained on good text |
| Deduplication | removes copies |
| PII removal | masks or removes personal data |
| Decontamination | removes the text of benchmark test sets |
| Mixing | decides how many tokens to take from each source and topic |
Setting. A text is a byte string \(s\in\Sigma^{*}\), \(\Sigma=\{0,\dots,255\}\).
A vocabulary is a finite set \(\mathcal V\subset\Sigma^{+}\) of tokens.
Definition. A tokenizer is a function \(\tau:\Sigma^{*}\to\mathcal V^{*}\) with \(\tau(s)=(x_1,\dots,x_T)\) and \(x_1x_2\cdots x_T=s\).
Remark. Tokens make the matrices of the Transformer smaller.
Algorithm 1 (BPE training; Gage 1994, Sennrich et al. 2016)
Input: a corpus, and a vocabulary size \(k\ge 256\).
Output: an ordered list of \(k-256\) merges.
| Model | Tokenizer | Vocabulary size \(|\mathcal V|\) |
|---|---|---|
| GPT-2 (2019) | byte-level BPE | 50,257 |
| Llama 2 (2023) | BPE (SentencePiece) | 32,000 |
| GPT-4 (2023) | byte-level BPE, cl100k_base | ≈100,000 |
| Llama 3 (2024) | byte-level BPE | 128,256 |
| GPT-4o (2024) | byte-level BPE, o200k_base | ≈200,000 |
| Qwen3.5 (2026) | byte-level BPE | ≈248,000 |
| DeepSeek-V4 (2026) | byte-level BPE | ≈129,000 |
Setting. \(\mathcal V\) is a vocabulary; its elements are called tokens.
A text is a sequence \(x=(x_1,\dots,x_T)\in\mathcal V^T\).
We write \(\Delta(\mathcal V)\) for the set of probability distributions on \(\mathcal V\).
Definition. A language model with parameters \(\theta\in\mathbb R^N\) is a map that assigns to every prefix a probability distribution of the next token:
Remark. By the chain rule, \(p_\theta\) defines a probability distribution on texts of each length \(T\):
Definition. The cross-entropy loss \(\mathcal L:\mathbb R^N\to[0,\infty)\), in nats per token, is
Definition. The perplexity is \(e^{\mathcal L(\theta)}\), i.e. we can think that the model is as uncertain as a uniform choice among \(e^{\mathcal L}\) tokens (for example, \(\ln 10 \approx 2.3\) nats per token, means that perplexity is 10).
Question 3
Answer. Every term of the loss equals \(-\ln(1/|\mathcal V|)\), so \(\mathcal L=\ln|\mathcal V|\approx 12\) nats per token.
The perplexity is \(e^{\mathcal L}=|\mathcal V|\approx e^{12}\).
Definition. \(\operatorname{softmax}:\mathbb R^{|\mathcal V|}\to\Delta(\mathcal V)\), \(\operatorname{softmax}(z)_v=e^{z_v}/\sum_{u\in\mathcal V}e^{z_u}\);.
Theorem (universal approximation; Cybenko 1989, Hornik 1991, Leshno et al. 1993).
Let \(\sigma:\mathbb R\to\mathbb R\) be continuous and not a polynomial.
For every compact \(K\subset\mathbb R^n\), continuous \(f:K\to\mathbb R^m\) and \(\varepsilon>0\) there are \(k\in\mathbb N\), \(W_1\in\mathbb R^{k\times n}\), \(b\in\mathbb R^k\), \(W_2\in\mathbb R^{m\times k}\) with
Examples of \(\sigma\): ReLU \(\max(0,t)\); sigmoid \(1/(1+e^{-t})\); SiLU \(t/(1+e^{-t})\) (in the MLP of Llama).
| Parameters | Operations | |
|---|---|---|
| matrix \(W_1\in\mathbb R^{k\times n}\) | \(kn\), all learned | \(kn\) multiply-adds, the fastest operation of a GPU |
| \(\sigma\) on \(\mathbb R^k\) | none: \(\sigma\) is fixed | \(k\) |
Note: \(u_k\) is a key, a pattern, and \(w_k\) its value.
Example: \(x\) is the vector of ␣exercise in “The proof is left as an exercise to the”.
| Key \(u_k\) matches | \(\sigma(u_k^{\top}x)\) | \(w_k\) |
|---|---|---|
| “proof” + “exercise” | 1.2 | a task left to the reader |
| “gym” + “exercise” | ?1.2 | a workout, training |
| Model | \(L\) | \(d\) |
|---|---|---|
| Llama 3 8B (2024) | 32 | 4,096 |
| Llama 3 405B (2024) | 126 | 16,384 |
| DeepSeek-V4-Pro (2026) | 61 | 7,168 |
| Kimi K3 (2026) | 93 | 7,168 |
Demonstration
The whole algorithm in one line: draw the next token from the model’s probabilities, write it at the end of the window, and drop the leftmost token if the window is full. Repeat until the model draws the end-of-text token or the length limit is reached.
for _ in range(max_new_tokens):
window = idx[:, -block_size:] # the last block_size tokens
logits, _ = model(window) # one pass of the network
probs = F.softmax(logits[:, -1, :], dim=-1) # probabilities of the next token
idx_next = torch.multinomial(probs, num_samples=1) # draw one token
idx = torch.cat((idx, idx_next), dim=1) # write it at the end
Demonstration
the base model OLMo 2 7B, a 4.5 GB file, run locally without a chat template; in a terminal: python3 base.py; this section types its steps 1 to 7, and step 8 is on “Hallucinations” (Section 8)
The base model is the network right after pretraining. It was trained only to continue documents, so it simulates documents: given the beginning of one, it writes a plausible rest.
Remark. The objective does not mention questions: a question in the prompt is continued as documents that begin with it continue, by an answer, more questions, or comments.
Demonstration
In the terminal: 2, the Kolmogorov prompt three times at temperature 1.
Before the draw, the scores of the tokens (the logits) are divided by the temperature and turned into probabilities again.
Example. Tokens after “… exercise to the”.
| ␣reader | ␣student | ␣audience | ␣interested | ␣listener | |
|---|---|---|---|---|---|
| score (logit) | 6.0 | 3.9 | 3.2 | 2.7 | 2.3 |
| temperature 0.5 | 0.980 | 0.015 | 0.004 | 0.001 | 0.001 |
| temperature 1 | 0.803 | 0.098 | 0.049 | 0.030 | 0.020 |
| temperature 2 | 0.514 | 0.180 | 0.127 | 0.099 | 0.081 |
Sampling occasionally draws a very unlikely token, and one bad token can derail the rest of the text. Truncation keeps only the plausible tokens, rescales their probabilities so that they sum to 1, and draws among them.
| Rule | Keeps |
|---|---|
| Top-k | the k most likely tokens |
| Top-p | the most likely tokens, until their probabilities add up to at least p |
| Min-p | every token at least min_p times as likely as the most likely token |
Usually the temperature is applied first, then the truncation.
Question 4
“The proof is by”; all other tokens have probability 0.
| token | ␣induction | ␣contradiction | ␣construction | ␣computation | ␣inspection |
|---|---|---|---|---|---|
| probability | 0.45 | 0.27 | 0.18 | 0.06 | 0.04 |
Answer. 1. ␣induction and ␣contradiction; their total is 0.72, so 0.625 and 0.375 (the ratio 5 : 3 stays).
2. Running totals 0.45, 0.72, 0.90: two tokens give only 0.72, so the third is needed; 0.5, 0.3, 0.2 (divide by 0.9).
3. The threshold is 0.2 × 0.45 = 0.09: the same three tokens, 0.5, 0.3, 0.2.
Demonstration
In the terminal: 3, the Kolmogorov prompt five times.
Demonstration
the originals, to compare with the model’s output word for word
In the terminal: 4, at temperature 0: the opening of the Wikipedia article on Shakespeare, “William Shakespeare (c. 23 April 1564 – 23 April 1616) was an English playwright, poet and actor. He is widely regarded as”, then the opening of the MIT License, “Permission is hereby granted, free of charge, to any person obtaining a copy”.
A passage of training text is extractable if, given the tokens that come before it in the training data, the model, always taking its most likely token, writes the passage out word for word (Carlini et al. 2023).
On GPT-Neo models (125M to 6B parameters, 825 GB of text), For the 6B model, 33% of the tested passages are extractable from 50 tokens of context and 65% from 450.
How much the weights can hold (Llama 3 8B). 8 billion parameters of 16 bits each, trained on over 15 trillion tokens:
16 bits × 8 billion / 15 trillion tokens ≈ 0.009 bits per training token.
Demonstration
In the terminal: 5, the first line of a news report on an event of July 2024; the model’s data end in December 2023:
BERLIN, July 14, 2024 — The final of Euro 2024 at the Olympiastadion ended
Definition. The knowledge cutoff of a model is the date of the latest text in its pretraining data.
| Model | Cutoff | Release |
|---|---|---|
| Llama 3 8B | March 2023 | April 18, 2024 |
| Llama 3 70B | December 2023 | April 18, 2024 |
| OLMo 2 7B (our demonstrations) | December 2023 | November 2024 |
| Gemma 4 31B | January 2025 | April 2, 2026 |
| GPT-6 Astra | April 30, 2026 | September 3, 2026 |
| Claude Opus 5.5 | June 2026 | September 22, 2026 |
Question 5
Answer. Begin a document in which an assistant answers, and put our question at the end:
A conversation between a user and
an assistant who answers briefly.
User: What is the derivative of sin x?
Assistant: cos x.
User: Is 91 a prime number?
Assistant: No, 91 = 7 × 13.
User: {question}
Assistant:
Demonstration
The prompt with {question} = “What is the integral of 1/x?”.
Example (GPT-3, 175B parameters, not trained further). The prompt contains solved pairs (sentence, translation); the score is BLEU, from 0 to 100, on the WMT’14 test set.
| Examples in the prompt | English → French | French → English |
|---|---|---|
| 0 (instruction only) | 25.2 | 21.2 |
| 1 | 28.3 | 33.7 |
| 64 | 32.6 | 39.2 |
Training a base model of GPT-2’s quality (1.5B parameters, 2019) from scratch:
| Run | Year | Hardware | Time | Cost |
|---|---|---|---|---|
| GPT-2, OpenAI | 2019 | 32 TPU v3 chips | 168 h | about $43,000 (estimate) |
| llm.c | 2024 | 8 H100 GPUs | 24 h | $672 |
| nanochat | January 2026 | 8 H100 GPUs | 3 h | about $73 |
| nanochat | March 2026 | 8 H100 GPUs | 1.65 h | about $40 |
Break
10 minutes
Demonstration
The system prompt is the first message of a conversation, with role system, written by the developer of the application. The chat template puts it at the start of the token sequence, before the user’s messages and the replies.
Remark. Nothing in the network marks the system prompt as special: i.e., the instruction hierarchy, is learned from fine-tuning data with conflicting instructions.
Supervised fine-tuning (SFT) continues training the base model with the same next-token loss as pretraining, on conversations instead of web documents.
| Model | Examples | Who wrote the assistant replies |
|---|---|---|
| InstructGPT (2022) | 13K | About 40 hired contractors, chosen by a screening test, following written labelling instructions |
| LIMA (2023) | 1,000 | 750 by users of Stack Exchange, wikiHow and Reddit, selected by the authors; 200 by the authors; 50 from Super-Natural Instructions |
| Tülu 3 (2024) | 939K | Existing replies by people or frontier models (e.g. GPT-4o); missing or weak replies newly generated with GPT-4o |
| DeepSeek-V3 (2024) | 1.5M | Reasoning: domain expert models trained on outputs of an internal DeepSeek-R1, then rejection sampling. Other tasks: DeepSeek-V2.5, checked by annotators |
Demonstration
Definition. A hallucination is a fluent and specific reply that is false, such as a wrong date, name, number or reference, stated in the same form as a correct reply.
Demonstration. In the terminal: 8, the same question three times to OLMo 2 7B, in the dialogue prompt of Question 5.
Remedy. Add to the fine-tuning data conversations in which the model does not know the answer and says so, for example:
User: When is the birthday of Adam Tauman Kalai?
Assistant: I don’t know his birthday.
A tool call is text that the model writes and the application executes (Llama 3.1 format):
Example of a call: <|python_tag|>brave_search.call(query="...")<|eom_id|>
Demonstration
login needed for both; web search and code execution are switched on in the composer
Question 6
User message. A course had 14 lectures of 240 minutes. Three were cancelled, and each remaining lecture had two breaks of 10 minutes. How many minutes of teaching took place?
| Reply | Text |
|---|---|
| A | 2,420 minutes. There were 14 − 3 = 11 lectures, each with 240 − 20 = 220 minutes of teaching. |
| B | 14 − 3 = 11 lectures took place. Each had 240 − 20 = 220 minutes of teaching. 11 · 220 = 2,420 minutes. |
Answer. B goes in, A does not. In A the result comes before any intermediate step, so its first token is computed in a single pass through the network, and the explanation after it cannot change it; a model fine-tuned on such replies learns to state results first and justify them afterwards. In B every intermediate result is written down and read by the later tokens.
A token enters the network as one learned vector, its row of the embedding table; its letters are not part of the input.
Example (o200k_base, GPT-4o; a box is one token, spaces included):
Example. Asked “How many Ds are in DEEPSEEK?”, DeepSeek-V3 answered 2 or 3 in ten trials (the answer is 1); DeepSeek-R1 first spelled the word and answered 1.
Example (o200k_base; a box is one token, spaces included; ␣ is the token consisting of one space). “9.11 > 9.9” and “12345 * 6789 =”:
| Model (tokenizer) | Task | Accuracy |
|---|---|---|
| Llama-3.1-8B (Llama 3) | compare X.Y with X.Z, one-digit Y, two-digit Z (e.g. 9.9 and 9.11) | 55% |
| Llama-3.1-8B (Llama 3) | the same comparisons when Z also has one digit | 100% |
| GPT-3.5 (cl100k_base) | add two 7–9-digit numbers | 75.6% |
| GPT-3.5 (cl100k_base) | the same sums but with commas | 97.8% |
3456789 + 12345678, cl100k_base: groups from the left
3,456,789 + 12,345,678: groups from the right
Sycophancy. A reply follows the beliefs the user has stated rather than what is true: the model withdraws a correct answer after the user disagrees, or praises an argument because the user says that they like it.
Example. In April 2025 OpenAI withdrew a sycophantic GPT-4o update within days; it named an added reward from users’ thumbs-up and thumbs-down.
Prompt injection. Text in data that the model reads, written so that the model follows it as an instruction, is a prompt injection.
Example. EchoLeak (CVE-2025-32711, 2025): injected text made Microsoft 365 Copilot leak data with no action by the user; rated critical, fixed by Microsoft.
| AIME 2024 (mathematics competition) | Problems solved |
|---|---|
| one sample, start of training | 15.6% |
| one sample, end of training | 77.9% |
| majority vote over the sampled answers, end of training | 86.7% |
Remark. Reflective words (“wait”, “verify”, “check”, “mistake”, …) become 5 to 7 times more frequent.
A reasoning model writes a chain of thought before its answer. Example (constructed):
User: What is the sum of the digits of 2^10?
Assistant: <think>2^10 = 1024. Digits 1, 0, 2, 4, sum 7. Wait, check 2^10:
2^5 = 32, and 32 · 32 = 1024. Yes.</think> The sum is 7.
Remark. Fine-tuning teaches the form of a reasoning text; RL makes the reasoning that reaches correct answers more likely.
Demonstration
login needed
A reasoning model first writes its thinking between <think> and </think>, then the answer.
| Method | How the length is controlled |
|---|---|
| Thinking budget (Qwen3, May 2025) | At inference: stop at the budget, insert a fixed sentence and </think> |
| Effort keyword (gpt-oss, Aug 2025) | Trained to follow “Reasoning: low / medium / high” in the system prompt |
| Budget reward (Kimi K3, Jul 2026) | In RL: reward −1 if the thinking is longer than a multiple of a budget estimated from the SFT model; a smaller multiple for lower effort |
Question 7
Answer. Yes. A test of 2025: “You have an apple and an orange. [one to five irrelevant statements with numbers and probabilities] Calculate how many fruits you have.” The answer is always 2.
A prompt such as “Write a limerick about a topologist” has no answer that a program can check. A reward model, a network trained on human comparisons, gives each response a score.
Example (illustrative numbers). Human judgements for one RL run, without and with a reward model: