RAG
Semantic search finds the information that matches a question. Retrieval-augmented generation (RAG) takes the next step: it gives the retrieved information to an LLM, which reads it and writes an answer, citing its sources.
LLMs know a lot about the world in general, but nothing about your organization's contracts, products, policies or customers. They also have a knowledge cutoff, and when they lack information they may produce a confident but wrong answer. RAG addresses this by supplying the relevant knowledge at the moment of the question. The approach was described in the paper Retrieval-Augmented Generation for Knowledge-Intensive NLP Tasks and has become the standard pattern for building LLM features on company knowledge.
Use RAG for knowledge: facts, documents, records that change over time. Fine-tuning is better suited to teaching a model a behavior, such as a format, style or narrow task. Updating a RAG index takes minutes; changing what a fine-tuned model "knows" requires training again.
From search to answers
RAG reuses the semantic search architecture (embeddings, vector index, hybrid search, reranking, index synchronization) and adds three things: chunking for the model, prompt assembly, and generation.
- Understand the question: optionally rewrite it (for example resolve "what about the second one?" using the conversation history) or split it into sub-questions.
- Retrieve the most relevant chunks using semantic search, filtered by the user's permissions.
- Build the prompt: instructions, the retrieved chunks with their sources, and the question.
- Generate the answer, grounded in the chunks and with references to them.
The indexing side is the same pipeline that keeps a search index in sync, with one step that matters much more for RAG: how documents are split into chunks.
Chunking
In search, the user sees results and opens the document. In RAG, the model sees only the chunks, so each chunk must carry enough meaning on its own. Chunks that are too small lose context; chunks that are too large dilute relevance and waste tokens.
| Strategy | Description | Good for |
|---|---|---|
| Fixed size with overlap | Split every N tokens, with some overlap between chunks | Simple start, uniform text |
| Structure-aware | Split along headings, sections, paragraphs, table rows | Manuals, policies, wikis, contracts |
| Record-based | One chunk per record (product, ticket, FAQ entry) | Structured or semi-structured content |
| Contextual chunks | Add a short description of the document and section to each chunk before embedding | Chunks that are ambiguous on their own |
The last technique, described in Anthropic's article on contextual retrieval, addresses a common problem: a chunk such as "the limit is 150 EUR per night" means little without knowing which document and section it came from.
Keep the document structure when parsing: headings, tables and lists. A PDF flattened into plain text loses exactly the context the model needs.
The prompt
The prompt tells the model how to use the retrieved information. It should state the task, require the answer to be based only on the provided documents, ask for citations, and say what to do when the answer is not there.
You answer questions from employees about company HR policies.
Use only the information in the documents below. If the documents do not
contain the answer, say that you could not find it and suggest contacting HR.
Cite the documents you used as [1], [2], ...
<documents>
[1] Travel Policy, section 4.2 "Hotel accommodation" (updated 2026-03-01)
...chunk text...
[2] Travel Policy, section 4.5 "Per diem rates" (updated 2026-03-01)
...chunk text...
</documents>
Question: What is the maximum hotel rate for a business trip to Munich?
Design considerations:
- How many chunks: enough to cover the answer, few enough to keep the prompt focused and cheap. Tune it with evaluations.
- Source metadata: include title, section and date with each chunk, so the model can cite precisely and prefer current versions.
- Allow "I don't know": an honest "not found" is better than an invented answer.
- Treat chunks as untrusted: documents can contain text that looks like instructions (prompt injection). The prompt should make clear that documents are data, and the application must not let the answer trigger sensitive actions unchecked.
- Permissions before the prompt: only chunks the user is allowed to see may reach the model, see filtering.
Variants of RAG
| Variant | How it differs | When to use |
|---|---|---|
| Agentic RAG | Retrieval is a tool the model calls when needed, possibly several times with different queries, before answering | Complex questions that need several lookups or sources |
| Structured data retrieval | Instead of searching text, the model generates queries (for example SQL) against data products, through a semantic layer | Questions about numbers and records: "revenue by region last quarter" |
| Graph RAG | Knowledge is organized as a graph of entities and relationships, and retrieval follows connections, for example GraphRAG | Questions about relationships across many documents |
| Long context | Whole documents are given to a model with a very large context window, without retrieval | Small, bounded document sets; one document at a time |
Modern assistants often combine these: the model has tools for document search, structured queries and business APIs, and chooses what it needs.
Evaluating RAG
Evaluate retrieval and generation separately, because they fail for different reasons. Retrieval quality is measured as for any semantic search. For generation, the questions are:
| Question | Metric |
|---|---|
| Is every statement in the answer supported by the retrieved chunks? | Faithfulness (groundedness) |
| Does the answer actually address the question? | Answer relevance |
| Are the citations correct? | Citation accuracy |
| Does the system say "I don't know" when the answer is not in the sources? | Correct refusals |
Build an evaluation set of real questions with known answers and source documents, and run it on every change to chunking, retrieval settings, prompt or model. Tools such as Ragas and promptfoo automate this. Production feedback (thumbs up or down, follow-up questions) shows where the system fails in practice.
Tools
| Category | Examples |
|---|---|
| Frameworks | LlamaIndex, LangChain |
| Search and vector stores | See semantic search |
| Managed RAG services | Search and knowledge services from cloud providers, such as Azure AI Search, Amazon Bedrock Knowledge Bases and Google Cloud search services |
| Evaluation | Ragas, promptfoo |
Frameworks speed up prototypes. For production, many teams keep the core pipeline in their own code, where each step can be tested, observed and tuned.
Common pitfalls
In addition to the pitfalls of semantic search:
- Poor source content: outdated, duplicated or contradictory documents produce contradictory answers. RAG makes content quality visible; it does not fix it.
- Flattened documents: chunking PDFs as plain text, losing tables, headings and context.
- Too much context: stuffing the prompt with many loosely related chunks, which raises cost and lowers answer quality.
- No "I don't know": prompts that push the model to always answer, which invites invented facts.
- No evaluation: tuning by trying a few questions by hand, and breaking other cases with every change.