All courses

What an LLM computes, end to end

Text is split into tokens: subword pieces from a fixed vocabulary of tens of thousands. Each token is looked up as a vector of numbers (its embedding), and the vectors flow through a stack of layers. At the end, each position's vector becomes a score (logit) for every vocabulary token, and the scores are turned into probabilities for the next token. The context window is the maximum number of tokens the model can look at. One pass predicts a next token at every position.

▶ Watch this lesson Start the whole course

What the animation says

What goes in?

You type a prompt, and a reply comes back. What is computed in between? Let's follow one single pass through the model, from text in to numbers out. First question: what does the model actually receive? Letters one at a time, whole words, or something else? Pick one.

Ids pick rows

Subword pieces. The word unbelievable becomes three tokens: un, believ, and able. Each piece comes from a fixed vocabulary of tens of thousands, and has an id number. In a toy model with ten tokens, ids seven, two and five each pick one row of a table: the embedding table. Each row is a vector of d numbers. Stack the rows for T tokens and you get a T by d grid. Your turn: tap the row that token id five selects.

Layers keep the shape

The grid now passes through a stack of layers. The shape never changes: T by d in, T by d out. Inside, each position blends in information from itself and earlier positions, never later ones. So the last position has seen the whole prompt. The first has seen only itself. At the end, the model scores possible next tokens. Which positions get a next-token guess?

Scores for every token

Every position. Each final vector is multiplied by an unembedding matrix, with d rows and V columns. T by d, times d by V, gives T by V. The d's meet in the middle and drop out. Row t holds one score, called a logit, for every token in the vocabulary. In our toy, three tokens and a vocabulary of ten give three rows of ten scores. A real model does this over about fifty thousand tokens, at every position at once.

Scores to probabilities

Scores aren't probabilities yet. Take one row, with four tokens: cat, dog, the, sat. The scores are two, one, zero, zero. Raise e to each: about seven point four, two point seven, one, and one. Those add up to about twelve point one. Divide each one by that sum. Cat gets sixty-one percent, dog about twenty-two, the and sat about eight each. Now raise cat's score from two to three. What happens to sat's probability?

The whole pass

Sat drops to about four percent. Cat's bigger share squeezes everyone else, but nobody hits zero. That's the softmax, and it runs on every row of scores. So, end to end: text, token ids, embedding rows, layers that keep the shape, scores, and probabilities. One next-token distribution at every position, from a single pass. And the model only ever sees the tokens that fit in its context window.

What happens between prompt and reply?

You send a prompt and text comes back. In between, what is actually computed? What goes in: characters, words, or something else? What shape does the data have inside? And what comes out: a single chosen word, or something richer? This pipeline is the frame that every later detail about these models fits into.

Tokens in, scores for every token out

Text is cut into **tokens**: subword pieces like ` un`, `believ`, `able`, drawn from a fixed vocabulary of tens of thousands. Each token id selects one row of a table, a vector of $d$ numbers called its **embedding**. The vectors pass through a stack of layers. Inside, each position blends in information from itself and earlier positions. Finally, each position's vector becomes one score per vocabulary token, and those scores become next-token probabilities.

The pipeline with shapes

Token ids $x_1,\dots,x_T$, with $T \le$ context window. 1. Lookup: $X$ = rows $x_1,\dots,x_T$ of $W_E\in\mathbb{R}^{V\times d}$, so $X\in\mathbb{R}^{T\times d}$. (Some models also add a vector encoding each position here.) 2. Layers: $X\mapsto H\in\mathbb{R}^{T\times d}$ (same shape). $H$ holds the final vectors. 3. Logits: $Z=HW_U\in\mathbb{R}^{T\times V}$; row $t$ is $z_t$. 4. $p_\theta(x_{t+1}=i\mid x_{\le t})=\dfrac{e^{z_{t,i}}}{\sum_j e^{z_{t,j}}}$ $\theta$ means all the learned numbers in the model.

Scores to probabilities, by hand

Toy vocabulary, $V=4$: {cat, dog, the, sat}. At position $t$: $z_t=[2,1,0,0]$. Exponentiate: $e^2=7.39$, $e^1=2.72$, $e^0=1$, $1$. Sum: $12.11$. Divide: $p=[0.610,\,0.225,\,0.083,\,0.083]$, which sums to 1 (up to rounding). A higher score gives a higher probability, but every token keeps some. A real model does this over $V\approx 50{,}000$ tokens, at all $T$ positions in one pass.

A toy end-to-end pass in numpy

The middle line stands in for the layer stack and keeps shape $[T,d]$. Real layers also let each position use earlier positions; this stand-in does not. The output has one probability distribution per position.
import numpy as np
rng = np.random.default_rng(0)
V, d, T = 10, 4, 3
W_E = rng.normal(size=(V, d))          # one row per token
W_U = rng.normal(size=(d, V))
ids = np.array([7, 2, 5])              # token ids x_1..x_T
X = W_E[ids]                           # [T, d] lookup
H = np.tanh(X @ rng.normal(size=(d, d)))  # stand-in for the layers
Z = H @ W_U                            # [T, V] logits
P = np.exp(Z) / np.exp(Z).sum(axis=1, keepdims=True)
print(Z.shape, P.sum(axis=1))          # (3, 10) [1. 1. 1.]

Remember this

Text → token ids → embedding rows $[T,d]$ → stack of layers $[T,d]$ → logits $[T,V]$ → a next-token probability distribution at **every** position. The model sees only the tokens that fit in its context window.

On Bytewise this lesson comes in small steps with a check at the end, and the app brings it back right before you would forget it.