# What is cosine similarity?

> Cosine similarity measures how close two vectors are by the angle between them. Semantic search uses it to compare a query embedding with document embeddings.

Canonical: https://promptlyfound.com/glossary/cosine-similarity
Updated: 2026-12-01

## Short answer

Cosine similarity is **a measure of how close two vectors are, based on the angle between them**. Semantic search uses it to compare the embedding of a query with the embeddings of documents, and ranks the closest documents first. Two vectors that point the same way score 1, whatever their length. OpenAI recommends cosine similarity for its embeddings.

## Key findings

- OpenAI recommends cosine similarity. Its embeddings are normalized to length 1, so cosine similarity and Euclidean distance give identical rankings. [2]
- Introduction to Information Retrieval defines cosine similarity as the dot product of two vectors divided by the product of their lengths. The division compensates for differences in document length. [1]
- Azure AI Search supports cosine, dot product and Euclidean metrics for vector search, and says to use cosine with Azure OpenAI embedding models. [3]
- Azure AI Search does not report the raw cosine as its vector score. It reports 1 / (1 + cosine distance), where cosine distance is 1 minus cosine similarity. [3]

Cosine similarity is the score that most semantic search systems use to decide which passage is closest in meaning to a query. It compares the direction of two vectors and ignores their length.

## How is cosine similarity calculated?

Cosine similarity divides the dot product of two vectors by the product of their lengths. The [section on dot products in Introduction to Information Retrieval](https://nlp.stanford.edu/IR-book/html/htmledition/dot-products-1.html), a Cambridge University Press textbook, gives the formula. The dot product multiplies the vectors position by position and adds the results. The length of a vector is the square root of the sum of its squared values. The result is the cosine of the angle between the two vectors.[1]

Take three short vectors: a = (1, 2), b = (2, 4) and c = (2, -1). The vector b points the same way as a and is twice as long. Its dot product with a is 10, and the product of the lengths is also 10, so the cosine similarity is 1. The dot product of a and c is 0, so their cosine similarity is 0.

## Why do search systems use cosine similarity?

Cosine similarity ignores length. The textbook explains that the distance between two document vectors can be large because one document is much longer than the other, even when the content is similar. Dividing by the two lengths turns both vectors into unit vectors, so only their direction counts.[1]

For embeddings, the choice of measure often makes little difference. [OpenAI's embeddings guide](https://developers.openai.com/api/docs/guides/embeddings) recommends cosine similarity and says the choice of distance function typically does not matter much. OpenAI embeddings are normalized to length 1. For that reason, a plain dot product gives the same value slightly faster, and Euclidean distance gives identical rankings.[2]

## How does cosine similarity work in semantic search?

A semantic search system embeds the query and compares it with the stored embeddings of documents or passages. [OpenAI's retrieval guide](https://developers.openai.com/api/docs/guides/retrieval) shows the effect for the query "When did we go to the moon?". The passage "The first lunar landing occurred in July of 1969." scored 65% semantic similarity, measured with cosine similarity, and 0% keyword similarity.[4]

A [vector database](https://promptlyfound.com/glossary/vector-database) uses the measure to find nearest neighbors quickly. Microsoft's page on [relevance in Azure AI Search vector search](https://learn.microsoft.com/en-us/azure/search/vector-search-ranking) says its HNSW algorithm moves through a graph toward vectors that are closer to the query by the configured metric, such as cosine similarity.[3] This search is the core of [dense retrieval](https://promptlyfound.com/glossary/dense-retrieval).

## What is the difference between cosine similarity and cosine distance?

Cosine distance is 1 minus cosine similarity, so a smaller distance means a closer match. Microsoft defines both terms. Azure AI Search turns the distance into a score of 1 / (1 + cosine distance), which falls between 0.333 and 1.00 and always decreases as similarity gets worse.[3]

[What are vector embeddings?](https://promptlyfound.com/llms/embeddings) explains how embeddings are made and how retrieval systems compare them.

## Sources

1. Dot products. Introduction to Information Retrieval, Cambridge University Press. https://nlp.stanford.edu/IR-book/html/htmledition/dot-products-1.html
2. Vector embeddings. OpenAI API documentation. https://developers.openai.com/api/docs/guides/embeddings
3. Relevance in vector search. Microsoft Learn. https://learn.microsoft.com/en-us/azure/search/vector-search-ranking
4. Retrieval. OpenAI API documentation. https://developers.openai.com/api/docs/guides/retrieval

---

This is a plain-text copy of https://promptlyfound.com/glossary/cosine-similarity from Promptly Found. The web page is the canonical version and includes the charts.
