Module 0 · Lesson 5 of 10

Inside the model: how it picks the next token

How the next token is picked
Tokens, the prompt in pieces, NOWTokensthe prompt in pie…5 tokensNOWVectors, numbers per tokenVectorsnumbers per tokenAttention, weighs earlier tokensAttentionweighs earlier to…Layers, repeat many timesLayersrepeat many timesScores, one per known tokenScoresone per known tok…
  • Tokens — the prompt in pieces — healthy — NOW — 5 tokens
  • Vectors — numbers per token — healthy
  • Attention — weighs earlier tokens — healthy
  • Layers — repeat many times — healthy
  • Scores — one per known token — healthy
  • Tokens
  • Vectors
  • Weighed mix
  • Scores
Step 1 of 6

Step 1 of 6 · Tokens

Done: The prompt is split into 5 tokens

What happens
"The cat sat on the" becomes "The", "cat", "sat", "on", "the".
Why
A model works on tokens, and each token has its own number in the list of tokens the model knows.
What changes
Next, each token becomes a list of numbers.
Next
Each token becomes a vector

Event log

5 more to go. Press a line to go back to that step.

Tokens read
5
Numbers per token
–
not yet
Next token
not yet

Tokens: The·cat·sat·on·the

Step 1 of 6: Tokens. The prompt is split into 5 tokens. "The cat sat on the" becomes "The", "cat", "sat", "on", "the".

What this simplifies. This is a small made-up example. The weights and chances are round values chosen to add up as real ones do, not output from a real model. Real models use thousands of numbers per token and run many attention heads side by side in each layer.

A writes one at a time. For each one, the so far goes through the same parts in order. The model ends with a score for every token it could write next.

First, each token becomes a : a list of numbers the model learned in . Then attention compares the tokens. Each token makes a query, a key and a value. The query of the newest token is matched against the key of every token so far, and the best matches get the most weight.

A layer is one round of attention, plus a network that works on each token alone. A large model stacks dozens of layers. At the end, the vector of the last token becomes a score for every token the model knows. The scores become chances that add up to 100%.

One token is then picked. At 0 the top score wins. At a higher temperature the token is drawn by chance, so answers vary. The picked token joins the prompt, and the whole run starts again for the next token.

Example. For "The cat sat on the", attention gives "cat" the most weight, 40% in the picture. The scores put "mat" at 42% and "floor" at 18%. At temperature 0 the model writes "mat". At temperature 1 it might write "floor".

In system design: Each new token is one more run, so a longer answer takes longer. The saves earlier keys and values, which spares work but takes memory.

Check yourself

The prompt has already been read. Why does a 400-token answer still take longer to arrive than a 40-token one?