Comparison
Cosine Similarity vs Dot Product vs Euclidean Distance for Embeddings
TL;DR
| Cosine | Dot product | Euclidean (L2) | |
|---|---|---|---|
| Measures | Angle between vectors (direction) | Projection — direction × magnitude | Straight-line distance |
| Ignores magnitude? | Yes | No | No |
| Range | -1 to 1 (1 = identical direction) | Unbounded | 0 to ∞ (0 = identical) |
| Higher = more similar? | Yes | Yes | No — lower is more similar |
| Best for | Text embeddings (default) | Normalized vectors / when magnitude matters | Clustering, low-dim geometry |
The options in depth
Cosine similarity
The default for text embeddings — cares only about direction.
Good for
- ·Semantic text search and RAG retrieval
- ·Comparing documents of different lengths
- ·Any model trained/evaluated with cosine
Avoid when
- ·Magnitude carries real signal you want to keep
- ·Vectors are already normalized and you want the cheaper dot product
Dot product
Cheaper than cosine, and identical to it on normalized vectors.
Good for
- ·Normalized vectors (equivalent to cosine but faster)
- ·When you deliberately want magnitude to influence the score
- ·High-throughput similarity at scale
Avoid when
- ·Vectors are not normalized and magnitude is noise
- ·You need an interpretable bounded score (-1..1)
Euclidean (L2) distance
Straight-line distance — smaller means closer.
Good for
- ·Clustering (k-means)
- ·Lower-dimensional or geometric data
- ·When absolute position matters
Avoid when
- ·High-dimensional text embeddings where cosine works better
- ·Comparing texts of very different lengths
Which one should you pick?
→ Which metric should I use for semantic search / RAG?
Cosine similarity, or dot product if your vectors are already normalized (then they're equivalent). Both rank by direction, which is what captures meaning. Use whatever your vector database defaults to — Pinecone, pgvector, Qdrant, and Milvus all support cosine and dot product.
→ My vectors are normalized — cosine or dot product?
Either; they give the identical ranking on unit-length vectors. Dot product is slightly cheaper to compute, so at scale many systems normalize on ingest and use dot product internally.
Common pitfalls
- ⚠Mixing metrics between indexing and querying: if your vector DB index was built for cosine, don't query it with Euclidean — the nearest-neighbour results will be wrong.
- ⚠Assuming dot product equals cosine on un-normalized vectors. It only does after L2-normalization. Check whether your embedding model already normalizes (OpenAI does; some open models don't).
- ⚠Using Euclidean on very high-dimensional embeddings and being surprised that everything looks equidistant — the 'curse of dimensionality' flattens L2 distances.
Frequently Asked Questions
Is cosine similarity the same as dot product?
Only when both vectors are normalized to unit length. In that case the magnitudes are 1, so the dot product equals the cosine. For un-normalized vectors, dot product is affected by magnitude and cosine is not, so they can rank results differently.
Why is cosine preferred for text embeddings?
Because it ignores vector magnitude and compares only direction, which represents meaning. A long document and a short note on the same topic point in a similar direction and so score as similar — exactly what semantic search needs.