Probabilistic Causal Language Modeling
Train a model on custom data and visualize how it predicts the next token based on learned probability distributions.
Train a model on custom data and visualize how it predicts the next token based on learned probability distributions.
Causal Language Modeling (CLM) is the basic principle behind how generative AI models like GPT work. At its core, it's a simple yet powerful task: predicting the next word (or "token") in a sequence given the words that came before it. This interactive lab explains that process. This allows you to train your own mini-model and see just how it makes decisions. The "causal" in the name means the model can just look left. When predicting token t, it sees tokens 1 through t-1 but never t+1 or beyond. This is enforced by a causal mask in the attention layer that sets future spots to negative infinity before the softmax. Contrast this with masked language modelling, where the model sees both sides of a blank and fills it in — that is the pretraining objective behind BERT and is better for grasp. But it cannot generate text since it has no notion of left-to-right sorting. CLM training is self-supervised: the training data is raw text with no labels. And each token's label is simply the next token in the sequence. The loss is cross-entropy averaged over all spots. And the model learns a chance distribution over the full vocabulary at each step. At generation time, a sampling plan (temperature, top-k, top-p) picks from that distribution to give the next token. And the process repeats autoregressively.
Train a model on custom data and visualize how it predicts the next token based on learned probability distributions.
Causal Language Modeling (CLM) is the fundamental principle behind how generative AI models like GPT work. At its core, it's a simple yet powerful task: predicting the next word (or "token") in a sequence given the words that came before it. This interactive lab explains that process, allowing you to train your own mini-model and see exactly how it makes decisions.
Causal Language Modeling (CLM) is the basic principle behind how generative AI models like GPT work. At its core, it's a simple yet powerful task: predicting the next word (or "token") in a sequence given the words that came before it. This interactive lab explains that process. This allows you to train your own mini-model and see just how it makes decisions. The "causal" in the name means the model can just look left. When predicting token t, it sees tokens 1 through t-1 but never t+1 or beyond. This is enforced by a causal mask in the attention layer that sets future spots to negative infinity before the softmax. Contrast this with masked language modelling, where the model sees both sides of a blank and fills it in — that is the pretraining objective behind BERT and is better for grasp. But it cannot generate text since it has no notion of left-to-right sorting. CLM training is self-supervised: the training data is raw text with no labels. And each token's label is simply the next token in the sequence. The loss is cross-entropy averaged over all spots. And the model learns a chance distribution over the full vocabulary at each step. At generation time, a sampling plan (temperature, top-k, top-p) picks from that distribution to give the next token. And the process repeats autoregressively.
A language model learns by studying vast amounts of text. It doesn't understand text like a human does; instead, it learns numeric patterns. The process, which you can simulate in this lab, involves:
When you click the "Update Learned Chances" button, you are running this training process on the text you've provided. The model builds its numeric grasp based just on that data.
Once trained, the model makes text one token at a time. Given your input prompt, it looks at the sequence and finds the chance distribution for the very next token. This is what you see in the "Next Token Predictions" list. Each bar represents the model's "confidence" that a given token should come next.
The simplest method is to always choose the token with the highest chance. This is called "greedy decoding." It's predictable but can lead to repetitive and boring text.
To add creativity, we add randomness via a "temperature" setting. A low temperature makes the model's choices more conservative (closer to greedy). While a high temperature raises randomness. This allows it to pick less likely tokens and give more surprising (but perhaps less coherent) text.
Causal language modelling is the training objective behind each generative model. Predict the next token from the tokens before it, and never look ahead.
P(w₁, w₂, …, wₙ) = ∏ᵗ P(wᵗ | w₁ … wᵗ₋₁)
The word "causal" means that one-one-way flow: token t may depend on its past and never on its future. (It is often written "casual" by accident, with in some published code.)
The method enforcing it is a mask. Attention scores for future spots are set to negative infinity before the softmax. So their weights turn just zero:
the cat sat on
the . -inf -inf -inf
cat . . -inf -inf
sat . . . -inf
on . . . .Without that mask, predicting "cat" from a context with "cat" would be trivial at training time and not doable at inference. Where the future does not exist.
The masking has an speed result that is easy to miss and is central to why this objective scales.
For a sequence of 1,000 tokens, a single forward pass gives predictions at all 1,000 spots at once, each correctly conditioned just on its own past. The loss is averaged over all of them.
So one pass over a document yields as many training signs as the document has tokens. No labelling, no annotation — any text at all is training data. And the supervision is free.
That property is the reason next-token prediction won. The available training set is the written output of humanity. And each token in it is a labelled case.
| Causal LM | Masked LM | |
|---|---|---|
| Sees | Past only | Both directions |
| Predictions per pass | Every position | Only the masked 15% |
| Can generate | Yes | No |
| Natural at | Generation | Understanding |
| Models | GPT, Llama, Claude | BERT, RoBERTa |
The second row is a real speed gap: masked language modelling extracts a training sign from roughly 15% of tokens per pass, causal modelling from all of them.
Trained at enough scale, next-token prediction gives powers nobody specified.
To predict text well a model must track syntax, resolve pronouns, hold facts, maintain consistency over long passages, and continue patterns. All of those reduce prediction error, so all of them are learned.
In-context learning is the most striking. Show three worked cases in the prompt and the model goes on the pattern with no weight updates at all. That is pattern follow-up, which is just what the objective rewards. And research on "induction heads" traces the ability to identifiable attention circuits that copy and complete earlier patterns.
Two things it does not give, and both follow from the objective. Truthfulness, since likelihood is not truth: a plausible falsehood scores well. And calibrated doubt, since the model outputs a distribution over tokens rather than a belief about the world.
The claim that a single forward pass trains each spot at once is the reason causal language modelling scales. This builds the mask that makes it true, checks that it really prevents the model from reading ahead, and counts what the other would have cost.
Building on the same setup, the next block shows the left cases.
The below block covers the left steps.
The next block goes on with the left demonstrations.
Use the controls to build intuition for how these models behave.
A causal language model trained just on this objective is a base model. It goes on text. Ask it a question and it may give more questions, since that is what a list of questions looks like in the training data.
Two further stages make it an assistant:
Supervised fine-tuning on cue-response pairs teaches the format — that a question should be answered, that output has a structure, that a conversation has turns.
Choice optimisation (RLHF or DPO) tunes helpfulness, tone and refusal behaviour from human checks.
Neither changes the method. The model is still predicting the next token; the distribution it has been tuned to predict is other.
This distinction matters nearly: base models and cue-tuned models behave very else on the same prompt. And a base model is often the wrong thing to build a product on.
The model gives chances; a thing must choose.
| Setting | Effect |
|---|---|
| Temperature 0 | Always the most likely token — deterministic |
| Temperature 0.7 | Moderate variety |
| Temperature >1.2 | Usually incoherent |
| Top-p 0.9 | Sample from the smallest set summing to 0.9 |
| Repetition penalty | Down-weight tokens already used |
Two facts worth carrying. Greedy decoding is not best — picking the most likely token at each step does not give the most likely sequence. This is why beam search exists and why greedy output tends to repeat. And temperature 0 is the right default for a thing factual: variety in a factual answer is error.
Why "causal"? Since data flows in one way just, past to future. The same sense as in causal systems. It is often misspelled "casual".
Is this the same as autoregressive? In effect yes. "Causal" describes the masking; "autoregressive" describes feeding outputs back as inputs.
Why not train bidirectionally and generate anyway? A bidirectional model has no notion of "next". And its training task assumes the close context exists. Tries to generate by iterative filling work poorly.
Does the model plan the whole sentence? Architecturally it emits one token at a time, and there is evidence internal states encode data about upcoming tokens. Whether that is planning is an open question.
Why does it repeat it? Low temperature and greedy decoding favour high-chance continuations, and repetition is high-chance. Raise temperature, use top-p, or apply a repetition cost.
Can a causal model do classification? Yes — by prompting, or by reading the final hidden state. At scale it matches encoders on most grasp benchmarks despite the one-one-way constraint.
Say the answer out loud before you reveal it — recalling it is what makes it stick, and rereading it is not.
What does this module say about “How GPT-style models learn to write”?
What does this module say about “Quick Context: Learning from Data”?
What does this module say about “Greedy Decoding”?
See how predicting the next token, over and over, is all it takes for a model like GPT to produce fluent text.