Embedding Models and Similarity

Compare types of embedding models, learn cosine similarity, dot product and Euclidean distance, and choose and evaluate a model for your own data.

20 minIntermediateCode Examples
The Core Idea: An embedding model decides what your whole retrieval system can notice, and a similarity metric decides how closeness is measured. Choosing the wrong model, or pairing a model with the wrong metric, quietly lowers quality everywhere downstream. This lesson shows the main model types, the three standard similarity measures, and a practical way to choose with evidence instead of hype.

1. Beginner: Types of Embedding Models

Embedding models are neural networks trained to turn text into vectors. They differ in what they were trained for, which languages they cover and how you run them.
General-purpose text
Trained on broad web and document text. A good default for English knowledge bases.
Multilingual
Place texts from many languages in one space, so a question in one language can find a passage in another.
Domain-specific
Tuned for areas such as code, legal, finance or biomedical text, where ordinary models miss the jargon.
Hosting choiceHosted APISelf-hosted open model
Setup effortVery lowHigher, you run the model
Data leaves your systemsYes, sent to a providerNo, stays with you
Cost shapePay per token processedPay for hardware and operations
Control and tuningLimitedFull, including fine-tuning

2. Intermediate: Cosine, Dot Product and Euclidean Distance

Three measures are used to compare two vectors aa and bb with dd dimensions.
a⋅b=∑i=1daibicos⁡θ=a⋅b∥a∥ ∥b∥∥a−b∥=∑i=1d(ai−bi)2a \cdot b = \sum_{i=1}^{d} a_i b_i \qquad \cos\theta = \dfrac{a \cdot b}{\lVert a \rVert \, \lVert b \rVert} \qquad \lVert a - b \rVert = \sqrt{\sum_{i=1}^{d} (a_i - b_i)^2}
Dot product
Grows with both direction match and vector length. A long vector can score high even if its direction is only roughly right.
Cosine similarity
Looks only at direction. Vector length is ignored, so it compares meaning rather than size.
Euclidean distance
The straight-line distance between the two points. Smaller means more similar, the opposite direction to the other two.
Beware: dot product and cosine are similarities where bigger is better, while Euclidean is a distance where smaller is better. Mixing up the direction of a metric silently returns the worst results first.

3. Intermediate: Normalization and Which Metric to Use

A vector is normalized when its length is scaled to exactly 1. For normalized vectors the three measures agree on the ranking, because dot product equals cosine and the squared Euclidean distance becomes 2−2cos⁡θ2 - 2\cos\theta. Many modern embedding models already return normalized vectors, which makes the metric choice less risky.
Rule 1. Use the metric the model was trained with. The model documentation says which.
Rule 2. Make the vector database use the same metric. A mismatch is a common silent bug.
Rule 3. If you normalize at indexing time, normalize query vectors the same way.
Rule 4. When unsure, cosine similarity on normalized vectors is a safe default.

4. Intermediate: Choosing an Embedding Model

Public leaderboards such as the MTEB benchmark are a useful way to build a shortlist, but they average over many tasks and datasets that are not yours. A model that tops a leaderboard can still do poorly on your jargon or your document types.
CriterionQuestion to ask
Quality on your dataDoes the right chunk appear in the top results for your real questions?
Input lengthHow much text can it embed at once before truncating?
LanguagesDoes it cover every language in your documents and your users?
Dimension and sizeHow much storage and search time does it need?
Cost and speedWhat does embedding the full corpus cost, and what is the query latency?
Hosting and licenseCan the data leave your systems, and may you use the model commercially?
The reliable method: write 30 to 100 real questions with known answer chunks, run each candidate model, and compare how often the right chunk appears in the top results. Pick the cheapest model that is good enough.

5. Advanced: Storage, Speed and the Cost of Switching

Embedding choices have production consequences. Some models are trained so that you can keep only the first part of each vector, or you can store numbers with fewer bits (quantization). Both cut memory and search time at the price of a small drop in quality that you should measure.
The most important consequence is switching cost. Vectors from two models cannot be mixed, so changing the model means re-embedding every chunk. Plan for this from day one:
Record the model name, version and settings as metadata for every index.
Build the new index next to the old one instead of editing it in place.
Compare retrieval quality on your test questions with both indexes.
Switch traffic to the new index and keep the old one briefly in case you need to roll back.

6. Visual Explanation: Three Metrics, Three Different Winners

Panel A compares the metrics. Panel B shows how they disagree on the same data when vectors have different lengths, using the numbers from the code below. Panel C is a model-choice explorer.
Panel A · The Three MetricsDirection, size and distance
Dot product
Bigger is better
Direction and length
↓
Favors long vectors
↓
Fast to compute
Cosine
Bigger is better
Direction only
↓
Ignores length
↓
Safe default
Euclidean
Smaller is better
Straight-line distance
↓
Sensitive to length
↓
Mind the direction
Panel B · Same Data, Different WinnerQuery is the vector 1, 1
DocumentVectorDotCosineDistance
A, same direction, long4, 48.01.00 best4.24
B, short, nearly same direction1, 0.81.80.990.20 best
C, other direction, very long9, 110.0 best0.788.00
Three metrics pick three different documents until the vectors are normalized
Panel C · Model Choice ExplorerClick a situation to open it
Click to see why test questions beat leaderboards▼
A leaderboard averages many tasks. Your questions, vocabulary and documents are specific, so only a test on your own data tells you which model will actually help your users.
How to read it: Panel A defines the metrics, Panel B proves they can disagree, and Panel C helps you shortlist models by situation.

7. Python Implementation: Comparing the Three Metrics

The program defines the three measures from scratch and compares one query against three documents with very different vector lengths. First the raw vectors are ranked by each metric, then the vectors are normalized to length 1 and the ranking is repeated. The last column checks the identity that squared distance equals 2 minus 2 cosine for unit vectors.
similarity_metrics.pyPython 3.10+ · Standard library only
import math def dot(a, b):    return sum(x * y for x, y in zip(a, b)) def norm(a):    return math.sqrt(dot(a, a)) def cosine(a, b):    return dot(a, b) / (norm(a) * norm(b)) def euclid(a, b):    return math.sqrt(sum((x - y) * (x - y) for x, y in zip(a, b))) def normalize(a):    n = norm(a)    return [x / n for x in a] query = [1.0, 1.0]docs = [    ("A same direction, long", [4.0, 4.0]),    ("B short, nearly same direction", [1.0, 0.8]),    ("C other direction, very long", [9.0, 1.0]),] print("Raw vectors")for name, v in docs:    print(" ", name.ljust(32), "dot", round(dot(query, v), 2), "| cosine", round(cosine(query, v), 2), "| distance", round(euclid(query, v), 2)) best_dot = max(docs, key=lambda d: dot(query, d[1]))[0]best_cos = max(docs, key=lambda d: cosine(query, d[1]))[0]best_dist = min(docs, key=lambda d: euclid(query, d[1]))[0]print("Winner by dot:     ", best_dot)print("Winner by cosine:  ", best_cos)print("Winner by distance:", best_dist) print("")print("After normalizing every vector")nq = normalize(query)unit_docs = [(name, normalize(v)) for name, v in docs]for name, v in unit_docs:    print(" ", name.ljust(32), "dot", round(dot(nq, v), 3), "| cosine", round(cosine(nq, v), 3), "| 2 - 2cos", round(2 - 2 * cosine(nq, v), 3), "| distance squared", round(euclid(nq, v) * euclid(nq, v), 3))print("Winner by dot:     ", max(unit_docs, key=lambda d: dot(nq, d[1]))[0])print("Winner by distance:", min(unit_docs, key=lambda d: euclid(nq, d[1]))[0])
Expected output
Raw vectors  A same direction, long           dot 8.0 | cosine 1.0 | distance 4.24  B short, nearly same direction   dot 1.8 | cosine 0.99 | distance 0.2  C other direction, very long     dot 10.0 | cosine 0.78 | distance 8.0Winner by dot:      C other direction, very longWinner by cosine:   A same direction, longWinner by distance: B short, nearly same direction After normalizing every vector  A same direction, long           dot 1.0 | cosine 1.0 | 2 - 2cos 0.0 | distance squared 0.0  B short, nearly same direction   dot 0.994 | cosine 0.994 | 2 - 2cos 0.012 | distance squared 0.012  C other direction, very long     dot 0.781 | cosine 0.781 | 2 - 2cos 0.438 | distance squared 0.438Winner by dot:      A same direction, longWinner by distance: A same direction, long
With raw vectors the three metrics choose three different documents: the long vector wins by dot product, the same-direction vector wins by cosine, and the short nearby vector wins by distance. After normalization the metrics agree. This is why you must know which metric your model and your vector database use, and keep them aligned.

Knowledge Check

Q1. Which similarity measure ignores vector length?
Q2. Your vector database uses Euclidean distance but the model was trained for cosine. What is the best action?
Q3. Why is switching embedding models expensive?

Key Points

✓
Embedding models differ in training domain, languages, size and hosting, and each choice has a trade-off.
✓
Dot product mixes direction and length, cosine uses direction only, and Euclidean measures distance where smaller is better.
✓
For normalized vectors the three metrics give the same ranking.
✓
Use the metric the model was trained with and configure the vector database to match.
✓
Choose a model by testing candidates on real questions, using leaderboards only as a shortlist.
✓
Changing models means re-embedding everything, so record the model name with every index and migrate side by side.
✓
Shorter vectors and quantization save memory but should be measured for quality loss.

The Big Picture

Model and metric are two settings that look small but touch every query. Treat them as part of your index definition: choose them with evidence, write them down, and keep indexing and querying consistent.
Pick a model on your data → Match the metric → Normalize consistently → Record the version
Up Next: Vector Databases and Indexing. You will learn how vectors are stored and searched at scale, why exact search becomes too slow, and how approximate nearest neighbor indexes such as the ones in FAISS, Chroma, Pinecone, Weaviate and Milvus work.