AI engineering path · Models in your system · Topic 6 of 12

Retrieval-augmented generation

Not started

Retrieval-augmented generation (RAG) answers a question in two steps: first search your own documents for the passages most likely to contain the answer, then give those passages to the model with the question and ask it to answer from them. The term comes from a 2020 paper by Lewis et al., which combined a model's built-in ("parametric") knowledge with a searchable index of documents ("non-parametric" memory). Today it usually means a search step wired in front of a general-purpose model.

Why it matters: A model knows only its training data and your prompt. Retrieval brings in your own, current documents at question time, and lets the answer point to where each claim came from.

  1. 1Chunk, embed, store

    Split documents into passages, turn each into an embedding vector, and keep vectors with their text and source.

  2. 2Search by meaning

    Embed the question and find the passages whose vectors are closest, often combined with keyword search.

  3. 3Re-rank and trim

    A second, more careful scorer reorders the candidates; only the best few go into the prompt.

  4. 4Answer with citations

    Tell the model to use only the supplied passages, cite them, and say when they do not contain the answer.

1

How it works

The explanation, with examples you can run.

Indexing: chunk, embed, store

  • Chunk. Split documents into passages small enough to be specific and large enough to make sense alone; a few hundred tokens is a common starting point. Split on structure (headings, paragraphs) rather than at fixed character counts, overlap neighbours slightly, and keep the title or heading path with each chunk so it has context.
  • Embed. An embedding model turns text into a vector of numbers such that texts with related meanings end up close together. Use the same embedding model for documents and questions.
  • Store. Keep each vector with its text, source, and metadata (owner, permissions, date) in a vector index: a dedicated vector database or a vector extension of a database you already run.

Retrieval: search, re-rank, trim

At question time, embed the question and find the nearest chunks, usually by cosine similarity. Pure vector search can miss exact terms (product codes, error numbers, names), so many systems run keyword search (such as BM25) alongside and merge the results ("hybrid search"). A re-ranker, a slower model that reads the question and each candidate together, then reorders the top few dozen so only the best handful go into the prompt.

The toy below uses word-count vectors instead of a real embedding model, which is enough to show chunking, cosine similarity, top-k and citing sources.

⌘/Ctrl + Enter
OutputNot run yet

Press Run to see what your code prints, in order.

Runs in this browser, in an isolated frame with no access to this page. console.log, info, warn and error are shown; code runs as a classic script, so use an async function for await.

Generation: answer from the evidence

Put the passages in clearly delimited, labelled blocks and instruct the model to use only them, to cite passage ids for each claim, and to say when the passages do not contain the answer. Some APIs have built-in citation features that return the exact quoted spans. Your code can then check that every cited id was actually supplied, and show users links to the sources.

Where RAG goes wrong

  • Retrieval misses. The answer exists but was not retrieved: chunks too large or too small, missing keyword search, no re-ranking, or the question phrased very differently from the document. Build a retrieval test set of questions with the chunk ids that should come back, and track recall at k.
  • Too much context. Stuffing in fifty passages raises cost and latency and can make answers worse; models tend to use the beginning and end of long inputs more reliably than the middle.
  • Stale or forbidden data. Re-index when documents change, delete chunks when documents are deleted, and filter by the user's permissions at query time, before anything reaches the prompt.
  • Poisoned documents. Retrieved text is untrusted input. A document can contain instructions aimed at the model (indirect prompt injection); see the security topic.
2

Check yourself

3 questions. Get them all right to finish this topic.

  1. 1.Users report wrong answers. You find the right passage was never in the prompt. Which part do you work on?
  2. 2.What does cosine similarity between two embedding vectors measure?
  3. 3.Why store the source (document, section, URL) with every chunk?

0 of 3 right so far. Get all three to finish this topic.