How RAG Works: A Step-by-Step Walkthrough
A step-by-step walkthrough of retrieval-augmented generation. From document ingestion to answer generation, how each stage works and why it matters.
A step-by-step walkthrough of retrieval-augmented generation. From document ingestion to answer generation, how each stage works and why it matters.
A RAG system works in two phases. The offline phase prepares your documents for search. The online phase answers questions in real time. Both phases share the same embedding model, which is what makes the system work.
Let's walk through each step.
Before the system can answer questions about your data, it needs to read your data. Ingestion handles the conversion from whatever format your documents are in (PDF, Word, HTML, email, spreadsheet) into clean, searchable text.
This step is more complex than it sounds. PDFs are notoriously difficult. They're a visual format, not a text format. Tables, headers, multi-column layouts, and scanned documents all need different handling.
A typical ingestion pipeline:
Common pitfall: Skipping proper ingestion and feeding raw PDF text into the system. The garbage-in, garbage-out principle applies hard here.
Once you have clean text, you split it into chunks, smaller pieces that can be individually indexed and retrieved. The chunk is the unit of retrieval. When someone asks a question, the system finds the most relevant chunks and passes them to the language model.
Why chunk? Language models have context limits, and not every part of a 50-page document is relevant to every question. Chunking lets the system find just the relevant passages.
Chunking strategies:
For most business use cases, sentence-based chunking with 500–800 token windows and 100 token overlap is a solid starting point.
Each chunk is converted into a vector, a list of numbers that represents its meaning. The embedding model maps text into a high-dimensional space where similar concepts are close together.
"How do I apply for leave?" and "What's the annual leave policy?" are different strings but similar in meaning. A good embedding model places them near each other in vector space, so when the system searches for one, it finds content relevant to both.
Embeddings are generated once during ingestion and stored in the vector database alongside the original text and metadata. At query time, the user's question is also embedded using the same model.
This is where the magic happens. The system takes the query embedding and searches the vector database for the most similar chunk embeddings. The top-k results (usually 3–10) are returned as context for the language model.
Retrieval quality directly determines answer quality. If the system retrieves the wrong chunks, even the best language model can't produce a good answer.
Ways to improve retrieval:
The retrieved chunks are assembled into a prompt along with the user's question and a system instruction (e.g., "answer using only the provided context"). The language model generates a response grounded in that context.
The system prompt typically includes:
Post-generation, the system may add source citations, validate the response against the retrieved context, or apply additional guardrails before returning it to the user.
The full flow looks like this:
Want the full architecture breakdown? Read RAG Systems Explained for component choices, trade-offs, and deployment considerations.
Meet the person
This article comes from real projects. If it raises a question about your own system, you can ask the founder directly.
HELLO PEOPLE designs, builds and looks after AI, software, app and data solutions for Australian businesses, with senior expertise on every project and a scope agreed before work starts. For software, that means starting with how your business runs, not with the code.
Since 2007, HELLO PEOPLE has delivered more than 100 projects from Perth for small and medium businesses across Australia: custom software and apps, system integrations, data migrations, reporting and dashboards, and AI that works inside the systems a business already runs.
I lead every engagement myself. I trained in accounting before moving into IT, hold accounting and IT professional qualifications and an MBA, and bring more than 20 years of experience across sales, service delivery, inventory and compliance. I am also a PhD candidate in AI at Curtin University, researching retrieval-augmented generation (RAG), so the technology is always judged by what it does for the business.
Small and boutique. The person who scopes your software is the person who builds it, and the same person is there on launch day.
No ticket queue and no account manager in between. You hear back within one business day, usually sooner.
The first release is the start, not the end. When you need the next system, integration or report, you call the same person, who already knows your business.
Ask the author
Ask it here and it comes straight to the founder. No sales call, no obligation, and a real answer even if the answer is that you do not need us.
Kasun Wijayamanna
Founder, replies within one business day
Tell us what you're working on. We'll come back with a practical recommendation and clear next steps.
Built here. Your data stays here.
Thanks for reaching out. We will get back to you within one business day.
See what else we do