Malhar Ujawane

Blog

Explainer·(updated October 31, 2026)

What is cosine similarity? Cosine vs dot product vs L2

Cosine similarity scores two embeddings by the angle between them. When it equals the dot product, when Euclidean distance agrees, with a Python demo.

What is cosine similarity? Cosine vs dot product vs L2

Cosine similarity measures how closely two vectors point in the same direction: their dot product divided by the product of their lengths, which gives a score from -1 (opposite) through 0 (unrelated) to 1 (same direction). For embeddings it is the default way to ask “how similar are these two texts?”, because it ignores vector length and compares direction only. If your vectors are already normalized to length 1, cosine similarity and the dot product are the same number, and Euclidean distance gives the same ranking.

What is the formula for cosine similarity?

For vectors a and b:

cos(a, b)  = dot(a, b) / (|a| * |b|)

dot(a, b)  = a1*b1 + a2*b2 + ... + an*bn      (dot product)
|a|        = sqrt(a1^2 + a2^2 + ... + an^2)   (length, or L2 norm)

The dot product grows when the vectors point the same way and when they are long. Dividing by both lengths removes the length part, which leaves the cosine of the angle between them. Cosine distance, which pgvector exposes as the <=> operator, is just 1 - cosine similarity, so that smaller means closer.

If embeddings are new to you, the short version is that a model maps a piece of text to a list of numbers so that texts with related meaning land near each other. Cosine similarity is the “near” in that sentence.

Cosine similarity vs dot product vs Euclidean distance: what is the difference?

Three scores show up in every vector database:

  • Dot product (inner product): rewards both direction and length.
  • Cosine similarity: direction only.
  • Euclidean (L2) distance: the straight-line distance between the two points, sensitive to both.

On raw vectors they can rank the same documents three different ways. On unit vectors they always agree, because of one identity: when |a| = |b| = 1, the squared Euclidean distance is 2 - 2 * cos(a, b). Ordering by smallest distance is then the same as ordering by largest cosine, which is the same as ordering by largest dot product.

OpenAI’s embeddings guide says this outright for its own models: embeddings are “normalized to length 1”, so cosine similarity “can be computed slightly faster using just a dot product”, and cosine and Euclidean distance “will result in the identical rankings”. The Sentence Transformers similarity docs say the same from the other side: “Dot product on normalized embeddings is equivalent to cosine similarity,” and cosine is their default.

What does that look like in code?

Here is the whole comparison on three hand-picked vectors. Document A points almost exactly where the query points but is short. Document B points the same way and is three times longer. Document C points somewhere else and is the longest of all.

# cosine.py: cosine similarity, dot product and Euclidean distance on the same vectors.
# Python 3 standard library only.
import math

def dot(a, b):    return sum(x * y for x, y in zip(a, b))
def norm(a):      return math.sqrt(dot(a, a))
def cosine(a, b): return dot(a, b) / (norm(a) * norm(b))
def l2(a, b):     return math.sqrt(sum((x - y) ** 2 for x, y in zip(a, b)))
def unit(a):      n = norm(a); return [x / n for x in a]

query = [0.9, 0.1, 0.3]
docs = {
    "A short, on-topic":   [0.8, 0.1, 0.4],    # same direction as the query, small length
    "B long, on-topic":    [2.4, 0.5, 0.9],    # same direction, three times longer
    "C long, off-topic":   [1.5, 2.5, 0.2],    # different direction, very long
}

def rank(score, reverse=True):
    order = sorted(docs, key=lambda d: score(query, docs[d]), reverse=reverse)
    return " > ".join(d[0] for d in order)

print("raw vectors")
for name, v in docs.items():
    print(f"  {name:<18} cos {cosine(query, v):.3f}  dot {dot(query, v):.3f}  "
          f"L2 {l2(query, v):.3f}  |v| {norm(v):.2f}")
print("  ranking by cosine:", rank(cosine))
print("  ranking by dot:   ", rank(dot))
print("  ranking by L2:    ", rank(l2, reverse=False))

query, docs = unit(query), {k: unit(v) for k, v in docs.items()}
print("after normalizing every vector to length 1")
for name, v in docs.items():
    c, e = cosine(query, v), l2(query, v)
    print(f"  {name:<18} cos {c:.3f}  dot {dot(query, v):.3f}  "
          f"L2 {e:.3f}  2-2cos {2 - 2 * c:.3f}  L2^2 {e * e:.3f}")
print("  ranking by cosine:", rank(cosine))
print("  ranking by dot:   ", rank(dot))
print("  ranking by L2:    ", rank(l2, reverse=False))

Output (Python 3.9.6):

raw vectors
  A short, on-topic  cos 0.990  dot 0.850  L2 0.141  |v| 0.90
  B long, on-topic   cos 0.995  dot 2.480  L2 1.664  |v| 2.61
  C long, off-topic  cos 0.595  dot 1.660  L2 2.476  |v| 2.92
  ranking by cosine: B > A > C
  ranking by dot:    B > C > A
  ranking by L2:     A > B > C
after normalizing every vector to length 1
  A short, on-topic  cos 0.990  dot 0.990  L2 0.141  2-2cos 0.020  L2^2 0.020
  B long, on-topic   cos 0.995  dot 0.995  L2 0.095  2-2cos 0.009  L2^2 0.009
  C long, off-topic  cos 0.595  dot 0.595  L2 0.899  2-2cos 0.809  L2^2 0.809
  ranking by cosine: B > A > C
  ranking by dot:    B > A > C
  ranking by L2:     B > A > C

On raw vectors, the three metrics produce three different orders. The dot product puts the off-topic document C above the on-topic A, purely because C is long. Euclidean distance puts A first because A happens to sit close to the query in absolute terms. Cosine ignores length and ranks by direction.

After normalizing, all three agree, and the last two columns show the identity: 2 - 2cos equals L2^2 to three decimals for every document. That is the whole practical lesson. Normalize once at write time, store unit vectors, and use the dot product, which is the cheapest of the three.

Which similarity metric should I use for embeddings?

Use the one the model was trained with. That is the rule, and it is in most model cards. A model trained with a cosine objective produces vectors whose direction carries the meaning; a model trained with a dot-product objective may encode something useful in length too, such as how confident or how specific a passage is.

OpenAI’s guide recommends cosine and adds that “the choice of distance function typically doesn’t matter much” for its normalized models. Sentence Transformers recommends the dot metric when a model ends with a Normalize module, since the vectors are already unit length and cosine would only normalize them again.

In a database, match the index operator to the metric. pgvector’s README lists <-> for L2 distance, <#> for negative inner product and <=> for cosine distance, and each has its own index operator class (vector_l2_ops, vector_ip_ops, vector_cosine_ops), so build the index for the operator you actually query with. The <#> operator returns the negative inner product because Postgres index scans only run in ascending order, so multiply by -1 if you want the actual score. More on that in the pgvector tutorial.

Where does cosine similarity break?

Scores are not probabilities. A cosine of 0.82 means nothing on its own. Different models spread their scores differently, and the same model gives different ranges for short and long texts. A fixed threshold such as “keep anything above 0.8” will behave differently after a model upgrade. Rank, then cut by count or by a threshold you calibrated on labelled data.

Learned embeddings can make cosine arbitrary. Steck, Ekanadham and Kallus showed in a 2024 paper that for some linear models the cosine similarities of learned embeddings are not even unique, and that regularization choices can make them “arbitrary and therefore meaningless”. Their analysis is on regularized linear models, but the warning carries over: cosine is only meaningful if training made direction meaningful.

Truncation breaks normalization. If you shorten a vector, for example with Matryoshka embeddings, the result is no longer unit length. OpenAI’s guide says that when you change the dimension manually “you need to be sure to normalize” it. Forget that step and dot product stops matching cosine.

Similar is not relevant. A query and its exact opposite statement (“how to enable 2FA” and “how to disable 2FA”) can sit very close together. Cosine finds texts about the same thing. Deciding which one answers the question is the job of a reranker.

What I would do

  1. Read the model card, use the metric it names, and set the same metric on the index.
  2. Normalize every vector to unit length on write, then use the dot product (or inner-product ops) for speed. Assert the norm in a test; it is a one-line check that catches a whole class of bugs.
  3. Never ship a hard-coded similarity threshold you did not calibrate on your own labelled queries, and re-calibrate on every model change.
  4. Treat cosine as a recall step. Fetch more candidates than you need and let a reranker or hybrid search sort the final order.

FAQ

Is cosine similarity the same as the dot product?

Only for vectors of length 1. In general, cosine is the dot product divided by both lengths. Many embedding APIs, including OpenAI’s, return unit-length vectors, which is why the two are often used interchangeably.

What is a good cosine similarity score?

There is no universal one. Score ranges depend on the model and on text length. Compare scores within one model and one corpus, and calibrate any threshold on labelled examples.

Can cosine similarity be negative?

Yes. It ranges from -1 to 1, and vectors pointing in opposite directions score below zero. How often real text pairs go negative depends on the model, so don’t build logic on the sign.

What is the difference between cosine similarity and cosine distance?

Cosine distance is 1 - cosine similarity. Databases use distance because index scans sort ascending, so smaller has to mean closer.

Should I use cosine or Euclidean distance for embeddings?

For normalized embeddings they give identical rankings, since squared Euclidean distance equals 2 - 2 * cosine. For unnormalized embeddings, use what the model was trained with.

Sources

Get new posts in your inbox

© 2026 Malhar Ujawane. Built with Astro.