Tokens become numbers
In the first tutorial series you learned that an LLM chops text into tokens. But a computer cannot reason about a token like "king" directly. It needs numbers.
So the model turns every token into a long list of numbers called an embedding, or a vector. You can think of a vector as the token's address in a giant map of meaning. Words that show up in similar contexts get similar addresses.
Key idea: the model does not read words. It reads vectors. All the grammar, meaning, and style the model learns is stored as patterns of numbers.
What is a vector?
A vector is just a list of numbers. In real LLMs, a vector might have hundreds or thousands of numbers. To make it visual, we will use only two: an x coordinate and a y coordinate.
If "king" has the vector [0.8, 0.6], that means we place a dot at x = 0.8 and y = 0.6 on a graph. Another word in a similar context will land near it.
Why two numbers? Real embeddings are high-dimensional. We are squashing them into 2D so our human brains can see the pattern. The math works the same way in 1,000 dimensions.
Explore word space
The graph below shows pretend 2D vectors for eight words. Click a word button to see where it lives and which other words are most similar. Closer directions mean closer meanings.
Selected word
—
Most similar words
The score is the cosine similarity. 1 means "pointing the same direction"; 0 means "unrelated"; negative means "pointing opposite."
Pick a word
Measuring similarity
To compare two vectors, we use the cosine similarity. It measures the angle between two arrows that start at the origin.
If two words usually appear in the same contexts — like "cat" and "dog" — their vectors point in similar directions and the cosine similarity is close to 1. If they are unrelated, the score drops toward 0.
Because we made our example vectors have length 1, the formula simplifies to just multiplying the matching coordinates and adding them up: x1*x2 + y1*y2. Try it for "dog" and "cat" in the demo above.
Why this matters: every time an LLM decides which word comes next, it compares vectors. Similar vectors tell the model that two words can swap into the same sentence.
The code behind similarity
Here is the same cosine similarity calculation, written in plain Python with NumPy. You can use it on any two vectors of the same length.
In a real LLM these vectors have hundreds of dimensions, but the function is the same: multiply matching numbers, add them up, and divide by the lengths. That one score tells the model how related two words are.
Check yourself
In the demo, why are "cat" and "dog" closer together than "cat" and "king"? Use the idea of context and vectors in your answer.
Your answer:
Sample answer: "Cat" and "dog" show up in many of the same sentences, like "My ___ is hungry." The model learns to give them similar vectors. "King" appears in very different contexts, so it lands far away.
Quick quiz
1. What is an embedding?
2. Why do similar words have similar vectors?
3. If two vectors point in almost the same direction, their cosine similarity is close to...
Keep learning about LLMs
Now you know how words become vectors. Next, see how those vectors feed into a neural network, the pattern-finding machine at the heart of an LLM.
Part of the “LLMs Under the Hood” tutorial series.