What Is RAG in AI? How It Works, Using a System We Built
RAG lets an AI model answer from your own documents and show its sources. Here's how it works, step by step, using the assistants we built for a Canberra building and pest inspection firm.
By Matthew Clarkson · October 2, 2025 · Updated November 17, 2026

RAG stands for retrieval-augmented generation. Before an AI model answers a question, the system searches a set of documents you control, picks out the passages that matter and hands them to the model with the question. The model writes its answer from those passages and says which ones it used.
On its own, a language model answers from its training. That stops at a cut-off date, never included your internal documents, and when the model doesn't know something it often guesses with confidence. RAG puts the right text in front of the model at the moment it answers. Nothing is retrained: when a document changes, you re-index it and the next answer uses the new version.
The hard part is retrieval: finding the right passage, from the right document, every time. Most of this post is about that.
A real example: assistants for building inspectors
We built an inspection management portal for a Canberra building and pest inspection firm. The case study covers the whole system. Part of it is four AI assistants for the inspectors: one for compliance, one for report comments, one for termite risk and one for energy ratings. An inspector asks a question in plain English, sometimes from a roof cavity, and gets an answer drawn from the firm's own library, with the sources listed underneath.
The library holds ACT Acts and Regulations, technical standards, the firm's internal procedures and reference guides, and corrections written by senior staff. These don't always agree, and an older internal note can be out of step with the current law, so the system has to know which source wins.
Step 1: documents in
Staff upload a document and assign it to one or more assistants. Each document has a source type, and each source type has an authority rank. Rank 1 is the most authoritative. The defaults are "Authoritative", "Standard" and "Procedure", and the firm can rename them or add its own.
Getting clean text out is more work than it sounds. For a PDF, the system reads the text layer and checks it twice: a structural check for obvious garbage, then an AI model compares the extracted text with the original pages to catch columns or table cells out of order. If the text layer fails, or there isn't one (a scan or a photo), a vision model reads the pages and returns Markdown with page markers.
Contents pages are removed first. A contents page mentions every subject in a long Act without saying anything about any of them, so it matches lots of questions and answers none.
Step 2: chunks
An Act running to hundreds of pages is too big to search as one block, so it's split into chunks: passages of up to about 1,500 characters. The split follows the document's structure. A new heading starts a new chunk, and the heading is stored with the chunk so a citation can point at the right section.
Legislation needed extra handling. A PDF of an Act doesn't mark its sections as headings, so sections were cut wherever the character limit fell. In one case, part of the retaining walls section ended up at the tail of the chunk about fences. The chunker now starts a new chunk at each section number.
Tables are kept whole where they fit. A larger table is split by rows, with the header row repeated in every part.
Step 3: context and embeddings
Each chunk then gets one sentence of context, written by a small, fast model (Claude Haiku 4.5). The sentence names the document, the part or section the passage sits in, and what the passage is about in the words an inspector would use.
This fixed a real problem. An inspector asks "does this retaining wall need approval", and the firm's internal guide answers in nearly the same words. The Act says "designated development for a Class 10b structure". Matching on meaning alone favours the guide, because its wording is closer to the question, so the Act that actually controls the answer gets passed over. The context sentence gives the statute something a plain-English question can match. Anthropic described this technique as contextual retrieval in 2024.
Next, each chunk and its context sentence are turned into an embedding: a list of 1,024 numbers that represents the meaning of the text, so that passages about similar things get similar numbers. We use Voyage AI's voyage-3-large model. It encodes stored passages and incoming questions slightly differently, which helps a short question find a long passage.
The embeddings are stored with pgvector, a PostgreSQL extension, in the same database as the portal's jobs, bookings and contacts. An HNSW index keeps the nearest-neighbour search fast. The same chunks also get a standard PostgreSQL full-text index.
Step 4: retrieval
When an inspector asks a question, the system runs two searches over the documents assigned to that assistant:
- a vector search, which finds passages with a similar meaning even when the words differ
- a keyword search, which finds exact terms such as a section number, which a vector search can miss
Each returns up to 40 candidates. The lists are merged with reciprocal rank fusion, which scores each passage by its position in each list, so the two searches' different scoring scales don't matter. A reranking model (Cohere Rerank 3.5) then reads the question against each candidate and sets the final order.
Step 5: ranking by authority
Similarity on its own favours whichever source is written in plain English, and that's usually the least authoritative one. So the system runs both searches a second time over the top authority tier alone, and it holds three of the final places for passages from that tier. The controlling source is always in front of the model, even when a summary matches the question more closely.
Each passage is then widened to its whole section (up to about 6,000 characters), or to its neighbouring chunks if the section is longer. A threshold and its exemptions often sit in different clauses of one section, and a lone chunk invites an answer from the wrong clause.
Six passages go to the model, each labelled with its document, section, source type and authority rank.
Step 6: the answer, with citations
The answer comes from a Claude Sonnet model. Its instructions come down to these:
- Answer only from the sources provided.
- Cite every substantive statement with a numbered marker.
- A lower rank outranks a higher one, and the answer states its basis.
- If two tiers conflict, say so instead of quietly reconciling them.
- If a fact the answer depends on is missing, such as the height of the wall or its distance from the boundary, ask for it before answering.
Temperature is set to zero. These answers interpret legislation, so two inspectors asking about the same wall should get the same determination.
Below each answer, a sources panel lists the passages the answer actually cited, grouped by document, with source type and rank. Each citation shows the section and page, links to the document at that page, and expands to show the exact passage.
When an inspector marks an answer as wrong, the assistant asks a few follow-up questions and drafts a correction. Once an admin approves it, it becomes a guidance document or a standing rule.
When nothing relevant is found
Retrieval has no score cut-off. It always returns its best six passages, even when the best are poor. So the model is told plainly: if the sources don't contain the answer, say so, and don't fill the gap from general knowledge. A question outside an assistant's scope gets the same treatment. If no passages come back at all, the model is told that none were retrieved.
I'd much rather an inspector reads "the sources don't cover this" than gets a plausible answer with nothing behind it. That's most of the reason to build RAG in the first place.
We check this with an evaluation set of scenarios written by the firm, each with its expected outcome. Any change to retrieval, the instructions or the model can be run against it and compared with the last run. It can also run with one authority tier switched off, because a staff correction can disagree with the Act it's meant to explain, and that's better measured than argued about.
When RAG is the wrong tool
- The documents fit in the prompt. If the relevant material is a few hundred pages or less, give the model all of it. That's simpler and often more accurate. Our post on training AI on your own data covers that choice and fine-tuning.
- The question is about numbers in a database. "How many inspections did we do in Belconnen last month" is a database query. Retrieving passages of text won't add anything up.
- Nobody owns the documents. If the library is full of outdated and contradictory files and nobody keeps it current, the assistant will cite them faithfully. An authority hierarchy helps, but someone still has to retire old versions.
- An off-the-shelf tool already does the job. If your files are in SharePoint and Microsoft 365 Copilot answers your team's questions well enough, use it.
Frequently asked questions
What does RAG stand for in AI?
Retrieval-augmented generation. The system retrieves relevant passages from your documents, then a language model generates an answer from them and cites them.
Is RAG the same as fine-tuning?
No. Fine-tuning changes a model's weights using training examples. RAG leaves the model as it is and gives it the right text when it answers, so updating a document updates the answers. Fine-tuning suits narrow cases, such as a fixed output format.
Do I need a separate vector database for RAG?
Usually not. pgvector adds vector search to PostgreSQL, and that's what we used here: the knowledge base sits in the same database as the rest of the app. A dedicated vector database scales further, but for one business's document library, having one less system to run matters more.
Does RAG stop AI hallucinations?
It reduces them and makes them easier to catch. The model can still misread a passage or lean on the wrong one. Citations let a person check the answer against the source, and an evaluation set tells you when a change has made answers worse.
Can RAG work with scanned PDFs?
Yes, if the text is extracted properly first. Here, scans, photos and PDFs with an unreliable text layer are read by a vision model, which keeps page markers so citations still point at the right page.
If you want an assistant like this over your own documents, see how we build custom AI assistants or book a 15-minute call.
Last updated on November 17, 2026
Free tool · 3 minutes
AI Readiness Assessment
Score how ready your business is for AI in a few minutes, with tailored next steps.
Continue Reading
View all
Post-Quantum Cryptography: What Australian Businesses Should Do Now
An AI model found a theoretical attack on the new encryption standards, and Cloudflare, OpenAI, Anthropic and others now want a faster move. What it means in plain English, and what Australian businesses should do.
Dec 4, 2026

AI Document Processing
How AI document processing reads, sorts and checks invoices, forms and records, what to verify on accuracy and privacy in Australia, and how to start with one document type.
Nov 23, 2026

AI Implementation Specialist: Hire One or Hire a Consultancy?
AI implementation specialist is the job title of the moment. Before you write the job ad, here is what the role covers, what the one-hire pitch leaves out, and why a process audit should come first.
Nov 13, 2026
Sick of reading about automation?
Book a free 15-minute intro call. We’ll talk through what you’re trying to automate and whether we’re a good fit.