Inside a prediction
How context changes a prediction
Try “river bank” and “savings bank”. The surrounding word changes which next words look likely. The model’s stored settings stay the same; the calculations change with your sentence. That is why changing a prompt is different from training a model. Change "river" to "savings" and follow the next-word probabilities. This shows how context can change a prediction without changing the model’s weights, a distinction that helps explain why editing a prompt differs from training a model.
Real calculations with hand-designed teaching weights. This is a miniature explanation, not a trained language model.
Change river to savings to compare the results. Both sentences use the same fixed parameters.
Rows: The, river, bank, is. Columns: f1 to f4. Depth: four calculation snapshots.
Each tile is a number, not a fact or physical memory location. Inspect the selected snapshot with its tiles or arrow keys. Choose another stage to inspect its numbers, or use Flat view to unlock every snapshot.
Step 1 of 5
Look up the token embedding
The word “bank” has ID 3. That ID selects four stored numbers from the table. This starting list is its embedding. Here each word gets one ID to keep the example small. The ID for “bank” selects row 3 in a fixed table. Its four numbers are an embedding. This word-level tokenizer keeps the example small.
At “bank”, f1 is 0; with savings, it is 0. Parameters stay fixed.
Inspect a number
Embedding, position 3 “bank”, f1: 0. * marks values that differ with the other context.
After the final word “is”
What could come next?
With “river”, the top next word is “muddy” (70.2%). With “savings”, it is “closed” (70.2%). Top token after “is”: “muddy” (70.2%) with river, versus “closed” (70.2%) with savings.
All eight words in this toy vocabulary. These are next-token probabilities, not confidence that a statement is true.
Change the context word to compare predictions, or select Next step to inspect the calculation.
Inspect the tables and calculations
The cube, tables and arithmetic show the same selected word and coordinate. Expand a section below to inspect its values or read how they are calculated.
Read the cube as a table
The table and cube read the same four arrays. Cube tiles round to three decimal places for readability; full values and exports preserve the browser numbers. More dimensions are possible; a three-dimensional picture is only one way to show them.
Where do the first numbers come from?
A token ID selects a row in an embedding table. That is an actual lookup. The ID itself is an address, not a score for meaning. Real models learn their embedding values during training; I authored this tiny table with AI coding agents to make the arithmetic legible.
| Token ID | Word | f1 | f2 | f3 | f4 |
|---|---|---|---|---|---|
| 0 | The | 0 | 0 | 0 | 1 |
| 1 | river | 2 | 0 | 1 | 0 |
| 2 | savings | 0 | 2 | 1 | 0 |
| 3 | bank | 0 | 0 | 1 | 1 |
| 4 | is | 0 | 0 | 1 | 0 |
| 5 | muddy | 1 | 0 | 0 | 0 |
| 6 | closed | 0 | 1 | 0 | 0 |
| 7 | quiet | 0 | 0 | 0 | 0 |
No coordinate is a universal “river” or “money” axis. Larger language models usually split text into subword tokens and learn distributed representations.
Are these numbers still made of bits?
Yes. Bits encode the numbers; tensors organize them into arrays. Select a tile above to see that number rounded to a 32-bit floating-point representation.
One sign bit, eight exponent bits and 23 fraction bits. This browser computes using 64-bit JavaScript numbers. The 32-bit display is a separate conversion, so rounding may change the value.
How does attention mix information?
Attention mixes information from the available words. It calculates how much each word contributes, with shares adding up to one, then combines their numbers using those shares. Select a word and a number in the cube to inspect the calculation. The technical names for these parts are query, key and value. A query is compared with each available key. The comparisons become shares that sum to one. Each share multiplies a value vector, and the results are added. Select a word and coordinate above to see its exact calculation.
Causal language models cannot use later words to predict an earlier one. Turn this off as a comparison experiment; it is no longer causal attention. The final position has no future word in either mode.
Q = XWq; K = XWk; V = XWv; A = softmax(QKᵀ / √4 + mask); C = AV
The next step applies a feed-forward transformation independently to each position: F = ReLU(CW1)W2. Finally softmax(FWout + bias) gives the eight output probabilities.
Each position is calculated from the same input snapshot. Within this one block, the updated “bank” row does not feed into “is”. A later block could use the updated rows. The final “is” row supplies the next-word prediction shown here.
Weights, working numbers and retrieved text
Parameters
These are the model’s stored settings. Real models usually learn them during training. They stay fixed while the model answers. This toy’s settings were designed by hand. Embedding and projection tables. In an ordinary model these are learned during training and held fixed while answering. This toy uses authored parameters.
Activations
These are the working numbers you see in the cube. A different sentence changes them. They are temporary calculations, not a permanent store of facts. The cube's intermediate calculations. They change with the input and context. They are not a permanent filing cabinet of facts.
External retrieval
A separate search can find documents and add text to a model's context. This exhibit does not retrieve documents. That process is different from selecting an embedding row or combining vectors.
Real language models repeat many blocks and commonly include position information, multiple attention heads, residual connections and normalization. Those are omitted here. The four slices are teaching snapshots, not four complete Transformer layers. An output also need not be the highest-probability token; sampling can choose another.
Training step vs. fixed-weight inference
Train the 32 output weights, then freeze them to run inference
In the cube above, the output table was written by hand. Here that 4 by 8 output table starts at all zeros (32 trainable weights). Each training step measures how far the predictions miss “muddy” (for river) and “closed” (for savings), then nudges those 32 weights to lower the error. Freezing the checkpoint shows how ordinary inference works: changing the sentence changes the working activations and prediction while all 32 weights stay unchanged. Start with a zeroed 4 by 8 output projection matrix WO (32 trainable parameters) while holding embeddings E and earlier matrices WQ, WK, WV, W1, W2 and BIAS fixed. Each gradient step lowers mean cross-entropy loss on (“river” -> “muddy”, “savings” -> “closed”). Freezing the checkpoint demonstrates fixed-weight inference: switching the input sentence changes activations and output probabilities while all 32 entries of WO remain exact (L1 weight delta = 0).
Scope note: This sandbox trains only the 32 numbers in the 4 by 8 output matrix WO. The main cube above keeps its hand-authored baseline WO so the two views stay distinct.
Checkpoint state: Untrained 32-weight output table (step 0, all zeros) Untrained 32-weight output matrix (step 0, all 0s); Mean cross-entropy loss: 1.302
“The river bank is”: top choice “muddy” (27.2%); target “muddy” gets 27.2% (error 1.302) “muddy” (27.2%); target “muddy” is 27.2% (loss 1.302)
“The savings bank is”: top choice “muddy” (27.2%); target “closed” gets 27.2% (error 1.302) “muddy” (27.2%); target “closed” is 27.2% (loss 1.302)
Step 0: average error (loss) across the two training sentences is 1.302. Each training step nudges only the 32 numbers in the output table WO (the columns for “muddy” and “closed” change most on f1 and f2), while earlier tables stay fixed. Step 0: mean cross-entropy loss across the two training pairs is 1.302. Gradient descent updates only the 32 numbers in WO (columns for “muddy” and “closed” move most on f1 and f2); E, WQ, WK, WV, W1, W2 and BIAS stay fixed.
| Feature | The | river | savings | bank | is | muddy | closed | quiet |
|---|---|---|---|---|---|---|---|---|
| f1 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 |
| f2 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 |
| f3 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 |
| f4 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 |
Five distinctions: roles of numbers, gradients, post-training, fixed-weight inference, and unseen generalization
- Roles of the numbers: Stored weights (the hand-written table above or the 32 trainable WO weights here) stay saved from one prompt to the next. Working numbers (activations X, Q, C, F) are recalculated for each sentence. Gradients are temporary nudges used only during a training step to update the weights. Stored parameters (the hand-authored baseline above or the 32 trainable WO weights here) persist across prompts; temporary activations (X, Q, C, F) are recomputed for each input sentence; gradients exist only during a training step to update parameters.
- Loss and gradient learning: The error score (loss) measures how much probability missed the target word. The gradient calculates which direction to nudge each of the 32 weights so that the error is smaller on the next step. Cross-entropy loss -log P(target) measures how much probability mass missed the target word. The analytical gradient dL/dWO[f,w] = 0.5 * sum_c h_c[f] * (P(w|c) - 1[w = target_c]) tells each of the 32 weights which direction reduces that loss on the next step.
- Post-training in full models: After first learning to predict next words on large text collections, full language models usually go through extra training steps to follow instructions and human preferences. This 32-weight sandbox shows one small supervised table update, not full model training. After pretraining on next-token prediction, production language models typically undergo supervised instruction tuning and preference optimization. This 32-weight toy demonstrates a single supervised matrix update, not full language-model pretraining or post-training.
- Ordinary fixed-weight inference: When you freeze the checkpoint and test inference, switching between “river” and “savings” changes the working numbers and the predicted next word while all 32 weights in WO stay completely unchanged (0.0000 weight change). When you press Freeze checkpoint and test inference, switching between river and savings changes the feed-forward activation vector and the output distribution while all 32 entries of WO remain unchanged (0.0000 weight delta).
- Unseen-case generalization and imperfect reliability: Lowering the error on “river -> muddy” and “savings -> closed” only shows that the table fits these two practice sentences. Checking those same two sentences again does not test new cases. Real models are tested on held-out examples they did not train on, and they can still make mistakes on unfamiliar inputs Driving loss down on river -> muddy and savings -> closed only proves in-sample fit on these two training sentences. Re-running or reordering those same two sentences is not an unseen evaluation. In real machine learning research, generalization must be measured on held-out examples not seen during training, and trained models remain fallible on unfamiliar inputs (for example, Vaswani and colleagues evaluating Transformers on held-out WMT translation test sets).
Try the idea on a new sentence.
Imagine “The wooden bat is” and “The flying bat is”. Before context is mixed, what happens to the embedding for the same token “bat”?
Choose a prediction. Assume the same token ID and fixed embedding table.
Save or rerun this experiment
Keep every input, parameter and result, or run the calculation yourself with standard Python. No package installation needed.
Related: See how including a missing source sentence changes a template answer. Retrieving a document supplies context; it is different from looking up a token's embedding.
Sources and how this was made
An original explanation by Jonathan Simone. I direct AI coding agents that write and test the code for this site. See the colophon. No pretrained model or live inference service is running here. All lesson calculations run in your browser.
- PyTorch: embedding lookup
- Vaswani and colleagues: attention, feed-forward layers and output softmax
- NVIDIA: binary floating-point representation
- Lewis and colleagues: retrieval-augmented generation
- Brendan Bycroft's LLM visualization and Transformer Explainer inspired the linked views. No code or artwork copied.