0Pricing
AI Agents · Lesson

Cosine Similarity for Retrieval

Cosine similarity measures the angle between two vectors — the standard distance metric for semantic search.

Measuring Similarity

How close are two vectors? Three common distance metrics:

  • Cosine similarity — angle between vectors
  • Dot product — projection
  • Euclidean (L2) distance — straight-line distance

For text embeddings, cosine is the default.

Cosine Similarity Formula

For vectors A and B:

cos(A, B) = (A . B) / (|A| * |B|)

Result is between -1 and 1:

  • 1 = identical direction
  • 0 = orthogonal (unrelated)
  • -1 = opposite

All lessons in this course

  1. What Embeddings Are (Vector Representations)
  2. Generating Embeddings with text-embedding-3
  3. Cosine Similarity for Retrieval
  4. Embedding Models Compared (OpenAI vs Cohere vs OSS)
← Back to AI Agents