presentsUNAIVERSE
The UN AI timeline

Learn AI /Search, Embeddings and RAG /Cosine similarity

Cosine similarity

Detail

In five seconds

What it is

A measure of how closely two non-zero vectors point in the same direction, commonly used to compare embeddings in search.

In plain words

It is a way to check whether two number patterns point in a similar direction. Search systems use it to rank text embeddings, even when the wording is different.

An everyday example

Imagine arrows drawn on transparent sheets. One arrow is short and another is long, but if both point north-east, they align exactly when the sheets are placed together. Cosine similarity treats them as maximally similar because it compares direction rather than length. An arrow pointing at a right angle has a score of zero, while one pointing the opposite way has a negative score. The comparison says how closely the headings align, not whether either arrow leads to the right destination.

Where the story stops working

A real checklist here uses simple yes-or-no items, while actual embedding vectors usually contain many continuous numbers, not a few binary checks. Also, cosine similarity is a geometric calculation on numerical coordinates, not a person visually matching forms. In some systems, vector length can still matter elsewhere, even if cosine similarity itself focuses on direction.

Remember thisCosine similarity ranks non-zero vectors by how closely their directions align, largely ignoring magnitude. In search, it is a useful retrieval signal, not proof that a passage is correct or fit for use.

Understand it

Understand it

Why it matters

If you work with search, retrieval or RAG, cosine similarity affects which passages are brought forward as evidence. That matters because the top-ranked passage can shape the answer a system produces, the citation it shows, and the errors it makes. A non-specialist does not need to derive the formula, but should know what signal the score captures: directional similarity between vectors, not truth, not exact wording, and not always relevance in context. This helps when choosing tools, interpreting rankings, and deciding when reranking or human review is needed.

Same direction versus same size

Show why cosine similarity cares about angle between vectors more than length, and how that affects search ranking.

  1. 01

    Two vectors enter

    Display two arrows from the same origin. Give each arrow numeric coordinates and a visible angle between them.

    In SVG, draw x and y axes, then two coloured arrows from the origin. Show short labels such as A=(2,4) and B=(1,2). Place a small angle arc between them.

  2. 02

    Angle drives the score

    Show that a smaller angle means a higher cosine similarity, even if one arrow is longer.

    Add a second pair of arrows with the same direction but different lengths, and label them with a cosine score near 1. Add another pair at about 90 degrees and label score 0.

  3. 03

    Magnitude is not the main signal

    Contrast equal-length vectors with different directions against different-length vectors with the same direction.

    Use side-by-side mini-panels. In one, equal-length arrows diverge and get a lower score. In the other, one arrow is twice as long but aligned and gets a higher score.

  4. 04

    From geometry to retrieval

    Apply the same idea to a query embedding and three document embeddings. Rank the documents by cosine score.

    Show one query arrow and three document arrows or cards. Connect each to a score badge such as 0.92, 0.71 and 0.18, then stack document cards in ranked order.

A two-panel comparison. The left panel shows pairs of arrows from a shared origin with different angles and lengths. The right panel shows a query vector compared with three document vectors, each labelled with a cosine score and ranked from highest to lowest. Read left to right: first understand direction, then see how that produces a search ranking.

Hover or tap a vector pair to highlight the angle and update the displayed cosine score; a toggle switches between geometric view and ranked retrieval view.

Describe this without the diagram

This diagram explains cosine similarity in two linked parts. First, it shows arrows called vectors starting from the same point. Vectors that point in nearly the same direction get a high score, even if one is longer. Vectors at a right angle get a score near zero. Second, it applies this to search: one query vector is compared with several document vectors, and the documents are ranked by cosine score. The main lesson is that the score mostly reflects direction, not size.

With reduced motion: A static two-panel image replaces animation. Panel one shows three labelled vector pairs with fixed cosine scores: same direction and different lengths gives a high score, different directions gives a lower score, and right angle gives zero. Panel two shows a query and three documents with fixed scores and a final ranked list.

Cosine similarity compares two non-zero vectors, which are lists of numbers used to represent items such as words, sentences or documents. It asks a directional question: do these two lists point in a similar direction in vector space? The output is a score from -1 to 1, where higher values mean the directions are more alike.

What goes in are two non-zero vectors of the same length. The calculation uses the cosine of the angle between them. If the angle is small, the cosine is close to 1, so the vectors are treated as similar. If the vectors are at right angles, the score is 0, meaning no directional similarity. Negative values mean the directions are more than 90 degrees apart, though many embedding systems mostly produce comparisons in the 0 to 1 range in practice.

What happens is important: cosine similarity ignores overall magnitude and focuses on direction. Vectors of different lengths can still score highly if their coordinates have similar proportions. That makes it useful when you care more about the type of meaning than about raw magnitude.

What comes out is a ranking signal. In semantic search or retrieval-augmented generation, a system converts a user query and many stored text chunks into vectors, computes cosine similarity between the query vector and each stored vector, and returns the highest-scoring chunks. Those retrieved passages can then be shown to a person or passed to a model as source material.

This term is often confused with semantic similarity itself. Semantic similarity is the broader idea of likeness in meaning. Cosine similarity is one mathematical way to estimate that likeness once text has already been turned into vectors.

How “Comparing the direction of arrows” maps onto the real thing

In the storyIn the AI system
the four completeness checksthe dimensions of a vector
whether two applications line up on the same checkshow closely two vectors point in the same direction
ignoring the thickness of the folderdownplaying vector magnitude and focusing on direction
grouping applications with similar checklist patternsranking stored text embeddings by cosine similarity to a query embedding

A worked example

The gender adviser checks how the internal search ranks three candidate passages for the question used in the talking points.

Starting from: Question embedding q = [0.8, 0.6]. Passage embeddings: P1 = [0.4, 0.3], P2 = [0.6, -0.8], P3 = [0.7, 0.7].

  1. Calculate the cosine similarity between q and each passage vector by dividing their dot product by the product of their lengths.
  2. P1 points in exactly the same direction as q. Its smaller magnitude does not affect the score, so its cosine similarity is 1.00.
  3. P2 is perpendicular to q: their dot product is zero, giving a cosine similarity of 0.00.
  4. P3 points in a slightly different direction from q, giving a cosine similarity of about 0.99.
  5. Rank the passages by score: P1 first, P3 second and P2 third.

Result: P1 ranks above P3 because its direction matches the question vector exactly, despite being shorter. The ranking identifies candidate passages for review, not which paragraph belongs in the cleared talking points.

In a United Nations workplace

A gender adviser in a regional bureau is finalising talking points for informals before close of business. In an approved internal search tool, she looks for paragraphs in cleared statements for delivery and background notes that are semantically close to a newly received question.

The tool compares the question embedding with embeddings of the stored paragraphs. Cosine similarity helps rank passages pointing in a similar direction, regardless of vector length, so the adviser can review the most likely matches before sending the talking points for clearance.

Before you try thisThe search corpus should contain only material approved for that system. Personal or protection-sensitive information must not be entered into an unapproved tool. A high cosine score indicates vector similarity, not that a paragraph is current, factually applicable or suitable as agreed language, so the underlying document and context still require review.

Where you may hear it

  • During a vendor demo for an internal search tool
  • In an ICT governance board review of embedding retrieval
  • During a procurement unit's technical evaluation of vector search
  • While testing retrieval before a knowledge portal is opened to staff

Avoid confusion

Avoid confusion

Often confused with

Common misconceptions

  • Common misconception: A higher cosine similarity means two passages say the same thing.

    Correction: Not necessarily. It means their vectors point in a similar direction under a particular embedding model. Passages can be topically close but still differ in detail, scope, time period, sentiment or factual accuracy. That is why retrieval systems often add metadata filters, reranking and human checking.

  • Common misconception: Cosine similarity measures word overlap.

    Correction: It usually does not. In embedding-based systems, it compares numerical representations of text, so two passages can score highly even if they share few exact words. That is the point of semantic retrieval, and it is also why results can sometimes feel surprising.

  • Common misconception: If cosine similarity ignores magnitude, magnitude never matters in retrieval.

    Correction: Cosine similarity itself downplays vector length, but the overall system may still be affected by how embeddings are normalised, indexed and compared. Some systems use dot product or Euclidean distance instead, and these can respond differently to vector magnitude.

Quick check

Why can two vectors with very different lengths still have a high cosine similarity?

Go deeper

Go deeper

The simple version

It is a way to check whether two number patterns point in a similar direction. Search systems use it to rank text embeddings, even when the wording is different.

The technical version

Given two non-zero vectors, cosine similarity is their dot product divided by the product of their lengths: cos(theta) = (a·b) / (||a|| ||b||). This normalises for magnitude and keeps the comparison focused on angle. In embedding retrieval, a query embedding is compared against document or chunk embeddings, often after normalisation. If vectors are unit-normalised, cosine similarity and dot product give the same ranking. In practice, systems use it as a fast similarity signal for nearest-neighbour search, then may apply filtering, reranking or task-specific scoring. It is a measure of vector orientation, not a direct measure of factual correctness, entailment or usefulness for a specific question.

Reviewed external resources

  • EncyclopaediaCosine similarity (opens in a new tab)Wikipedia

    This Wikipedia link gives a broad reference-style explanation of the mathematical idea and suits readers who want definitions, formulae and background.

    Intermediatelink checked 2026-07-31

  • CourseEmbeddings (opens in a new tab)Google

    This Google course link gives a practical introduction to embeddings and suits readers who want to see where cosine similarity is used in modern AI systems.

    Intermediatelink checked 2026-07-31

  • InteractiveEmbedding Projector (opens in a new tab)TensorFlow

    This TensorFlow interactive lets readers explore embeddings visually and suits people who learn best by manipulating points and seeing relationships change.

    Starterlink checked 2026-07-31

  • VideoVideo explanations: Cosine similarity (opens in a new tab)YouTube search

    This YouTube search results page helps readers find video explanations from different creators and suits those who prefer to compare teaching styles before choosing one.

    search results, not one pagelink checked 2026-07-31

  • DiscussionCommunity discussion: Cosine similarity (opens in a new tab)Reddit search

    This Reddit search results page helps readers see community questions and explanations on the topic and suits readers who want informal discussion, with careful fact-checking of claims.

    search results, not one pagelink checked 2026-07-31

Where this came from

Drafted with a language model through an offline pipeline, checked against this site's schema, and every external link above fetched to see whether it answers. Treat it as a starting point and check anything that matters against the primary sources. How this site is built and what that means.

The one thing to remember

Cosine similarity ranks non-zero vectors by how closely their directions align, largely ignoring magnitude. In search, it is a useful retrieval signal, not proof that a passage is correct or fit for use.