---
title: "What is cosine similarity for embeddings?"
description: "Cosine similarity measures the angle between two embedding vectors. Learn the formula, why it equals the dot product for normalized vectors, and when to use it."
type: "glossary-term"
canonical_url: "https://theaiengineering.com/glossary/cosine-similarity"
markdown_url: "https://theaiengineering.com/glossary/cosine-similarity.md"
topic: "RAG and vector search"
author: "The AI Engineering Editors"
publisher: "The AI Engineering"
updated: "2026-11-06"
keywords: ["Cosine similarity","cosine distance","cosine similarity score"]
citation: "The AI Engineering Editors. \"What is cosine similarity for embeddings?\" The AI Engineering, updated Nov 06, 2026. https://theaiengineering.com/glossary/cosine-similarity"
---

# What is cosine similarity for embeddings?

Also called cosine distance, cosine similarity score.

> **Definition of Cosine similarity:** Cosine similarity is a measure of how closely two vectors point in the same direction, computed as their dot product divided by the product of their lengths, with values from -1 to 1.

Cosine similarity is a measure of how similar two vectors are based on the angle between them, ignoring their lengths. It is a standard score for comparing [embeddings](https://theaiengineering.com/glossary/embeddings): a retriever embeds a query, computes its cosine similarity with stored document vectors, and returns the highest-scoring ones. A score of 1 means the vectors point the same way, 0 means they are orthogonal, and -1 means they point in opposite directions. Cosine distance, used by vector stores such as pgvector and by semantic chunking, is 1 minus cosine similarity.

## How cosine similarity is calculated

Cosine similarity is the dot product of two vectors divided by the product of their lengths. The dot product multiplies matching components and sums them; each length is the square root of the sum of that vector's squared components. Dividing by the lengths removes magnitude, so only direction affects the score.

cosine_similarity(A, B) = (A · B) / (‖A‖ × ‖B‖)

For A = (1, 2) and B = (2, 4), the dot product is 1×2 + 2×4 = 10, the lengths are √5 and √20, and their product is 10, so the similarity is 1.0. B is twice as long as A, but both point the same way. A [vector database](https://theaiengineering.com/glossary/vector-database) computes the score against millions of stored vectors through an [approximate nearest neighbor](https://theaiengineering.com/glossary/approximate-nearest-neighbor) index. PostgreSQL's [pgvector](https://github.com/pgvector/pgvector) exposes cosine distance as the <=> operator and recovers similarity as 1 minus that distance.

## Cosine similarity vs dot product vs Euclidean distance

When vectors are normalized to length 1, cosine similarity equals the dot product and ranks results in the same order as Euclidean distance. The three metrics differ only for vectors of unequal length: the dot product rewards magnitude, Euclidean distance counts differences in magnitude as distance, and cosine similarity ignores magnitude.

For unit vectors the denominator of the cosine formula is 1, which leaves the dot product. Squared Euclidean distance between unit vectors equals 2 minus 2 times their cosine similarity, so sorting by either gives the same ranking. OpenAI's [embeddings guide](https://developers.openai.com/api/docs/guides/embeddings) states that its embeddings are normalized to length 1, so cosine similarity can be computed slightly faster with a dot product and cosine and Euclidean distance produce identical rankings. The same guide warns that an embedding shortened by hand must be normalized again.

| Metric | Formula | Range | Affected by vector length |
|---|---|---|---|
| Cosine similarity | A · B / (‖A‖ × ‖B‖) | -1 to 1 | No |
| Dot product | Sum of Aᵢ × Bᵢ | Unbounded | Yes, rewards longer vectors |
| Euclidean distance | Square root of the sum of (Aᵢ − Bᵢ)² | 0 and up | Yes |

In practice, normalize once at indexing time and then use the cheapest inner-product operator. The pgvector documentation recommends inner product for best performance when vectors are normalized to length 1, and the [Faiss index guide](https://github.com/facebookresearch/faiss/wiki/Faiss-indexes) handles cosine with its inner-product index on vectors normalized beforehand. Use the metric the embedding model was trained with when its documentation names one.

A cosine score also works as a threshold. A [semantic cache](https://theaiengineering.com/glossary/semantic-caching) returns a stored answer only when the similarity between a new query and a cached one passes a cutoff. Tune that cutoff on labeled pairs and re-tune it whenever the embedding model changes, because each model spreads its scores differently. Cosine scores and [BM25](https://theaiengineering.com/glossary/bm25) scores sit on different scales, which is why [hybrid search](https://theaiengineering.com/glossary/hybrid-search) fuses ranks instead of raw scores.

[What is RAG](https://theaiengineering.com/articles/what-is-rag) shows where similarity search fits in a retrieval pipeline.

## Related terms

- [Embeddings](https://theaiengineering.com/glossary/embeddings): Embeddings are fixed-length vectors of numbers that represent data such as text or images so that items with similar meaning sit close together, which lets software compare meaning with arithmetic.
- [Approximate nearest neighbor search](https://theaiengineering.com/glossary/approximate-nearest-neighbor): Approximate nearest neighbor (ANN) search is a family of indexing methods that find vectors close to a query vector without comparing it to every stored vector, accepting that some true nearest neighbors are missed.
- [Vector database](https://theaiengineering.com/glossary/vector-database): A vector database is a database built to store embedding vectors and quickly return the vectors most similar to a query vector, usually with approximate nearest neighbor indexes such as HNSW.
- [Semantic caching](https://theaiengineering.com/glossary/semantic-caching): Semantic caching is a technique that returns a stored LLM response when a new query is close in meaning to an earlier one, measured by embedding similarity instead of exact text match.
- [Hybrid search](https://theaiengineering.com/glossary/hybrid-search): Hybrid search is a retrieval method that runs keyword search, typically BM25, and vector similarity search on the same query and merges the two ranked lists into one result set.

## Guides that cover cosine similarity

- [Chunking strategies for RAG: how to choose, tune, and evaluate them](https://theaiengineering.com/articles/chunking-strategies-for-rag): Chunking strategies for RAG compared: fixed-size, recursive, document-aware, semantic, parent-child, and late chunking, with tested Python and an eval method.
- [What is retrieval-augmented generation (RAG)? How RAG works](https://theaiengineering.com/articles/what-is-rag): Retrieval-augmented generation (RAG) grounds LLM answers in your documents. Learn the RAG pipeline, hybrid search, agentic RAG, evaluation, and failure modes.
- [Semantic caching for LLMs: how it works and when it pays off](https://theaiengineering.com/articles/semantic-caching): Semantic caching returns a stored LLM response for prompts with similar meaning. Learn how a semantic cache works, how to tune it, and what never to cache.

Part of the [RAG and vector search](https://theaiengineering.com/topics/rag) topic hub.

## Cite this definition

The AI Engineering Editors. "What is cosine similarity for embeddings?" The AI Engineering, updated Nov 06, 2026. https://theaiengineering.com/glossary/cosine-similarity

- Title: What is cosine similarity for embeddings?
- Author: The AI Engineering Editors
- Publisher: The AI Engineering
- Updated: 2026-11-06
- URL: https://theaiengineering.com/glossary/cosine-similarity
- Markdown: https://theaiengineering.com/glossary/cosine-similarity.md
