A list of numbers
A vector is an ordered list of numbers. That is all. The order matters: each position always means the same thing.
Say you describe fruit with three numbers from 0 to 10: how sweet it is, how sour, and how big. Each fruit becomes a vector of three numbers, and fruits that are alike get similar lists.
| Sweet | Sour | Big | As a vector | |
|---|---|---|---|---|
| Mango | 9 | 2 | 6 | [9, 2, 6] |
| Banana | 8 | 1 | 5 | [8, 1, 5] |
| Lemon | 1 | 9 | 3 | [1, 9, 3] |
| Watermelon | 7 | 1 | 10 | [7, 1, 10] |
The number of entries is the vector’s dimension. These fruit vectors have 3 dimensions. The vectors inside language models are much longer. In Llama 3.1 8B, every token becomes a vector of 4,096 numbers. In Qwen3.8-27B it is 5,120, and in DeepSeek-V3 7,168. This length is called the model’s hidden size, and you can read it in the model’s config.json.
Vectors as arrows
A vector with two numbers can be drawn. Read [3, 2] as “go 3 to the right and 2 up”. Draw an arrow from the starting point to where you end up: that arrow is the vector. It has a direction (where it points) and a length (how far it goes).
length of [3, 2] = √(3² + 2²) = √13 ≈ 3.61Pythagoras: square each entry, add them up, take the square root.
length of [a, b, c, …] = √(a² + b² + c² + …)The same rule for any number of dimensions.
Nobody can picture 4,096 dimensions, and you do not need to. The rules are exactly the same as for 2 or 3; there are just more terms in each sum. People who work with models picture 2 or 3 dimensions and trust the maths for the rest.
Adding and stretching
Two things you can do with vectors come up everywhere in AI.
[3, 2] + [1, 4] = [4, 6]Adding: add the matching entries. As arrows, walk along the first, then along the second.
2 × [3, 2] = [6, 4]Scaling: multiply every entry by the same number. Same direction, twice as long.
−1 × [3, 2] = [−3, −2]A negative number flips the arrow to point the opposite way.
Adding is how a model combines information. Inside a transformer, each layer works out a small change and adds it to the token’s vector, so the vector slowly collects everything the model has worked out about that token. That running sum has a name you will meet often: the residual stream.
The dot product: how much two vectors agree
The single most important operation in AI is the dot product. Multiply the matching entries of two vectors, then add up the results. You get one number.
[3, 2] · [1, 4] = 3×1 + 2×4 = 11[a, b, c] · [x, y, z] = a×x + b×y + c×zThat number says how much the two vectors point the same way. It is large and positive when they agree, zero when they are at a right angle (unrelated), and negative when they point in opposite directions.
You will see the dot product again and again. A neuron is a dot product plus a small twist. Matrix multiplication, which is most of a model’s work, is a grid of dot products. Attention decides which words matter to each other with dot products. Learn this one well.
Quick check
Cosine similarity: same direction, any length
The dot product has one catch: it grows when the vectors are longer, even if their direction stays the same. To compare only the direction, divide by both lengths. The result is the cosine similarity, always between −1 and 1.
cos = (a · b) ÷ (length of a × length of b)1 means the same direction, 0 means a right angle, −1 means opposite.
[3, 2] and [6, 4]: 26 ÷ (3.61 × 7.21) = 1.00One is just a longer copy of the other, so they point exactly the same way.
Here are ten words placed as 2-dimensional vectors, the way a model might arrange them. Tap two words to see how similar they are. Then try the analogy button.
This interactive part needs JavaScript.
Real word vectors have hundreds or thousands of dimensions, but the calculation is identical. Turning a whole sentence or document into one vector, called an embedding, and comparing embeddings by cosine similarity is how semantic search works: it finds text with the same meaning even when the words are different.
Do these pairs point the same way, at a right angle, or opposite ways? (Hint: the dot product’s sign tells you.)
-
[2, 1]and[4, 2]Answer: Same way
-
[1, 0]and[0, 3]Answer: Right angle
-
[1, 1]and[−2, −2]Answer: Opposite
-
[3, −1]and[1, 3]Answer: Right angle
-
[−1, 2]and[−3, 6]Answer: Same way
-
[5, 0]and[−1, 0]Answer: Opposite
Where vectors show up next
From here on, almost everything is vectors. A short preview of where you will meet them:
- Embeddings: each token starts as a learned vector. Similar tokens get similar vectors.
- Attention: each token makes a “query” vector and a “key” vector, and dot products between them decide what each token looks at.
- The residual stream: each layer adds its findings to the token’s vector.
- The KV cache: the memory that makes chat fast is a store of vectors, one set per token.
- Search and RAG: documents are stored as embeddings, and the ones closest to your question are handed to the model.
Remember
- A vector is an ordered list of numbers. Its dimension is how many numbers it has; models use thousands (4,096 in Llama 3.1 8B).
- Drawn as an arrow, a vector has a direction and a length (square the entries, add, take the square root).
- Adding vectors combines them; scaling stretches or flips them. Models add each layer’s work into the residual stream.
- The dot product (multiply matching entries, add up) measures how much two vectors agree. It is the most-used operation in AI.
- Cosine similarity compares direction only, from −1 to 1. It powers semantic search.
Words to know
- Vector
- An ordered list of numbers, which can be drawn as an arrow.
- Dimension
- How many numbers a vector has.
- Hidden size
- The length of the vector a model uses for each token, such as 4,096.
- Dot product
- Multiply matching entries of two vectors and add the results. Large when they agree.
- Cosine similarity
- The dot product divided by both lengths: how closely two vectors point the same way, from −1 to 1.
- Embedding
- A vector that stands for a token, a sentence, an image or a document, learned so that similar things get similar vectors.
- Residual stream
- A token’s vector as it passes through a model, with each layer adding its findings to it.
- Semantic search
- Finding text by meaning, by comparing embeddings instead of matching words.
Go to the source
This lesson is built on these papers and documents. Paper links open arxiv.org, a separate site.
-
Essence of Linear Algebra (3Blue1Brown, videos)
The best visual introduction to vectors and matrices there is. Watch the first three chapters alongside this part of the course.
-
Efficient Estimation of Word Representations in Vector Space (word2vec)
The paper that made word vectors famous, including the “king − man + woman” result.
-
Sentence-BERT: Sentence Embeddings using Siamese BERT-Networks
How to turn whole sentences into vectors you can compare with cosine similarity, and why that is so much faster.
Check yourself
5 questions. Tap the answer you think is right.
0 of 5 answered
Lesson complete
Answer every question above to complete this lesson.
Your progress is saved on this device only.
Next lessonMatrices: machines that transform vectors