Skip to the lesson
FixTheRobot Explore Robotics / LLM / LM1 Understanding Large Language Models / LM1-T1

LLM · Theory · LM1

LM1-T1 — What a Language Model Is

Strip away the hype and a language model does one thing: given some text, it says how likely each possible next piece of text is.

  • Lesson 1 of 5
  • Module 1: Understanding Large Language Models
  • Theory
  • Difficulty: Beginner
  • About 9 minutes to read
  • No prerequisite lessons

Everything in this track grows from a single idea. A language model looks at the text so far and assigns a probability to every possible next token. Chat assistants, code completion and translation are all built on top of that one operation, repeated. This lesson makes the idea exact, shows the simplest possible language model (a table of counts), and explains why that table cannot scale and why a neural network, the subject of the rest of the track, is used instead. The browser lab that goes with it lets you read and decode from such a distribution.

Learning objectives

After this lesson, you should be able to:

  • Define a language model as a probability distribution over the next token given a context.
  • Write the probability of a sentence with the chain rule and compute it from counts.
  • Explain why counting works for short contexts and fails for long ones.
  • Relate the counting model to a neural network that computes the same kind of distribution.

The idea: autocomplete with probabilities

Your phone suggests a next word as you type. Behind the suggestion is a ranking: some words are more likely than others after “see you”. A language model is that ranking made precise. For any piece of text (the context), it gives each possible next token a probability, and the probabilities add to one.

A next-word distributionA horizontal bar chart of the probability of each word that follows the word "the" in a ten-sentence corpus: robot 25 percent, red 25 percent, green 15 percent, arm 10 percent, blue 10 percent, drone 10 percent, cart 5 percent. The bars add up to 100 percent.Next word after “the”, counted in ten sentences red25%robot25%green15%arm10%blue10%drone10%cart5% A language model is this table, for every context, produced by a network instead of by counting.
The simplest language model: counts from a tiny corpus turned into the probability of each word that follows “the”.

Tokens, not just words

Models do not read letters or whole words; they read tokens, pieces of text such as “robot”, “ing” or “,”. Module 2 builds a tokenizer. For now a token is a word, and the vocabulary is the list of all tokens the model knows. A model with a vocabulary of 50,000 tokens gives 50,000 probabilities at every step.

Why probabilities and not one answer?

Language is not deterministic. After “the robot picks up the” many continuations are reasonable. A model that returned only its single best guess would throw away information that is useful: how confident it is, which alternatives are close, and what happens if you choose differently. Keeping the whole distribution lets you decode in different ways (Lesson LM1-T2), measure how well the model fits text (Module 5), and tell when it is guessing.

The simplest model: count

Take a corpus. For a context, count how often each token followed it, and divide by the total. In the corpus of the figure, “the” occurs 20 times: 5 times followed by “robot” and 5 by “red”, so each has probability 5/20 = 25%. That table, built for every context, is a complete (if tiny) language model, and it is exactly what you compute in Lab LM1-L1.

Where it breaks, and what replaces it

Counting needs the context to have been seen. With one word of context there are only as many contexts as the vocabulary; with the last 100 tokens there are more possible contexts than atoms in the universe, and almost none appears in any corpus. A neural network fixes this by computing the distribution from the context with learned parameters, so that similar contexts give similar distributions even if this exact context was never seen. The network is the same kind of object as the table (context in, distribution out) but it generalises. Building one is the goal of this track.

The mathematics: the chain rule and conditional probabilities

A text is a sequence of tokens w1, w2, …, wn. A language model assigns a probability to the whole sequence. The chain rule of probability writes it as a product of next-token probabilities:

chain rule for a token sequence
P(w1, …, wn)=P(w1)⋅P(w2 ∣w1)⋅P(w3 ∣w1, w2)⋅…⋅P(wn ∣w1, …, w(n−1))P(w_{1},\,\ldots ,\,w_{n})=P(w_{1})\cdot P(w_{2}\,\mid w_{1})\cdot P(w_{3}\,\mid w_{1},\,w_{2})\cdot \ldots \cdot P(w_{n}\,\mid w_{1},\,\ldots ,\,w_{(n-1)})
wt
the token at position t, an element of the vocabulary.
P(wt | w1, ..., wt-1)
the conditional probability of token t given all the tokens before it (the context). This is what a language model computes, one position at a time.
n
the length of the text in tokens.

Every factor is a distribution over the vocabulary: for each context, the probabilities of all possible next tokens are non-negative and sum to one. An autoregressive model (Lesson LM1-T2) is one that is built exactly this way.

Estimating by counting. With a one-token context (a bigram model) the maximum-likelihood estimate is a ratio of counts:

bigram probability from counts
P(b ∣a)=count⁡(a, b)count⁡(a, anything)P(b\,\mid a)=\frac{\operatorname{count}(a,\,b)}{\operatorname{count}(a,\,\text{anything})}
count(a, b)
the number of times token b immediately followed token a in the corpus.
count(a, anything)
the number of times a occurred with any token after it; the denominator makes the probabilities sum to one.

Longer contexts use the same ratio with longer prefixes, but the number of distinct contexts grows as Vk for vocabulary size V and context length k. For V = 50,000 and k = 3 that is already 1.25 × 1014 possible contexts, far more than any text provides examples of.

A neural model. A neural network with parameters θ computes the same conditional distribution, written Pθ(wt | w1, …, wt−1). Training (Module 5) adjusts θ so that the real next tokens in a large corpus get high probability.

A worked example: scoring a sentence

The corpus of Lab LM1-L1 has ten sentences, such as “the robot picks up the red box”. Counting gives these probabilities (each is a ratio of counts):

Probability of each step of the sentence “the robot picks up the red box”
StepContext → next tokenCountsProbability
1the → robot5 of 200.25
2robot → picks3 of 50.60
3picks → up4 of 41.00
4up → the4 of 41.00
5the → red5 of 200.25
6red → box3 of 50.60
7box → <end>4 of 41.00

By the chain rule the probability of the sentence (given that it starts after the previous sentence ended) is the product:

worked sentence probability
0.25×0.60×1.00×1.00×0.25×0.60×1.00=0.02250.25\times 0.60\times 1.00\times 1.00\times 0.25\times 0.60\times 1.00=0.0225

About 2.3%. Probabilities of whole sentences are always small, so models work with logarithms: log2(0.0225) = −5.47 bits, or about 0.78 bits per token over the 7 tokens. A sentence such as “the robot picks up the box” contains a step with probability zero, because “box” never directly follows “the” in this corpus (only a colour or a name does). The counting model calls it impossible, although it is perfectly good English: a symptom of counting’s inability to generalise. Lower scores mean the text is more surprising to the model; Lesson LM5-T1 turns this into the training loss.

The same computation in Python

from collections import Counter

tokens = "the robot picks up the red box <end> the robot picks up the green bin <end>".split()
pair = Counter(zip(tokens, tokens[1:]))      # count(a, b)
single = Counter(tokens[:-1])                # count(a, anything)

def p(next_tok, prev):
    return pair[(prev, next_tok)] / single[prev]

print(p("robot", "the"))   # 2 of 4 occurrences of "the" -> 0.5

zip(tokens, tokens[1:]) pairs every token with the one after it; Counter counts the pairs. The last token has no successor, so tokens[:-1] is used for the denominator. The probability is a pair count divided by the context count, exactly the formula above.

Engineering implications and limitations

Everything is a distribution over the vocabulary. The output of a real model is a vector with one number per token (the logits, converted to probabilities by a softmax, Lesson LM5-T5). Remember this shape; every later module works with it.

Counting models are still useful. They are fast baselines, they are easy to inspect, and they are good for sanity checks. If a neural model cannot beat a bigram model on your data, something is wrong with the pipeline.

A language model knows text, not truth. It is trained to match the statistics of its corpus. Probabilities say how likely a continuation is under that corpus, not whether a statement is correct; Module 9 returns to this.

The training data define the model. A model fitted to robotics manuals assigns different probabilities from one fitted to poetry. Data quality and coverage matter as much as architecture (Module 2 and Module 5).

Smoothing in counting models. A pure count model assigns probability zero to anything unseen, which makes a whole sentence impossible. Real count-based systems smooth the estimates; neural models avoid the problem by generalising.

Robot interfaces. A language model attached to a robot produces a distribution over text, not over actions. Converting that text into safe, validated robot commands is an engineering problem handled by structured outputs, validation and a safety layer; the Physical AI and VLA tracks cover robot-specific designs.

Common misconceptions

“A language model stores sentences and looks them up.”

It stores parameters that compute a distribution for any context. A counting model comes closest to lookup; a neural model generalises to contexts it never saw.

“The model outputs a word.”

It outputs a probability for every token. A separate decoding step (Lesson LM1-T2) chooses the token.

“High probability means true.”

High probability means likely under the training text. False statements that are common in the corpus get high probability.

“Longer context is just a bit better.”

The number of possible contexts explodes with length; handling long contexts at all is what neural models, and later attention, make possible.

Engineering takeaways

  1. A language model gives P(next token | context) for every token in the vocabulary; the probabilities sum to 1.
  2. The chain rule writes the probability of a text as a product of these next-token probabilities.
  3. Counting estimates P(b | a) = count(a, b) / count(a, anything), and fails for long contexts because the contexts are almost never repeated.
  4. A neural network computes the same distribution with learned parameters and generalises across similar contexts.
  5. Model probabilities describe the training text, not the truth of a statement.

References and further reading

The core learning path is Chapter 1 of the book; the textbook below is optional background on count-based models.

Knowledge check

Three conceptual questions. Write an answer, then reveal the explanation. Your answers stay in your browser.

Question 1

In a corpus, “robot” is followed by “picks” 6 times and by “moves” 4 times and by nothing else. What are P(picks | robot) and P(moves | robot)?

Explanation

6/10 = 0.6 and 4/10 = 0.4. The denominator is the total number of times “robot” was followed by any token, so the two probabilities sum to 1.

Question 2

Using the chain rule, a three-token text has P(w₁) = 0.5, P(w₂ | w₁) = 0.4 and P(w₃ | w₁, w₂) = 0.25. What is its probability, and why do we usually take logarithms?

Explanation

0.5 × 0.4 × 0.25 = 0.05. Products of many small probabilities become tiny (and underflow in floating-point arithmetic); the logarithm turns the product into a sum, log₂ 0.05 = −4.32 bits, which is easier and numerically stable.

Question 3

Why can a table of counts not model the last 100 tokens of context for a 50,000-token vocabulary, and what does a neural network do differently?

Explanation

There are 50,000¹⁰⁰ possible contexts, and almost none occurs in any corpus, so most contexts have no counts. A neural network computes the distribution from the context with shared parameters, so a context it has never seen still gets a sensible distribution because it resembles contexts it has seen.

What’s next

Next: how a distribution becomes text, one token at a time: autoregressive generation and decoding.

Practice this lesson

Explore Next-Token Probabilities and Decoding

Build a count-based next-token model and compare greedy decoding with sampling.

Next lesson

LM1-T2 — Autoregressive Generation

Generate text one token at a time by repeatedly predicting, choosing and appending a token.

Nothing below is required to finish this lesson.