What is cosine similarity?
Cosine similarity compares two vectors by asking how aligned their directions are. It computes the cosine of the angle between them: vectors pointing the same way score near 1, unrelated vectors score near 0, and opposed vectors score negative. Because it ignores vector length, it measures what the vectors are about rather than how strongly.
For embeddings, where direction carries the meaning, this makes cosine the natural and most widely used similarity measure.
Key takeaways
- Cosine similarity measures the angle between two vectors.
- It ranges from -1 to 1, with higher meaning more similar.
- It is the default metric for comparing embeddings.
How it works
The score is the dot product of the two vectors divided by the product of their lengths. When embeddings are normalized to unit length, as many models do by default, cosine similarity and dot product coincide, which is why the two are often used interchangeably in vector search. Distance metrics like Euclidean distance rank neighbors identically on normalized vectors.
Why it matters
Every find-similar query, near-duplicate check, and semantic search ultimately reduces to similarity scores, and cosine is usually that score. Knowing what it measures, and that it should match how the embedding model was trained, helps debug why a search returns the neighbors it does.
Frequently asked questions
Why use cosine similarity instead of Euclidean distance?
For normalized embeddings they rank neighbors the same way. Cosine is preferred because embedding direction, not magnitude, carries the meaning.
What is a good cosine similarity threshold?
It depends on the model and task. Thresholds are usually chosen empirically by inspecting matches at different score levels.
Related terms
Go deeper