Browse AI Foundation

What Is Cosine Similarity? The Math Behind Comparing Embeddings

The specific calculation most semantic search and RAG systems use to measure how close two embeddings are — and why it focuses on direction rather than magnitude.

Measuring the angle, not the distance

Cosine similarity measures how similar two vectors — like two embeddings — are by comparing the angle between them, rather than the straight-line distance. Two vectors pointing in nearly the same direction score close to 1 (highly similar), regardless of how long each vector happens to be.

Why direction, not length, is what matters

For embeddings, the direction a vector points in generally carries the meaning, while its magnitude can vary for reasons unrelated to semantic content. Measuring the angle rather than raw distance makes similarity comparisons more robust to that kind of magnitude variation between different embeddings.

Where it's actually used

Cosine similarity is the default distance metric behind most semantic search and nearest neighbor search implementations in a vector database — it's the concrete calculation underneath the more general idea of finding embeddings that are "close" in meaning.

Frequently Asked Questions

What is cosine similarity?

A measure of how similar two vectors are, based on the angle between them rather than their straight-line distance — commonly used to compare embeddings, where direction generally carries the meaning.

Why is cosine similarity preferred over simple distance for embeddings?

An embedding's direction generally carries its semantic meaning, while its magnitude can vary for unrelated reasons. Measuring the angle rather than raw distance makes comparisons more robust to that magnitude variation.

Keep Exploring