Brytalearn.How things workFind something
INTERACTIVE EXPLANATION

Where does a language model’s next token come from?

Open a genuinely trained miniature Transformer. Change token tiles, inspect real attention and vector calculations, sample a continuation, and test the limits of a confident prediction.

Enable JavaScript to change the conditions and run the interactive experiment.

Make a discovery

A fixed model computes a distribution from its current context. Attention mixes vectors, later transformations produce token scores, and sampling chooses an output. Changing context, changing sampling and changing trained parameters are different interventions.

  • Distinguish text, token IDs, embeddings and position vectors.
  • Trace an actual attention score, normalized row and mixed value coordinate.
  • Follow residual and feed-forward transformations before output probabilities.
  • Test a causal mask by changing a later input and checking earlier logits.
  • Separate context edits, sampling changes and parameter training.
  • Reproduce a sampled token using a documented seed and cumulative interval.
  • Explain the difference between training-task accuracy and mean next-token loss.
  • Recognize that high probability on an unsupported prompt is not independent evidence of truth.

Make a prediction

The model predicts red, but its largest attention weight belongs to first. What should the visualization do?

  • Move the largest arrow to red
  • Keep the measured values and inspect the full computation
  • Declare the red input irrelevant
Read the explanation

Attention mixes vectors before further transformations. Preserving a surprising result is essential to an accurate explanation.

Understand it

Build the context from tiles

Start with a marker, two color labels, and first or last. Every tile has an exact ID in a seven-token vocabulary. The model predicts a next token from those IDs; it does not receive a hidden English interpretation.

Add a learned position

Each ID selects an eight-number embedding row. A learned vector for that input position is added. Replacing a token changes its embedding; moving it also changes the position contribution.

Calculate attention

Query and key vectors produce scaled dot-product scores. Causal masking forbids later positions. Softmax normalizes the allowed scores, and those weights mix value vectors rather than token IDs.

Keep following the calculation

The mixed value is projected, added through a residual path, transformed by a ReLU feed-forward network, added through another residual and projected into seven output logits. A second softmax normalizes candidate tokens.

Investigate a surprising arrow

For red–blue–first, the next-token winner is red although the final attention row mostly weights first. The display retains those actual values. Inspect the full computation instead of replacing the attention pattern with a convenient story.

Draw and append

A categorical sample chooses a token from the output distribution. Append it, then recompute from the extended context. Stop at the end marker or when another inference would exceed five input positions; no tokens are silently discarded.

Probe an unsupported format

Give the model first without any color. It still places high probability on red. The output exists, but that format has no established color-selection answer in the training task.

Look closer at the science

A deliberately reduced trained Transformer

This original decoder has one block, one head, width 8, feed-forward width 16, 695 parameter slots, learned positions, separate input/output matrices and no normalization or dropout. It is a symbolic color-selection model, not an English chatbot or a replica of a modern large model.

Exact vocabulary and tokenization

IDs 0–6 mean <bos>, <eos>, red, blue, green, first and last. The editor canonicalizes whitespace and rejects unknown labels. An ID is a row selector; its numeric magnitude is not its meaning or probability. The checkpoint and vocabulary must stay matched.

A separate subword arithmetic card

The optional red redder exercise begins with characters and retains the space. Four chosen merges produce red, space, red, der. This demonstrates that tokens need not equal words. Its hand-constructed vocabulary and ranks are not used by the neural checkpoint.

Queries, keys and values

For row vectors x, the model forms q=xWq, k=xWk and v=xWv. Each allowed attention score is q·k/√8. A stable softmax subtracts the maximum allowed score before exponentiating. Future positions have score −∞ and weight exactly zero.

Two different softmax operations

Attention softmax distributes mass over allowed context positions. Output softmax distributes mass over seven candidate next-token IDs. A 0.9 attention weight on first does not mean a 0.9 probability of emitting first.

The entire downstream path matters

Attention’s weighted value sum passes through Wo and adds to x. A position-wise ReLU network and a second residual produce z. Output logits are zU+b. The exploded stack shows actual vectors at one selected position; cell height is a declared compressed visual scale, not a physical dimension.

A worked value coordinate

For <bos> red blue first, the final attention weights are approximately 0.000858485, 0.003125844, 0.096708236 and 0.899307436. Multiplying them by value-coordinate-0 entries and summing gives approximately −1.957841407. Full-precision values drive the visualization.

A defined artificial intervention

Connection removal sets one allowed attention entry to zero, renormalizes the remaining row, and recomputes downstream layers with the original queries, keys, values and parameters. Removing the only remaining allowed connection is undefined. Replacing a token changes its vectors and normalization, so it tests a different intervention.

Attention is evidence, not a complete causal account

Published work debates which explanatory claims attention supports and how to test them. A heatmap alone, or one ablation, does not establish a complete mechanism. This workbench shows measured intermediates and specific changed calculations without inventing a natural-language inner monologue.

Causal prediction and shifted targets

All 18 six-token sequences supply five input–next-target pairs. At input position i, the current token is known, so attention to itself is allowed. Inputs at positions later than i cannot influence its logits. Showing a full training worksheet does not grant the model access to those future answers.

Teacher-forced training and autoregressive inference

Training uses the ground-truth preceding tokens while computing 90 next-token targets per full batch. Generation instead appends its selected output before the next forward pass. Ordinary inference neither computes a gradient update nor changes the checkpoint.

Temperature operates after logits

For positive temperature τ, output probabilities are proportional to exp((logit−maximum)/τ). Changing τ leaves current logits and attention fixed. Greedy choice is a separate operation, with exact ties resolved by lowest token ID. A different sampled token can change later context and therefore later attention.

Reproducible categorical draws

The educational 32-bit LCG advances state once for each random draw, then u=state/2³². Choose the first cumulative probability strictly greater than u. Seed 49 begins with 0.2550580417737365 and 0.7230514015536755. It is a classroom generator, not cryptography.

A sampling calculation you can do on paper

For the separate logits [ln 6, ln 3, 0], temperature 1 gives [0.6,0.3,0.1]. Temperature 0.5 gives [36/46,9/46,1/46]. This card is intentionally distinct from the trained model’s often much sharper distributions.

All training data are disclosed

The corpus enumerates three first colors, three second colors and two queries: 18 sequences. All are used in training, without a held-out split. The 18/18 answer-position result is a training-task check, not evidence of generalization or a reasoning benchmark.

Why loss has a nonzero floor

Earlier identical prefixes genuinely admit several next targets: three first colors, three second colors and two queries. The empirical conditional loss floor is (ln 3+ln 3+ln 2)/5≈0.578074352 nats per target. Near-perfect answer selection does not make mean loss over all five targets zero.

Recorded training, fixed inference

The downloadable original code initializes from NumPy PCG64 seed 49 and performs 2000 full-batch Adam updates in float64. The final loss is approximately 0.578083150. The ledger’s points are recorded values, not live training. The exact checkpoint and corpus have separately recorded SHA-256 hashes.

Where this analogy ends

Larger systems can have different architectures, richer pretraining, post-training, reasoning behavior, tools and context handling. A next-token interface does not settle what algorithms a learned computation implements. This tiny experiment neither demonstrates nor rules out those larger capabilities.

Where this is used

Reading an AI visualization critically

Ask whether arrows show measured attention, a proposed explanation or actual intervention results. Follow the values and later computations; do not assume the prettiest arrow identifies a cause.

Comparing prompts and decoding settings

A prompt edit changes context computation; temperature changes selection from fixed logits; training changes parameters. Keeping these separate helps design an interpretable comparison.

Checking a confident answer

A categorical model probability is not a verified probability that an independent claim is true. Use appropriate external evidence and task evaluation, especially for formats outside the model’s demonstrated data.

Try it yourself: Draw a token, then audit your procedure

Supplies

  • Paper and pencil
  • A ruler for a probability line
  • Optional large color-label cards
  1. Mark cumulative intervals

    For the separate arithmetic card at temperature 1, draw red [0,0.6), blue [0.6,0.9), green [0.9,1). At a boundary choose the interval starting there.

  2. Predict the supplied draws

    Use 0.12, 0.72, 0.94, 0.31, 0.58, 0.88, 0.03, 0.65, 0.47, 0.99. Write labels before checking. They give 5 red, 3 blue, 2 green; expected counts for ten draws are 6,3,1.

  3. Change one condition

    Use the same values at temperature 0.5, with boundaries 0,36/46,45/46,1. Counts become 7,2,1. Explain why a finite supplied sequence need not match the expected counts.

  4. Make a context sequence

    In a separate exercise choose two colors uniformly, then first or last equally. Append the indicated color and the end marker. There are 18 equally likely full rule-based sequences.

  5. Name the different models

    Your paper sequence samples the exact empirical corpus rule. The browser checkpoint computes a learned approximation. A hand-written answer rule must not be presented as neural inference.

  6. Write a reproducible explanation

    Record the probabilities, draw values, chosen intervals, context and stop condition. Explain which action changes sampling and which action changes the next context.

Can another person reproduce your token using the same row and draw?

No personal text, account, powered equipment or external inference is needed. The paper exercise is probability arithmetic and a conditional rule, not a model of brain training or a full language system.

Check your understanding

After the four specified merges, how many tokens encode red redder in the separate arithmetic card?

  • Two words, so two tokens
  • Four: red, space, red, der
  • Ten unchanged characters
Answer and explanation

Four: red, space, red, der The chosen merges combine characters while retaining the space. This result belongs to that tokenizer, not every model.

Change a later input while keeping positions 0 and 1 fixed. Their logits should…

  • Remain unchanged
  • Improve by reading the new future
  • Stop attending to their own known tokens
Answer and explanation

Remain unchanged Causal computation uses only the relevant prefix, including its current input.

The worked attention/value sum at coordinate 0 is approximately…

  • −1.957841407
  • 0.899307436
  • The average token ID
Answer and explanation

−1.957841407 Multiply each attention weight by the corresponding learned value coordinate and sum all allowed contributions.

A small attention weight on red proves…

  • The animation must be wrong
  • Red is causally irrelevant
  • Neither; inspect the full path and defined interventions
Answer and explanation

Neither; inspect the full path and defined interventions Connection removal and token replacement alter different computations. A heatmap alone does not establish a complete mechanism.

Temperature 0.5 on logits [ln 6, ln 3, 0] gives…

  • [36/46,9/46,1/46] with unchanged parameters
  • New training evidence that red is true
  • Doubled attention weights
Answer and explanation

[36/46,9/46,1/46] with unchanged parameters Dividing logits by 0.5 sharpens their output softmax. It does not modify the current attention or checkpoint.

u=0.7230514 on the [0.6,0.3,0.1] arithmetic card selects…

  • Red because it is most probable
  • Blue because u lies in [0.6,0.9)
  • Green because the seed is large
Answer and explanation

Blue because u lies in [0.6,0.9) Categorical sampling can select a lower-probability token. Real autoregressive generation then appends it and recomputes.

Why can 18/18 training answer selections coexist with loss near 0.57808?

  • Earlier prefixes have multiple possible next targets
  • Every prompt secretly retrains the weights
  • A perfect answer count forces zero total loss
Answer and explanation

Earlier prefixes have multiple possible next targets The answer-position count and mean loss over all five targets measure different things. Ambiguous prefixes create a nonzero conditional entropy floor.

A high red probability for <bos> first, without a supplied color, establishes…

  • A verified red fact
  • A confident continuation outside the trained grammar
  • That all larger models cannot reason
Answer and explanation

A confident continuation outside the trained grammar The output has no established color-selection answer on this unsupported format. This miniature cannot settle larger systems’ abilities.

Sources and model limits

  • Original seven-token symbolic task, one reduced Transformer block and at most five input positions. No general English, external memory, tool use or remote inference.
  • No encoder, cross-attention, multiple heads, normalization, rotary positions, dropout, quantization or tied input/output matrix.
  • All 18 sequences are training data. The training-task score cannot distinguish memorization from a reusable selection rule or establish broad generalization.
  • Attention rows mix value vectors. The selected connection-removal operation is artificial and does not claim a comprehensive causal interpretation.
  • The default trained distribution is often very sharp; a temperature change does not promise a visibly different token on every sample.
  • The recorded training curve is not a live training animation. Context edits and sampling leave the checkpoint fixed.
  • Token/position/vector shapes and the exploded stack are original mathematical visualizations, not physical wires or brain anatomy.
  • Source reading, complete numerical comparisons and gradient checks do not certify learner understanding or browser/device/export behavior.

Scaled attention, causal masking, residual paths and feed-forward transformations

Vaswani and colleagues, original Transformer paper. Our reduced decoder differs substantially; all product diagrams are original, not redistributed paper figures.

Vaswani et al. · Attention Is All You Need

Subword units and tokenizer-specific segmentation

Sennrich, Haddow and Birch, ACL 2016. Our optional ordered-merge arithmetic card is hand constructed and separate from the checkpoint vocabulary.

Sennrich et al. · subword units

Tokenization is a specified encoding procedure

Kudo and Richardson, EMNLP 2018. No SentencePiece model or external vocabulary is used in this seven-token lesson.

Kudo & Richardson · SentencePiece

Adaptive training updates in the original reproducer

Kingma and Ba, Adam. The corpus, seed, learning rate and finite run here are explicitly authored choices.

Kingma & Ba · Adam

Sampling and output-distribution transformations

Holtzman and colleagues: autoregressive decoding and sampling. No paper-specific performance claims are transferred to the miniature.

Holtzman et al. · neural text degeneration

Output likelihood and truth require separate evaluation

Lin, Hilton and Evans, ACL 2022. Historical TruthfulQA results do not rate current systems or calibrate this color model.

Lin et al. · TruthfulQA

Larger learned systems and reasoning need their own evidence

Wei and colleagues study intermediate-step prompting in larger models. This miniature does not reproduce their training or results.

Wei et al. · chain-of-thought prompting

Recorded seed and checkpoint are different reproducibility artifacts

NumPy qualifies cross-version random-stream guarantees. The exact checkpoint and complete corpus are supplied with hashes rather than relying on a seed alone.

NumPy · random compatibility policy

Independent subject review is pending.

Read the sources and model assumptions