Back to Journal
Published Updated •6 min read

Vector Embeddings: What a Similarity Score Actually Tells You

Why can different vectors have a cosine similarity of 1? Start with two arrows, work through the math, and see what it means for search.

Vector DatabasesRetrieval-Augmented Generation (RAG)LLM Agents
Vector Embeddings: What a Similarity Score Actually Tells You

Why can two vectors have a cosine similarity of 1 and still be different?

Take [3, 4] and [6, 8]. Draw them as arrows from the origin: the second is twice as long, but both point in exactly the same direction. Cosine sees that shared direction and gives them a perfect score. It ignores the difference in length.

That small example is a useful way into vector search. Before choosing a similarity function, it helps to know which differences you want it to notice.

From text to coordinates

An embedding model turns text into a list of numbers. A search query and a document can then be compared numerically, even when they use different words. A query about a car might be close to a document about an automobile if the model has learned that relationship.

Think of those numbers as coordinates on a learned map, rather than a list of named attributes. There usually isn't a single coordinate for “vehicle.” Meaning is spread across the vector, and nearby points suggest a relationship—not proof that two statements are equivalent or true.

The map matters as much as the coordinates. Two models can produce lists of the same length without sharing a coordinate system. Keep the model space, revision, dimensions, and prescribed query/document preprocessing compatible. Some models let you choose the number of dimensions; more dimensions alone do not guarantee better search.

Two arrows, four different questions

Using our two arrows, we can ask four different questions:

  • How long is each arrow? The L2 norms are 5 and 10.
  • What is their dot product? Multiply matching coordinates and add: 3 × 6 + 4 × 8 = 50. Length affects this score.
  • How far apart are their tips? The Euclidean distance is 5.
  • Do they point the same way? Their cosine similarity is 1.

None of these answers contradicts the others. Each measures something different. The same calculations extend to vectors with hundreds of coordinates:

∥a∥2=∑i=1nai2\|\mathbf a\|_2 = \sqrt{\sum_{i=1}^{n}a_i^2}

a⋅b=∑i=1naibi\mathbf a\cdot\mathbf b = \sum_{i=1}^{n}a_i b_i

d2(a,b)=∑i=1n(ai−bi)2d_2(\mathbf a,\mathbf b) = \sqrt{\sum_{i=1}^{n}(a_i-b_i)^2}

cos⁡(a,b)=a⋅b∥a∥2∥b∥2\operatorname{cos}(\mathbf a,\mathbf b) = \frac{\mathbf a\cdot\mathbf b}{\|\mathbf a\|_2\|\mathbf b\|_2}

Cosine is undefined if either vector is zero. Its range is [-1, 1]; larger means a smaller angle, not guaranteed relevance. Cosine distance is commonly 1 minus cosine. Smaller distances rank first, while larger similarity scores rank first.

Modern dense embedding norms do not generally measure document length or word count. Some providers already return unit vectors. Magnitude may also be meaningful for a model trained with dot-product scoring; normalization can discard that signal. Follow the model's recommended scoring method rather than declaring cosine universally best.

Give both arrows the same length

Now divide each arrow by its own length. [3, 4] / 5 and [6, 8] / 10 both become [0.6, 0.8]. This is normalization: keep the direction, set the length to 1.

Once both lengths are 1, the denominator in cosine's formula is also 1. Cosine becomes a dot product, and squared Euclidean distance is directly related to it:

a^=a∥a∥2,cos⁡(a,b)=a^⋅b^\hat{\mathbf a} = \frac{\mathbf a}{\|\mathbf a\|_2},\qquad \operatorname{cos}(\mathbf a,\mathbf b)=\hat{\mathbf a}\cdot\hat{\mathbf b}

∥a^−b^∥22=2−2(a^⋅b^)\|\hat{\mathbf a}-\hat{\mathbf b}\|_2^2=2-2(\hat{\mathbf a}\cdot\hat{\mathbf b})

Thus cosine, dot product, and Euclidean distance produce equivalent exact rankings on those unit vectors, allowing for ties and floating-point rounding. This is a geometric identity, not a guarantee of better retrieval quality. It does not mean every database normalizes all data or that query normalization is free. OpenAI documents unit-length outputs for its embeddings; other models need their own contract checked.

Try it in TypeScript

Store the first example as similarity.ts. Scaling before summing squares avoids overflow and underflow from squaring extreme coordinates. Invalid dimensions, empty vectors, nonfinite coordinates, and zero vectors throw instead of returning a plausible score.

TYPESCRIPT
export function normalize(vector: readonly number[]): number[] {
  if (vector.length === 0) throw new RangeError("A vector must not be empty.");

  let scale = 0;
  for (const value of vector) {
    if (!Number.isFinite(value)) throw new TypeError("Coordinates must be finite.");
    scale = Math.max(scale, Math.abs(value));
  }
  if (scale === 0) throw new RangeError("A zero vector has no direction.");

  const scaled = vector.map(value => value / scale);
  const norm = Math.sqrt(scaled.reduce((sum, value) => sum + value * value, 0));
  return scaled.map(value => value / norm);
}

export function cosine(a: readonly number[], b: readonly number[]): number {
  if (a.length !== b.length) throw new RangeError("Vector dimensions must match.");
  const left = normalize(a);
  const right = normalize(b);
  const score = left.reduce((sum, value, index) => sum + value * right[index], 0);
  return Math.max(-1, Math.min(1, score));
}

Let's run the two-arrow example. These are hand-picked numbers to make the geometry visible, not embeddings from a language model:

TYPESCRIPT
import { cosine, normalize } from "./similarity";

const a = [3, 4];
const b = [6, 8];
console.log({
  cosine: cosine(a, b),
  normalized: normalize(a),
});
// { cosine: 1, normalized: [0.6, 0.8] }

The output brings us back to the opening question: different lengths, same direction. Normalization turns both arrows into the same unit vector. That explains the score without asking us to imagine a thousand-dimensional space.

A good score is not yet a good search result

pgvector exposes separate L2, cosine-distance, and negative-inner-product operators. Match the query operator to the index operator class. Its default exact search differs from HNSW or IVFFlat approximate search, which trades recall for speed; filters and index settings affect results. Do not assume changing metrics preserves an approximate index's returned neighbors.

When choosing a scoring method, start with the embedding model's recommendation. Then check whether the search retrieves useful documents. Lexical search already does more than literal matching, so a hybrid approach can be worth testing too.

Use labeled queries to evaluate recall@k or ranking quality alongside latency, memory, filtering, and reranking cost. A high similarity score is not permission to read a document: authorization still needs its own checks, and embeddings can contain sensitive information.

The distinction to keep is simple: the math tells you how vectors compare. Your search evaluation tells you whether that comparison helps a reader find what they need.

Primary sources

Share this article