Skip to main content
PreparationZone Logo
PrepZone

RAG Architecture and the Six-Step Workflow

Retrieval plus generation: indexing, top-k search, prompt augmentation, and every box in a production pipeline.

Pass checkpoint below

Why this matters

  • PolicyDesk's design review failed when the engineer described RAG as "we call the LLM with the PDF." The panel wanted the boxes: load, split, embed, store, search, augment, generate.
  • You cannot debug a wrong leave answer until you know which box failed.
  • The same six steps work for HR policy, a support bot, or a research assistant. Only the loaders change.
  • This is the diagram interviewers draw on the whiteboard before they ask about chunk size.

What the architecture is for

RAG combines information retrieval with generative models. Retrieval finds a small set of passages that are actually about the question. Generation writes the reply. The LLM is allowed to use private or domain text without that text having been baked into the weights.

The system is a pipeline with independent stages. You can swap the PDF loader, the embedding model, or the chat model without rewriting the others. That modularity is the reason production systems do not put "call the model" in one function with the file parser.

What you are building

  • A way to pull relevant text from files, sites, databases, or APIs.
  • A prompt that places that text next to the user's question.
  • A generator that writes the final answer from the augmented prompt.
  • An index you can rebuild when the handbook changes, without retraining.

Two phases, not one request

Most of the work happens before anyone asks a question. That is the indexing phase. The query phase is the short path the user waits on.

Indexing, ahead of time
DocumentsPDF, wiki, DB
Chunk and clean
Embed chunks
Vector database
Query, on each question
User query
Embed the query
Top-k chunks
Prompt + LLM
Final answer
Indexing is offline and slow. The query path should only embed the question, search, and generate.

If you embed the whole handbook on every question, latency and cost collapse the product. Embed documents when they change. Embed only the question at request time.

High-level query path

User query
Retrieval
Query embedding
Vector search
Top-k chunks
Generation
Prompt augmentationQuery + context
LLM
Final answer
Retrieval finds text. Generation writes. Do not merge those responsibilities in your explanation.

The six steps

Step by step

  • 1. User query. The person asks in normal language: "What is the leave policy at Northline Bank?" No schema, no keywords required, though keywords still help retrieval later.
  • 2. Query embedding. A model turns that sentence into a vector, a list of numbers. The same embedding model must be used for chunks and for queries. Mixing text-embedding-3-small on the index and another model on the query puts them in different spaces. Search then returns neighbors that are not neighbors.
  • 3. Document retrieval. The vector database finds chunks whose vectors are close to the query vector. "Close" is a similarity metric, usually cosine. You ask for the top k, often 3 to 8, not the whole corpus.
  • 4. Context preparation. You do not paste raw search hits blindly. You keep the text, attach the source, drop duplicates, and respect a token budget. A chunk that is rank 1 but from an archived policy can be filtered out here.
  • 5. Prompt augmentation. The user question and the prepared chunks are placed in a template. The template also carries instructions: answer only from the context, cite the source, say you do not know if the context is silent.
  • 6. Answer generation. The LLM writes the final answer. It should be a reader of the context, not a second retriever. If the context lacks the answer, the correct generation is a refusal, not a guess.

The full indexing pipeline

Between "we have PDFs" and "we can search" there is a preparation line. Skip a stage and retrieval quality drops in a way that looks like a model bug.

StageWhat happensPolicyDesk example
LoadRead bytes from the source and extract textOpen the HR handbook PDF
SplitBreak the file into sections before chunkingOne document per policy chapter
CleanStrip headers, footers, page numbers, extra whitespaceRemove the repeated 'Northline confidential' footer
ChunkCut text into retrieval-sized pieces with overlapAbout 500 tokens, overlap so a rule is not sliced off its exception
MetadataAttach source, page, title, department, versiondepartment=HR, version=2026-03
EmbedConvert each chunk to a vector1536 numbers from the embedding model
StoreUpsert vector plus payload into the indexQdrant collection hr_docs
  • Load

    What happensRead bytes from the source and extract text
    PolicyDesk exampleOpen the HR handbook PDF
  • Split

    What happensBreak the file into sections before chunking
    PolicyDesk exampleOne document per policy chapter
  • Clean

    What happensStrip headers, footers, page numbers, extra whitespace
    PolicyDesk exampleRemove the repeated 'Northline confidential' footer
  • Chunk

    What happensCut text into retrieval-sized pieces with overlap
    PolicyDesk exampleAbout 500 tokens, overlap so a rule is not sliced off its exception
  • Metadata

    What happensAttach source, page, title, department, version
    PolicyDesk exampledepartment=HR, version=2026-03
  • Embed

    What happensConvert each chunk to a vector
    PolicyDesk example1536 numbers from the embedding model
  • Store

    What happensUpsert vector plus payload into the index
    PolicyDesk exampleQdrant collection hr_docs

Cleaning matters more than people admit. A footer repeated on every page becomes a chunk that matches almost every query, because it is on every page. You then retrieve the footer instead of the leave table.

Prompt shape

The augmented prompt has three jobs: role, rules, and evidence.

Java
You are PolicyDesk, Northline Bank's HR assistant.
Answer the question using only the context below.
If the answer is not in the context, say you do not know.
Cite the source title and page.

Context:
{retrieved chunks}

Question:
{user question}

Answer:

The braces here are slots your code fills. They are not something the user types. Keep the context visually separated from the question so the model can tell evidence from the ask. That separation also matters for safety when the document itself contains instructions, which is a later topic.

Worked example

Question: "What is our leave policy?"

User queryWhat is our leave policy?
RetrieveTop-k HR chunks
AugmentQuery + policy text
LLMGenerate
AnswerGrounded, with page
Retrieval returns the HR chunk. The model never sees the rest of the handbook.

A good answer names the numbers that are in the chunk and points at the file: "Casual leave is 8 days and privilege leave is 18 days, from Leave Policy, page 5, March 2026 edition." A bad answer adds a carry-forward rule the chunk never mentioned. That extra sentence is the model leaving the context. The template exists to make that departure less likely.

The components

ComponentResponsibility
Data loaderRead PDF, HTML, office files, databases, or APIs into text plus metadata
Text splitterCut documents into chunks small enough to embed and retrieve
Embedding modelTurn text into vectors with a fixed dimension
Vector databaseStore vectors and search the nearest ones, with filters on metadata
Prompt augmentationBuild the final prompt from the question, the chunks, and the rules
LLMGenerate the answer from that prompt
  • Data loader

    ResponsibilityRead PDF, HTML, office files, databases, or APIs into text plus metadata
  • Text splitter

    ResponsibilityCut documents into chunks small enough to embed and retrieve
  • Embedding model

    ResponsibilityTurn text into vectors with a fixed dimension
  • Vector database

    ResponsibilityStore vectors and search the nearest ones, with filters on metadata
  • Prompt augmentation

    ResponsibilityBuild the final prompt from the question, the chunks, and the rules
  • LLM

    ResponsibilityGenerate the answer from that prompt

Add two more boxes in any system you expect to operate: an evaluation set (questions with known good sources) and a citation renderer (show the user which chunk was used). They are not required to draw the happy path. They are required to know if the happy path is lying.

Why this shape wins

Benefits

  • Answers can track documents that change monthly, without a training run.
  • Private text stays in your index. You send only the retrieved snippets to the model API, which is still a privacy decision you must make deliberately.
  • Domain accuracy improves because the domain words are in the prompt, not hoped for in the weights.
  • You can explain an answer by showing the chunk. Trust follows the citation.
  • You can scale the corpus by scaling the index, and scale traffic by scaling the stateless query service.

Where people use it

Common use cases

  • Company knowledge and HR policy. Handbooks, benefits, holiday calendars. PolicyDesk is this case.
  • Customer support. Past resolutions and product docs, so the bot stops inventing refund rules.
  • Research assistants. Papers and internal notes, with citations the reader can open.
  • Enterprise search. Find the paragraph, then summarize it, instead of returning ten blue links alone.
  • Legal, healthcare, and finance. High-stakes text where an unsourced sentence is a defect, not a style choice. These domains also need access control and a human for decisions the document does not settle.

Quick recall

Everything you need if you only revisit this box.

  • RAG is retrieval plus generation, connected by a prompt.
  • Index documents ahead of time. At query time, embed only the question.
  • Six query steps: question, query embedding, retrieval, context prep, prompt augmentation, generation.
  • Indexing stages: load, split, clean, chunk, metadata, embed, store.
  • Same embedding model for documents and queries.
  • Use cases include policy, support, research, enterprise search, and regulated domains that need citations.
  • Debug by asking which stage produced the wrong text.

Test yourself

Answer these before moving on — recall is what makes it stick.

Checkpoint quiz

Pass 2 of 3 to verify this article on your path — scrolling alone does not count.

Question 1 of 3

Service A calls B synchronously. When B gets slow, A's threads pile up and A goes down too. What helps most?