Skip to content
Nitmonk
Tokens & Embeddings

Cosine Similarity

Measuring similarity by the angle between vectors.

Cosine Similarity — infographic explaining Measuring similarity by the angle between vectors.
Cosine Similarity — visual explainer by Nitmonk.

Cosine similarity measures how similar two vectors are by the angle between them. It's the standard metric for comparing embeddings in semantic search, because it focuses on direction (meaning) rather than length.

In simple terms

A score from −1 to 1 for how close two vectors point — the standard way to compare embeddings.

How it works

  1. 1Convert text into vectors (embeddings).
  2. 2Compare the vectors by looking at the angle (θ) between them.
  3. 3Calculate the angle between the two vectors.
  4. 4Compute cosine: cos(θ) = (A · B) / (‖A‖ ‖B‖).
  5. 5Interpret the score: higher means more similar direction.

Key points

  • Range is −1 to 1: 1 = same direction, 0 = unrelated, −1 = opposite.
  • It ignores magnitude and focuses on direction, so it works well for text.
  • Efficient and widely supported in vector databases.
  • Used in semantic search, recommendations, clustering and deduplication.

Why it matters

Cosine similarity is how vector databases decide which embeddings are 'closest'. It's the core comparison behind semantic search and RAG retrieval.

Frequently asked questions

Why cosine and not distance?
Cosine focuses on direction (meaning) and ignores vector length, which suits text embeddings well.
What score counts as 'similar'?
It depends on the data, but higher (closer to 1) means more similar; you tune a threshold per use case.