almessadi.
Back to Index

Cosine Similarity vs Dot Product Depends on Your Embeddings_

The right vector similarity metric depends on whether embeddings are normalized and how the retrieval system interprets magnitude versus direction.

PublishedFebruary 12, 2025
Reading Time5 min read

Vector search quality is not only about the database. It starts with the math you use to compare embeddings. Teams often choose a similarity metric almost accidentally, then wonder why retrieval quality feels inconsistent.

The Practical Difference

Cosine similarity cares about angle, which means it measures whether two vectors point in the same direction. Dot product combines angle and magnitude.

That difference matters because some embedding models produce normalized vectors and others do not. If vectors are normalized to unit length, cosine similarity and dot product become closely related:

function dot(a: number[], b: number[]) {
  return a.reduce((sum, value, index) => sum + value * b[index], 0);
}

function cosine(a: number[], b: number[]) {
  const normA = Math.sqrt(dot(a, a));
  const normB = Math.sqrt(dot(b, b));
  return dot(a, b) / (normA * normB);
}

Why This Matters in Retrieval

If magnitude carries meaning in your embedding pipeline, dot product may be useful. If only directional similarity matters, cosine is often the safer lens.

That means the right metric depends on:

  • whether embeddings are normalized
  • whether vector magnitude encodes useful signal
  • what your retrieval model expects

Better Rule

Do not choose cosine or dot product because a tutorial did. Check the embedding model documentation, inspect whether vectors are normalized, and evaluate the metric against your actual relevance judgments.

Vector search is math plus data plus product expectations. Leaving out any of those three usually produces weak retrieval.

Further Reading