Please enable JavaScript to use CodeHS

AI Explained

How a model predicts the next word

A language model is a next-word guessing machine. It reads the words so far, turns them into a probability distribution over what could come next, and samples one.

AI ~12 min High school
The big idea

A guessing machine

Every large language model (LLM) is trained to do one simple thing: given the words so far, predict the next word. It does not think or plan. It just outputs probabilities for every word in its vocabulary, then picks one.

If you have ever used a phone keyboard that suggests the next word, you have used a tiny version of this idea. The difference is scale: modern models have seen trillions of words and can keep thousands of words of context in mind at once.

Core idea: language models predict the probability of every possible next word, then sample one. Doing this over and over generates an entire sentence.


N-grams

From context to probabilities

One simple way to predict the next word is to count how often word pairs or triples appear in a large collection of text. A model that looks at the previous two words is called a trigram model.

ngram.py
# Given the previous two words, count what comes next.
counts = {
    ("the", "robot"): {"sat": 3, "opened": 2, "fixed": 1},
    ("robot", "sat"): {"down": 3, "quietly": 2},
}

def predict(prev1, prev2):
    c = counts.get((prev1, prev2), {})
    total = sum(c.values())
    return {w: n/total for w, n in c.items()}

Big models use neural networks instead of raw counts, but the idea is the same: the model has learned which words tend to follow which contexts. It converts that knowledge into a probability for each next word.


Interactive

Try the next-word predictor

The widget below is trained on a tiny story corpus. Type a few words (or click a seed) and see the model’s top guesses. The bars show the probability of each next word.

1.0
Story will appear here.

Sampling

Temperature

Once the model has a probability distribution, it still has to choose one word. The simplest choice is to pick the most likely word every time. But that produces boring, repetitive text.

Temperature controls how random the choice is. A temperature of 0 makes the model always pick the most likely word. A temperature of 1 samples from the raw probabilities. A high temperature flattens the distribution, making surprising choices more likely.

Key analogy: temperature is like shaking a bag of Scrabble tiles. Low temperature = pick the most common letter. High temperature = shake hard and pick any letter.


Check yourself
?

Why does the model need context?

Compare these two prompts in your head: “I walked to the bank to deposit my” and “I sat by the river bank to watch the.” The word that comes after “bank” depends on the earlier words. That is context.

Context resolves ambiguity. The same word can mean different things, and the surrounding words tell the model which meaning is likely right now.


Quick quiz

Three questions

1. What does a language model predict at each step?

2. What happens when temperature is very low (near 0)?

3. Why are neural language models better than simple count-based n-grams?

Keep learning

Predicting the next word is a local operation. The breakthrough of modern LLMs is the ability to use all the previous words at once, focusing on the ones that matter most. That mechanism is called attention.

Part of the AI Explained tutorial series.