Choosing Distance Metrics (cosine, L2, dot)
When to pick cosine, L2 (Euclidean), or dot-product distance — and why most embedding models prefer cosine.
Choosing Distance Metrics (cosine, L2, dot) is a free AI Agents lesson on CoddyKit — lesson 4 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the AI Agents learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
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))Dot Product
Range: -infinity to +infinity. Higher = more similar.
def dot(a, b):
return np.dot(a, b)
# Equivalent to cosine when both vectors are unit-normalized.Euclidean (L2)
Range: 0 to +infinity. Lower = more similar.
def l2(a, b):
return np.linalg.norm(np.array(a) - np.array(b))Which Should You Use?
For text embeddings, the convention:
- OpenAI text-embedding-3 — cosine (vectors are pre-normalized)
- Cohere — cosine
- Sentence Transformers / BGE — usually cosine, check the model card
When in doubt, cosine.
Dot Equals Cosine When Normalized
If |a| = |b| = 1, then cos(a,b) = a . b. Dot product is faster (no division) — production systems often use dot on pre-normalized vectors.
When L2 Is Used
Some research models (older Sentence-BERT variants, image embeddings) are trained with L2 distance and perform better with it. Always check the model card.
Why It Matters for the Index
The index structure (HNSW, IVF, FAISS) is built for a SPECIFIC metric. Changing metric after indexing usually means rebuilding the index from scratch.
Setting Metric in Pinecone
from pinecone import ServerlessSpec
pc.create_index(
name='docs',
dimension=1536,
metric='cosine', # or 'dotproduct' or 'euclidean'
spec=ServerlessSpec(cloud='aws', region='us-east-1')
)Setting Metric in Qdrant
from qdrant_client.models import VectorParams, Distance
client.create_collection(
collection_name='docs',
vectors_config=VectorParams(size=1536, distance=Distance.COSINE)
)Inconsistent Metric Across Systems
If you re-rank with a cross-encoder later, it has its own score scale. Do not mix metrics into a single composite without normalizing first.
Visualizing the Difference
For two unit vectors:
- cos = 1, L2 = 0 -> identical
- cos = 0.5, L2 ~ 1 -> related
- cos = 0, L2 ~ sqrt(2) -> orthogonal
- cos = -1, L2 = 2 -> opposite
Pre-Normalize Once
Normalizing on ingest is cheap (one division). It locks in compatibility with cosine / dot and avoids per-query normalization later.
vec = vec / np.linalg.norm(vec)Default Metric
For OpenAI text embeddings, which distance metric is the default?
Recap
Cosine for text. Pre-normalize once, use dot product for speed. Set the metric at index creation — changing it later means a rebuild.
Frequently asked questions
Is the “Choosing Distance Metrics (cosine, L2, dot)” lesson free?
Yes — the full text of “Choosing Distance Metrics (cosine, L2, dot)” is free to read here on the web, and the AI Agents course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the AI Agents course, upgrade to CoddyKit PRO.
What will I learn in “Choosing Distance Metrics (cosine, L2, dot)”?
When to pick cosine, L2 (Euclidean), or dot-product distance — and why most embedding models prefer cosine. You practise AI Agents with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start AI Agents?
No prior experience is required. AI Agents on CoddyKit is structured for beginners through advanced learners; this is — lesson 4 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Choosing Distance Metrics (cosine, L2, dot)” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this AI Agents lesson?
Yes. Every AI Agents lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- Pinecone, Weaviate, Qdrant: Comparison
- Metadata Filtering for Hybrid Search
- Updating and Deleting Vectors
- Choosing Distance Metrics (cosine, L2, dot)