Euclidean Distance
In short: Euclidean distance is the straight-line distance between two points, so smaller means more similar and 0 means identical.
On this page
Level: Beginner · Reading time: 6 min · Phase: 1 — RAG Foundations
Before you start: Embeddings and Cosine Similarity. You should know that an embedding is a vector (a list of numbers) and that cosine compares direction.
Explain it like I'm new
Put two pins on a paper map and stretch a ruler between them. The ruler reading is the Euclidean distance. It is the "as the crow flies" distance you learned with Pythagoras: for a right triangle with sides 3 and 4, the long side is 5.
An embedding is a point in a space with many dimensions. The same ruler idea works in 384 or 1,536 dimensions; you just add more squared terms.
Words to know:
- Distance: a number saying how far apart two things are; lower means more alike.
- Dimension: one position in a vector. A 384-number embedding has 384 dimensions.
- L2 distance: another name for Euclidean distance.
- Normalized: scaled so the vector has length 1.
Unlike cosine similarity, Euclidean distance cares about length too. Two vectors that point the same way but have different lengths are "far apart" by Euclidean distance, yet "identical" by cosine. It is also called the L2 distance (after the L2 norm). Vector databases and FAISS often call it L2.
Watch it
It plays on a loop. Read the caption in the box, one step at a time. Want to go at your own pace? Step through it with Previous and Next.
How it works
- Subtract the vectors element by element.
- Square each difference.
- Add them up.
- Take the square root:
d(a, b) = √((a₁−b₁)² + (a₂−b₂)² + …).
y
4 | * b (3,4)
3 | /
2 | / d = 5
1 | /
0 *----+----+----+-- x
a (0,0)
Worked examples:
a = [0, 0],b = [3, 4]:√(9 + 16) = √25 = 5.a = [1, 2, 3],b = [2, 4, 6]: differences[−1, −2, −3], squares[1, 4, 9], sum 14, distance√14 ≈ 3.742. Cosine said these are identical in direction (1.0). Euclidean says they are 3.7 apart, becausebis twice as long.
Worked example
A query q = [0, 0] is compared with three stored vectors [3, 4], [1, 1] and [0, 2].
[3, 4]: squares 9 + 16 = 25, distance √25 = 5.[1, 1]: squares 1 + 1 = 2, distance √2 ≈ 1.414.[0, 2]: squares 0 + 4 = 4, distance √4 = 2.- Smaller is closer, so the order is
[1, 1],[0, 2],[3, 4].
If you used squared distance (2, 4, 25) the order would be the same, which is why libraries skip the square root.
Squared distance: many libraries skip the square root. Ranking is the same (the square root keeps order), and it is cheaper.
The useful identity. For unit-length vectors u and v: ‖u − v‖² = 2 − 2·cos(u, v). So on normalized embeddings, ranking by smallest Euclidean distance gives exactly the same order as ranking by highest cosine similarity. code/rag_vector_math.py checks this in its tests.
Code example
From code/rag_vector_math.py:
import math
def euclidean_distance(a, b):
if len(a) != len(b):
raise ValueError("vectors must have the same length")
return math.sqrt(sum((x - y) ** 2 for x, y in zip(a, b)))
def normalize(a):
n = math.sqrt(sum(x * x for x in a))
return [x / n for x in a]
print(euclidean_distance([0, 0], [3, 4])) # 5.0
print(euclidean_distance([1, 2, 3], [2, 4, 6])) # 3.741...
u, v = normalize([1, 2, 3]), normalize([2, 4, 6])
print(euclidean_distance(u, v)) # 0.0: same point after normalizing
In real projects (FAISS, pip install faiss-cpu numpy):
import faiss
import numpy as np
xb = np.random.rand(10_000, 128).astype("float32") # stored vectors
xq = np.random.rand(5, 128).astype("float32") # queries
index = faiss.IndexFlatL2(128) # exact search, squared L2
index.add(xb)
distances, ids = index.search(xq, 4) # 4 nearest per query
When to use it / when not to
- Use it when the model card recommends L2, or when absolute position matters (for example, some image or clustering tasks).
- On normalized embeddings, it ranks like cosine, so pick whichever your database computes fastest.
- On unnormalized vectors, length differences dominate; prefer cosine for text unless told otherwise.
- IVF indexes use k-means, which is based on Euclidean distance.
| Metric | Higher or lower is better? | Sensitive to length? |
|---|---|---|
| Cosine similarity | Higher | No |
| Dot product | Higher | Yes |
| Euclidean (L2) | Lower | Yes |
Common mistakes
- Sorting distances in descending order as if they were similarities.
- Comparing raw L2 scores across different models or collections.
- Forgetting that FAISS
IndexFlatL2returns squared distances. - Using L2 on unnormalized text embeddings and getting results biased by vector length.
Recap
If you remember only three things:
- Euclidean (L2) distance is the straight-line distance between two vectors; smaller means more similar.
- Unlike cosine it is affected by vector length, but on normalized vectors it ranks results the same as cosine.
- Sort distances ascending, and remember many libraries return squared distance.
Interview questions
1. How do Euclidean distance and cosine similarity differ? <details><summary>Answer</summary> Euclidean measures straight-line distance and depends on vector length; cosine measures only the angle. Two vectors in the same direction but with different lengths have cosine 1 but a non-zero Euclidean distance. </details>
2. When do they produce the same ranking? <details><summary>Answer</summary> When all vectors are normalized to length 1, because then squared Euclidean distance equals 2 − 2·cosine, a decreasing function of cosine. </details>
3. Why do libraries often use squared L2? <details><summary>Answer</summary> The square root does not change the order of results and costs extra compute, so skipping it is faster with the same ranking. </details>
Go deeper
- FAISS docs (IndexFlatL2, metric types) — https://faiss.ai/
- Johnson et al., Billion-scale similarity search with GPUs (FAISS) — https://arxiv.org/abs/1702.08734
Prev: Cosine Similarity · Next: Dot Product · Back to phase: Phase 1