Concept · Chapter 8: The Rise of Large Language Models
Autoregressive Next-Token Prediction
A GPT-style model reads the tokens so far and outputs a probability for every possible next token; generating text means picking one, appending it, and repeating.
The problem
We want one training objective that uses raw text with no labels and one procedure that can produce any kind of text.
The solution
Train a causal Transformer to predict each next token with cross-entropy loss; at generation time, sample a token from its output distribution, append it to the input and run again.
The consequence
Every position in every document becomes a training example, and one model can continue any text, which later turns out to include answering questions and following instructions.
You should understand first
Intuition: autocomplete, all the way down
Your phone's keyboard suggests the next word. A GPT model does the same thing with a much longer memory and a much better model of text: given everything so far, it assigns a probability to each of its tens of thousands of tokens. To write a paragraph, it predicts, commits to one token, and predicts again with that token included.
Training: every position is a lesson
Take the sentence "the cat sat on the mat", tokenized. With a causal mask, one forward pass produces a prediction at every position at once:
| Sees | Should predict |
|---|---|
| the | cat |
| the cat | sat |
| the cat sat | on |
| … | … |
The loss is the average cross-entropy: at each position. If the model gave "mat" probability 0.4, that position costs nats. A document of 2,000 tokens provides 2,000 of these lessons, with labels that came free with the text. This is self-supervised learning.
Generation: one token at a time
Predict
Run the model on the tokens so far; take the final position's logits and apply softmax.Choose
Pick a token: the most likely one (greedy) or a random draw shaped by temperature, top-k or top-p (decoding).Append and repeat
Add the chosen token to the input. Stop at an end-of-text token or a length limit.
Try it · toy model
Turn the temperature, top-k and top-p dials on a model's next-word probabilities, build a sentence word by word, and compare whole generations.
Why should I care?
As a researcher
It is the objective behind every GPT-style model; understanding it is the starting point for scaling, post-training and interpretability.
As an engineer
It explains why output streams token by token, why longer answers cost more and take longer, and why the same prompt can give different answers.
Modern systems that depend on it
- decoding strategies
- in-context learning
- chat assistants
- the KV cache (Chapter 11)
Historical context
Before
N-gram and recurrent language models predicted the next word too, but with short contexts or sequential computation.
After
A causal Transformer predicts every next token of a long sequence in parallel during training.
Used today
Every GPT-style chat model generates this way, one token per forward pass.
What to remember
- Output: one logit per vocabulary token → softmax → P(next token | everything so far).
- Training: at every position, cross-entropy against the token that actually came next. One document = thousands of examples.
- Causal masking lets training score all positions in one pass; generation still goes one token at a time.
- Generation loop: predict → choose (decode) → append → repeat, until an end token or a length limit.
Key papers
Attention Is All You Need
Ashish Vaswani, Noam Shazeer et al. · 2017 · NeurIPS 2017
Introduced the Transformer — the architecture behind BERT, GPT and nearly every modern large language model, and later adapted to vision, audio and more.
How to read it: Section 3 is the architecture — read it with Figure 1 open. Sections 3.2.1–3.2.2 contain the attention equation. You can skim the training details on a first pass.
Improving Language Understanding by Generative Pre-Training
Alec Radford, Karthik Narasimhan et al. · 2018 · OpenAI technical report
GPT-1: a 12-layer decoder-only Transformer pretrained to predict the next token on over 7,000 unpublished books, then fine-tuned. It improved the state of the art on 9 of 12 tasks and set the template for every GPT since.
Watch
3Blue1Brown
Transformers, the tech behind LLMs | Deep Learning Chapter 5
A visual tour of a GPT from input text to next-token probabilities — ideal before or right after Chapter 7.
Andrej Karpathy
Let's build GPT: from scratch, in code, spelled out.
The best way to reach IMPLEMENT level on Transformers: write one yourself, line by line, in PyTorch.