AI EngineeringIntermediate16 min

RAG: retrieval-augmented generation

Fetch relevant context at question time and make the model answer from it, with sources.

See it on the map

RAG splits the job in two. Retrieval finds the passages most likely to contain the answer. Generation writes a response using only those passages. The model never needs to memorise your data, and you can update knowledge by changing the index.

The pipeline is: split documents into chunks, embed and store them, then at query time embed the question, retrieve the top results, put them in the prompt with their source ids, and instruct the model to answer only from them and cite ids. If nothing relevant was retrieved, the right answer is that it does not know.

Most RAG failures are retrieval failures. If the right passage is not in the prompt, no prompt wording will rescue the answer. Debug by looking at what was retrieved first, then at what the model did with it.

Key points

  • Retrieval quality caps answer quality.
  • Pass source ids and require citations so answers can be checked.
  • Instruct the model to say it does not know when context is insufficient.
  • Log the retrieved chunks with every answer for debugging.

Answer these out loud

  1. 1A user asks a question whose answer spans two documents. What breaks in a naive top-3 retrieval and how do you fix it?
  2. 2How do you stop the model from mixing retrieved facts with its own recall?

Build it

Build a question-answering endpoint over your own notes that returns an answer plus the chunk ids and scores it used.

Use this from an agent

Any MCP client, including Grok, can read this lesson. See how to connect.

REST
curl https://engineeros-delta.vercel.app/api/v1/topics/rag-pipeline

This unlocks