the wizard

How an LLM works: from your text to the next token

One prompt through a language model, step by step: tokens, the hidden state, layers, and how the next token is picked. Measured on a real model.

Part 1 of 6 · How an LLM runs
  1. How an LLM works: from your text to the next token
  2. How attention works in an LLM, with real numbers
  3. What the KV cache is, and why LLMs need it
  4. Prompt caching explained: why an LLM re-reads your whole chat
  5. What an inference engine does: batching, KV memory and quantization
  6. Why LLMs run on GPUs: reading a prompt vs writing a reply

About this series

This part follows one prompt through a language model, from the text you type to the token it writes next. Where I measured something, I ran it on SmolLM2-135M, a small open model from Hugging Face that is built the same way as Meta's Llama, only smaller. It runs on an ordinary CPU, so anyone can check the numbers below. How I ran it is at the end.


A model is a file of numbers

An LLM is two things: a file of numbers, called the weights, and a program that runs them, called an inference engine (llama.cpp, vLLM and SGLang are three). It works like Python. The interpreter runs script.py on your arguments. The engine runs the weights on your text. The weights are the program; your text is the input.

Training sets the weights once. Using the model doesn't change them: every request, from every user, goes through the same numbers.

The "8B" in Llama 3 8B is how many numbers there are: about 8 billion. Meta's Llama 3 paper (Table 3) gives the shape: a vocabulary of 128,000 tokens, 32 layers, a hidden state of 4,096 numbers, and a feed-forward size of 14,336. Multiplying those out:

Part Shape Weights
Input table (embeddings) 128,000 tokens × 4,096 0.52 billion
Attention, 32 layers 4 matrices per layer 1.34 billion
Feed-forward, 32 layers 3 matrices per layer, 4,096 ↔ 14,336 5.64 billion
Output table 4,096 × 128,000 tokens 0.52 billion
Total 8.03 billion

70% of the weights sit in the feed-forward layers, not in attention. More on what they do below.

The file really is just numbers. SmolLM2-135M's config gives 134.5 million weights. Stored as 32-bit numbers, 4 bytes each, that is 538 MB, and the model file I downloaded is 540 MB.

Step 1: your text becomes tokens

A tokenizer cuts your text into pieces from a fixed list, the vocabulary, and swaps each piece for its number in the list. SmolLM2's tokenizer turns "The capital of France is" into five tokens:

Piece The ·capital ·of ·France ·is
Token ID 504 3575 282 4649 314

The space belongs to the word after it (shown here as ·). SmolLM2's vocabulary has 49,152 tokens; Llama 3's has 128,000.

Step 2: each token gets a starting vector

The input table, the embedding table, has one row per token in the vocabulary. Each token's ID picks its row: a list of numbers, 4,096 of them in Llama 3 8B and 576 in SmolLM2. This list is the token's hidden state. Think of it as the token's working note: everything the model works out about this token, in this sentence, ends up in it.

The table is used once per token, at the start. After that the hidden state comes from the layer below.

Step 3: every layer updates every hidden state

Llama 3 8B has 32 layers, SmolLM2 has 30. Each layer has its own weights and runs two steps on every token:

  1. Attention. Each token looks back at itself and every earlier token and takes in information from them. To do it, the token's hidden state is multiplied by three weight matrices, making three new vectors: a query, a key and a value. This is the only step where tokens exchange information. Part 2 is about how it works.
  2. Feed-forward. Each token's hidden state goes alone through a bigger network: in Llama 3 8B, out from 4,096 numbers to 14,336 and back. This is where the 70% of the weights are. Research that traced how models recall facts found much of it in these layers (Geva et al., 2021; Meng et al., 2022).

Each step's result is added to the hidden state rather than replacing it, so the state builds up layer by layer. Token 5's hidden state at layer 7 is token 5's state as it left layer 6, plus what layer 7 adds.

Step 4: the last token scores every possible next token

After the top layer, only the last token's hidden state is used to predict. Through attention at every layer it has taken in information from all the tokens before it.

It is multiplied by the output table, which gives one score for every token in the vocabulary: 49,152 scores in SmolLM2. A fixed formula, softmax, turns the scores into probabilities that add up to 100%.

Here are SmolLM2's top five after "The capital of France is":

Next token Probability
the 26.2%
Paris 9.4%
located 7.3%
called 4.4%
a 3.9%

Paris comes second. The model isn't answering a question; it predicts how the text goes on, and "the" is a fine start, as in "the city of Paris". SmolLM2-135M is a base model, trained only to continue text. Chat models start from a base model and are trained further to answer.

Step 5: temperature decides how the pick is made

The engine now picks one token, and temperature controls how. Before softmax, every score is divided by the temperature:

A weighted random pick does what its probabilities say. I made 1,000 picks at temperature 1: "the" came up 272 times (probability 26.2%), "Paris" 101 times (9.4%), "located" 71 times (7.3%).

Try it with SmolLM2's real numbers. Move the temperature and make a few picks:

Measured on SmolLM2-135M: its eight most likely next tokens, and everything else, at each temperature.
After “The capital of France is”
Next tokenTemp. 0Temp. 0.5Temp. 1Temp. 1.5
the100.0%77.2%26.2%5.0%
Paris0%9.9%9.4%2.5%
located0%6.0%7.3%2.1%
called0%2.2%4.4%1.5%
a0%1.7%3.9%1.4%
situated0%1.5%3.6%1.3%
in0%0.3%1.6%0.8%
(a space)0%0.2%1.2%0.6%
Any of the other 49,1440%1.1%42.4%84.8%
After “I opened the fridge and found a”
Next tokenTemp. 0Temp. 0.5Temp. 1Temp. 1.5
box100.0%22.7%5.4%1.4%
bottle0%14.1%4.3%1.2%
bag0%9.4%3.5%1.1%
small0%8.6%3.3%1.0%
bunch0%4.0%2.3%0.8%
couple0%3.7%2.2%0.8%
few0%3.6%2.2%0.8%
jar0%3.6%2.1%0.8%
Any of the other 49,1440%30.4%74.8%92.1%

Two things stand out. At temperature 1 the fridge prompt puts 75% of its probability on tokens outside its top eight. And at 1.5, even "The capital of France is" gives 85% to tokens outside its top eight.

Step 6: the loop

The picked token is added to the end of the text and goes through the whole model, layer by layer, like any other token. Its final hidden state predicts the token after it. This repeats until the model writes a special end token or hits a length limit. One pass, one token: that's why replies appear word by word.

Twelve tokens from SmolLM2, each continuing "The capital of France is":

Temperature What it wrote
0 "…the capital of the country. The capital of France"
0.8, run 1 "…in northern France (The French Republic). The current capital of"
0.8, run 2 "…the city of Paris. French is a language with more than"
0.8, run 3 "…the city of Paris, which is the seat of the French"

At temperature 0 it took the top token every time and went round in a circle. Temperature 0 gives the likeliest next token at each step, not the best text.

Above temperature 0, the random pick alone means the same question can get different answers. Even at temperature 0, hosted models aren't fully repeatable: a Thinking Machines study (Horace He, 2025) traced it to results that change with how many other requests the server is running at the same time.

Where the weights come from

Every weight was learned the same way. Training starts with random numbers. The model reads real text and gives a probability for each possible next token. The loss measures how little probability it gave the token that actually came next. Backpropagation works out, for every weight, which way to move it to make that token more likely, and every weight moves a tiny step. Meta trained Llama 3 on about 15 trillion tokens of text this way (the same paper). Over that much text, the random numbers become an embedding table, attention and feed-forward layers, and an output table that work. Nobody decides what any single weight means.

Next, part 2: attention, the step where tokens look back at each other.


How I measured

← All memories