Type "The cat will sit on the ___" and a model will most likely answer mat. This page walks that one sentence through the whole machine, tokens, embeddings, attention, probabilities, in numbers small enough to read, and ends with what next-token prediction means for anyone who tests AI.
Our running sentence is "The cat will sit on the ___." and the model has exactly one job: predict the next token after that final "the". A transformer does this by converting words to numbers, processing the relationships between those numbers, and producing probabilities for possible next words. It does not understand the sentence the way you do; it computes.
The Next-Token Machine
what actually happens inside a transformer
Type "The cat will sit on the" and the model completes it. Between your text and that one word sit tokens, embeddings, attention, and a probability table over the whole vocabulary. This page walks the full path with one sentence, in numbers small enough to read.
text becomes integer IDs
IDs look up feature vectors
tokens read each other
a decoding rule picks the word
How one token gets predicted
[The][cat][will][sit][on][the]Six tokens, six integer IDs
cat -> [0.018, -0.246, ...]Numbers that carry learned features
embedding + positionCat-chases-dog is not dog-chases-cat
softmax(QKᵀ/√d) x Vsit and on pull the most weight
mat 8.2 -> 36%Every vocabulary token gets a probability
greedy or sample + tempOne token out, then loop again
All numbers on this page are simplified teaching examples; real models run thousands of dimensions through many layers.
what the stack is holding
tokens: The cat will sit on the6ids: 791 8415 690 2503 389 2796dims per token (real models)1000sattention heads readingmanylayers stackeddozensvocabulary scoredalldecoding
temperature
tokenize › embed › attend › score › pick
on (34%) and sit (31%)mat 36%, floor 20%, chair 10%mat, then the whole stack runs againprediction traced to
"on" (a surface follows)34%"sit" (the action)31%"cat" (who sits)19%The pipeline, one word each
tokenstext in pieces
idspieces as integers
vectorsintegers as features
attentioncontext read in
logitsevery token scored
softmaxscores to percents
What this page settles
8415 is a label, not a meaningQuickstart (local)
# 1. get a small model running ollama pull llama3.2:3b # 2. at the prompt, watch it complete ollama run llama3.2:3b "The cat will sit on the" # 3. then learn to score outputs pip install deepeval
Every LLM answer is a draw from a probability table, so identical prompts can differ and fluent can still be false. Once you see the machine, flaky AI features and hallucinations stop being mysteries and start being testable behavior.
The whole pipeline compresses into three moves:
Here is the full journey from raw text to a chosen token. The IDs and results in this table are simplified teaching examples, not values from any real system; the rest of this guide walks through each step in slow motion.
| Step | Operation | Simplified result |
|---|---|---|
| 1 | Tokenization | The, cat, will, sit, on, the |
| 2 | Token IDs | [791, 8415, 690, 2503, 389, 279] |
| 3 | Embeddings | Each token becomes a vector of numbers |
| 4 | Positional information | The model learns each token's location |
| 5 | Query, Key, Value | Three vectors per token, from learned matrices |
| 6 | Self-attention | Tokens examine other relevant tokens |
| 7 | Multi-head attention | Several attention calculations run in parallel |
| 8 | Feed-forward network | Processes the combined meaning |
| 9 | Residual connections + layer normalization | Keep information and numbers stable |
| 10 | Repeat transformer layers | Representation becomes more contextual |
| 11 | Output projection | Logits: one raw score per vocabulary token |
| 12 | Softmax | Scores become probabilities (they sum to 100%) |
| 13 | Decoding | Select a token (example: mat) |
| 14 | Continue generation | Append the token, run everything again, one token at a time |
Next: the first two steps in slow motion, watching "The cat will sit on the" become six tokens and then six integers, in the Tokens tab.
Before any math can happen, the sentence has to stop being text. Step 1 splits "The cat will sit on the ___." into tokens; step 2 swaps each token for an integer ID. From this point on, the model works only with numbers.
The tokenizer splits our sentence into six tokens and leaves position 7 empty, because that is the slot the model must fill. The position numbers simply show reading order (like every number on this page, this is the simplified teaching view).
| Position | Text | Token |
|---|---|---|
| 1 | The | [The] |
| 2 | cat | [cat] |
| 3 | will | [will] |
| 4 | sit | [sit] |
| 5 | on | [on] |
| 6 | the | [the] |
| 7 | ___ | Missing: model must predict this |
As a plain sequence, the model's working input is:
[The] [cat] [will] [sit] [on] [the]
Every token in the model's vocabulary has its own integer ID, so this step is a mechanical lookup. The IDs below are simplified teaching examples, not values taken from any particular tokenizer or model.
| Token | Token ID |
|---|---|
| The | 791 |
| cat | 8415 |
| will | 690 |
| sit | 2503 |
| on | 389 |
| the | 279 |
So the sentence the model actually receives is:
[791, 8415, 690, 2503, 389, 279]
Next: how each of those six IDs becomes a vector of numbers that can actually carry meaning, in the Embeddings tab.
Step 2 turned the sentence into IDs: [791, 8415, 690, 2503, 389, 279]. But an ID is just a label, the way a defect ID points at a bug without describing it. The number 8415 does not contain the meaning of "cat". The embedding is where the features actually live.
Here is the running sentence, "The cat will sit on the ___.", in a simplified 4-dimensional embedding space. Every value in this table is a simplified teaching example: real models learn thousands of dimensions.
| Token | Animal-related | Action-related | Location-related | Grammar-related |
|---|---|---|---|---|
| The | 0.05 | 0.02 | 0.04 | 0.92 |
| cat | 0.94 | 0.15 | 0.10 | 0.32 |
| will | 0.03 | 0.40 | 0.01 | 0.88 |
| sit | 0.15 | 0.96 | 0.35 | 0.25 |
| on | 0.02 | 0.08 | 0.95 | 0.61 |
| the | 0.05 | 0.02 | 0.04 | 0.92 |
A real embedding does not come with four tidy columns. It looks like this:
cat -> [0.018, -0.246, 0.774, ..., -0.091] (thousands of numbers)
Embeddings say what a token is, not where it sits. And order changes everything:
"The cat chased the dog." -> the cat does the chasing
"The dog chased the cat." -> same words, opposite meaning
Position by position, here is what location adds in our sentence. The readings are simplified teaching interpretations, not literal labels inside the model.
| Position | Token | What the position tells the model |
|---|---|---|
| 1 | The | Sentence beginning |
| 2 | cat | Subject position |
| 3 | will | Comes after the subject |
| 4 | sit | The main action |
| 5 | on | Introduces a location or surface |
| 6 | the | A noun probably comes next |
Every token now knows what it is and where it stands: open the Attention tab to watch those vectors start asking each other for help.
Attention is how the final position asks the rest of the sentence for help. On its own, the last "the" only knows that a noun phrase has started: to set up the prediction it has to pull in who is acting (cat), what the action is (sit), and what kind of word fits the blank.
| Vector | Plain-English meaning |
|---|---|
| Query (Q) | "What information am I looking for?" |
| Key (K) | "What type of information do I contain?" |
| Value (V) | "What information should I pass forward?" |
Q = Embedding x WQ
K = Embedding x WK
V = Embedding x WV
Conceptually, the Query of the final the behaves like: "I need information that helps identify the noun that should come next."
And here is what each token's Key advertises. Real Keys are vectors of numbers: these paraphrases are simplified teaching examples.
| Token | What its Key advertises (simplified) |
|---|---|
| The | Sentence beginning and grammar |
| cat | The subject, an animal |
| will | A future construction |
| sit | A sitting action |
| on | A surface or location relationship |
| the | A noun phrase has started |
One formula runs the whole conversation:
Attention(Q, K, V) = softmax(QK^T / sqrt(d)) x V
For the final position in "The cat will sit on the ___.", a simplified attention pattern could look like this. The percentages are teaching examples, not real model weights.
| Token | Attention weight | Why it matters |
|---|---|---|
| The | 3% | General sentence structure |
| cat | 19% | Who will be doing the sitting |
| will | 5% | Future tense |
| sit | 31% | Defines the action |
| on | 34% | Signals a surface or location follows |
| the | 8% | A noun comes next |
| Total | 100% | Softmax guarantees the weights sum to 100% |
Blend the Values with those weights and the final position now holds an idea close to: "An animal is going to sit on a particular surface or object."
The sentence has now been read from several angles at once: the Layers tab shows how repeated transformer blocks refine that blended reading into a prediction-ready vector.
Attention decided where each token should look. The machinery that follows does the refining: a feed-forward network sharpens what each position now knows, residual connections and normalization keep everything intact and stable, and then the whole block repeats, layer after layer.
After attention has gathered context from across the sentence, each token's representation passes through a small network:
linear transformation -> activation -> linear transformation -> improved contextual representation
| Condition encoded at the final position | Strength |
|---|---|
| The next token is probably a noun | Very high |
| It describes a surface or a place | High |
| It is compatible with sitting | High |
| It is plausible for a cat | High |
| The sentence is in future tense | Medium |
These strengths are simplified teaching examples: inside the model this is a pattern of numbers, not a labeled checklist.
Two safety rails wrap around attention and the feed-forward network so useful information survives and the numbers stay well behaved:
new representation = old representation + processed information
Put together, one transformer block has this shape:
That block does not run once. It repeats, and a model may stack dozens or even hundreds of these layers, with the representation becoming more contextual at every pass.
| Depth in the stack | What it tends to learn |
|---|---|
| Early layers | Tokens and basic grammar |
| Middle layers | Subject, action, and the relationships between them |
| Later layers | Sentence-level meaning and likely continuations |
| Final layer | Preparing next-token scores |
"predict a noun describing something a cat can sit on"The stack has done its thinking; the Decoding tab shows how those final numbers become one score per vocabulary token, then a probability table, then the word on your screen.
The stack of layers ends with one score for every token in the model's vocabulary. Decoding is the final stretch: raw scores become probabilities, a selection rule picks one token from the table, and that pick is the word you actually see.
The refined vector at the final position is multiplied by an output matrix, and out comes one raw score, called a logit, for every token the model knows:
final vector x output matrix -> one raw score per vocabulary token (a logit)
| Candidate token | Logit (raw score) |
|---|---|
| mat | 8.2 |
| floor | 7.6 |
| chair | 6.9 |
| sofa | 6.5 |
| bed | 6.1 |
| table | 4.8 |
| moon | 0.7 |
| banana | -0.5 |
| running | -1.2 |
These logits are simplified teaching numbers; a real model scores every token in its vocabulary in one go.
Softmax converts the whole set of logits into a clean probability table:
Probability(token i) = e^(logit i) / sum of e^(all logits)
| Token | Probability |
|---|---|
| mat | 36% |
| floor | 20% |
| chair | 10% |
| sofa | 7% |
| bed | 5% |
| table | 1% |
| Thousands of other tokens (combined) | 21% |
| Total | 100% |
Again, simplified teaching numbers: the exact values do not matter, the shape of the table does.
One probability table, several ways to pick from it. The whole finish in miniature:
| Temperature | Effect on the pick |
|---|---|
| 0 or very low | Usually the top token: highly predictable |
| 0.7 | Balanced mix of predictability and variety |
| 1.0 | More variation |
| Above 1 | More randomness, less predictability |
Treat the effect descriptions as simplified rules of thumb for teaching.
Greedy (always the top token): The cat will sit on the mat
Sampling (one possible run): The cat will sit on the sofa
That is the full journey from prompt to token; the QA angle tab turns it into what this mechanism means for the people who test it.
You now know the machine. Text became tokens, tokens became vectors, attention mixed them, and a probability table picked mat. This last tab is about what that machine means for anyone who has to test it: why generation is a loop, why LLM features feel flaky, and the whole pipeline recapped on one screen.
Picking mat did not finish the job, it produced exactly one token. The selected token is appended, the input becomes "The cat will sit on the mat", and the entire stack you just walked through, tokenization all the way to softmax, runs again from the top on that longer text.
| Candidate next token | Probability (simplified example) |
|---|---|
| "." | 65% |
| and | 9% |
| near | 5% |
| because | 2% |
| all other tokens combined | 19% |
As with every number on this page, these values are simplified teaching examples, not real model output. In pseudocode, the whole generator is a short loop:
while not stopped:
tokens = tokenize(text)
probs = full_forward_pass(tokens) # steps 1 to 12
next_token = pick(probs, temperature) # step 13
text = text + next_token # append, repeat
Everything above was mechanics. Read the same story as a tester and each mechanical fact turns into a testing consequence.
The full journey from raw text to the next token, one row per stage. Every numeric value here is a simplified teaching example; real models use thousands of dimensions, billions of parameters, and many transformer layers.
| Stage | Input | Operation | Output |
|---|---|---|---|
| Tokenization | "The cat will sit on the" | split the text into tokens | The, cat, will, sit, on, the |
| Token IDs | tokens | look up each token's vocabulary ID | [791, 8415, 690, 2503, 389, 279] (example IDs) |
| Embeddings | token IDs | fetch a learned vector per token | lists of numbers carrying learned features |
| Positional information | embeddings | combine each vector with its position | vectors that know what and where |
| Q, K, V | position-aware vectors | multiply by learned matrices WQ, WK, WV | three vectors per token |
| Self-attention | Q, K, V | softmax(QK^T / sqrt(d)) x V | context-mixed vector (last token leans on "on" 34%, "sit" 31%, "cat" 19%) |
| Multi-head attention | the same vectors, several heads | parallel attention runs, results combined | one richer representation |
| Feed-forward network | attention output | linear transformation, activation, linear transformation | improved contextual representation |
| Residual + layer norm | block input and output | add the original back, normalize the numbers | stable output with nothing useful lost |
| Repeat layers | one block's output | run the block again, layer after layer | increasingly contextual representation |
| Logits | final vector at the last position | multiply by the output matrix | one raw score per vocabulary token (mat 8.2, floor 7.6) |
| Softmax | logits | e^(logit) divided by the sum over all tokens | probabilities summing to 100% (mat 36%, floor 20%) |
| Decoding | probability table | greedy pick or temperature-shaped sampling | one selected token: mat |
| Next cycle | "The cat will sit on the mat" | the whole stack runs again | the next table: "." 65%, and 9%, near 5% |
If you keep one line from this page, keep the chain: text -> tokens -> token IDs -> vectors -> attention -> transformer layers -> vocabulary scores -> probabilities -> selected next token. The transformer does not retrieve a fixed answer from a database. Training taught it statistical and structural patterns, roughly "cat" plus "sit on the" points toward mat, floor, chair, sofa, bed, and it calculates which continuation is most probable for the current context.
A short ladder for a QA reader who wants to turn this theory into day-job skills, in the order that builds best.
You have now watched a transformer predict a single token end to end, and you know why testing one is a different sport from testing regular code. The next practical step is to get the machine running on your own laptop: start with the AI Tester Blueprint setup guide.