RAG (retrieval-augmented generation) is a setup in which the model answers from retrieved passages of your documents instead of its own memory. The system first finds relevant pieces of text, then passes them to the model with the question and asks it to answer only from them, citing the sources.
How RAG works.
01 /
Preparation
Documents are converted to text, split into passages of a few paragraphs, and stored with metadata: source, section, date, access rights.
02 /
Index
Each passage gets an embedding (a numeric vector used to find texts with similar meaning), stored in a vector index: pgvector in PostgreSQL, Qdrant, OpenSearch.
03 /
Retrieval
For the user’s question, the system finds the closest passages among those this user is allowed to see.
04 /
Answer
The model receives the question and the retrieved passages and answers from them, showing where each statement comes from.
The key point
If search does not find the right passage, the model cannot answer correctly. That is why most of the work on RAG goes into search and documents.
First check whether you need RAG.
01 / OPTION
Documents fit in the context
If there is little material, say a few dozen pages, and it rarely changes, you can give it to the model in full. That is simpler and often more accurate.
02 / OPTION
You need exact data from a database
Stock levels, order statuses, and amounts come from a database or API query. That is a job for an agent tool, for example through an MCP server. Text search gets these wrong.
03 / OPTION
RAG
There are many documents, they change, different employees have different access rights, and the answer must cite its source.
A question to check
Which questions do employees or customers ask most often, and where do the answers live today?
Documents are the main part of the work.
An answer cannot be better than the text extracted from the documents. The problems only show up when you look at real files:
- Scans and PDFs. Scans need text recognition. In PDFs, tables and columns often get jumbled during extraction. Such files are checked by hand on a sample.
- Chunking. Passages that are too small lose context; passages that are too large blur the search. Split along the document’s structure (sections, clauses) and add the document and section title to each passage.
- Versions and duplicates. Three editions of the same policy produce three different answers. You need a marker for the current version; older editions are excluded from search or flagged.
- Updates. When a document changes, its passages are recomputed; when it is deleted, they are removed from the index. Otherwise the assistant cites things that no longer exist.
Search: vectors, keywords, and reranking.
Vector search finds texts with similar meaning even when the wording differs. But it handles part numbers, contract numbers, error codes, and rare terms poorly. So hybrid search is the usual choice: vector search together with full-text search (BM25), with the results merged.
The second step is reranking: a separate model (a reranker) re-scores the retrieved passages against the question and keeps the best ones. It usually improves accuracy at the cost of a little latency.
For non-English content, choose an embedding model that supports the language and test it on your own questions: results from English benchmarks do not always carry over.
Access is checked at search time.
If an employee cannot see a document in the source system, the assistant must not quote it. Permissions are stored in each passage’s metadata and applied at search time, before any text reaches the model. Asking the model to “hide restricted content” does not work: anything in its context can end up in the answer.
- Permissions come from the source system (file storage, Confluence, CRM) and are synced when they change.
- Decide which data may be sent to an external model. If needed, run a model inside your own infrastructure.
- A log of questions and retrieved sources is needed to investigate errors and to check who saw what.
How to evaluate quality.
RAG quality is measured on a set of real questions with known answers. The set is collected before development: questions from employees or customers, each with the correct answer and the document that contains it.
- Retrieval. Is the right document among the top results (recall@k)? If not, changing the answer model will not help.
- Answer. Does the answer rely only on the retrieved passages, and are the citations correct? Libraries such as Ragas help automate this, but a person still reviews a sample.
- Refusal. If the documents do not contain the answer, the system should say it found nothing instead of making something up.
- Cost and latency. What one answer costs and how many seconds the user waits for it.
A simple rule
Every change to chunking, the embedding model, or the prompt is run against the test set before release.
What to bring to the first conversation.
You do not need an architecture. Describe who will ask questions, about which documents, and where those documents are stored.
- Which documents and in what formats: PDF, Word, scans, Confluence, a knowledge base?
- Roughly how many are there, and how often do they change?
- Is access restricted, and which data must not be sent to external services?