How an LLM works

From raw text to a written answer: the three mechanisms behind a language model. No prior knowledge needed.

Course outline
  1. 0What a language model is
  2. 1The minimum vocabulary
  3. 2How a machine reads text
  4. 3How a machine stores meaning
  5. 4Attention: a word looks at its neighbours
  6. 5How the model answers

Chapter 0What a language model is

A language model is a program that has learned, from enormous amounts of text, to answer a single question: what is the most likely piece of text after these ones? Everything else follows from that. It queries no database and searches nowhere: it computes a probability.

How do you get from "predict what comes next" to a machine that writes a structured answer?

Through four mechanisms, which are the next four chapters. The text is first cut into numbered pieces, that is the tokenizer. Each number then becomes a position in a space where meaning can be computed, that is the embedding. Those positions correct one another according to the sentence, that is attention. Finally the model produces its answer one piece at a time, that is generation.

The full journey

Your text
Tokensnumbers
Vectorsmeaning
Attentionmeaning in context
Answerone token at a time
Chapters 2 to 5 unpack each of these arrows.
A word on size

A model's parameters, also called its weights, are the numbers adjusted during training. They run into the billions. They carry everything the model retained, and they do not change afterwards unless the model is retrained.

Chapter 1Minimum vocabulary

Seven words are enough to read everything else. Come back to this if a term blocks you.

tokenA piece of a word. The smallest unit the model handles. "Hello" might be 1 token, "Schtroumpfology" 4.
weightsThe numbers learned during training, also called parameters. An "8B" model holds 8 billion of them. They stop moving once training is over.
attentionThe mechanism that lets each token look at the others and correct its vector accordingly. It is the heart of the Transformer architecture, the one behind every current LLM.
vectorA list of numbers, for example [0.2, −0.7, 0.1, …]. This is what the machine actually computes.
embeddingThe vector that represents a text. Its special property: two texts with similar meaning have nearby vectors.
context windowThe maximum amount of text a model can read at once, counted in tokens.
LLMLarge Language Model, the model that composes the final answer (GPT, Claude, Mistral…).

Chapter 2How a machine reads text

A computer only handles numbers. Before anything else, you have to turn "cat" into something computable. This is the job of the tokenizer, the entry point to every language model.

How do you split text so a machine can process it?

Three splits are possible, and you understand the right one by ruling out the other two:

  • Letter by letter? The vocabulary is tiny (a hundred or so symbols), but sequences become huge, and computing cost explodes with length.
  • Word by word? Sequences are short, but the vocabulary is infinite: proper names, typos, new words would all be unknown.
  • By pieces of words, subwords. This is the compromise chosen: frequent words fit in one piece, rare words are rebuilt from known pieces.

The path of text, toward meaning

cattext
ch · atsubwords
331 · 266ids
0.9 −0.5 …vector
↑ the tokenizer stops here · ↑ meaning starts there
The first three boxes are mechanical. The fourth is where meaning lives.

Each piece gets a number (its ID), looked up in a fixed dictionary. And this is where you need to be very clear about one thing:

The point that blocks everyone

These numbers mean nothing. They are arbitrary labels, like barcodes: IDs 331 and 332 have no relation to each other. The tokenizer understands nothing. It counts, it splits, it numbers.

The number serves only as an address: it tells which row to read in the big table where vectors are stored, the embedding table. The dictionary (text → number) is made once then fixed; the table (number → vector) is learned during training and is part of the model.

Where does this dictionary come from?

From a statistical algorithm called BPE (Byte Pair Encoding), which is not intelligent. You start with characters, then repeat the same operation thousands of times: merge the most frequent pair of symbols in the corpus. "e" followed by "s" appears everywhere, so you create "es". Then "es" + "t" becomes "est". After roughly 100,000 merges, you lock the result: that's your vocabulary.

BPE: merge what appears often

l · o · w · e · s · twe start with characters
l · o · w · es · te + s becomes es
l · o · w · estes + t becomes est
Repeated thousands of times on a huge corpus, then locked permanently.

Two important consequences follow from this mechanism.

First consequence: nothing can break. Under BPE merges, you always keep the 256 basic bytes. An unknown emoji, an exotic character, a corrupted file? The tokenizer falls back to the raw byte, one token per byte. It fragments, but never stops. This is called byte-level BPE.

Second consequence: the vocabulary is optimized for English, because it was mostly extracted from English. Other languages are therefore split more finely, so more tokens to say the same thing:

Tokens needed for 100 words

English≈110
French≈160
Arabic / Korean≈400
Since APIs meter by token, the same sentence costs about 1.5× more in French.
What to remember

1. Everything is counted in tokens, never in characters or words, since model limits are expressed that way.
2. A tokenizer is married to its model: its numbers point to the right vectors only for that model.

→ The full details (merges by rank, KV cache, O(N²) cost, tiktoken) are in Sheet 01, under "Sheets".

Chapter 3How a machine stores meaning

At this point, you only have arbitrary numbers. Meaning arrives at the next step, with the idea that makes everything else work.

How do you turn "the meaning of a word" into something you can compute?

The answer: you make it a position in space. Every word, every passage, becomes a point. And you arrange things so that points with nearby meaning are physically close.

The space of meaning (simplified 2D view)

animals fruits vehicles cat dog rabbit apple banana car train a word = a point · distance = difference in meaning
In reality, this space has 768 dimensions or more. You can't draw it, but you can compute distances in it.

That is what an embedding is: a position. And that is what lets the model treat two different phrasings alike. If "the sensor saturates" and "the sensor overflows" land at the same place, it understands them the same way, without them sharing a single word.

No one writes these numbers by hand

They are learned, on a simple principle: a word is defined by its surroundings. The model reads billions of sentences, and two words you see in the same patterns ("feed the ___", "the ___ sleeps") are pushed toward nearby positions. No one declared that cats and dogs are alike: it is inferred from usage.

Strict consequence

Each model learns its own space. One model's coordinates mean nothing to another. You never mix vectors from two different models. Two models do not speak the same geometric language, and nothing flags the mistake.

The trap of the two-meaning word

"Bank" means the edge of a river and also a place that holds money. If each word has only one fixed position, where do you put it? Answer: in the middle, so in neither zone, which helps no one.

One point for two meanings: the compromise

rivers & nature money & finance river shore loan cash bank in the middle, so in neither of the two zones
This is the limit of the "static" vector: one word, one position, no matter the context.

The solution is called attention: before locking a word's position, the model lets it look at its neighbours in the sentence and shifts its position accordingly. "We fished from the bank" sends it towards rivers, "the bank approved my loan" towards finance.

It is the central mechanism of Transformers, and it deserves more than one sentence. It is the whole of the next chapter.

→ The arithmetic of directions (king − man + woman ≈ queen) and the detail of static vectors are in Card 02, "Cards" tab.

Chapter 4Attention: a word looks at its neighbours

The previous chapter ended on a problem: "bank" has only one position, stuck between rivers and money. Attention is the mechanism that moves it. It is the central piece of Transformers, and the only place in the whole model where tokens talk to each other.

How can a token change its position depending on the words around it?

To keep this concrete, this chapter and the next follow a real model whose numbers are public: Llama 3 8B. Every figure quoted is its own.

What goes in: a table, not a sentence

Once the tokenizer and the embedding table have done their job, your text no longer exists as text. It is a table: one row per token, and on each row 4,096 numbers.

"the black cat sleeps" entering the model

4,096 columns the 0.21 −0.77 0.05 0.62 … black −0.44 0.13 0.91 0.08 … cat 0.67 0.30 −0.12 0.55 … sleeps 0.02 −0.61 0.48 −0.33 … 1 row = 1 token 4 tokens = 16,384 numbers
A 2,000 token prompt is the same table with 2,000 rows, so 8 million numbers.

Position information is then added. Without it, "the cat eats" and "eats the cat" would be strictly identical to the model: nothing in a table of numbers says in which order to read the rows.

How position is encoded

Two methods exist. Older models (GPT-2, BERT) add a second vector to the input vector, one that encodes "I am at position 1, 2, 3…".

Recent models, Llama among them, use RoPE: instead of adding anything, they rotate the Q and K vectors by an angle proportional to the position. Useful consequence: the score between two tokens then depends only on the gap between their positions, not on their absolute positions. That is what makes it possible to extend a model's context window after training.

The idea in one sentence

Each token asks the others a question, each one answers with a label, and the asker mostly picks up the content of those whose label matches. Three roles, so three vectors, all drawn from the same starting vector.

Three vectors drawn from the same input vector

the token's vector4,096 numbers
Qwhat I want
Kwhat I show
Vwhat I give
Q for query, K for key, V for value. Three multiplications, by three learned matrices called WQ, WK and WV.
Multiplying a vector by a matrix, concretely

A matrix is a grid of numbers. The multiplication produces a new vector in which each output number is a weighted sum of all the input numbers. Nothing more.

With a 3 number vector and a 3 × 2 matrix:

InputWeights toward output AWeights toward output B
20.5−1.0
50.20.3
11.00.4
Output2×0.5 + 5×0.2 + 1×1.0 = 3.02×(−1.0) + 5×0.3 + 1×0.4 = −0.1

Inside the model it is identical, with 4,096 inputs and 4,096 outputs. That takes 4,096 × 4,096 = 16.8 million weights, which is exactly the size of WQ in the next chapter's table.

The four steps of the computation

  • Three projections. The token's vector is multiplied by WQ, WK and WV. Out come Q, K and V. Every token in the sequence does this, at the same time.
  • Scores. The current token's Q is compared to the K of every preceding token, by dot product. The 500th token of a text therefore produces 500 scores.
  • A normalisation. Those scores go through a softmax. The result is a set of positive attention weights that add up to exactly 1: a way of spreading attention over the past.
  • A blend. The output is the sum of the V vectors of all preceding tokens, each multiplied by its attention weight. The one that got 0.60 supplies 60% of the result.
The dot product in twenty seconds

Multiply term by term, add them up, and out comes a single number.

Q = [2, 5, 1] and K = [1, 0, 3] give 2×1 + 5×0 + 1×3 = 5.

That number measures how well the two vectors line up:

same direction high score perpendicular score ≈ 0 opposite negative score

The scores are then divided by the square root of the vector size, here √128 ≈ 11.3. Without that division, adding 128 products gives huge values, the softmax locks onto a single candidate, and training never gets going.

Softmax, line by line

A softmax turns any list of numbers into probabilities. Two operations: apply the exponential to each score, then divide each one by the total.

The exponential makes everything positive, crushes small gaps downward and stretches large ones upward. The division guarantees a total of 1.

Raw scoreExponentialDivided by the total
3.020.120.1 / 48.4 = 41%
2.411.011.0 / 48.4 = 23%
1.96.76.7 / 48.4 = 14%
1.23.33.3 / 48.4 = 7%
all the others7.37.3 / 48.4 = 15%
Total48.4100%

The same computation is used twice in the model: here to spread attention over past tokens, and at the very end to spread probability over the 128,256 tokens of the vocabulary.

Who looks at whom, and how much

the cat black sleeps on the 1.0 cat .30 .70 black .10 .55 .35 sleeps .05 .60 .15 .20 on .05 .25 .10 .45 .15 row: who looks · column: who is seen grey = forbidden, never look ahead
Each row totals 1. Here "sleeps" turns towards "cat", its subject. Illustrative values.

The grey zone is not a detail. A token never sees what follows it, which forces the model to write left to right without cheating, and makes the cache described in the next chapter possible.

The counter-intuitive part

Steps 2 and 4 are the only moment in the whole model where tokens communicate. And they use no weights at all: they are nothing but products between vectors. The weights sit in the three projections of step 1, and in the final blend.

32 heads in parallel, not one

A single set of Q, K, V could only track one relationship at a time. So it gets cut up: the Q, K and V vectors are sliced into 32 slices of 128 numbers, and the four steps run separately on each slice.

The Q vector, sliced into 32 heads

4,096 numbers h1 h32 32 slices of 128 numbers
Each slice is a head. They do not talk to one another.

Each head specialises during training: one tracks the subject of the verb, another gender agreement, another the quotation mark still to be closed. The 32 results are stitched back together, which rebuilds 4,096 numbers, then blended one last time by WO. That fourth matrix is what decides how to combine whatever the 32 heads brought back.

Why K and V are 4 times smaller than Q

In the next chapter's table, WQ is 4,096 × 4,096 while WK and WV are 4,096 × 1,024. That is not a typo.

Llama 3 uses grouped-query attention: the 32 query heads share only 8 sets of K and V, in groups of 4 heads. Quality barely moves, and the cache memory is divided by 4.

And the bank finds its side

Take "we fished from the bank". The token "bank" emits a Q which, thanks to training, lines up well with the K of "fished". That neighbour therefore collects a high attention weight, and its V weighs heavily in the blend. Result: the vector for "bank" shifts towards the river zone. In "the bank approved my loan", it is the K of "loan" that wins, and the vector moves towards finance.

The same word, two output positions

"we fished from the bank"
attention"fished" weighs .62
river zone
"the bank approved my loan"
attention"loan" weighs .58
finance zone
Same static vector going in, two contextual vectors coming out. Illustrative weights.
The right words for it

The vector that enters attention is the static embedding, read from the table. The one that comes out is the contextual embedding. And since the model stacks 32 layers, attention runs 32 times: from the second one onward, it is already working on contextual vectors.

→ The static versus contextual distinction, and the two moments where context acts, are covered again in Card 02, "Cards" tab.

Chapter 5How the model answers

Attention is understood. What remains is where it sits inside the complete machine, and how a table of numbers becomes a written word.

The key

The model produces one token at a time. For each token produced, every single one of its weights is used, and all the text already there is traversed again.

Seven matrices, repeated 32 times

The weights are not a formless heap. They are matrices, held in 32 layers stacked on top of one another. Each layer contains exactly seven matrices: the four attention ones you have just met, and three others. The count is checkable:

Full anatomy of Llama 3 8B

BlockMatrixSizeWeights
AttentionWQ4,096 × 4,09616.8 M
WK4,096 × 1,0244.2 M
WV4,096 × 1,0244.2 M
WO4,096 × 4,09616.8 M
MLPgate4,096 × 14,33658.7 M
up4,096 × 14,33658.7 M
down14,336 × 4,09658.7 M
One layer218 M
× 32 layers6.98 B
Embedding table + output layer1.05 B
Total8.03 B
That is where the "8B" in the name comes from. The sizes are taken from the model's public config file.
Two things to take from that table

① The 32 layers share the same structure, but no two hold the same values. The model repeats the same operation 32 times with 32 different sets of weights.
4 weights out of 5 sit in the MLP, not in attention. Attention is the famous mechanism, but it is not where the bulk is stored.

A layer does two things

Always the same two, in this order.

The contents of one layer

1. Attentiontokens look at each other
2. MLPeach token works alone
That is the entire content of a layer. Repeat it 32 times and you have a language model.

The MLP: each token, alone

The second move is called the MLP, short for multi-layer perceptron. Once attention is done, every row of the table goes its own way: its 4,096 number vector is stretched to 14,336, run through a non-linear function, then brought back to 4,096. No exchange between tokens here: the same operation is applied to each row, separately.

This is where 4 weights out of 5 live. The fairest picture: attention gathers the useful information, the MLP processes it. Reverse engineering work on models locates most of the learned factual associations here.

What the non-linear function is for

Without it, stacking 32 layers would achieve strictly nothing. A chain of matrix multiplications always collapses into one equivalent matrix: 32 linear layers would have exactly the same power as a single one.

The non-linear function breaks that collapse. It is what makes depth useful. In Llama it is a variant called SwiGLU, and it is the reason the MLP has three matrices instead of two: gate and up expand in parallel, the first acting as a valve on the second.

The third operation, quiet but present

Before each of the two moves, the vector passes through a normalisation that brings its numbers back to a stable scale (RMSNorm in Llama). Without it, values blow up or collapse across the 32 layers and training fails.

It changes nothing in the reasoning above, but if you open a Transformer diagram you will see those little "Norm" blocks everywhere, and now you know what they do.

The vector is never replaced

Neither of the two moves returns a fresh vector: its result is added to the vector that came in. A token's vector therefore crosses the 32 layers being enriched 64 times, without ever losing what it carried at the start.

That shortcut has a name, the residual connection. Without it a 32 layer stack would be close to untrainable: the signal degrades on the way down and the early layers stop learning anything.

Crossing the stack

input vector4,096 numbers
layer 1+ attention, + MLP
layer 2+ attention, + MLP
layer 32+ attention, + MLP
final vector4,096 numbers, same shape
Input and output have exactly the same shape. That is what allows layers to be stacked identically.

From the last vector to 128,256 scores

Coming out of the 32nd layer, only one row of the table is kept: the last token's. The others served to feed it, and are of no further use at that instant.

That 4,096 number vector is multiplied by one final matrix of 4,096 × 128,256. Out come 128,256 scores, one per vocabulary entry. Their technical name is logits, and you will meet it everywhere in the documentation. Each score is the dot product between the final vector and that token's output vector: the better aligned, the more likely.

The output is not a word, it is a distribution

Those raw scores are not probabilities: they can be negative, and there is no reason for them to sum to anything in particular. The softmax turns them into positive probabilities that add up to exactly 1. It is the same operation as in the previous chapter, applied this time to the whole vocabulary.

"The cat is sleeping on the ___"

rug41%
sofa23%
bed14%
floor7%
everything else15%
These are the numbers from the worked example in chapter 4, under "Softmax, line by line".

Pick one, then start over

Always taking the most likely token gives correct but flat text, and sometimes loops of repetition. So a controlled amount of randomness is added, tuned by temperature.

Its mechanism fits in one line: the raw scores are divided by the temperature before the softmax. Dividing by 0.5 doubles the gaps, so it concentrates probability on the favourites. Dividing by 1.5 shrinks them, so it flattens the distribution and lets rare choices through.

Same raw scores, three temperatures

T = 0.5 · predictable

rug69%
sofa21%
bed8%
floor2%

T = 1.0 · the model's raw output

rug49%
sofa27%
bed16%
floor8%

T = 1.5 · unpredictable

rug41%
sofa27%
bed20%
floor12%
Raw scores 3.0 / 2.4 / 1.9 / 1.2. Only the top four candidates are shown. At T = 0 there is no randomness left at all: the favourite always wins.

A second setting, top-p, works differently: it sorts candidates by decreasing probability and keeps only the first ones until it reaches p, say 0.9. Everything else is discarded before the draw, which stops an absurd candidate from slipping out by bad luck.

The loop, token after token

The text so far
The model128,256 probabilities
One chosen token
↑ the chosen token is appended to the text, and the loop starts again ↑
This is called autoregressive generation. It stops on a special end token, or at the requested length limit.
The bill, for a single token produced

Every weight in the model is used at least once, which is roughly 16 billion operations (one multiplication and one addition per weight). At 50 tokens per second, the machine is sustaining 800 billion operations per second for your answer alone. That is why this runs on GPUs.

Not every model activates all of its weights

What precedes describes a dense model: every weight serves every token. Llama 3 8B is one.

Other models use a mixture of experts: the MLP is split into blocks, and a router activates only a few of them per token. DeepSeek-V3 advertises 671 billion parameters, but puts only 37 billion to work for each token produced. A large model in memory, a small model in compute.

The KV cache

"It rereads everything on each pass" is true mathematically, but false inside the machine. The K and V of tokens already processed never change: they depend only on the token and its position, never on what comes after. So they are kept in memory.

The first pass therefore takes the whole prompt through the 32 layers at once. After that, on every turn, a single token crosses the stack: it computes its Q, and reads everyone else's K and V straight from the cache.

With cacheA 1,000 token answer costs 1,000 passes of one token through the stack.
Without cacheThe same answer would cost 500,000 token passes, for a strictly identical result.
What the cache costs

Memory, and it grows with every token. On Llama 3 8B: 32 layers × 8 heads × 128 numbers × 2 (K and V) = 65,536 numbers per token, so 128 KB. A full 8,192 token window is 1 GB of GPU memory, on top of the model's own 16 GB (8 billion weights at 2 bytes each). It is often the cache, not the model, that caps how many users can be served in parallel.

The context window

Everything the model has in front of it at a given instant, your question, whatever you paste along with it, and its own answer in progress, has to fit inside its context window, measured in tokens. Past that, something has to be cut.

Two costs grow with length, but not at the same rate. The projections and the MLP cost in proportion to the number of tokens. Attention compares each token to all the preceding ones, so its cost grows with the square of the length. On a short prompt the first one dominates; on a very long prompt the second ends up drowning everything else.

Why it makes things up

Nothing in this mechanism verifies anything. The model optimises the plausibility of the next token given what it read during training, not truth. Faced with a question whose answer is not in its weights, the most plausible continuation is still a well formed sentence, so it produces one. That is a hallucination: a consequence of how it works, not an occasional breakdown.

There are two ways to reduce it: through training, by teaching the model to recognise when it does not know, and above all by handing it the material directly in its prompt.

→ That second method has a name and a course of its own: RAG, giving your own documents to the model.

The tokenizer & BPE

Text → sequence of integers. Nothing else. Meaning comes later.

green = readable text amber = numbers / vectors purple = model / computation red = trap
Sheet outline
  1. §1The role: entry gate
  2. §2Dictionary vs embedding table
  3. §3BPE: how the dictionary is born
  4. §4Byte-level: never crashes
  5. §5Fragmentation & cost
  6. §6The linguistic tax
  7. §71 tokenizer, multiple LLMs?

The complete journey, from text to meaning

chattext
ch · atsub-words
331 · 266ids
0.9 −0.5 …vector
↑ tokenizer (first 3 boxes) · ↑ model (last)
The tokenizer stops at integers. Meaning appears only in the vector.
Don't miss

The tokenizer never touches meaning. An ID is an arbitrary label, like a barcode: 331 and 332 have no connection. Meaning is learned much later, in the embedding table.

The essentials in 9 points

  1. Tokenizer = text → list of integers. No meaning in it.
  2. An ID = a row number pointing to a vector. Meaning lives in the vector.
  3. Two layers: base bytes = coverage · BPE above = efficiency.
  4. BPE merges the most frequent pair in the corpus, in a loop, then freezes the dictionary.
  5. Frequent word = 1 token · rare word = a few sub-words · unknown = raw bytes.
  6. The dictionary = algorithm + corpus. Change either one, get a different tokenizer.
  7. A tokenizer is tied to its model. Open source almost always, unlike weights.
  8. More tokens = more expensive (attention in O(N²), KV cache).

§1The role: entry gate

The key

The model never reads text. It reads vectors. The ID just fetches the right vector from memory by simple index access.

The output is an array of integers. Thanks to BPE (§3), a frequent word fits in a single ID.

Frequent = 1 token · rare = many

chat
[9015]1 token
Schtroumpfologie
[2647, 8901, 412, 1205]4 tokens
Illustrative IDs, real ones depend on vocabulary.
Trap

The space before a word is stuck to the token. So chat and  chat can give different IDs depending on position in the sentence.

§2Dictionary vs embedding table

The key

Two tables. The dictionary (text → ID) is frozen, delivered in a few MB. The embedding table (ID → vector) is learned and is part of the model.

The ID is just a row number

VOCABULARY (frozen)
tokenID
Good12
day8973
ch331
at266
🦊→ bytes
↓ lookup: ID 331 = row 331 ↓
EMBEDDING TABLE (learned)
rowlearned vector
12−0.1  0.4  0.8
2660.2  −0.3  0.1
3310.9  0.3  −0.5
89730.0  0.6  −0.2
The vector carries the meaning. The ID just points.

That's what ties a tokenizer to its model: the same IDs point to the right vectors only for the model trained with that specific vocabulary.

§3BPE: how the dictionary is born

The key

Two stacked layers. Below: the 256 bytes → total coverage, nothing can crash. Above: BPE → efficiency, a known word = 1 token.

The stack

Layer 2 · BPEfrequent sequences → 1 ID = EFFICIENCY
Layer 1 · base bytesthe 256 bytes → all languages = COVERAGE
Coverage below, efficiency above.

The dictionary is built by repeatedly merging the most frequent pair:

Building by merging

l o w e s t most frequent pair: e + s → es l o w es t then: es + t → est l o w est
Repeated thousands of times → ~100,000 entries, then dictionary is locked.
Trap

The merged pair is most frequent across the entire corpus, not in this isolated word. "es" appears everywhere, "ow" almost never.

Compilation vs inference · and the difference with WordPiece

Compilation (outside AI): a statistical program repeats merges until a fixed size (e.g. 100,000), then locks.

Inference (production): incoming text is re-split by replaying these merges in their learning order, which amounts to reconstructing the largest known blocks.

It's the merge order that decides, not the "longest prefix". That's more WordPiece (BERT). BPE applies its rules by rank.

§4Byte-level: never crashes

The key

The unknown word (Out Of Vocabulary) no longer exists. At worst, you fall back to raw bytes: 1 token per byte.

Fallback to bytes

🦊
0xF0
0x9F
0xA6
0x8A
100% resilience: the tokenizer fragments, but never stops.

§5Fragmentation & cost

The key

Extreme splitting = fragmentation. Two effects on the machine: the O(N²) cost of attention, and KV cache saturation (VRAM).

Quadratic cost Attention costs O(N²) over the total sequence length.
KV cache · VRAM Each token takes up video memory. Fragmented = cache saturated sooner = useful context window reduced.
Important caveat

A word going from 1 to 10 tokens in a 2,000-token context adds only 9 tokens, not a 100× factor. The 100× only applies if that word was the entire input. But cumulative across a whole text, fragmentation adds up fast.

§6The linguistic tax

The key

The BPE vocabulary is over-optimized for English. Everything else costs more tokens, so more expensive and slower.

Tokens per 100 words

English≈110
French≈160
Arabic / Korean≈400
Order of magnitude. Same effect for code, raw logs, serial numbers.

Consequences: more latency (time-to-first-token), more memory bandwidth, higher API bill.

§71 tokenizer, multiple LLMs?

The key

Not "one tokenizer per company" but one tokenizer per model lineage. You don't plug one model's tokenizer into another: the IDs would point to wrong rows.

One encoding shared by a lineage

cl100k_baseencoding
GPT-4
GPT-3.5-turbo
text-embedding-3
Recent models move to o200k_base. History: r50k → p50k → cl100k → o200k.
Open source?

Yes, almost always, including at OpenAI. pip install tiktoken and you see exactly how text is split, locally. What's closed are the weights. The tokenizer isn't the model.

Sheet 01 recap

  1. The tokenizer produces numbers, not meaning.
  2. Frozen dictionary (text→ID) vs learned embedding table (ID→vector).
  3. BPE = merges of the most frequent pair, by rank, up to ~100k entries.
  4. Byte-level = zero crashes, worst case 1 token per byte.
  5. Fragmentation = O(N²) + KV cache saturated.
  6. Linguistic tax: FR ≈ 1.5× English, AR/KO ≈ 4×.
  7. Tokenizer tied to its model lineage · open source, unlike weights.

Embeddings

The ID becomes a position in space. That's where meaning appears.

Plan of this sheet
  1. §1From ID to vector
  2. §2Meaning = a position
  3. §3Learned numbers, not given
  4. §4The TWO embeddings ⚠

The semantic space (2D projection of ~768 dimensions)

animals fruits vehicles cat dog rabbit apple banana bank car train Each word = a point · distance = closeness of meaning
"bank" landed on the fruit side, next to nothing it means. §4 explains why that is a problem.
The shift

An embedding is a position, not a label. The ID 331 meant nothing. Here it's the opposite: the place in space IS the meaning. Two close vectors = two close meanings.

The essentials in 7 points

  1. Embedding = vector = position. Not an arbitrary label.
  2. Proximity = close meaning · direction = relation (king - man + woman ≈ queen).
  3. Numbers are learned: a word is defined by its surroundings.
  4. One space per model. Never mix two spaces.
  5. TWO embeddings: static (no context) vs contextual (after attention).

§1From ID to vector

The key

The embedding is simply the contents of the row pointed to by the ID. Mechanically trivial. The whole leap is in what these numbers mean.

ID 9015
[0.21, −0.10, 0.88, …]768 numbers
Unreadable to a human. But its relative position carries the meaning.

§2Meaning = a position

Two properties, not one

Proximity → similarity of meaning.
Direction → type of relation.

Directions encode relations

+ feminine + royalty king queen man woman same direction = same relation
queen ≈ king - man + woman.

§3Learned numbers, not given

The key, in one sentence

A word is defined by its surroundings. Two words appearing in similar contexts are pushed toward similar vectors.

"cat" and "dog" come closer because you encounter them in the same patterns ("feed the ___", "the ___ sleeps"). Meaning is never declared: it is deduced from co-occurrences.

Lock

The space depends on the model that learned it. Two models → two unrelated spaces. Same lock as the tokenizer (sheet 01 §7).

§4The TWO embeddings

The classic mistake

There is no one embedding, but two. They don't serve the same purpose.

Type 1 · static (per token) A fixed vector per token, without context. "bank" the river edge and "bank" the money place receive the same vector. It's the table from sheet 01. A starting point, not the final meaning.
Type 2 · contextual After passing through the attention of the Transformer (which mixes each token with its neighbors), the token gets a new vector, which depends on the sentence. The two "bank"s finally diverge.

Attention is what separates the two meanings

"fished from the bank"
Transformerattention
[0.4, 0.0, …]≈ rivers region
"the bank approved it"
Transformerattention
[−0.2, 0.5, …]≈ finance region
Same token, same static vector on input. Two contextual vectors on output.
When one, when the other?

Type 1 = the input to the model, right after the tokenizer.
Type 2 = the output, after the attention layers.

Context acts at TWO different moments

During training · once
The corpuscontext in bulk
Trainingaverages contexts
Static tablefrozen vectors
During use · each sentence
Token "bank"1 token
Lookupno context read
Contextual vectorvia attention
Context freezes the table (training), then attention reads it again (use).

The pipeline in order: text → tokenizer (IDs) → static vectors → attention → contextual vectors.

Where does the STATIC vector of a two-sense word land?

rivers & nature money & finance river shore loan cash bank (static) one point, in the middle, in NO zone
Soft position: somewhat close to both, truly close to neither. Attention then pulls it to the right zone.

Recap sheet 02

  1. Embedding = position in space, not label.
  2. Proximity = meaning · direction = relation.
  3. Numbers learned by co-occurrence → one space per model.
  4. Two embeddings: static (input) vs contextual (after attention).