Data & Retrieval

Cosine Similarity

How aligned two vectors are, ignoring their length — 1 identical, 0 unrelated, −1 opposite.

Cosine similarity is the dot product of two vectors divided by their magnitudes: cos(θ) = (a·b) / (‖a‖‖b‖). Because it ignores length and measures only direction, it’s the standard score for comparing embeddings in search and RAG.

Worked example: for a = [1, 0] and b = [1, 1], the dot product is 1, the magnitudes are 1 and √2, so cosine ≈ 1 / 1.414 ≈ 0.71 — a 45° angle. Identical direction gives 1, orthogonal gives 0, opposite gives −1. Gotcha: if you normalize vectors to unit length first (many embedding models already do), cosine similarity is just the dot product — which is why vector databases can rank by a plain dot product and get cosine ordering for free.