A vector is a list of numbers that you can treat as a point, an arrow, or a meaning. Almost every retrieval system, embedding model, and attention layer is built on three operations on vectors: add them, measure them, and take their dot product.
When a RAG system answers a question, it does not read your documents. It turns the question into a list of numbers, turns every stored text chunk into a list of numbers, and finds the chunks whose numbers sit closest to the question's. Everything in that sentence is vector math. This lesson assumes only high-school algebra and builds the pieces in order: what a vector is, how to combine and measure vectors, the dot product, and the three ways retrieval systems score "closeness" (dot product, cosine similarity, Euclidean distance), which often disagree.
A vector is an ordered list of numbers. The count of numbers is its dimension. Order matters: (3, 1, 2) and (1, 3, 2) are different vectors. We write a vector in bold or with an arrow, and its entries in a column or row. Here we use parentheses: q = (3, 1, 2) is a 3-dimensional vector with entries 3, 1, and 2.
The same list can be read three ways, and AI work switches between them constantly:
One row of a dataset: (age, tokens per request, errors per day) = (31, 420, 2). Each position has a meaning you chose.
Three numbers locate a point in 3-D. Two vectors that are close together describe similar things, if the numbers were produced sensibly.
The same point drawn as an arrow from (0, 0, 0). The arrow has a length and a direction, and both turn out to matter.
An embedding is a vector that a model produces to represent a word, sentence, image, or document, so that similar items get nearby vectors. Real text embeddings typically have hundreds to a few thousand dimensions (768 and 1,536 are common sizes). To keep every number checkable by hand, this lesson uses a toy 3-dimensional embedding. Nothing here depends on the dimension being 3; only the pictures do.
Two operations need no new ideas. To add vectors of the same dimension, add entry by entry. To scale a vector, multiply every entry by one ordinary number (a scalar).
Geometrically, 2a is the same arrow stretched to twice the length, pointing the same way. A negative scalar flips the arrow. Adding two arrows means placing the second's tail at the first's tip; the sum goes from the start to the end. Subtracting a - b gives the arrow that gets you from b to a, which is why subtraction measures "difference."
These two operations are everywhere in model code. Averaging a vector across tokens (adding them up and scaling by 1 divided by the count) is the usual way to get one embedding for a whole sentence. A residual connection in a transformer adds a layer's output vector to its input vector. Updating weights in training is a scaled vector subtraction: new weights = old weights - (step size) × (gradient), which the calculus lessons develop.
Early word-embedding work popularized the observation that differences between embeddings can encode relationships: the vector from "man" to "king" roughly matches the vector from "woman" to "queen". Here is a deliberately tiny toy version, with two made-up dimensions (royalty, gender), constructed so the arithmetic works exactly:
The length of a vector, also called its norm, is written with double bars, ‖v‖. The everyday one is the L2 norm: square every entry, add them, take the square root. It is the Pythagorean theorem extended to any number of dimensions.
The L1 norm adds the absolute values of the entries instead: ‖v‖1 = |3| + |-4| + |12| = 19. It measures "total size" like city-block travel rather than a straight line. You will meet it in regularization, where an L1 penalty on weights pushes many of them to exactly zero, and an L2 penalty shrinks them smoothly (the statistics course, Lesson 8, covers why a penalty on weight size fights overfitting). If a norm is written without a subscript, assume L2.
The distance between two points is the length of their difference: distance(a, b) = ‖a - b‖. That is Euclidean distance, and it is why subtraction from section 2 matters.
The dot product of two vectors of the same dimension multiplies matching entries and adds the results. The answer is a single number, not a vector.
That is the by-components recipe. The second description is geometric:
Both recipes give the same number for the same pair of vectors. The second explains what the number means. Take two vectors in 2-D, a = (4, 1) and b = (2, 3):
Read the dot product as "how much do these two arrows point the same way, scaled by how long they are." Three cases cover everything: if the arrows point roughly the same way, cos is near 1 and the dot product is large and positive; at a right angle, cos is 0 and the dot product is 0; pointing opposite ways, it is negative.
Two vectors are orthogonal (perpendicular) exactly when their dot product is 0. Check: (2, -3, 0) · (3, 2, 5) = 6 - 6 + 0 = 0, so these two are orthogonal even though you could not draw them easily. Orthogonal means "carries no shared information in the dot-product sense." You will see this again in the later linear algebra lessons, and in attention, where a query orthogonal to a key contributes a score of zero.
A unit vector has length 1. To normalize any nonzero vector, divide it by its own length: u = v / ‖v‖. The direction is unchanged and only the length is removed.
Normalization matters because of the geometric formula. If both ‖a‖ and ‖b‖ equal 1, then a · b = cos(θ): the dot product of two unit vectors is purely an angle measurement. Many embedding models output unit-length vectors for exactly this reason, and many vector databases offer a "normalize on insert" option.
Retrieval needs one number saying how well a stored chunk matches a query. Three standard choices:
| Score | Formula | Best match is the | Sees length? |
|---|---|---|---|
| Dot product | q · c | largest value | Yes: longer vectors score higher |
| Cosine similarity | (q · c) / (‖q‖ ‖c‖) | largest value (max 1) | No: only the angle counts |
| Euclidean distance | ‖q - c‖ | smallest value | Yes: length differences add distance |
Cosine similarity is just the dot product with both lengths divided out, so it is the cos(θ) from section 4. It ranges from -1 (opposite) through 0 (orthogonal) to 1 (same direction). A related fact from the statistics course, Lesson 6: if you first subtract each vector's own mean from its entries, cosine similarity becomes the Pearson correlation.
A team's retrieval system embeds a query and three document chunks into a toy 3-dimensional space. The numbers are invented for the example:
First the lengths. ‖q‖ = √(9 + 1 + 4) = √14 ≈ 3.742. ‖A‖ = √(36 + 4 + 16) = √56 ≈ 7.483. ‖B‖ = √(4 + 1 + 4) = 3. ‖C‖ = √100 = 10.
Dot products:
Cosine similarities (dot product divided by ‖q‖ × ‖chunk‖):
Euclidean distances (length of q - chunk):
| Chunk | Dot product | Cosine | Distance |
|---|---|---|---|
| A | 28 | 1.000 (best) | 3.742 |
| B | 11 | 0.980 | 1.000 (best) |
| C | 30 (best) | 0.802 | 7.348 |
Three metrics, three different winners. Chunk A points in exactly the same direction as the query (it is 2× the query, since 2 × (3, 1, 2) = (6, 2, 4)), so cosine rates it a perfect match. Chunk B is the closest point in space, so distance picks it. Chunk C has a huge length and a big component that overlaps with the query's biggest entry, so it wins on raw dot product, even though its direction is the worst of the three. The dot product mixed "does it match" with "how big is this vector."
Divide every vector by its length first, and the disagreement disappears. For unit vectors, dot product = cosine, and Euclidean distance is a function of cosine: ‖u - v‖2 = 2 - 2cos(θ). Using B as a check: 2 - 2(0.980) = 0.040, which matches the squared distance between the normalized q and normalized B (about 0.0401). Chunk C: 2 - 2(0.802) = 0.396. A: 2 - 2(1) = 0. So after normalization all three metrics rank A, then B, then C.
Real embeddings have hundreds to thousands of dimensions, and geometry behaves differently there. Take two directions chosen at random in d dimensions. Their cosine is centered on 0, with a typical spread of about 1/√d:
In high dimensions, two unrelated directions are almost always nearly orthogonal, because there are so many ways to differ. That is useful: it leaves lots of room to store distinct meanings. It also means real similarity scores sit in a narrow band. A cosine of 0.78 versus 0.74 can be the difference between "relevant" and "not," and there is no universal cutoff, because the typical background level depends on the model. Calibrate a threshold against your own labeled queries rather than copying one from a blog post.
A vector database stores one embedding per chunk and, for each query, returns the chunks with the best score under one of the three metrics above. When retrieval returns odd results, check the metric and normalization before changing the chunking or the prompt. A longest-chunk-wins pattern is the signature of raw dot product on unnormalized vectors.
In a transformer, each token produces a query vector and a key vector, and the attention score between two tokens is a dot product of query and key. High score means "pay attention here." Because dot products grow with dimension, implementations divide the scores by √(key dimension), which is a normalization of the same kind as section 5. M2 shows how these query, key, and value vectors are produced from a token's embedding by matrices.
One neuron computes a dot product between its weight vector and its input vector, adds a bias, and applies a nonlinearity. A layer is many such dot products at once. That bundling is what a matrix is, and it is where this lesson hands off.
You now have vectors as data and the dot product as the basic measuring tool. M2 (Matrices & Transformations) stacks many vectors into a matrix, uses a matrix to turn one vector into another, and shows that a whole layer of a neural network, and the query, key, and value projections in attention, are just matrix-vector multiplication: lots of dot products done together.
1. What is the dot product of a = (2, 0, 5) and b = (1, 4, -1)?
2. Two embeddings have a dot product of exactly 0. What does that tell you about the angle between them?
3. A retrieval system keeps returning the longest chunks, whose vectors have the largest lengths, even when shorter chunks are clearly more on-topic. The index scores with raw dot product on unnormalized vectors. What is the most direct fix?
4. What is the L2 norm of v = (6, -8)?
5. For two unit vectors u and v, which statement is true?