1 · What happens when a model answers
In one sentence: a language model only ever guesses the next small piece of text, a token, and an answer is that guess repeated in a loop, each new token added to the input for the next.
The problem
You ask a chat assistant a question and the reply doesn’t arrive all at once: it appears a few letters at a time, as if someone were typing. Sometimes it starts well and then states something false with total confidence. Ask again and the wording changes. None of that makes sense if you picture the model as a program that looks up an answer and prints it.
It makes sense once you see what the model actually computes. It never writes an answer. It produces one small piece of text at a time, and the reply is that step repeated.
The idea: guess the next piece, then repeat
Think of the autocomplete on a phone keyboard: it looks at what you typed so far and suggests the next word. A language model does the same, with three differences: its pieces are smaller than words, it gives a score to every possible piece, and instead of waiting for you it takes one of its own suggestions, appends it, and asks itself again.
"The capital of France is" ──> model ──> " Paris" (0.395) ──┐
"The capital of France is Paris" ──> model ──> "." (0.531) <─┘ ──┐
"The capital of France is Paris." ──> model ──> " It" (0.200) <──┘ ... and so on
Each arrow through the model is one forward pass: one full run of the model’s calculation. One pass, one new piece. A 12-piece reply is 12 passes.
The words you need
Token. The piece of text the model works with. A tokenizer cuts text into tokens using a fixed list built before training: common words are one token, rare words are split into parts. Each token in the list has a number, and those numbers are all the model receives:
llama/llama-tokenize -m models/qwen2.5-0.5b-instruct-fp16.gguf -p "The capital of France is" --log-disable
785 -> 'The'
6722 -> ' capital'
315 -> ' of'
9625 -> ' France'
374 -> ' is'
Five tokens, and the space before a word belongs to the word’s token. unbelievably is 3 tokens
(un, belie, vably); Hello is 1.
Vocabulary. The fixed list of tokens. This course’s model, Qwen2.5-0.5B-Instruct, has 151,936 of them.
Weights (or parameters). The numbers the model learned in training and never changes while answering. This model has 630.17 million of them, stored at 16 bits (2 bytes) each, which is why its file is 1.17 GiB (the name says 0.5B; llama.cpp counts 630.17 million).
The model as a function. Put the whole picture together and a model is one fixed function: tokens in, one score for each of the 151,936 vocabulary entries out. The scores become probabilities (lesson 2 shows how), and here are the five highest after our prompt:
1 chose ' Paris' ' Paris' 0.395 | ' ______' 0.096 | ':\n' 0.058 | ':\n\n' 0.047 | ' __' 0.047
Paris wins with 0.395. Second place is ______, a blank to fill in, as on a quiz sheet. The model doesn’t know an answer; it knows what text tends to come
next.
The loop, run for real
Here are 12 passes, always picking the most likely token. Each line is one pass: the token chosen and the top 5 candidates.
1 chose ' Paris' ' Paris' 0.395 | ' ______' 0.096 | ':\n' 0.058 | ':\n\n' 0.047 | ' __' 0.047
2 chose '.' '.' 0.531 | ',' 0.251 | '.\n' 0.070 | ' and' 0.057 | '.\n\n' 0.052
3 chose ' It' ' It' 0.200 | ' The' 0.131 | ' Paris' 0.091 | ' Which' 0.048 | ' In' 0.041
...
6 chose ' largest' ' largest' 0.370 | ' most' 0.132 | ' capital' 0.075 | ' oldest' 0.065 | ' seat' 0.062
7 chose ' city' ' city' 0.732 | ' and' 0.172 | ',' 0.029 | ' metropolitan' 0.024 | ' French' 0.007
8 chose ' in' ' in' 0.792 | ' of' 0.087 | ' and' 0.061 | ' by' 0.032 | ',' 0.009
9 chose ' Europe' ' Europe' 0.403 | ' France' 0.270 | ' the' 0.257 | ' both' 0.017 | ' western' 0.009
...
12 chose ' second' ' second' 0.224 | ' third' 0.215 | ' fifth' 0.077 | ' largest' 0.075 | ' most' 0.058
The reply reads Paris. It is the largest city in Europe and the second. Fluent, and false: Paris is not Europe’s largest city. Every step picked a likely continuation; nothing checked the claim. That is also why replies stream in: each token exists only after its pass, so the program shows it as soon as it has it.
How a chat fits in: the template
A chat model was trained on conversations written in a fixed layout, so your question is wrapped in that layout, the chat template, before tokenizing. For this model, “What is the capital of France?” becomes:
<|im_start|>system
You are Qwen, created by Alibaba Cloud. You are a helpful assistant.<|im_end|>
<|im_start|>user
What is the capital of France?<|im_end|>
<|im_start|>assistant
The bare question is 7 tokens; wrapped, it’s 36. <|im_start|> (151644) and <|im_end|> (151645) are single
special tokens. The model then continues after assistant, and when it picks <|im_end|> itself, the program
stops: that is how an answer ends.
Try it on your Mac
Practical 0 · Your llama.cpp, token by token, after lesson 2: you install llama.cpp, count tokens and watch these probabilities yourself.
Go deeper: how the tokenizer picks its pieces
The vocabulary is built before training by byte-pair encoding (BPE): start from single bytes, then repeatedly merge the pair of pieces that occurs most often in a large sample of text, until the list has the size wanted. Frequent words end up as one token; rare ones are left in parts. Because it starts from bytes, any text can be tokenized, even a character the list never saw as a whole. You can see both effects here:
30124 -> 'Sch'
21140 -> 'aden'
24716 -> 'fre'
793 -> 'ude'
220 -> ' '
16 -> '1'
...
23 -> '8'
25125 -> ' �'
243 -> '�'
Schadenfreude 12345678 ☕ is 15 tokens: the German word in four parts, a lone space, then each of the 8 digits as its own token
(this model’s tokenizer splits numbers into digits), and ☕, 3 bytes in UTF-8, as two tokens that each hold part of
those bytes (printed � because neither is a whole character).
Token counts are what context limits and API prices are measured in, so a number or a rare word costs more of them than a common word of the same length.
The Hugging Face course explains BPE step by step: Byte-Pair Encoding tokenization.
Go deeper: what is inside one forward pass
llama.cpp prints the model’s shape when it loads it (-v):
0.00.077.940 I print_info: n_embd = 896
0.00.077.940 I print_info: n_layer = 24
0.00.077.959 I print_info: model params = 630.17 M
0.00.077.960 I print_info: n_vocab = 151936
Each token’s number first picks a row of 896 numbers, its embedding. Those vectors then pass through 24 layers; in each one, every token’s vector is updated from the vectors of the tokens before it (attention) and then on its own (a small feed-forward network).
After the last layer, the last token’s vector is multiplied against the whole vocabulary to give 151,936 scores, the logits. Every pass reads all the weights, whatever the length of the reply so far: lesson 3 measures what that costs, and why the work for earlier tokens isn’t redone each time.
The model was trained with up to 8192 tokens of text at once (n_ctx_train): its context. The architecture is
described in the Qwen2 technical report.
Common misunderstandings
- “The model writes the whole answer, then the app shows it slowly.” The answer doesn’t exist yet: each token is computed after the one before it is chosen.
- “A token is a word.” Often it’s part of a word, a space plus a word, a single digit, or part of a character’s bytes.
- “It answered Paris, so it knows geography.” It gave
Paris0.395 because that text is likely there; the same loop started a falsehood five tokens later (largest, step 6).
Check yourself
1. A reply of 200 tokens: how many times did the model run its full calculation?
About 200 forward passes, one per new token. The first one also reads your whole prompt and gives token 1 (lesson 3 shows the prompt is done in one go).
2. An API charges per token. Why does an invoice number like 20261129 cost more than the word "Hello"?
This tokenizer splits numbers into single digits: 8 digits are 8 tokens, while Hello is 1. Rare words are split
too (unbelievably is 3).
3. You send "Hi" to a chat model and the request reports 30-odd prompt tokens. Where did they come from?
The chat template: the system message and the <|im_start|>/<|im_end|> markers wrap your text. Here the bare
question was 7 tokens and the wrapped one 36.