Skip to content
AgenticAIWiki
  1. Learn
  2. Part 1
  3. 4. Embeddings and semantic search
  4. 4.1

Lesson 4.1 · 1 of 10 · 4 min read · Reviewed 24 Nov 2026

What an embedding is

By the end, you can

  • Explain what an embedding is.
  • Picture texts as points in a meaning space.

Text as a point

An embedding modelEmbedding model: A model trained to turn text (or images) into embeddings for search and comparison, rather than to generate text. turns text into a vector: a list of numbers, often hundreds or thousands long.

"reset my password"   -> [0.12, -0.40, 0.88, ...]
"forgot login code"   -> [0.10, -0.37, 0.91, ...]   close
"best pizza in Rome"  -> [-0.70, 0.22, 0.05, ...]   far

Think of each text as a point in a space with many dimensions. Points close together mean similar things.

        * best pizza in Rome

                          * reset my password
                          * forgot login code
   * quarterly revenue

Learned, not hand-made

EmbeddingEmbedding: A list of numbers (a vector) that represents the meaning of a piece of text, so similar meanings land close together. models are trained on huge amounts of text so that related meanings end up near each other. No one picks what each number means.

Embedding models vs chat models

Claude generates text; it doesn’t produce embeddings through the API. Claude’s docs point to dedicated embedding providers and explain how to choose one. Many systems use an embedding model for search and Claude for reasoning over what’s found.

Uses

Search by meaning, finding duplicates, grouping similar items, recommendations, and retrieval for RAGRetrieval-augmented generation (RAG): Retrieving relevant documents at request time and adding them to the prompt so the answer is grounded in them. (Part 4).

Key points

  • An embedding is a list of numbers that represents the meaning of a piece of text.
  • Texts with similar meaning get embeddings that are close together, even with different words.
  • Embeddings come from embedding models, which are separate from chat models like Claude.
Hands-on labRuns in the Playground

Build a toy embedding from topic words

Turn sentences into vectors with a hand-made word table and see that similar meanings land close together.

You will have: A script that embeds six sentences and prints each one's closest neighbour.

Steps

  1. Run the starter: it prints a vector for each sentence, but every vector is all zeros.
  2. Fill in embed(): add up the vectors of the known words in the sentence.
  3. Fill in closest(): for each sentence, find the other sentence with the highest cosine similarity.
  4. Check that the two account sentences pair up even though they share no words.
  5. Add a sentence of your own and predict its neighbour before you run it.

Done when

  • Every sentence has a non-zero vector.
  • 'reset my password' and 'my login is locked' are each other's closest neighbour.
  • The output shows 6 pairs.

Open in the Playground No API key needed.

Starter code (Python)
import math

# Toy embedding: 5 named dimensions. Real models learn hundreds you can't name.
DIMS = ["account", "billing", "shipping", "device", "pets"]
WORDS = {
    "password": [1, 0, 0, 0, 0], "login": [1, 0, 0, 0, 0], "locked": [0.8, 0, 0, 0, 0], "code": [0.6, 0, 0, 0.2, 0],
    "charged": [0, 1, 0, 0, 0], "invoice": [0, 1, 0, 0, 0], "refund": [0, 0.8, 0.3, 0, 0], "twice": [0, 0.4, 0, 0, 0],
    "parcel": [0, 0, 1, 0, 0], "delivery": [0, 0, 1, 0, 0], "late": [0, 0, 0.6, 0, 0], "courier": [0, 0, 0.9, 0, 0],
    "laptop": [0, 0, 0, 1, 0], "screen": [0, 0, 0, 0.9, 0], "battery": [0, 0, 0, 0.9, 0], "power": [0, 0, 0, 0.7, 0],
    "cat": [0, 0, 0, 0, 1], "kitten": [0, 0, 0, 0, 1], "nap": [0, 0, 0, 0, 0.6], "sleeps": [0, 0, 0, 0, 0.6],
}

SENTENCES = ["reset my password", "my login is locked", "I was charged twice", "where is the invoice",
             "my cat sleeps all day", "the kitten loves a nap"]

def embed(text):
    vec = [0.0] * len(DIMS)
    # TODO: for each word in text.lower().split(), if it is in WORDS, add its vector to vec
    return vec

def cosine(a, b):
    dot = sum(x * y for x, y in zip(a, b))
    la, lb = math.sqrt(sum(x * x for x in a)), math.sqrt(sum(x * x for x in b))
    return dot / (la * lb) if la and lb else 0.0

def closest(i, vecs):
    # TODO: return the index of the other sentence with the highest cosine to vecs[i]
    return i

vecs = [embed(s) for s in SENTENCES]
for s, v in zip(SENTENCES, vecs):
    print(s, "->", v)
for i, s in enumerate(SENTENCES):
    print(f"{s!r} is closest to {SENTENCES[closest(i, vecs)]!r}")
Show a solution
import math

# Toy embedding: 5 named dimensions. Real models learn hundreds you can't name.
DIMS = ["account", "billing", "shipping", "device", "pets"]
WORDS = {
    "password": [1, 0, 0, 0, 0], "login": [1, 0, 0, 0, 0], "locked": [0.8, 0, 0, 0, 0], "code": [0.6, 0, 0, 0.2, 0],
    "charged": [0, 1, 0, 0, 0], "invoice": [0, 1, 0, 0, 0], "refund": [0, 0.8, 0.3, 0, 0], "twice": [0, 0.4, 0, 0, 0],
    "parcel": [0, 0, 1, 0, 0], "delivery": [0, 0, 1, 0, 0], "late": [0, 0, 0.6, 0, 0], "courier": [0, 0, 0.9, 0, 0],
    "laptop": [0, 0, 0, 1, 0], "screen": [0, 0, 0, 0.9, 0], "battery": [0, 0, 0, 0.9, 0], "power": [0, 0, 0, 0.7, 0],
    "cat": [0, 0, 0, 0, 1], "kitten": [0, 0, 0, 0, 1], "nap": [0, 0, 0, 0, 0.6], "sleeps": [0, 0, 0, 0, 0.6],
}

SENTENCES = ["reset my password", "my login is locked", "I was charged twice", "where is the invoice",
             "my cat sleeps all day", "the kitten loves a nap"]

def embed(text):
    vec = [0.0] * len(DIMS)
    for word in text.lower().split():
        for d, x in enumerate(WORDS.get(word, [0] * len(DIMS))):
            vec[d] += x
    return vec

def cosine(a, b):
    dot = sum(x * y for x, y in zip(a, b))
    la, lb = math.sqrt(sum(x * x for x in a)), math.sqrt(sum(x * x for x in b))
    return dot / (la * lb) if la and lb else 0.0

def closest(i, vecs):
    others = [j for j in range(len(vecs)) if j != i]
    return max(others, key=lambda j: (cosine(vecs[i], vecs[j]), -j))

vecs = [embed(s) for s in SENTENCES]
for s, v in zip(SENTENCES, vecs):
    print(s, "->", [round(x, 2) for x in v])
for i, s in enumerate(SENTENCES):
    print(f"{s!r} is closest to {SENTENCES[closest(i, vecs)]!r}")
Quick check'How do I reset my password?' and 'I forgot my login code' have embeddings that are...
How sure are you?
Pick an answer and how sure you are