Skip to main content

Embeddings for Bunnies

How a warren finds the right carrot… and why your RAG bot sometimes doesn't.

Updated 8 min readMarkdown

Let's go back to the meadow.

If you've read the earlier System Design for Bunnies post, you know the warren by now. This time the bunnies have a simpler problem: finding food.

Over the years, the older bunnies have figured out a pattern. Sweet young carrots grow near the eastern hedge. The woody old ones grow by the rocky patch nobody likes. The bitter wild ones are out by the stream. Nobody drew a map. They just know that carrots which taste alike tend to grow close to each other.

So when a young bunny says, "I want something sweet," nobody tells it to search the whole meadow. They point it east.

Keep that picture in your head. It covers most of what you need to understand embeddings.

So what is an embedding?

An embedding is a list of numbers that represents a piece of content. A word, a sentence, a whole document, even an image. The model that produces it places those numbers at a point in a space where distance carries meaning. Things that mean similar things end up near each other.

Take two sentences:

  • "The puppy is sleeping."
  • "The dog is resting."

They share almost no words. A keyword search would treat them as strangers. A decent embedding model puts them right next to each other, because during training it saw thousands of examples where puppies, dogs, sleeping and resting showed up in similar places.

That's the eastern hedge. Similar things, close together.

To measure "close", you'll mostly see two options.

Cosine similarity checks whether two vectors point the same way, ignoring how long they are:

\([\cos(\theta) = \frac{A \cdot B}{|A| |B|}]\)

Dot product, \(A \cdot B\), skips that normalisation, so longer vectors can score higher.

You can ignore this until the day your vector database asks which one you want. Quick rule for that day: if your model's docs say the vectors are normalised, both give you the same ranking.

Why RAG works

Here's the thing with large language models. You can't paste your entire company wiki into every prompt. It's expensive, it's slow, and past a point it simply doesn't fit.

RAG (retrieval-augmented generation) gets around this with the meadow trick:

  1. Embed all your documents once and store the vectors.
  2. When a question comes in, embed the question too.
  3. Find the documents sitting closest to it.
  4. Hand only those to the model, along with the question.

The model does not see the whole meadow. It sees the five or ten carrots nearest to what the user asked for.

Most of the time this works well. When it doesn't… it usually breaks in one of three ways...

1. The question lands in the wrong patch

Imagine Aaina, an engineer at a fintech startup in Bengaluru, building a support bot. Her help docs are in clean English: "How to link your PAN with Aadhaar." In testing, she types "How do I link PAN and Aadhaar?" and the bot nails it every time.

Then real users show up.

"pan aadhaar link hua kya?"

"PAN wala kaam pending dikha raha hai"

Notice how they are the same intent. Very different words, and half of them in Hinglish. If her embedding model learned mostly from English web text, these questions land in a different part of the meadow from her docs. The bot retrieves something vaguely related and answers with full confidence.

This is called query drift. The fix starts with testing on what users actually type. Pull a few hundred real queries from your logs before you trust any retrieval number. If your users mix languages, shortlist models built for that. Cohere's Embed 5, for example, covers 100+ languages (Cohere).

2. The carrot got cut in half

Before embedding, documents get split into chunks. If a chunk boundary lands in the middle of a thought, say the refund policy in one chunk and its exceptions in the next, each piece gets embedded as half an idea.

Now a user asks about refund exceptions. Retrieval brings back the first chunk, which looks relevant and is missing the one paragraph that matters.

This one is sneaky because nothing errors out. The results look reasonable. When a RAG system keeps returning context that is technically related and practically useless, chunking is the first place I'd look. Split on headings and sections where you can, keep a little overlap between chunks, and read a sample of your chunks yourself. Boring work… and it fixes a lot.

Models are starting to help here too. Perplexity's pplx-embed-v2-context models, released on November 3, embed each chunk with the whole document in view (Perplexity). So the exceptions chunk still knows it belongs to the refund policy. Worth a test if chunking keeps biting you.

3. Last season's map

Embeddings are computed at a point in time. The meadow keeps changing. Carrots get eaten, new ones come up by the stream.

If your index was built in March and your pricing changed in August, the bot will happily retrieve the March pricing page. Nothing in the retrieval step knows what date it is.

Re-embed on a schedule, store a last-updated date with every chunk, and prefer the fresher document when two are close.

One carrot or every leaf?

Most embedding models squeeze a whole document into a single vector. One point in the meadow per carrot.

There's another approach getting attention right now. Late interaction keeps a small vector for every token, and at query time compares the question word by word against the document. Perplexity released two models built this way on Hugging Face on October 7, at 0.6B and 9B parameters, with a 128-number vector per token (Perplexity).

In bunny terms, instead of remembering where each carrot is, you remember where every leaf is. You'll find better matches inside long, detailed documents. You'll also store a lot more vectors.

One vector per document One vector per token
Storage Small Much larger
Long, detailed documents Can miss specifics Better matches
Setup Works with any vector DB Needs late-interaction support

For most teams starting out, one vector per document is fine. Reach for late interaction once you can see retrieval missing specifics inside long documents.

Picking a model without overthinking it

A couple of recent releases show where things are going. Google released EmbeddingGemma 2 on October 6, an open 740M-parameter model that puts text, code, images, audio and video into the same space, and runs in about 191MB of memory on a phone for text (Google). Cohere's Embed 5 came out a week earlier, on September 30.

Leaderboards will keep moving. Before chasing them, I'd check three things:

  1. Test on your own data. Fifty real questions with known right answers will tell you more than any public benchmark.
  2. Match the domain. A model trained on general web text can struggle with legal contracts or medical notes.
  3. Watch the size of the vectors. Every document you store is a list of numbers, and you pay for every number. More numbers per document means more storage, more memory and slower search. At a few thousand documents you won't notice. At a few crore, you will.

Do you always need all those numbers? Think of the uncle at your local kirana store. Ask him where the namkeen is and he'll say "second aisle." Push him and he'll say "second aisle, top shelf, next to the Haldiram's." Both answers come from the same memory of the store. Most days, "second aisle" is enough to find it.

Some newer models give you the same choice. EmbeddingGemma 2 produces 768 numbers per item… and you can keep just the first 512, 256 or 128 and still get useful results. Google says that's up to 6x less storage (Google). Try a few sizes on your own data and stop at the smallest one where your results hold up.

Back to the meadow

Every agent that answers questions from your documents is a bunny walking toward a patch. If it walks to the wrong patch, the answer is wrong, however good the model writing it is.

That's why I wanted this crossover with my AI For Bunnies book currently in the writing. The idea is simple enough to explain with carrots, and once it clicks, debugging RAG gets a lot less mysterious.

No comments yet. Be the first to comment.