Skip to main content

Technique Guide

Retrieval-Augmented Generation

RAG connects AI models to external knowledge — your documents, databases, and APIs — so they can answer questions grounded in real data, not just training memory. It's the most widely used technique for building production AI systems.

What is RAG?

Retrieval-Augmented Generation (RAG) is a pattern where, instead of asking an AI model to answer from its training data alone, you first retrieve relevant documents from a knowledge base, then inject them into the prompt as context.

The basic flow: user asks a question → system retrieves relevant document chunks → retrieved chunks are added to the prompt as context → model answers using both the context and its training knowledge.

RAG prompt structure

Use the following documents to answer the question.

Only use information from the provided documents.

If the documents don't contain the answer, say so.

[Document 1]: ...

[Document 2]: ...

Question: [user question]

Read: How RAG Works in Detail

Key RAG Prompting Techniques

Constrain the model to the retrieved context

"Only use information from the provided documents. If the answer is not in the documents, say you don't know." This prevents the model from hallucinating beyond what was retrieved.

Ask the model to cite its sources

"For each claim, cite which document it comes from using [Doc N] notation." Citations make it easy to verify the output and identify retrieval failures.

Handle the no-answer case explicitly

"If the retrieved documents don't contain enough information to answer, say so clearly rather than guessing." Without this, models often synthesize a plausible-sounding but fabricated answer.

Ask for confidence when relevant

"Rate your confidence (high/medium/low) based on how directly the retrieved documents address the question." Useful for downstream quality filtering.

Articles

Related Lessons

Structured lessons on RAG and the techniques that work alongside it.

Related Guides

Learn RAG in Context

The Intermediate track covers RAG alongside XML tags, long documents, and other production techniques.

Go to RAG Lesson