Retrieval-Augmented Generation

Before asking a language model to answer, look up the most relevant passages from your own documents and give them to the model as context.

Description

A language model only knows what was in its training data, which doesn't include your product docs, policies or yesterday's tickets, and it may invent answers. RAG fixes that at question time: retrieve the most relevant chunks of your own content, augment the prompt with them, and let the model generate an answer grounded in those sources, ideally with citations.

Documents are prepared in advance: split into chunks, turned into embeddings (vectors that capture meaning) and stored in a vector index. At question time the question is embedded too, the nearest chunks are found (often combined with keyword search and re-ranking), and they go into the prompt.

System design

question1embed question2top 5 chunks for this user3question + chunks4answer with citations5UserAssistant APIEmbedding modelVector indexchunks + metadataLanguage model

Retrieval-Augmented Generation system design diagram

  1. User (client).
  2. Assistant API (service).
  3. Embedding model (language model).
  4. Vector index (vector index): chunks + metadata.
  5. Language model (language model).
  6. Step 1: User → Assistant API: question.
  7. Step 2: Assistant API → Embedding model: embed question.
  8. Step 3: Assistant API → Vector index: top 5 chunks for this user.
  9. Step 4: Assistant API → Language model: question + chunks.
  10. Step 5: Assistant API → User: answer with citations.
  1. The user asks a question.
  2. The question is turned into an embedding, a vector that captures its meaning.
  3. The vector index returns the closest chunks the user is allowed to see, often combined with keyword search and re-ranked.
  4. The model gets the question plus those chunks, with instructions to answer only from them and cite sources.
  5. The user gets an answer grounded in your documents, with links to check it.
AWS
Amazon Bedrock Knowledge Bases; OpenSearch or Aurora pgvector; Amazon Kendra.
Azure
Azure AI Search with Azure OpenAI; Cosmos DB vector search.
Google Cloud
Vertex AI Search and RAG Engine; AlloyDB or Cloud SQL pgvector.

When to use it

Use it when

  • Answers must come from your own, changing content: documentation, knowledge bases, contracts, support history.
  • You need citations so users can check where an answer came from.
  • Content changes too often to bake into a model by fine-tuning.

Avoid it when

  • The answer depends on calculations or live data better fetched by a tool or API. Use an agent with tools.
  • The whole relevant content fits comfortably in the prompt. Just include it.
  • You need the model to learn a style or format, not facts. That's prompting or fine-tuning.

Pitfalls

  • Bad retrieval, bad answers: chunks too large or small, missing metadata, no keyword search for exact terms like product codes. Measure retrieval quality separately.
  • No evaluation set. Build a list of real questions with expected sources and track it on every change.
  • Leaking documents users shouldn't see. Apply access control at retrieval time, per user.
  • Stale indexes. Re-index when documents change, and remove deleted ones.

Alternatives

AI Agent

Choose it when answering needs several lookups, tool calls or decisions, not one retrieval step.

Long context

Choose it when the relevant material is small enough to put in the prompt directly.

Fine-tuning

Choose it when you want to change the model's behaviour or style; it's a poor way to add changing facts.