All posts

Cosine similarity

/AI/4 min read

Cosine similarity compares the direction of two vectors and ignores their length: (3, 4) and (6, 8) score exactly 1. In high dimensions, unrelated vectors score near 0 — random 768-dimensional vectors land within about ±0.037 — which is what makes a score of 0.4 meaningful.

Embedding models turn text, images and other data into vectors so that similar things end up close together. "Close" needs a definition, and the one used almost everywhere — semantic search, RAG retrieval, deduplication, recommendation — is cosine similarity.

The definition

Cosine similarity is the cosine of the angle between two vectors:

cos(a, b) = (a · b) / (|a| × |b|)

The dot product on top measures how much the vectors point the same way; dividing by both lengths removes the effect of how long they are. The result is always between −1 and 1.

abcosinemeaning
(3, 4)(6, 8)1.000same direction
(3, 4)(4, 3)0.960nearly the same
(3, 4)(4, −3)0.000perpendicular — unrelated
(3, 4)(−3, −4)−1.000opposite

Note the first row. (6, 8) is twice as long as (3, 4), but it points in exactly the same direction, so the similarity is a perfect 1. Cosine similarity is blind to length.

Why ignore length?

In many embedding spaces, a vector's length carries information that is irrelevant to meaning — how long the text was, how frequent its words are, how confident the model was. Two passages about the same topic, one short and one long, should count as similar. Cosine similarity says they are; plain Euclidean distance would be thrown off by their different lengths.

Many embedding models normalise their outputs to length 1 anyway. For unit vectors, cosine similarity is the dot product, and ranking by cosine, by dot product, or by Euclidean distance all give the same order. That is why vector databases often just compute dot products on pre-normalised vectors: it is the same answer for less work.

What the numbers mean in high dimensions

In two or three dimensions, two random directions are often fairly aligned. In hundreds of dimensions, they almost never are. Drawing pairs of random vectors and measuring their cosine:

dimensionstypical spread of cosine between random vectors
3±0.570
100±0.097
768±0.037
4,096±0.016

The spread is almost exactly 1 / √d. In a 768-dimensional space, unrelated directions sit within a few hundredths of zero. That is why a cosine of 0.3 or 0.4 between two embeddings is a strong signal, not a weak one: it is ten standard deviations above chance.

It also means cosine scores are not probabilities and are not comparable between models. One embedding model's "similar" might be 0.8, another's 0.45, depending on how it spreads vectors through its space. Thresholds must be calibrated per model, on examples labelled relevant or not.

ANGLE, NOT LENGTH (6,8) · cos 1 (3,4) (4,3) · 0.96 (4,−3) · 0 random pairs: cosine spread d = 3 d = 768 · ±0.037 −101 in high dimensions unrelated means ≈ 0, so modest scores carry real signal
Length is discarded; only direction counts. And in many dimensions, random directions are nearly perpendicular.

Using it well

Normalise once, at index time. Store unit-length vectors and compute dot products at query time. Comparing a query against a million 768-dimensional vectors is 768 million multiply-adds — trivial for a GPU, and vector indexes avoid most of it with approximate search.

Embed queries and documents the way the model expects. Many models use different prefixes or modes for queries and passages; mixing them up lowers every similarity and scrambles the ranking.

Calibrate thresholds on labelled examples. "Above 0.75 is a match" from one model's documentation means nothing for another.

Do not compare scores across queries too literally. A broad query's best match may score lower than a narrow query's worst. Rank within a query; use thresholds cautiously across them.

Remember what it cannot see. Cosine similarity measures topical closeness in the model's space. "The drug is safe" and "the drug is not safe" can be very close. Retrieval systems add keyword search and rerankers for exactly these cases.

What to take away

Cosine similarity is the dot product of two vectors divided by both their lengths: it compares direction and ignores magnitude, so (3, 4) and (6, 8) score 1 and perpendicular vectors score 0. For unit vectors it equals the dot product, which is why systems normalise once and multiply thereafter.

In high dimensions, random vectors score within about ±1/√d of zero — ±0.037 at 768 dimensions — so moderate scores are meaningful, but only relative to the specific embedding model that produced them.