# Cosine similarity

A score of how close two embeddings are in meaning. 1 means the same meaning, 0 means unrelated.

AI developers · Part 05: Embeddings + RAG · Card 2 of 14 · Last updated 2026-10-13

## Analogy

A compatibility score for sentences. "Revert a deploy" and "rollback steps" swipe right on each other.

## Code example

`cosine.ts`

```typescript
function cosineSimilarity(a: number[], b: number[]): number {
  const dot = a.reduce((sum, value, i) => sum + value * b[i], 0);
  const lengthA = Math.hypot(...a);
  const lengthB = Math.hypot(...b);
  return dot / (lengthA * lengthB);
}
```

## In an agent

Vector search sorts your chunks by this score and hands the top few to the agent.

## Practice

### Flashcards

- **What is cosine similarity?** A score of how close two embeddings are in meaning: 1 is the same, 0 is unrelated.

### Interview questions

**What are embeddings?**

- Vectors that represent meaning.
- Similar meanings give nearby vectors, measured with cosine similarity.
- They power semantic search, RAG, deduplication and clustering.

## Questions people ask

### What does cosine similarity measure?

How close two embeddings are in meaning, by comparing the angle between the vectors. A score near 1 means the texts mean nearly the same thing; near 0 means they are unrelated. Vector search sorts stored chunks by this score and hands the closest few to the model.

### Do I need to calculate cosine similarity myself?

Usually not. Vector databases compute it for you; in pgvector, the <=> operator gives cosine distance, where smaller means closer. Writing the formula once in TypeScript is still useful for understanding it: the dot product of the two vectors divided by the product of their lengths.

## Related concepts

- [Embedding](https://studyvisually.com/learn/tech/ai-developers/embeddings-rag/embedding)
- [Semantic search](https://studyvisually.com/learn/tech/ai-developers/embeddings-rag/semantic-search)
- [Vector database](https://studyvisually.com/learn/tech/ai-developers/embeddings-rag/vector-database)
- [Local embeddings](https://studyvisually.com/learn/tech/ai-developers/hugging-face/local-embeddings)

Web page: https://studyvisually.com/learn/tech/ai-developers/embeddings-rag/cosine-similarity
Study this card in the app: https://studyvisually.com/app/learn?category=ai-developers&card=cosine-similarity
