Zenaique
Part ofFresher / New Grad·Week 1: Core ConceptsView roadmap →

Why is cosine similarity preferred over Euclidean distance for text embeddings?

Flashcard·Easy·4.4 · 67·~30s·Asked atAnthropicCohereOpenAI·Relevant atDatabricksElasticHugging FaceMicrosoft
Attempt it
TL;DR

Text embedding norms vary with content length and density, so cosine measures direction (semantic meaning) while ignoring magnitude. Euclidean distance would conflate length with semantic difference.

Memory aid
Sign in to see the mnemonic that makes this stick.
Easy to grasp

Imagine arrows on a giant map, all starting from the centre and pointing outward. Each arrow stands for a sentence. We want to measure how alike two sentences are. One way: ask if the arrows point in the same direction, no matter how long they are. Another way: ask how close the tips of the arrows are. The first way only cares about direction. The second cares about direction and length together. For sentences, the direction is what carries meaning. The length usually just reflects boring things like how many words were in the sentence. If we measured by tip position, two paragraphs about the same topic but with different lengths would look unrelated. By only looking at direction, they line up the way a person would expect: same idea, same heading on the map.

Concept explanation~2 min read

Everything you need to truly understand this topic: intuition, mechanics, step by step explanation, code, formulas, and worked example. Click to expand.

The cosine versus Euclidean question is one of the first design decisions in any vector retrieval system, and it is one where the technically correct answer is more nuanced than the textbook answer suggests. The textbook says cosine, because text embeddings have varying magnitudes. The production reality is that most modern embedding APIs return unit-norm vectors anyway, which makes the two metrics rank-equivalent.

What matters is understanding the underlying reason cosine became the convention, so that you can recognise the cases where the convention does not apply and the cases where it does.

Direction versus magnitude

Every vector in embedding space has two pieces of information: which way it points and how long it is. Cosine similarity uses only the first piece, by dividing the dot product by both norms:

cos(u,v)=uvuv\text{cos}(u, v) = \frac{u \cdot v}{\lVert u \rVert \, \lVert v \rVert}

Euclidean distance uses both pieces, because subtracting two vectors and taking the norm of the difference depends on both their directions and their lengths. Two vectors pointing the same way will still have a nonzero Euclidean distance if one is twice as long as the other.

For text, only the direction carries the meaning the embedding model learned. Lengths reflect mechanical properties of the input and the pooling step, not semantic content. So using a metric that listens to direction and ignores magnitude is the natural choice.

Where embedding magnitudes come from
Why training objectives shape the inference metric
Production realities and edge cases
Sign in to unlock the full deep dive.

Situations where this technique stops working.

Sign in to see when this approach fails.

2–4 min · Everything important, quickly.

Sign in to see the quick scan of the deep dive.

Real products, models, and research that use this idea.

  • OpenAI's `text-embedding-3-large` returns unit-normalised vectors, so dot-product equals cosine and either is the natural index metric.
  • Voyage AI's `voyage-3` and Cohere's `embed-english-v3` both follow the same unit-norm convention for the same reason.
Sign in to see more production examples.

What an interviewer would ask next. Try answering before peeking at the approach.

QWhy does the contrastive training objective most modern embedding models use favour cosine at inference?
A

Look at the InfoNCE loss used in models like SimCSE and E5. The loss operates on normalised dot products, which is cosine similarity. The model literally learns to push positives close in angle and negatives apart in angle, so the inference metric should match the training metric.

2 more follow-ups an interviewer would ask next. Sign in to reveal them.

Red flags & common mistakes

The phrases that signal junior thinking. Click to expand.

Most common mistake

Assuming cosine and Euclidean rank results identically. They agree only when all vectors are normalised to unit length. Modern embedding APIs often already L2-normalise, hiding the distinction in practice.

Sign in to see all red flags and common mistakes.

60 second bullets to scan on the way to the call.

  • State that cosine measures direction and Euclidean measures absolute position.

  • Explain why direction carries semantic content while magnitude does not.

Sign in to unlock the revision sheet.

Primary sources. Browse if you want the original framing.

Similar questions

Same topic, related formats. Practice these next.

4 curated
Next question
HNSW vs IVF, when…
Flashcard·Medium