AI & automation · Guide
What is RAG, and when should a business use it?
RAG (retrieval-augmented generation) is a technique in which a system retrieves the most relevant passages from company documents and databases and passes them to the language model along with the question. The answer is grounded in that data and cites its source. It fits when the knowledge is your own and changes often.
Master TimmUpdated on 6 min read
On this page
How RAG works, step by step
A language model only knows what it learned in training and what it receives in its context window. RAG takes care of the second part: for every question, it finds what matters and puts it in the context. The work splits into preparing the data, once and on every change, and answering, on every question.
- Ingestion and chunking: Documents are extracted, cleaned and split into passages (chunks), sized and overlapped so an idea is not cut in half.
- Embeddings: An embedding model turns each passage into a numeric vector that represents its meaning.
- Indexing: Vectors, text and metadata (source, date, permissions) go into a vector store, such as pgvector on PostgreSQL.
- Hybrid search: The question is matched by meaning (vectors) and by keywords, which finds paraphrases as well as exact codes, acronyms and names.
- Reranking: A model reorders the candidates by relevance to the question, and only the best ones enter the context.
- Generation with citations: The model answers from the passages it received and shows which document each statement came from.
When a business should use RAG
RAG is the natural choice when the answer depends on the company's knowledge rather than the model's general knowledge. The most common signs:
- Your own knowledge base: Manuals, policies, contracts, tickets or technical documentation the model has never seen.
- Information that changes: Catalogs, rules and procedures updated often; you reindex instead of retraining the model.
- Answers that must be audited: The person asking needs to see the source, as in legal, compliance, technical support and customer service.
- A large corpus: The content does not fit in the context window, or would fit only at a high cost per question.
When the data is tabular, such as orders, balances or metrics, retrieval is usually a structured query (SQL or an API) rather than a similarity search. In Contas Casal, Master Timm's own product, the assistant runs on a structured RAG pipeline: intent routing, 10 parallel SQL retrieval tools and a semantic cache.
RAG, fine-tuning or an agent: what each one solves
The three are often confused, but they change different parts of the system. RAG changes what the model knows at question time. Fine-tuning changes how the model behaves: tone, format or a narrow task. An agent changes what the model can do, by calling tools over several steps.
- RAG: Answer what the current return policy is, citing the document and its version.
- Fine-tuning: Sort tickets into internal categories, when an eval shows a gain over prompt and context.
- Agent: Look up the order in the ERP, check the policy and open the return, with approval on sensitive actions.
They combine: an agent can have RAG search as one of its tools and decide when to use it. The choice between RAG and fine-tuning for company data is covered in detail on the AI & LLM engineering page, linked right below.
Why RAG fails, and how to prevent it
When RAG fails, the cause is usually retrieval, not the model: if the right passage never reaches the context, the model answers with what it has, and the mistake looks like a correct answer.
- Badly cut chunks: The rule sits in one passage and the exception in another; splitting by the structure of the document, with overlap, fixes much of it.
- Different vocabulary: People phrase things one way and documents another; hybrid search and rewriting the question help.
- Stale or duplicate sources: Two versions of the same policy compete; date and version metadata and a cleaned-up corpus prevent the mix-up.
- Ignored permissions: Search returns a passage the person may not see; the access filter has to apply inside the search itself.
- Hidden instructions: A document carries text that tries to change how the model behaves (indirect prompt injection); retrieved content is treated as data, never as a command.
- Calculation questions: Sums, averages and rankings call for SQL, not text similarity.
How to measure RAG quality
RAG is measured in two parts, because each one fails in its own way: retrieval and the answer. Both use the same evaluation dataset, with real questions, the expected answer and the document that should be found.
- Retrieval: The right passage shows up among the top results (recall@k), and irrelevant ones stay out.
- Faithfulness: Every statement in the answer is supported by the retrieved passages, with nothing made up.
- Relevance and citation: The answer addresses the question and points to the right source.
- Operations: Latency, tokens and cost per question, tracked through traces in production.
The evaluations combine code checks, sampled human review and LLM-as-a-judge, and they run on every change to chunking, the embedding model, the prompt or the language model.
Related service
AI & LLM Engineering
AI agents, RAG and MCP in production, not in demos.
Free first conversation · Reply within 1 business day
See also: AI Customer Service Agents and AI for E-commerce.
Frequently asked questions
With large context windows, do we still need RAG?
In most cases, yes. Putting the whole corpus in the prompt makes every question more expensive, because every token is billed on every call, and quality can drop when the relevant passage gets lost in the middle of a long text. For a small, stable knowledge base, a long context with prompt caching can be enough; a test on an evaluation dataset decides.
Do we need a dedicated vector database for RAG?
Not always. pgvector adds vector search to PostgreSQL, with HNSW and IVFFlat indexes, and combines with the database's own full-text search for hybrid search, next to the relational data and permissions you already have. Master Jobs, Master Timm's own product, runs on PostgreSQL with pgvector. A dedicated vector database makes sense at very large volumes or when it offers something the project needs.
Can RAG show confidential documents to people who should not see them?
It can, if permissions are ignored at search time, and that is one of the risks OWASP lists for systems built on embeddings. The protection is to store each passage's permissions as metadata and filter the search by the access of the person asking, with isolation between customers in multi-tenant systems and a log of every retrieval.
How does RAG stay current when documents change?
By reindexing, with no retraining. A routine detects new, changed or deleted documents and updates only the affected passages, with the date and version in the metadata. The answer then reflects the current version, and the evaluation shows whether the change affected quality.
Sources
Master Timm · Updated on