
What is Cosine Similarity?
Cosine similarity is a mathematical measure of similarity between two vectors by computing the cosine of the angle between them. In AI, it's the standard way to compare embeddings — measuring how semantically similar two pieces of text, images, or other data are.
Why It Matters
Cosine similarity is the math behind semantic search, RAG, recommendation systems, and duplicate detection. Whenever an AI system needs to answer "how similar are these two things?", cosine similarity is typically the metric used. Understanding it demystifies how vector databases and embedding-based systems make decisions.
How It Works
The formula:
cosine_similarity(A, B) = (A · B) / (||A|| × ||B||)
Where:
- A · B is the dot product of vectors A and B
- ||A|| and ||B|| are the magnitudes (lengths) of the vectors
Properties:
- Returns a value between -1 and 1 (for normalized embeddings, typically 0 to 1)
- 1 = vectors point in the same direction (identical meaning)
- 0 = vectors are perpendicular (unrelated)
- -1 = vectors point in opposite directions (opposite meaning)
Why cosine over Euclidean distance?
- Cosine similarity measures the angle between vectors, ignoring their magnitude
- Two documents about "AI" should be similar regardless of length
- Euclidean distance is affected by vector magnitude; cosine is not
In practice:
- Text embeddings from OpenAI, Cohere, or open-source models produce 768–3072 dimensional vectors
- Vector databases compute cosine similarity between the query vector and millions of stored vectors in milliseconds
- Top-k results (highest similarity scores) are returned as search results
Example
Suppose we embed three sentences:
- A: "The cat sat on the mat" → vector [0.2, 0.8, 0.1, ...]
- B: "A kitten rested on the rug" → vector [0.19, 0.78, 0.12, ...]
- C: "Stock markets rallied today" → vector [0.7, 0.1, 0.6, ...]
Cosine similarity: A↔B ≈ 0.95 (very similar meaning), A↔C ≈ 0.15 (unrelated). This is how semantic search knows that B is relevant to a query like A, while C is not.