DevKits

Comparison

Cosine Similarity vs Dot Product vs Euclidean Distance for Embeddings

Once text is turned into embedding vectors, you need a way to score how similar two vectors are. The three standard metrics — cosine similarity, dot product, and Euclidean distance — can give the same ranking or very different ones depending on whether your vectors are normalized. Picking the metric your embedding model and vector database expect is the difference between great and useless semantic search.

TL;DR

 CosineDot productEuclidean (L2)
MeasuresAngle between vectors (direction)Projection — direction × magnitudeStraight-line distance
Ignores magnitude?YesNoNo
Range-1 to 1 (1 = identical direction)Unbounded0 to ∞ (0 = identical)
Higher = more similar?YesYesNo — lower is more similar
Best forText embeddings (default)Normalized vectors / when magnitude mattersClustering, low-dim geometry

The options in depth

Cosine similarity

The default for text embeddings — cares only about direction.

Cosine similarity divides the dot product by the product of the vectors' magnitudes, so it measures the angle between them and ignores length entirely. Two documents about the same topic get a high score even if one is much longer (and therefore has a larger-magnitude vector). This magnitude-invariance is exactly what you want for semantic text search, which is why cosine is the default metric in most embedding pipelines.

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

Try it: Cosine Similarity Calculator →

Dot product

Cheaper than cosine, and identical to it on normalized vectors.

The dot product sums the element-wise products of two vectors. It's affected by magnitude, so a longer vector can score higher just for being bigger. But when vectors are normalized to unit length (magnitude 1), dot product equals cosine similarity — so many vector databases normalize on ingest and then use the faster dot product internally. OpenAI's embeddings, for example, are normalized, so dot product and cosine give the same ranking.

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)

Try it: Cosine Similarity Calculator →

Euclidean (L2) distance

Straight-line distance — smaller means closer.

Euclidean distance is the geometric straight-line distance between two points. Unlike the other two, lower is more similar (0 = identical). It's sensitive to magnitude and to the number of dimensions, and in very high-dimensional spaces distances tend to concentrate, which can blunt its discriminating power. It shines in clustering (k-means uses it) and lower-dimensional geometric problems rather than raw text-embedding retrieval.

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

Try it: Cosine Similarity Calculator →

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.

Related comparisons