Embeddings / Representation
Cosine, dot product, and Euclidean, and when each fits.
Reviewed by Yuvaraj
An embedding turns a word, sentence, or image into a vector, but a vector on its own means nothing. Meaning only appears when you compare two vectors, and the function you pick for that comparison is what turns a pile of embeddings into a ranked list of search results. Choose the wrong comparison and your rankings degrade silently, no error, just worse answers. This lesson covers the three metrics you will actually use, cosine similarity, dot product, and Euclidean (L2) distance, and exactly when each is correct.
First, a convention worth internalizing. A similarity score goes up as two vectors get closer: the cosine similarity of two identical directions is . A distance goes down as they get closer: identical vectors have distance . Vector stores mix both conventions, so always confirm whether your index returns "top by highest score" or "top by smallest distance" before you trust the order it hands back.
| Metric | Formula | Measures | Closer means |
|---|---|---|---|
| Cosine similarity |
Ask about this lesson, or about anything in AI. Answers cite the lessons they draw on.
Finished this lesson?
Mark it complete to earn XP, keep your streak, and schedule a review.
| angle only; magnitude-invariant |
| higher (max ) |
| Dot (inner) product | angle and magnitude | higher |
| Euclidean / L2 | straight-line gap in space | lower (min ) |
Use cosine when only direction matters, the default for text embeddings, where a longer document should not outrank a short one just because its vector is bigger. Use the dot product when magnitude carries signal, for example when a model lets a vector's norm encode confidence or popularity and you want stronger vectors to rank higher. Use L2 when you genuinely care about geometric proximity, such as clustering or coordinates in a learned space.
For a curious beginner
Cosine asks "are these arrows pointing the same way?" and ignores how long the arrows are. The dot product asks the same question but rewards longer arrows. If one document's vector is simply bigger, the dot product can rank it higher even when its direction is a worse match.
How it is actually used
Cosine is just the dot product after L2-normalizing both operands. Most text-embedding models are trained to be compared with cosine, so normalize once at index time and once at query time. If your store only exposes an inner-product operator, normalize your vectors first and the two metrics become identical, letting you use the faster kernel.
The underlying mechanism
The identity says it all. Cosine divides out the magnitudes and keeps only . The dot product keeps all three factors, so it responds to both angle and length.
Here is the fact that removes most of the confusion. If every vector is L2-normalized so that , the three metrics agree on order:
Therefore ranking by cosine = ranking by dot product = inverse ranking by squared L2 distance. On normalized data you can use whichever kernel your index computes fastest and get identical neighbors.
Work in three dimensions. Take the query , with , and compare two candidates.
| Vector | Dot with | Norm | Cosine with |
|---|---|---|---|
The dot product ranks A first because , A wins purely because its vector is longer. Cosine ranks B first because : B points in exactly the query's direction (a perfect, match), while A sits off. If these were text embeddings, B is the better result, and cosine is the metric that surfaces it; the raw dot product was fooled by magnitude. Normalize all three vectors to unit length and the dot products become for A and for B, identical to the cosine scores, and now the dot product agrees that B ranks first.
Common mistakes