Hello! Welcome to the third lesson in our course on Generative Sequence Modeling.
Introduction
In our last two lessons, we established the foundational pipeline for processing text:
- Tokenization: We learned how to convert a raw string of text into a sequence of integer token IDs (e.g.,
[11241, 4712, 318, 12691, 0]). - Embedding: We then saw how an embedding layer maps these discrete IDs into dense, meaningful vectors, resulting in a tensor of shape
(batch_size, sequence_length, embedding_dim).
Now we have a numerical representation of text that a neural network can process. The fundamental question is: what is the core task the model is trained to perform with these vectors?
This lesson answers that question by introducing autoregressive language modeling. You will learn that the primary objective of a decoder-only model like GPT is to predict the next token in a sequence given all the tokens that came before it. This simple yet powerful principle is the engine behind the generative capabilities of all modern large language models.
The Core Idea: A Next-Token Predictor
At its heart, a large language model is a sophisticated function that predicts what word (or token) comes next. The model generates long, coherent responses not by planning the whole text at once, but by performing this next-token prediction task iteratively.
This short, high-level video from 3Blue1Brown provides an excellent intuition for this process.
Large Language Models explained briefly
This video, titled 'Large Language Models explained briefly,' offers a clear and concise overview of the fundamental task of an LLM.
Please watch the first 1 minute and 28 seconds. Focus on two key ideas: An LLM is a machine that takes text and assigns a probability to all possible next words. A chatbot generates a full response by repeatedly using this next-word prediction capability, feeding its own output back as input for the next step.
The process described in the video—where the model's output at one step becomes part of the input for the next—is known as autoregression. The term "auto" signifies that the model relies on its own past outputs to generate future ones.
This image provides a clear, step-by-step visualization of the autoregressive process:

Formalizing Autoregressive Language Modeling
We can formalize this intuitive idea using the language of probability. The goal of a language model is to learn the probability distribution over sequences of tokens, which we can write as , where is a sequence of tokens.
Using the chain rule of probability, we can decompose this joint probability into a product of conditional probabilities:
This can be written more compactly as:
This equation is the mathematical definition of autoregressive language modeling. It states that the probability of an entire sequence is the product of the probabilities of each token given all the tokens that came before it. The core task for the neural network is to learn how to effectively model this conditional probability, .
This resource provides a clear mathematical definition and explains how it leads to the generative process.
Small Language Models: An introduction to autoregressive ...
This tutorial, 'Small Language Models: an introduction to autoregressive ...', formally defines the language modeling task.
Please read the first three sections, down to the end of 'Autoregressive language modeling'. Focus on understanding the mathematical formulation and how it directly maps to the iterative generation process we've discussed.
Because the prediction for any token only depends on "past" tokens (), this is also referred to as causal language modeling. The model cannot look ahead in the sequence during training.
A Concrete Example: The Bigram Model
To make this concept tangible, let's build the simplest possible character-level language model: a bigram model. This model makes a strong simplifying assumption: the probability of the next character depends only on the single most recent character.
While this is a very basic model, implementing it from scratch is an excellent way to understand the mechanics of autoregressive generation. Andrej Karpathy's "makemore" tutorial does this brilliantly.
The following segments from his video will walk you through:
- How to estimate the probabilities by simply counting character pairs in a large dataset.
- How to use these probabilities to autoregressively sample new sequences of characters.
The spelled-out intro to language modeling: building makemore
In 'The spelled-out intro to language modeling', Andrej Karpathy builds a character-level bigram model from scratch. This is a practical demonstration of the concepts we've just discussed.
Please watch the following two segments: Building the Bigram Model (04:18 - 12:46): Watch how he iterates through a dataset of names, counts pairs of consecutive characters (bigrams), and stores these counts. This demonstrates a simple way to 'learn' the conditional probabilities. Sampling from the Model (24:00 - 36:14): This is the most important part. Observe the while loop where he starts with a special token, predicts the next character based on the learned probabilities, appends it, and repeats. This is the autoregressive loop in action.
As you saw, even this simple bigram model can generate name-like sequences, though they are often nonsensical. The poor quality highlights a key point: the model's context is too small. It only knows about the single preceding character. This limitation is precisely what more complex architectures like the Transformer are designed to overcome, by allowing the model to consider a much longer history of previous tokens when making its next prediction.
Tying It All Together
We can now summarize the entire process for a causal language model. This diagram illustrates the key stages.

The key steps in Autoregressive Inference are:
- Start with a prompt (e.g., "The quick brown fox").
- Pass the sequence of tokens through the model to get a probability distribution for the next token.
- Sample a token from this distribution (e.g., "jumps").
- Append the new token to the sequence ("The quick brown fox jumps").
- Repeat from step 2 until an end-of-sequence token is generated or a desired length is reached.
This article provides a concise written summary of this autoregressive loop.
Language Model Training and Inference: From Concept to Code
This article, 'Language Model Training and Inference: From Concept to Code', provides a clear, step-by-step description of the generation process.
Please read the short section titled 'Autoregressive Inference Process'. It clearly lists the three repeating steps that define autoregressive generation.
Conclusion
In this lesson, you learned about the fundamental operating principle of generative language models.
Key Takeaways:
- Autoregressive Language Modeling is the task of predicting the next token in a sequence given all the tokens that came before it.
- This task is formalized by modeling the conditional probability distribution: .
- Text generation is an iterative (autoregressive) process: the model predicts one token, appends it to its input, and repeats the process to generate a sequence.
- The quality and coherence of the generated text depend heavily on the amount of prior context the model can consider. A simple bigram model has very limited context, while a Transformer can attend to the entire history.
Preview of the Next Lesson:
We've established that the model's job is to produce a probability distribution for the next token. But how does a neural network, which outputs raw numerical values (called logits), produce a valid probability distribution? And how do we measure how "good" or "bad" its prediction is during training?
In the next lesson, we will answer these questions by exploring two essential components:
- The Softmax function, which converts logits into probabilities.
- The Cross-Entropy Loss, which quantifies the error in the model's prediction, driving the learning process.