0Pricing
AI Agents · Lesson

Choosing Distance Metrics (cosine, L2, dot)

When to pick cosine, L2 (Euclidean), or dot-product distance — and why most embedding models prefer cosine.

Three Common Metrics

Vector DBs let you pick a distance metric. The three you will see most:

  • Cosine similarity — angle between vectors
  • Dot product — projection of one onto the other
  • Euclidean (L2) — straight-line distance

Cosine

Range: -1 to 1. Higher = more similar.

import numpy as np
def cosine(a, b):
    return np.dot(a, b) / (np.linalg.norm(a) * np.linalg.norm(b))

All lessons in this course

  1. Pinecone, Weaviate, Qdrant: Comparison
  2. Metadata Filtering for Hybrid Search
  3. Updating and Deleting Vectors
  4. Choosing Distance Metrics (cosine, L2, dot)
← Back to AI Agents