esc
  • WorkflowsPages
  • Case studiesPages
  • Machine Learning FoundationsLearn
  • AI EngineeringLearn
  • AI Guardrails & Red TeamingLearn
  • UI/UXLearn
  • Design SystemsLearn
  • Production System DesignLearn
  • AI papersPages
  • Interview KitPages
  • Book a callContact
  • Download ResumeContact
Let’s talk

I’ll build the agent for it.

Cosine vs Dot Product vs Euclidean Distance?

Cosine, dot product and Euclidean distance compared: what each one measures, when they agree, and which to pick, for your AI interview.

In short

Cosine looks only at the angle between two vectors. Dot product mixes angle and length, and Euclidean distance measures the straight-line gap. OpenAI and Azure both point to cosine for their own models, and all three give the same ranking when vectors are normalised to length 1.

11 min readIntermediateVery common · estimateFacts checked 26 November 2026

Say it out loud

Read it once, then say it without looking. Start with the short one; use the long one when they ask you to go deeper.

Cosine, dot product and Euclidean distance all compare two vectors, which are lists of numbers that stand for text. Cosine looks only at the angle between them, so length does not matter. Dot product mixes angle and length, so a long chunk can outscore a better match. Euclidean distance measures the straight-line gap, where smaller means closer. I start with cosine, and when vectors are normalised to length 1, all three rank the same.

What are cosine, dot product and Euclidean distance, in plain words?

A search over text works with vectors. Each chunk of a document (a short piece of it, such as one FAQ answer) and each question becomes a vector, a list of numbers. A vector has a length, which is how big its numbers are overall, and a direction, which is where the list points. The search compares the question's vector with every chunk's vector and sorts the chunks by score.

Think of a new hire using a staff handbook. Two pages both cover leave. Cosine asks whether they cover the same topic. Dot product also counts how long each page is. Euclidean asks how far apart the two pages sit in meaning. A customer who asks how long they have to dispute a card charge needs the help page that answers it, and the measure decides which page comes first.

Key words

Chunk
A short piece of a document, such as one FAQ answer or one policy clause, that the search compares.
Vector
A list of numbers that stands for a piece of text. Google Cloud's vector search docs say it has magnitude and direction.
Embedding
A vector made by an embedding model, so that text with similar meaning gets similar numbers.
Magnitude
The length of a vector: how big its numbers are overall.
Normalised
Scaled so the length is exactly 1. Only the direction is left.

How does each measure work?

Cosine gives a score from the angle between two vectors. Vectors pointing the same way give 1, opposite ways give minus 1, and right angles give 0. The length of each vector is ignored, so only the angle counts.

  • Cosine

    Angle only

    Compares direction and ignores length. Higher means more similar, from minus 1 to plus 1.

  • Dot product

    Angle and length

    Multiplies matching numbers and adds them up. Long vectors score high, so length can decide the order.

  • Euclidean

    Straight-line gap

    Measures how far apart the two points are. Smaller means closer, so sort in the opposite direction from cosine.

Here is one question scored three ways, in a made-up bank example. The numbers are small, but the pattern is the one you will meet at work.

One question, three scores

The question

How long to dispute a card charge?

Embedding model

same model for question and chunks

Three measures

Cosine

angle only, length ignored

Dot product

length can lift a long chunk

Euclidean

gap between points, lower wins

Top result by each measure

FAQ: 60-day rule

points the same way as the question

Card terms, section 7

long page wins on length

  1. 1The question is turned into a list of numbers.
  2. 2Its numbers are compared with each chunk's cosine score.
  3. 3The same numbers also get a dot product score.
  4. 4And a Euclidean distance to each chunk.
  5. 5Cosine ranks the 60-day FAQ first.
  6. 6Dot product ranks the long card terms first.
  7. 7Euclidean also picks the FAQ, the closest point.
One question is scored against three help-page chunks. Cosine and Euclidean both put the 60-day FAQ first, while dot product puts the long card-terms page first because it is long. The numbers are made up.

Read the scores. Cosine ranks the FAQ first. Dot product ranks the long card terms first, at 3.4 against 0.49. Euclidean also picks the FAQ, then the fee table, and the card terms come last, because the card terms sit furthest away.

Python
# A bank's card-dispute assistant scores three chunks of its help text against one question.
# Made-up numbers: real embeddings have hundreds or thousands of values (OpenAI's text-embedding-3-small uses 1536), not three.
import math

query = [0.9, 0.1, 0.4]
chunks = {
    "FAQ: dispute within 60 days": [0.45, 0.05, 0.2],
    "Card terms, section 7": [3.0, 0.6, 1.6],
    "Wire fee table": [0.1, 0.9, 0.2],
}

def dot(a, b):
    return sum(x * y for x, y in zip(a, b))

def cosine(a, b):
    return dot(a, b) / (math.sqrt(dot(a, a)) * math.sqrt(dot(b, b)))

def euclidean(a, b):
    return math.sqrt(sum((x - y) ** 2 for x, y in zip(a, b)))

for name, vec in chunks.items():
    print(
        f"{name}: cosine {cosine(query, vec):.3f}, "
        f"dot {dot(query, vec):.3f}, euclidean {euclidean(query, vec):.3f}"
    )
print("best by cosine (higher is better):", max(chunks, key=lambda n: cosine(query, chunks[n])))
print("best by dot product (higher is better):", max(chunks, key=lambda n: dot(query, chunks[n])))
print("closest by Euclidean (lower is better):", min(chunks, key=lambda n: euclidean(query, chunks[n])))

Output

FAQ: dispute within 60 days: cosine 1.000, dot 0.490, euclidean 0.495
Card terms, section 7: cosine 0.995, dot 3.400, euclidean 2.470
Wire fee table: cosine 0.283, dot 0.260, euclidean 1.149
best by cosine (higher is better): FAQ: dispute within 60 days
best by dot product (higher is better): Card terms, section 7
closest by Euclidean (lower is better): FAQ: dispute within 60 days

Tip

Python's max() picks the highest score and min() picks the lowest. Euclidean needs min(), because a small distance is the good result. Mixing the two up is a common bug, and it returns the worst chunks first.

What changes when the vectors are normalised?

Normalising scales every vector to length 1, so only direction is left. Then dot product gives the same ranking as cosine, and Euclidean distance also ranks the results the same way. Google Cloud's index settings say the same thing: normalise, then dot product matches cosine.

OpenAI's embedding guide says its own vectors are normalised to length 1, and that on vectors like that, cosine can be computed with a plain dot product, which is a little faster. It adds that cosine and Euclidean give identical rankings.

Tip

Before you choose, check whether your vendor already normalises its vectors, and make sure every vector in the index is treated the same way.

Which one should you pick?

OpenAI and Azure name cosine in their own guides. Google names a different setup. Here is what the three vendors say:

  • “We recommend cosine similarity. The choice of distance function typically doesn't matter much.”

    OpenAI

  • “Azure OpenAI embedding models use cosine similarity, so if you're using Azure OpenAI embedding models, cosine is the recommended metric.”

    Azure AI Search

  • “We strongly suggest using DOT_PRODUCT_DISTANCE + UNIT_L2_NORM instead of the COSINE distance.”

    Google Cloud

Google's guide leans the other way: dot product with unit-length vectors, which ranks the same as cosine. Azure's index setting says cosine for its OpenAI models, and for anything else, the metric the embedding model was built for.

Picking the metric

Embedding model

its guide may name a metric

Guide names one?

check the vendor's docs first

Use the named one

the vendor's own pick

Start with cosine

the usual default, length ignored

Test on real questions

keep the one that finds the answer

  1. 1Your team asks the model's guide which metric it uses.
  2. 2If the guide names one, that metric is the first choice.
  3. 3If it names none, cosine is the starting point.
  4. 4The named metric goes to the testing step.
  5. 5The cosine default goes to the same testing step.
A short path for picking a metric. Start with what the model's guide says, then test every choice on real questions before you trust it.
  1. 1

    Read the model's guide

    Look for the metric the vendor names. Azure points to cosine for its OpenAI-based models.

  2. 2

    Match the index setting

    The search index must use the metric the vectors were made for, or the scores mean something else.

  3. 3

    Test on real questions

    Write questions with known right answers, and keep the metric that puts those answers on top.

How would you pick the metric for a bank's help search?

A mid-size bank has 40,000 help pages, and its search keeps returning long policy pages first. I would start with the embedding model's guide and the index settings, then check that they agree. Next I would write about 200 real staff questions, each with the page that answers it, and score every metric against that list.

Tip

Decide the metric before the index is filled, so you do not have to redo the work on 40,000 pages.

What does the interviewer want to hear?

A strong answer covers

  • Vectors are lists of numbers, with a length and a direction.
  • Cosine ignores length. Dot product and Euclidean distance do not.
  • Euclidean: smaller is closer. Cosine and dot product: bigger is closer.
  • With vectors of length 1, all three give the same ranking.
  • Pick the metric the model's guide names, then test on real questions.

Common traps

  • Trap"Cosine is always the best measure."

    BetterIt is the usual default. Check the model's guide and test on your own questions.

  • Trap"A bigger score means closer for every measure."

    BetterFor Euclidean, smaller is closer. Sort the other way, or the nearest chunks end up last.

  • Trap"Dot product is the same as cosine."

    BetterOnly when both vectors have length 1. Otherwise long vectors win on dot product.

Can you spot the mistake?

Spot the bug

Which team setup has a bug?

Quick check

A bank's help search keeps returning a long policy page first, and its vectors are not normalised. What is the most likely cause and fix?

Can you answer these from memory?

Flash cards · tap a card to flip it

1 / 6

What will they ask next?

  1. Q111What is RAG, and why use it?13 min
  2. Q116How do you choose an embedding model?12 min
  3. Q117What is a vector database?12 min
  4. Q119What is approximate nearest-neighbour search?10 min
  5. Q120How does HNSW work?11 min

Learn it in depth

Last updated on 26 November 2026

Building a search over your company's documents?

I design and build AI products and agents, including the search behind them and the screens people use. Book a short call and tell me what your documents hold and who will search them.