Blog
What Is RAG? Retrieval Augmented Generation Explained for AI Agents
What Is RAG (Retrieval Augmented Generation)
RAG stands for Retrieval Augmented Generation: instead of relying only on what a language model learned during training, it first retrieves relevant information from an external source - a document library, internal procedures, legal texts - and only then generates an answer grounded in what it found.
Without RAG, a model answers from memory that has a training cutoff and no knowledge of a company's internal documents. Asked about a leave policy or a recent invoice, it can produce a confident-sounding answer that is simply wrong - a failure mode known as hallucination. RAG fixes this by grounding the answer in real documents instead of guesswork.
In practice, RAG has become a standard building block of AI agents. Any system meant to answer employee or customer questions using company knowledge needs some form of document retrieval before the model writes a word. Clients rarely ask for RAG by name, but they almost always need a mechanism that keeps the agent's answers tied to their own documents, not the model's general knowledge.
How RAG Works, Step By Step
The process starts with a user's question - an employee on WhatsApp asking how many vacation days they have left, or a customer asking about an order. The system converts that question into a numeric vector (an embedding) and searches a vector database for chunks with similar meaning, since an embedding model maps text so that similar meanings end up close together even with different words.
The retrieved chunks - usually a handful of passages - get passed to the model together with the original question, as context. That is the augmentation step: the model receives not just the question but the source material it should rely on. A well-built system often adds a reranker here too, re-scoring the chunks from most to least relevant.
The last step is generation. A well-built RAG setup instructs the model to cite its source - an article number, a document name, a date - so the answer can be checked, not just trusted. That has to be enforced deliberately in the system prompt, since a model will not always cite a source unless told to.
RAG vs Fine-Tuning vs a Long Prompt
Fine-tuning retrains a model on your own data, adjusting its internal weights. It is expensive, slow, and static: every document change means retraining, which is a poor fit for knowledge that shifts weekly - rates, procedures, HR paperwork.
Stuffing every document into one long prompt has limits too - even large context windows have a ceiling, and the more text packed in, the harder it is to find the right passage and the more each query costs.
RAG sits between the two and wins for most business cases: the knowledge base updates by adding or swapping a document, no retraining involved, and only passages relevant to a given question enter the prompt. The approaches often get combined - RAG for facts, a system prompt for tone, business rules for anything the model should not interpret at all.
Hybrid Search: Semantic Meaning Plus Exact Keywords
Semantic search alone (via embeddings) sometimes misses exact phrases - a statute number or a proper name that needs a literal match, not just a meaning match. That is why production RAG systems pair it with classic full-text search such as BM25, which handles literal matches well, and matters even more in highly inflected languages where exact-match search has to account for word forms.
That is the hybrid approach behind Janina, an HR assistant in production since 2026. It searches over 900 documents and 40 legal acts, including Polish Supreme Court rulings, combining semantic search with BM25, and every answer cites the statute article it is based on so an HR team can verify it rather than take the model's word for it. The weighting between the two takes experimental tuning against real user questions - there is no universal setting.
The same logic applies to any field with dense, formal documentation - labor law, ISO procedures, internal policies. The more literal references a domain has - numbers, dates, names - the more hybrid search pays off over pure semantic retrieval. In less formalized fields, like customer support, semantic search alone is usually enough.
RAG Inside AI Agents: From Answering To Acting
RAG by itself answers questions. Paired with an AI agent, it can also act - checking a CRM record, booking a meeting, generating a document. That is what my AI agents service does: it picks up requests from WhatsApp, email, phone, or an app, searches company documents through RAG, and carries out tasks inside the client's own systems, built on Anthropic and OpenAI models, MCP, and tool use.
For simpler cases - one channel, one question, one answer from a knowledge base - a single agent with RAG is enough. For multi-step processes touching several systems, I add orchestration, subagents, evals, and observability - that is multi-agent systems, a separate service on the same retrieval foundation.
One example between the two: a support agent handling service tickets checks RAG for coverage in the technical knowledge base and, if found, answers immediately, citing the procedure; if not, it hands the case to a human with a summary of what it learned, instead of guessing. That decision - answer or escalate - matters as much as the retrieval itself.
Common Mistakes When Implementing RAG
The most common mistake is bad chunking - pieces too large dilute retrieval accuracy, too small lose the context of a sentence or clause. Good chunking follows the document's natural structure - headings, bullet points, paragraphs - rather than a fixed character limit.
The second mistake is skipping source citations, so nobody can tell whether the model relied on a document or started making things up. The third is treating the knowledge base as a one-time project - documents change, and without re-indexing, RAG quietly starts answering from stale data.
The fourth mistake is skipping evals and observability - no way to measure answer accuracy before real users see them, or whether a chunking change actually helped.
A fifth mistake, common in projects handling sensitive data - HR, healthcare, finance - is ignoring data residency and GDPR from the start, instead of planning for pseudonymization and a clear processing location from day one. Related to this is access control - a RAG system spanning departments has to respect who is allowed to see which document.
Cost And Timeline: Rough Market Ranges
The cost of a RAG rollout depends on how many and how complex the documents are, whether hybrid search is needed, and whether data stays in the client's cloud, an EU cloud, or on-premise. These are rough market ranges for small and mid-size companies in 2026, not a fixed price list - an actual quote always follows a conversation about the specific case.
My process is the same regardless of scale: first a 60-minute call, where you describe what eats your team's time and I describe what can realistically be automated, then a two-week prototype - a working agent with RAG on your company's real data, not sample data. Only after you verify it do we move to production, with cost controls, logging, and GDPR compliance from day one, and ongoing monitoring for as long as needed.
The two-week prototype on real data is a deliberate choice, not a shortcut. A client's documents almost always have their own quirks - inconsistent formatting, scans instead of text, outdated versions next to current ones - that only surface once you work with the real set. Better to find those problems in week two than months into a system that cannot handle the client's actual documentation.
Frequently asked questions
Does RAG completely eliminate hallucinations?
No, but it sharply reduces them. The model can still misread a retrieved passage, which is why a solid RAG setup cites its source and measures answer accuracy with evals before going to production.
Do I need my own vector database to use RAG?
In most cases, yes - it stores document embeddings and lets the system quickly find passages matching a question's meaning. For documents full of literal references, like legal texts, I add full-text search such as BM25 alongside it.
How many documents do I need for RAG to make sense?
There is no fixed threshold. Janina searches over 900 documents and 40 legal acts, but RAG works well with much smaller collections too - what matters is the quality and upkeep of the documents, not the raw count.
Can RAG handle documents in mixed formats, like scans or PDFs?
Yes, but it needs an extra step before indexing - OCR for scans, text extraction from PDFs and tables, and formatting cleanup. This only surfaces in a prototype built on the client's real documents, which is exactly why I start there instead of a demo sample.
See also
If you are wondering whether RAG makes sense for your business, let's talk it through in a short call.
Contact
Got a process that eats your team's time?
Send me two sentences about the problem. I'll reply and tell you straight whether it's worth automating.