Embeddings and RAG: answering from your own documents
Models don't know your handbook, your tickets or your product docs, and those are too big to paste into every request. Retrieval-augmented generation (RAG) finds the few passages that matter and gives only those to the model. It's Module 03's "open book", automated, and the most common pattern in AI products.
1. The idea
Two ideas make it work:
- Embeddings turn meaning into numbers. An embedding model turns a piece of text into a list of numbers (a vector, often 1,024 or more of them) so that texts with similar meaning get similar numbers. "How do I get my money back?" and "refund policy" end up close together even though they share no words.
- RAG retrieves, then generates. Ahead of time you split documents into chunks, embed them and store them (the index). At question time you embed the question, find the closest chunks (plus a keyword search), put the best ones into the prompt, and ask the model to answer only from them, with citations.
Similarity is usually measured by the angle between vectors (cosine similarity): 1 means the same direction (same meaning), near 0 means unrelated.
2. How it works
3. Try it yourself: the meaning map
Real embeddings have hundreds of dimensions; this map squeezes some help-centre chunks into two so you can see the idea. Pick a question: the closest chunks (by meaning) are retrieved. Compare with keyword matching on the right.
4. Why it matters at work
Most RAG quality problems are retrieval problems: the right passage never reached the model. What actually moves the needle:
- Chunk sensibly. Split by headings and paragraphs, a few hundred tokens each, with a little overlap. Too small loses context; too big dilutes the match.
- Add context to each chunk. "Revenue grew 3%" means nothing alone. Anthropic's contextual retrieval prepends a one-line summary of where each chunk sits in its document before indexing it.
- Combine meaning and keywords (hybrid search). Embeddings catch paraphrases; keyword search (BM25) catches exact names, codes and error messages. Use both.
- Rerank. Retrieve broadly (say 50 chunks), then let a reranker model pick the best 5–20 for the prompt.
- Answer only from the sources, with citations, and allow "not found" (Module 03).
- Measure retrieval separately from answers: did the right chunk appear in the top results? (Module 17)
Anthropic measured the effect of these techniques on retrieval failures (how often the right chunk was missing from the top 20):
| Setup | Failure rate |
|---|---|
| Standard embeddings | 5.7% |
| Contextual embeddings | 3.7% (−35%) |
| Contextual embeddings + contextual BM25 | 2.9% (−49%) |
| …plus reranking | 1.9% (−67%) |
5. Embedding models and built-in options
Checked against vendor documentation, October 2026.
| Provider | Embedding models | Notes |
|---|---|---|
| Voyage AI (MongoDB) | voyage-4-large, voyage-4, voyage-4-lite, voyage-code-4; reranker rerank-3 | Anthropic has no embedding model of its own and points to Voyage. 32K-token inputs; contextualized and multimodal variants |
gemini-embedding-2 | Multimodal (text, images, audio, video, PDF); 8,192 tokens; 128–3,072 dimensions | |
| OpenAI | text-embedding-3-small, text-embedding-3-large | 8,192 tokens; 1,536 and 3,072 dimensions, reducible |
Built-in retrieval: instead of building the whole pipeline, you can use a vendor's managed option: OpenAI's file search tool in the Responses API, Google's File Search tool in the Gemini API, or Claude's citations for documents you pass in. In editors, Cursor and Copilot index your codebase with embeddings for their semantic search.
Storage: any vector database works (pgvector in Postgres, MongoDB, Elasticsearch, or a dedicated one). Start with what your stack already has.
6. Remember it
How this hook works: what “Index, fetch, cite” means
What the slogan means, phrase by phrase
- “Index”
- Ahead of time: chunk the documents, add context to each chunk, embed them, and also build a keyword index. Re-index when documents change.
- “fetch”
- Per question: embed it, search by meaning and by keywords, rerank, and keep the best few chunks.
- “cite”
- Generate the answer only from those chunks, citing each one, and say "not found" when they don't cover it.
Why this picture fits: each word is a stage you can test and fix on its own. Bad answers usually trace back to one stage: a missing document (index), the wrong passages (fetch), or invented claims (cite).
The story → the concept
| The librarian | RAG |
|---|---|
| Cataloguing books by topic | Chunking + embeddings + keyword index |
| A note on each page saying which book it's from | Contextual retrieval |
| Understanding what you mean, not just your words | Vector (semantic) search |
| Also searching for the exact part number | Keyword search (BM25), hybrid |
| Picking the best five of fifty pages | Reranking |
| "Page 12 says…" | Citations |
| "The library doesn't have that" | Answering "not found" |
Watch it on an example
- Index: your 400-page help centre becomes 3,000 chunks, each prefixed with "From: Returns policy › Damaged items".
- Fetch: "my parcel arrived crushed, what now?" matches the damaged-items chunks by meaning (no shared words), and the keyword search adds the chunk mentioning "crushed".
- Cite: "You can request a replacement within 30 days [Returns policy, §4]." Every claim is checkable.
The trap it saves you from
Blaming the model for wrong answers when the right passage never reached it. Check "fetch" first.
It still works when…
An agent does the retrieving (Module 16): the search becomes a tool it can call several times, refining the query, which is called agentic retrieval.
7. Common mistakes
8. Hands-on lab (roadmap weeks 16–17)
- Pick a document set you know well (your project docs, a handbook) and write 20 questions with the passage that answers each.
- Build the simplest RAG: chunk, embed with one provider, store, retrieve the top 5, answer with citations. Your coding agent can scaffold it.
- Measure retrieval: for how many of the 20 questions is the right passage in the top 5?
- Improve one stage at a time: add keyword search, then chunk context, then a reranker. Re-measure after each.
- Test "not found" with three questions your documents can't answer.
9. Self-check quiz
What does an embedding capture?
Questions quoting exact error codes retrieve the wrong chunks. Fix?
RAG gave a wrong answer. What do you check first?
Where must per-user access control happen?
10. Go deeper
| Resource | Time | Why |
|---|---|---|
| Introducing contextual retrieval (Anthropic) | 20 min | The chunk-context technique and the measurements in section 4. |
| Embeddings (Claude docs) | 15 min | Voyage models, query vs document embeddings, quantisation. |
| Embeddings (Gemini) and Embeddings (OpenAI) | 20 min | The other two providers' models and options. |
| Mastering LLMs open course, RAG talks | 2 h | Practitioners on what actually improves RAG in production. |