Skip to content

LearnRepresent

Cosine similarity

A similarity score between two vectors equal to the cosine of the angle between them: 1 for the same direction, 0 for perpendicular, −1 for opposite.

In DocSlurpIllustrated explainer

On this page
Cosine similarity is the angle between two arrowsArrow a has length 1.5 and points right. Arrow b has length 1.2 at 40 degrees from a. Scaled to length 1, b lands on the unit circle, and its shadow on a's axis sits at 0.766, which is cos 40°. The dot product a·b is 1.5 × 1.2 × 0.766 = 1.379. Making b longer or shorter changes the dot product but leaves the cosine at 0.766.unit circle−101θabcos θ = 0.766READOUTangle θ40°cos θ0.766|a| · |b|1.50 · 1.20a · b = |a| |b| cos θ1.379Stretch b: the dot product moves,the cosine stays put.−101oppositesame direction
Cosine is the shadow of a unit arrow, so only the angle counts. Scale b to length 1 and drop it onto a’s axis: where it lands is cos θ. At 40° that is 0.766 whether b has length 0.5 or 2. The dot product, |a| |b| cos θ, grows with length; that is why embeddings are normalized before they are compared. Turn the angle past 90° and the cosine goes negative.

The problem: length can drown out direction

You have embedded every chunk of the corpus and the question “Are we on track to beat our FY2027 earnings?” Now you need one number per chunk that says how close it is to the question.

The first idea is the dot product: multiply the two vectors position by position and add. It works, with a catch. The dot product grows with the length of the vectors as well as with how well they align. If a model produced longer vectors for longer or denser text, a rambling four-page appendix could outscore the one sentence that answers the question, just by being long. You want a score that ignores length and asks only: do these point the same way?

The fix: measure the angle

Cosine similarity divides the dot product by both lengths:

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

where |a| is the length of a: the square root of the sum of its squared values. Dividing by the lengths cancels them out, and what is left is the cosine of the angle between the two arrows.

Angle Cosine Meaning
0° 1 same direction
60° 0.5 related
90° 0 unrelated, perpendicular
180° −1 opposite

In the figure, scale b down to length 1 and drop it straight onto a’s axis. The point where it lands is cos θ. Stretch b and its shadow on the unit circle doesn’t move. That is the whole idea.

Work it by hand

Take three 2-dimensional vectors, all checkable on paper:

  • a = (3, 4), length √(9 + 16) = 5
  • b = (4, 3), length 5
  • c = (8, 6), which is b doubled, length 10
a · b = 3×4 + 4×3 = 24     cos(a, b) = 24 / (5 × 5)  = 0.96
a · c = 3×8 + 4×6 = 48     cos(a, c) = 48 / (5 × 10) = 0.96

The dot product doubled when c doubled. The cosine didn’t change, because c points exactly where b points. Two more:

d = (−4, 3)   a · d = −12 + 12 = 0     cos = 0      perpendicular
e = (−3, −4)  a · e = −9 − 16 = −25    cos = −25 / 25 = −1   opposite

The shortcut every system takes

If every vector is scaled to length 1 when it is stored (see vector normalization), both lengths in the denominator are 1 and cosine equals the plain dot product. Systems normalize once at write time and then use the cheapest comparison for every query.

Normalization also ties cosine to Euclidean distance. For unit vectors, squared distance is exactly 2 − 2 × cos. Cosine 1 means distance 0; cosine 0 means squared distance 2. Sorting by smallest distance and sorting by largest cosine give the same order. That is why a vector index built for L2 distance can serve a cosine search without anyone noticing.

What real cosines look like

In textbooks, unrelated means 0. In real embedding models, it rarely does. With all-MiniLM-L6-v2, “flight” and “revenue” score 0.284, and the question above scores 0.185 against an invoice sentence that has nothing to do with it. Models squeeze text into a narrow cone of directions, so almost everything has a positive cosine with almost everything else. Where “unrelated” sits differs by model, by pooling and even by quantization.

Two practical consequences:

  • Compare cosines within one model and one query. 0.6 can be a strong match in one model and noise in another.
  • Don’t treat cosine as a probability. 0.5 doesn’t mean “50% relevant”. If you need a cutoff, measure where unrelated text lands for your exact model and setup, then set the floor above it.

Where it breaks

  • It throws away length on purpose. If length carries information in your setup, such as a model trained with dot product to encode confidence or popularity, cosine discards it. Use the metric the model was trained with.
  • It only sees what the embedding kept. Cosine is exact about the vectors and blind to everything the model averaged away: a negation, a version number, a date. A high cosine says the passages point the same way, not that one answers the other.
  • Zero vectors are undefined. An empty string or a failed embedding can produce all zeros, and dividing by a length of zero is undefined. Validate vectors before storing them.
  • High dimensions compress the range. In hundreds of dimensions, random directions are nearly perpendicular and real ones cluster tightly. Small cosine differences can be the whole signal, so ties and near-ties are common.

When to use it

Use cosine (or dot product on normalized vectors) for comparing text embeddings unless the model’s documentation says otherwise. Normalize at ingest, compare within one model, and calibrate any threshold on your own data. When you need to know whether a passage actually answers the question, follow similarity with a reranker that reads both texts.

In DocSlurp

In DocSlurp

Embeddings are L2-normalized, so the index’s squared L2 distance equals 2 − 2cos. Search recovers cosine from the adapter score to apply the relevance floor (core/server/src/search/relevance.ts), and MMR computes cosine directly (core/server/src/search/mmr.ts).

Sources

Jump to