Skip to main content

Lesson 2 · 9 min

Cosine similarity in 5 lines of code

Retrieval is just "find the closest vectors". The math is one dot product and two norms.

Why cosine

The go-to similarity for embeddings is cosine similarity — it measures the angle between two vectors, ignoring magnitude.

cos(a, b) = (a · b) / (|a| × |b|)

Values range from −1 (opposite direction) to 1 (same direction). For typical sentence embeddings, scores fall mostly in the 0.0–0.9 range; > 0.7 usually means "about the same topic".