EXPLAINER

What are Embeddings in AI?

An embedding is a list of numbers that represents the meaning of a piece of content, so similar meanings end up close together and can be found mathematically.

Short answer: An embedding turns text (or an image, or audio) into a vector of numbers so that items with similar meaning are near each other. It powers semantic search, recommendations, clustering and RAG.

What is it?

Computers compare numbers easily, but "meaning" is hard to compare directly. An embedding model converts content into a fixed-length list of numbers — a vector, typically hundreds to a few thousand values long depending on the model. The model is trained so that content with similar meaning produces similar vectors.

So "How do I reset my password?" and "I forgot my login credentials" land close together, even though they share almost no words, while "Best pizza in Rome" lands far away.

Why does it matter?

Keyword search only finds exact or near-exact words. Embeddings allow semantic search: finding items by meaning. That underpins:

  • searching a knowledge base with natural-language questions;
  • retrieval for What is RAG (Retrieval-Augmented Generation)?;
  • "related articles" and recommendations;
  • de-duplicating or clustering similar items;
  • classification using a few labelled examples.

How does it work?

  1. Choose an embedding model (a different model from a chat/generation model; many are small enough to run in a browser or on a laptop).
  2. Convert each document or chunk into a vector and store it (see What is a Vector Database?).
  3. At query time, embed the question with the same model.
  4. Measure closeness between the query vector and each stored vector. The most common measure is cosine similarity, which compares the direction of two vectors (1 means same direction, around 0 unrelated).
  5. Return the top-k most similar items.

Key facts: vectors from different models are not comparable with each other; changing models means re-embedding everything; and the numbers themselves are not human-readable — only distances between them mean something.

Example

A tiny, runnable illustration of the comparison step in JavaScript (the vectors here are made up; real ones come from an embedding model):

function cosine(a, b) {
  let dot = 0, na = 0, nb = 0;
  for (let i = 0; i < a.length; i++) {
    dot += a[i] * b[i]; na += a[i] ** 2; nb += b[i] ** 2;
  }
  return dot / (Math.sqrt(na) * Math.sqrt(nb));
}

const query   = [0.9, 0.1, 0.0];   // "forgot my login"
const docA    = [0.8, 0.2, 0.1];   // "reset your password"
const docB    = [0.0, 0.1, 0.9];   // "pizza recipes"
console.log(cosine(query, docA));  // high  (~0.98)
console.log(cosine(query, docB));  // low   (~0.01)

For a few thousand items you can do exactly this in memory. Specialised indexes only become necessary at larger scale (What is a Vector Database?).

When should I use it?

Use embeddings when users describe what they want in their own words and the wording varies, when you need "find similar", or when you build What is RAG (Retrieval-Augmented Generation)?. Combine them with keyword search (a hybrid approach) when exact terms like product codes, names or error numbers matter — embeddings can blur those.

Common mistakes

Learning path: Retrieval and grounding (RAG) (step 2 of 7)