
Cosine similarity measures how similar two vectors are by the angle between them. It's the standard metric for comparing embeddings in semantic search, because it focuses on direction (meaning) rather than length.
In simple terms
A score from −1 to 1 for how close two vectors point — the standard way to compare embeddings.
How it works
- 1Convert text into vectors (embeddings).
- 2Compare the vectors by looking at the angle (θ) between them.
- 3Calculate the angle between the two vectors.
- 4Compute cosine: cos(θ) = (A · B) / (‖A‖ ‖B‖).
- 5Interpret the score: higher means more similar direction.
Key points
- Range is −1 to 1: 1 = same direction, 0 = unrelated, −1 = opposite.
- It ignores magnitude and focuses on direction, so it works well for text.
- Efficient and widely supported in vector databases.
- Used in semantic search, recommendations, clustering and deduplication.
Why it matters
Cosine similarity is how vector databases decide which embeddings are 'closest'. It's the core comparison behind semantic search and RAG retrieval.
Frequently asked questions
- Why cosine and not distance?
- Cosine focuses on direction (meaning) and ignores vector length, which suits text embeddings well.
- What score counts as 'similar'?
- It depends on the data, but higher (closer to 1) means more similar; you tune a threshold per use case.
More in Tokens & Embeddings
Token
The basic unit of text AI models read and generate.
Tokenization
Breaking text into tokens the model can read.
Tokenizer
Breaks text into tokens the model can understand.
Token Embeddings
Turning tokens into meaningful vectors.
Embedding
Turning words and data into numbers AI understands.
Context Window
How much text an AI can consider at once.