# Cosine similarity

> Canonical: https://skillalmanac.com/glossary/cosine-similarity | Last verified: 2026-11-25 | Index status: index-eligible

Cosine similarity is a measure of how closely two vectors point in the same direction, scored from -1 to 1. In AI search it commonly compares embeddings, which are lists of numbers standing for meaning.

Also called: cosine similarity embeddings, cosine similarity formula, cosine similarity search.

## In plain language

Imagine each piece of text turned into an arrow in space, where texts with similar meaning point in similar directions. Cosine similarity measures the angle between two arrows: close to 1 means they point almost the same way, 0 means the nonzero vectors are orthogonal, and negative values point apart. Search systems use it to rank which stored passages best match a question. It tells you about direction, not about truth, so a passage that sounds like the question can score high even when it does not answer it.

## Technical definition

For two nonzero vectors, cosine similarity is their dot product divided by the product of their lengths (the standard definition), which equals the cosine of the angle between them. Pinecone's guide describes it as a measure of the angle between two vectors, ranging from -1 to 1, and not affected by vector size. OpenAI's embeddings guide says its text search example uses the cosine similarity between the embedding vectors of the query and each document. The angle formula is undefined for zero vectors; software APIs may define their own convention, such as scikit-learn returning zero similarity for a zero-vector input.

## How it shows up when you use agents

Cosine similarity is a common comparison in embedding-based search, but other similarity measures and retrieval methods are also used. When a [vector database](/glossary/vector-database) returns the "closest" passages for a question, it is comparing the question's [embedding](/glossary/embedding) with stored ones using a metric, and cosine is a common choice. That is the retrieval step in [RAG](/glossary/retrieval-augmented-generation) and in [semantic search](/glossary/semantic-search).

What breaks when it is misunderstood: a high score is read as a correct answer. It only says two texts sit in a similar direction, so a passage that repeats the question's wording can outrank the one that answers it.

Next action: when a retrieval looks wrong, print the scores next to the chunks and read them. Use one embedding model for both the question and the documents. If the right chunk is there but ranked low, consider [reranking](/glossary/reranking). Test any threshold on your own questions.

## Common confusions

- **Cosine similarity versus cosine distance.** Similarity is higher for closer vectors. Distance is a related quantity where smaller means closer.
- **Cosine versus Euclidean distance.** Cosine compares direction and ignores vector length, as Pinecone's guide states. Euclidean distance compares position in space.
- **Cosine similarity versus an embedding.** An embedding is the vector. Cosine similarity is a way to compare two of them.
- **Myth.** A score of 1 means two texts mean the same thing. It means their vectors point the same way, which is evidence of similarity in the model's view, not proof.

## Worked examples in this catalogue

### [What Is RAG? Retrieval-Augmented Generation Explained](https://skillalmanac.com/learn/what-is-rag)

Status: illustrative example from the item's own documentation, not executed for this page.

What you see: The guide explains that a question is embedded and the closest chunks are retrieved and placed in the prompt.

Why it exercises the concept: Closeness between the question vector and chunk vectors is the step a metric such as cosine similarity scores.

### [rag-chunking-planner](https://skillalmanac.com/skills/skill-almanac/rag-chunking-planner)

Status: illustrative example from the item's own documentation, not executed for this page.

What you see: Your documents chunked by three strategies, with defects and offline recall measured for each, so chunk settings are chosen with numbers.

Why it exercises the concept: Offline recall asks whether the right chunk comes back for a question. A similarity score is one part of how a retriever decides what comes back.

### [duplicate-passage-finder](https://skillalmanac.com/skills/skill-almanac/duplicate-passage-finder)

Status: illustrative example from the item's own documentation, not executed for this page.

What you see: Exact and near-duplicate paragraphs found across a folder of text and Markdown files, reported by group with file and line number.

Why it exercises the concept: Finding near-duplicates is a similarity problem. Read the skill to see how it decides two passages are alike, which may differ from an embedding comparison.

## Related terms

- [Embedding](https://skillalmanac.com/glossary/embedding.md)
- [Vector database](https://skillalmanac.com/glossary/vector-database.md)
- [Semantic search](https://skillalmanac.com/glossary/semantic-search.md)
- [Retrieval-augmented generation (RAG)](https://skillalmanac.com/glossary/retrieval-augmented-generation.md)
- [Reranking](https://skillalmanac.com/glossary/reranking.md)

## Sources

- [Vector Similarity Explained](https://www.pinecone.io/learn/vector-similarity/), Pinecone (accessed 2026-11-24)
- [Vector embeddings](https://developers.openai.com/api/docs/guides/embeddings), OpenAI (accessed 2026-11-24)
- [cosine_similarity: normalized dot product and zero-input example](https://scikit-learn.org/stable/modules/generated/sklearn.metrics.pairwise.cosine_similarity.html), scikit-learn (accessed 2026-11-25)

Definitions are written by Skill Almanac from the sources above and checked against them on 2026-11-25. [Glossary index](https://skillalmanac.com/glossary.md).
