· 15 min read

RAG Architecture: Components, Patterns and Trade-Offs

A RAG demo is three steps: embed the question, fetch similar text, ask the model. A RAG system that gives correct, permitted and current answers to real users has about a dozen components, and each one hides a decision that is cheap to make now and costly to reverse later. This guide walks through the reference architecture, the choices inside each component, the four main patterns and the mistakes that are hardest to undo.
Documents flowing through chunks into an index, and a search query pulling results into a chat answer, illustrating RAG architecture
Designing a RAG system?Tell us what your users need to ask and where the answers live. We’ll help you pick the pattern, the components and a first version you can measure.
Review my RAG architecture

What a RAG architecture is

Retrieval-augmented generation (RAG) is a way to make a language model answer from your documents instead of from its training data alone. The term comes from a 2020 paper by Lewis and colleagues from Facebook AI Research, University College London and New York University. A RAG architecture is the set of components that makes this work, and it always has the same shape: two pipelines that share one index.

The ingestion pipeline runs offline, whenever documents are added or changed. It parses each document, splits it into chunks, converts the chunks into embeddings and writes them to an index together with metadata. The query pipeline runs online, on every request. It takes the user's question, retrieves the most relevant chunks the user is allowed to see, reranks them, assembles a prompt, has the model generate an answer and returns it with citations. Evaluation, tracing and caching wrap both pipelines. New to the idea itself? Start with what RAG is in AI.

RAG architecture diagram

The diagram below is the reference architecture most production systems converge on.

RAG reference architecture: two pipelines, one index INGESTION: offline, on every document change QUERY: online, on every request Sources docs, wiki, tickets Parse text, tables, layout Chunk plus metadata Embed pinned model Index vectors, keywords, ACL User question with identity Query processing rewrite, route Retrieve vector + keyword Rerank keep the best few Prompt assembly context, source IDs LLM generate Output checks grounding, citations Answer with sources Evaluation set, per-stage tracing, cost and latency, caching chunks the user may see Failed questions from production become new evaluation cases. The model is one box of twelve. Answer quality is mostly decided before the prompt reaches it.
Ingestion fills the index when documents change. The query path reads from it on every request, filtered by the permissions of the person asking.

The ingestion side: where answer quality is set

The model can only use what retrieval hands it, and retrieval can only find what ingestion stored well.

Parsing

Parsing turns files into text the rest of the pipeline can use. Plain HTML and Markdown are easy. PDFs with multi-column layouts, tables, scanned pages, slides and spreadsheets are not. A table flattened into a stream of numbers loses the link between a value and its row and column headers, and no later stage can restore it. Decide per source type: a text extractor for clean digital files, OCR or a layout-aware parser for scans and complex PDFs, and a rule for tables. Check parser output by eye on your ugliest documents first.

Chunking

Chunks are the unit of retrieval. Too large, and one embedding has to represent several topics, so matches get vague and you spend context on irrelevant text. Too small, and a chunk loses the surrounding meaning: "the limit is 30 days" is useless without knowing which limit. The main options:

  • Fixed size with overlap. Simple and predictable. A few hundred tokens with a small overlap is a common starting point. It cuts through sentences and tables without noticing.
  • Structure-aware. Split on headings, sections, list items or clauses. Usually the best default for documentation, policies and contracts, because authors already grouped the content by topic.
  • Small chunks, larger context. Search over small passages, then pass the model the parent section they came from. This buys precise matching and enough context, at the cost of a more complex index.

Whichever you choose, attach metadata to every chunk: source document, title and heading path, URL or page number, last-modified date, document type, language and access rights. You will filter, cite and debug with these fields.

Embeddings

An embedding model maps each chunk to a vector so that texts with similar meaning land close together. The choice affects retrieval quality, language coverage, cost and vector size. Two rules matter more than the leaderboard position of any model. First, test candidates on your own questions and documents, because rankings on public benchmarks do not transfer reliably to a specific domain. Second, treat the model as part of your data schema: vectors from different models are not comparable, so changing the model means re-embedding the whole corpus. Store the model name and version with every vector and keep the original chunk text so that a re-index is a batch job, not a rebuild.

The index

The index holds vectors for semantic search, usually an inverted index for keyword search, and the metadata for filters. Vector search at scale uses approximate nearest-neighbor algorithms such as HNSW, which give up a guarantee of finding the exact nearest matches in exchange for speed. The balance between recall and speed is tunable, which matters when a chunk you can see in the database never shows up in results. Plan the update path too: how a changed document replaces its old chunks, and how a deleted or restricted document disappears from the index.

The query side: from question to cited answer

Query processing

Raw user input is often a poor search query. In a conversation, "and what about for contractors?" means nothing without the previous turns, so a model call rewrites it into a standalone question first. Other useful steps: splitting a compound question into parts, extracting filters (a product name, a date range) and routing to the right index when you have several. Each step adds latency, so add them when your evaluation shows the failure they fix.

Vector search vs hybrid search

Vector search matches meaning. It finds a passage about cancelling a subscription when the user asks how to stop being billed. It is weaker on exact strings: part numbers, error codes, people's names, internal acronyms. Keyword search, typically BM25, is the opposite. Hybrid search runs both and merges the two result lists, often with reciprocal rank fusion, which combines rankings without needing the two scoring scales to be comparable. For business content full of identifiers and jargon, hybrid is the safer default.

Reranking

First-stage retrieval is built for speed across the whole corpus, so its ordering is rough. A reranker is a second model that reads the question and each candidate together and scores how well the passage answers it. A common starting point is to retrieve a wide set, for example 50 to 100 candidates, and keep the best 5 to 10 after reranking, then tune both numbers on your evaluation set. Reranking can be one of the largest quality gains you can add to a basic pipeline. It costs extra latency and a per-query fee or GPU time.

Prompt assembly

Prompt assembly decides what the model reads: instructions, the retrieved passages with source identifiers, conversation history and the question. More context is not better. Research on long contexts, including the 2023 "Lost in the Middle" study by Liu and colleagues, found that models use information at the start and end of the context more reliably than information in the middle. So send fewer, better passages, put the strongest ones first, remove near-duplicates and set a token budget. Tell the model what to do when the passages do not contain the answer: say so, and do not fill the gap from memory.

Generation and citations

Ask the model to cite the source ID of each passage it used, then resolve those IDs to titles and links in your own code. Citations let users verify an answer and let you audit one. They are not proof of correctness: a model can attach a real source to a claim the source does not make. For answers where an error is costly, add a check that each cited passage supports the sentence it is attached to, and treat "no relevant passage found" as a valid outcome with its own response. Our guide to RAG hallucinations covers these checks in detail.

The parts that are not on the happy path

Permissions

If your corpus contains anything not every user may read, access control belongs in retrieval. Store access rights (users, groups, tenant) on every chunk at ingestion and apply them as a filter inside the search query, using the identity of the person asking. A chunk that is never retrieved cannot leak. A prompt instruction such as "do not reveal salary data" can. The hard part is sync: when someone loses access in the source system, the index has to reflect it quickly.

Retrieved text is untrusted input

A document, email or web page in your index can contain instructions aimed at the model. This is indirect prompt injection, and the 2025 edition of the OWASP Top 10 for LLM Applications, published by the OWASP Gen AI Security Project, lists prompt injection as its first risk (LLM01). Keep retrieved content clearly separated from your instructions in the prompt, and never let model output trigger actions such as sending email or changing records without checks in your own code.

Caching

Three layers are worth considering. Cache embeddings so unchanged chunks are not re-embedded on every ingestion run. Use the model provider's prompt caching for the stable prefix of your prompt if your provider offers it. Cache full answers only for frequent, non-personal questions, and make the cache key include the user's permission scope and the index version. An answer cache that ignores permissions is a data leak.

Evaluation

Measure the two halves separately. Retrieval metrics ask whether the right passages were found: recall at k, precision, rank of the first relevant result. Generation metrics ask whether the answer is faithful to the passages, relevant to the question and correct. A wrong answer with the right passages retrieved is a prompt or model problem. A wrong answer with the wrong passages is an ingestion or search problem. Without the split you cannot tell which to fix. As a starting point, collect 50 to 200 real questions with expected answers and source passages, and run them on every change. More in our guide to RAG evaluation.

Observability

Log each request as a trace: the original and rewritten query, the filters applied, the IDs and scores of retrieved and reranked chunks, the final prompt, the answer, token counts, latency per stage and user feedback. This trace is how you find out which box in the diagram failed. These logs contain customer data, so give them access controls and a retention period.

RAG architecture patterns compared

Four patterns cover most systems in production. Each adds capability and cost to the one before.

PatternHow it worksFits whenMain cost
Naive RAGEmbed the question, fetch the top chunks by vector similarity, generatePrototypes, small and clean corpora, conversational questions, no access rulesMisses exact terms, weak ranking, no way to recover from a bad retrieval
Hybrid search with rerankingVector and keyword search merged, metadata filters, reranker, then generateMost production knowledge assistants, support and internal searchTwo indexes to maintain, reranker latency and fees, more tuning
Graph-based RAGEntities and relationships extracted into a knowledge graph; retrieval follows links or uses summaries of related groupsQuestions that connect facts across many documents, or ask for themes across the whole corpusExpensive, model-heavy indexing; extraction errors; harder incremental updates
Agentic RAGAn LLM agent plans, picks sources or tools, retrieves, judges the results and retriesMulti-step questions, several data sources, answers that need live data or calculationSeveral model calls per question, higher latency, variable cost, harder testing

Agentic RAG architecture: where an LLM agent fits

In an agent RAG architecture, retrieval stops being a fixed step and becomes a tool. The LLM agent receives the question, decides whether to search at all, writes its own queries, chooses between sources (the document index, a SQL database, an API), reads the results and decides whether to answer or search again. The agent sits where "query processing" was in the diagram and can loop through retrieve and rerank several times.

That loop is the benefit and the cost. A question like "which of our enterprise customers are on a contract that predates the new data policy" needs a database lookup and a document search, which a single retrieval pass cannot do. But each loop adds model calls, so latency and cost per question vary, and a test that passed yesterday can take a different path today. Cap the number of steps, log every tool call and keep the simple path for simple questions. See what a RAG agent is and RAG vs agentic AI for the full comparison.

How to choose your architecture

  1. Collect real questions firstGather the questions your users actually ask (50 to 200 is a workable starting set), with the correct answers and the documents that contain them. This set tells you which pattern you need and later becomes your evaluation set.
  2. Classify the questionsSingle-fact lookups point to hybrid search with reranking. Questions that connect many documents point to graph-based retrieval. Multi-step questions and those needing live data point to an agent.
  3. Audit the sourcesList formats, volume, update frequency and who may read what. Scanned PDFs, heavy tables and per-document permissions each add components before any model is involved.
  4. Build the baseline and measure itStart with structure-aware chunking, hybrid search and a reranker. Score retrieval and answers separately against your question set.
  5. Add complexity only where the numbers ask for itQuery rewriting, a graph, an agent loop: add each one to fix a measured failure, and confirm the score moved.

Built by Gilzor

Results we’ve shipped

70+products launched
98%delivered on time
85%clients come back
5.0on Clutch
Art Scherbakov, Co-FounderAndrew Laminsky, CTOYuri Rudenya, Head of Mobile Development at GilzorAlena Timofeeva, Product Marketing Lead

Talk to the people who build it. Tell us about your project and get a free estimate of scope, timeline and cost.

See how we’d approach yours

Mistakes that are expensive to undo

  • Adding permissions after launch. If access rights are not in the chunk metadata, adding them means new connectors, a full re-index and a review of every cache and log. Design them in, even if the first corpus is public.
  • Throwing away structure at ingestion. If you store only chunk text, you cannot cite a page, filter by date or re-chunk without parsing everything again. Keep the parsed document, the heading path and the source reference.
  • Coupling to one embedding model without a re-index path. Models get replaced and retired. Without stored source text and a versioned index, a model change becomes a migration project.
  • No evaluation set. Every later decision, from chunk size to model choice, becomes opinion.
  • Starting with the most complex pattern. A graph or a multi-agent setup built before a measured baseline hides basic retrieval defects under layers that are slow to debug. Earn each layer.
  • Ignoring deletes and updates. An index that only ever grows will eventually answer from a withdrawn policy. Document removal and permission revocation need a tested path from day one.

RAG architecture review checklist

Tick what is already true for your design or your running system.

RAG architecture review

FAQ

What is RAG architecture?
RAG architecture is the design of a system that answers questions with a language model using your own documents. It has two parts that share an index. The ingestion pipeline parses documents, splits them into chunks, turns the chunks into embeddings and stores them with metadata. The query pipeline takes a user's question, retrieves the most relevant chunks the user is allowed to see, reranks them, puts them into a prompt and has the model write an answer with citations. Evaluation, tracing and caching sit around both.
What are the main components of a RAG system?
On the ingestion side: connectors to the sources, a parser, a chunker, an embedding model and an index that holds vectors, keyword data and metadata, including access rights. On the query side: query processing, a retriever (vector, keyword or both), a reranker, prompt assembly, the language model and output checks with citations. Around them: permission enforcement, caching, an evaluation set and per-stage logging.
Do I need a vector database for RAG?
You need a place to store embeddings and search them, but it does not have to be a separate product. Many relational databases and search engines support vector search next to keyword search and filters. A dedicated vector database earns its place when the corpus is large, query volume is high or you need features your current store lacks. What matters more is that the store supports metadata filters and hybrid search, because you will need both.
What is the difference between naive RAG and agentic RAG?
Naive RAG runs a fixed sequence: embed the question, fetch the top chunks, generate an answer. It makes one retrieval call per question. In agentic RAG, an LLM agent decides what to search for, which source or tool to use, whether the results are good enough and whether to search again. That handles multi-step questions and several data sources, at the price of more model calls, higher latency and behavior that is harder to test.
Is hybrid search better than vector search?
For most business content, yes. Vector search matches meaning, so it finds a passage about cancelling a subscription when the user asks how to stop being billed. It is weaker on exact strings such as product codes, error numbers, names and rare terms, which keyword search handles well. Hybrid search runs both and merges the results, so you get both strengths. The cost is a second index and one more thing to tune.
How do you keep a RAG system from showing users documents they should not see?
Store access rights as metadata on every chunk during ingestion and apply them as a filter inside the retrieval query, using the identity of the person asking. Chunks the user cannot open never reach the prompt, so the model cannot quote or paraphrase them. Instructions in the prompt such as 'do not reveal confidential data' are not access control. You also need to sync permission changes from the source systems, so a revoked right takes effect in the index quickly.
How long does it take to build a production RAG architecture?
A working prototype over one clean source takes days to a few weeks. A production system takes months, and the time goes into the parts around the model: connectors and parsing for real documents, permission sync, an evaluation set, tuning retrieval against it, citations, monitoring and the product interface. Our guide to building an AI app gives 5 to 9 months as a typical range for a product grounded in your own data.

Where Gilzor fits

Architecture decisions are cheapest before the first index is built. Gilzor offers RAG development services: we design and build custom RAG systems, add retrieval to existing products, and review architectures that already run, with RAG consulting for teams that want a second opinion on patterns, components and evaluation before they commit. Our AI work includes an AI payroll platform that extracts data from PDFs and cut salary calculation errors by 17%, and image and text search with recommendations for the QLode fashion commerce product. The team works from Poland with partial overlap with the US working day, and a 2-week fixed-price Discovery Sprint is a practical way to turn your questions, sources and access rules into an architecture and a scoped first version.

No sales pitch

Get a straight answer for your project

Tell us what you’re building. We’ll reply with options, a rough cost and timeline. If we’re not the right fit, we’ll say so.

Next, a few optional questions so the first call is useful. We use your details only to reply to your request. Privacy Policy

Andrew Laminsky
Written byAndrew Laminsky

CTO of Gilzor. Responsible for architecture and the engineering standards our teams work by.

LinkedIn →

Gilzor · AI/ML Solutions partner

Need a team for your AI product?

95%referred by business partners
70+successful launches
85%repeat business
98%delivered on time

The team behind them

Art Scherbakov
Art ScherbakovCo-Founder
Andrew Laminsky
Andrew LaminskyCTOLinkedIn
Yuri Rudenya
Yuri RudenyaHead of Mobile Development at GilzorLinkedIn
Alena Timofeeva
Alena TimofeevaProduct Marketing LeadLinkedIn
Tell us what you’re buildingOptions, a rough cost and timeline for your project. No commitment.

More insights