How Does RAG Architecture Work? An End-to-End Guide

GT

GigaRAG team

Retrieval18 min read
On this page

TL;DR: RAG, or retrieval-augmented generation, is a system architecture that connects external information to a language model. A production RAG system is commonly organized into two connected workflows: an offline workflow that prepares searchable content and an online workflow that retrieves authorized evidence for each query. Results depend on the whole pipeline. Strong retrieval helps, but it does not guarantee an accurate, grounded, or properly cited answer.

What happens inside a RAG architecture?

A simple explanation of RAG sounds easy: find relevant information, add it to a prompt, and ask a language model to answer. The real system must also prepare, secure, rank, package, and validate that information. When an answer is stale or unsupported, the failure may begin upstream. The wrong document may have been indexed, the right passage may have fallen below the cutoff, or the prompt may have buried it under noise. This guide explains how RAG architecture works from source ingestion to final response. It also covers design choices, evaluation, security, and common production risks.

1.png

What is RAG architecture?

RAG architecture is the end-to-end system used to retrieve external information, supply it to a language model, and attempt to produce an evidence-supported answer. RAG stands for retrieval-augmented generation:

  • Retrieval finds information that may help answer the query.
  • Augmentation adds selected information to the model's input.
  • Generation uses the query, instructions, and supplied context to produce a response.

The architecture includes more than these three actions. It connects data sources, parsing, document chunking, metadata, permissions, searchable representations, retrieval, optional reranking, prompt construction, generation, citations, validation, evaluation, and operations.

How is RAG different from an LLM?

An LLM is a model. RAG is a system pattern built around one or more models and retrieval sources. An application can change its language model while keeping most of its RAG pipeline. It can also use a language model without using RAG. If the application sends a user question directly to a model without retrieving external information, it is not performing retrieval-augmented generation. In research, RAG model may describe a setup that combines parametric and non-parametric memory. In production, RAG system architecture is often clearer because results depend on the surrounding data and workflow, not only on model weights.

How does the RAG workflow operate at a glance?

Each stage answers a different question and creates a different failure mode.

StageQuestion to answerTypical failure
IngestionIs the source content readable, current, and complete?Missing, duplicated, stale, or poorly parsed content
Chunking and metadataAre useful retrieval units and filters being created?Broken context, weak scoping, or inconsistent tags
IndexingCan the information be searched using the required signals?Missing, outdated, or incompatible representations
AuthorizationIs retrieval limited to data the user may access?Restricted content enters the candidate set
RetrievalDoes the system find useful candidates?Low recall or irrelevant matches
Optional rerankingAre the best candidates near the top?Relevant evidence remains buried
Prompt constructionDoes the model receive clear and relevant context?Noise, duplication, or conflicting instructions
Generation and validationIs the answer supported and safe to deliver?Unsupported claims, incorrect citations, or unsafe output
Evaluation and operationsCan the team detect and diagnose failure?Hidden regressions, unexplained errors, or stale indexes

Use this as a diagnostic map. A poor answer does not automatically mean the model failed; first identify where useful evidence was lost or misused.

2.png

Figure 1. A general RAG architecture connects source preparation with query-time retrieval, generation, and validation. 

What are the two main workflows in a RAG system?

A production RAG architecture is commonly understood through two connected workflows. They run at different times and have different operational needs.

Offline workflow: ingestion and indexing

The offline workflow runs when content is added, changed, or removed. It may run continuously, on events, or on a schedule.

  1. Collect sources. Pull approved content from documents, wikis, tickets, PDFs, databases, or APIs.
  2. Parse and clean. Extract usable text and structure while removing repeated navigation, boilerplate, and formatting noise.
  3. Create chunks. Split content into units that can be retrieved and cited.
  4. Attach metadata. Add fields such as source, section, language, version, timestamp, tenant, and access scope.
  5. Create a searchable representation. Generate embeddings, keyword terms, graph relations, structured records, or another searchable form.
  6. Write to the data layer. Store the representation with the text, metadata, and source identifiers needed later.
  7. Process updates and deletions. Refresh changed content and remove material that should no longer be returned.

The final step is easy to overlook. If deletion does not propagate, the system may continue retrieving content that users believe is gone.

Online workflow: retrieval and generation

The online workflow runs for each user request.

  1. Receive the query. Capture the question and relevant application state.
  2. Establish identity and access scope. Determine who is asking and which data they may use.
  3. Prepare the query. Normalize, rewrite, expand, classify, or decompose it when the use case requires that work.
  4. Retrieve authorized candidates. Search only within the permitted scope. Metadata pre-filters may narrow the candidate space.
  5. Apply relevance and quality filters. Remove unsuitable or duplicate candidates. Post-retrieval filtering must not replace authorization.
  6. Optionally rerank. Reorder candidates with a stronger scoring method when evaluation shows a useful gain.
  7. Construct the context. Combine instructions, the query, selected evidence, and required source identifiers.
  8. Generate the response. Ask the language model to answer using the supplied context.
  9. Validate and deliver. Check structure, policy rules, citations, and other application requirements before responding.

Where do the workflows connect?

The workflows commonly connect through a searchable data layer: a vector or keyword index, database, knowledge graph, API, search service, or combination. Versioning matters here. If the offline workflow changes the embedding model, schema, chunking, or metadata rules, the online retrieval configuration may also need to change.

3.png

Caption: Figure 2. A general query-time RAG workflow retrieves evidence within the user's authorized scope, constructs context, and validates the generated response.

What are the core components of RAG architecture?

The main RAG components fit into four layers.

1. Knowledge and source layer

This is the raw material: product documentation, policies, support articles, tickets, code, tables, or structured records. The system should retain stable source identifiers so retrieved content can be traced back to its origin.

2. Representation and searchable data layer

This layer makes content searchable. It may use an embedding model and vector database, but neither is mandatory. Keyword indexes, SQL queries, graph traversal, APIs, and tools can also retrieve useful evidence.

3. Retrieval and orchestration layer

The retriever selects candidates. Optional components may rewrite queries, combine search methods, rerank results, deduplicate passages, apply fallbacks, call tools, or route queries across sources. Authorization must be enforced before or during retrieval. Metadata can help scope search, but metadata filtering alone is not access control.

4. Generation and output layer

The language model receives the query, instructions, and selected evidence. The application then validates the response, attaches verified citations, applies policy checks, formats output, and decides when to abstain or escalate. These layers depend on each other. Weak preparation reduces retrieval quality, and poor prompt construction can waste good evidence. Output controls cannot repair restricted data that should never have reached the model.

How should documents be chunked, tagged, and indexed?

4.png

Caption: Figure 3. A general ingestion pipeline turns source content into searchable, permission-aware units and keeps them current.

Document chunking

Document chunking defines the units the system can retrieve. There is no universal chunk size or overlap percentage. The right configuration depends on document structure, query type, retrieval method, model, context limits, citation needs, and evaluation.

  • Smaller chunks may improve precision, but they can separate a procedure from the warning or definition that gives it meaning.
  • Larger chunks preserve more context, but they can weaken the match signal and use tokens on irrelevant text.
  • Overlap may preserve continuity across boundaries, but it increases index size and can return near-duplicate passages.

Prefer meaningful structural boundaries such as headings, steps, code blocks, and table rows. Then test whether each representative query retrieves a useful, self-contained passage.

Metadata and access attributes

Metadata can include source, document type, product version, section, language, timestamp, tenant, and intended audience. It can act as a pre-filter, post-filter, or ranking signal. Consistent metadata improves relevance and scoping. It does not automatically provide authorization. Identity and permission rules should be enforced in application and storage layers so restricted data never enters an unauthorized user's retrieval set.

Embedding models

An embedding model converts text into numeric vectors. Related passages may appear near one another in that vector space, which helps dense retrieval find conceptual matches and paraphrases. Embeddings are not a complete representation of meaning. Domain terminology, code, legal phrases, and unusual identifiers may behave differently from general prose. Two operational rules are important:

  • Vectors created by different embedding models should not be treated as interchangeable.
  • Changing the embedding model usually requires re-embedding the affected content and validating retrieval again.

Track the embedding model, chunking version, and index version. Treat a representation change as a migration, not a simple setting change.

Vector databases and other retrieval sources

A vector database is one implementation option, not a requirement for RAG. A RAG system may retrieve through:

  • Dense vector search
  • Sparse or keyword search
  • Hybrid search
  • SQL or another structured query
  • A knowledge graph
  • A search API
  • Tool calls
  • A combination of these methods

Choose based on the corpus, queries, update needs, permissions, latency budget, and evaluation.

5.png

How do retrieval, top-k, and reranking work?

At query time, the system must decide which evidence reaches the model. Candidate generation, filtering, ranking, and context selection all affect that decision.

Dense, sparse, and hybrid retrieval

Dense retrieval uses embeddings. It often helps with paraphrases and conceptual similarity. A question about login problems after a domain change may match a document titled “SSO redirect URI configuration.” Sparse retrieval uses lexical or keyword signals. It often helps with exact names, error codes, versions, product identifiers, legal clauses, and rare technical terms. Hybrid search combines dense and sparse signals. It may improve coverage, but it adds infrastructure, score-combination choices, tuning, and evaluation. Test it against simpler methods first.

Top-k retrieval

Top-k retrieval controls how many candidates move forward from a retrieval stage. There is no universal value. A larger candidate set may improve recall while increasing latency, cost, duplication, and prompt noise. Retrieval top-k need not equal final context size; a system can retrieve broadly and pass only the strongest candidates forward.

Optional reranking

Reranking uses another relevance signal to reorder retrieved candidates. It may help when initial retrieval has good recall but weak ordering, especially for ambiguous queries or many similar passages. Reranking cannot recover missing evidence and adds compute and latency. Keep it only when evaluation shows enough benefit.

How should context and prompts be constructed?

Prompt construction determines what the model sees and how the task is framed. It cannot restore missing evidence.

Separate instructions from evidence

Keep task instructions, the user query, retrieved material, and output requirements clearly separated. Retrieved documents are data, not trusted commands. Source content may include instructions copied from tickets or web pages, including malicious prompt-injection attempts.

Treat retrieved content as untrusted input

Use defensive context injection:

  • Label retrieved passages as source material.
  • Prevent retrieved text from overriding system-level instructions.
  • Remove irrelevant and duplicate passages.
  • Validate links, identifiers, and actions derived from retrieved content.
  • Restrict tool calls and data access independently of model instructions.

Pack context deliberately

The context window must hold instructions, the query, evidence, metadata, history, and the expected answer. More context is not always better. Test ordering and packing with realistic inputs rather than assuming one universal rule.

Build citations from preserved source data

Asking a model to cite sources does not guarantee correct citations. A stronger citation workflow should:

  • Preserve source and chunk identifiers through retrieval and generation.
  • Confirm that each cited source was included in the model's context.
  • Map claims to evidence when the use case requires that precision.
  • Build trusted links and identifiers in application code rather than accepting model-generated URLs.

Grounding instructions can encourage the model to answer only from supplied evidence. They may also increase abstentions when retrieval is weak. Both effects should be measured.

What does a RAG architecture look like in practice?

Consider a support assistant answering this question:

Why did SSO stop working after our domain changed?

Here is one possible RAG workflow.

  1. Ingest sources. Collect help articles, admin guides, changelogs, and approved internal runbooks.
  2. Parse and chunk. Preserve the relationship between the domain-migration warning and the setup steps it affects.
  3. Attach metadata and access attributes. Label product version, audience, date, and tenant or role scope.
  4. Index current content. Store searchable chunks and remove deprecated material.
  5. Establish authorization. Identify the user's tenant and role before retrieval.
  6. Retrieve candidates. Search for conceptual matches and exact terms such as “SSO,” “domain change,” and “redirect URI.”
  7. Optionally rerank. Move the migration procedure above a general SSO overview if testing shows the extra stage helps.
  8. Construct context. Remove duplicates, preserve source IDs, and separate evidence from instructions.
  9. Generate the answer. Ask the model to use only the supplied evidence and to abstain when it is insufficient.
  10. Validate citations and output. Confirm that cited sources were provided and that no restricted details appear.
  11. Record a privacy-safe trace. Store the minimum identifiers, scores, filters, prompt version, latency, and outcome needed for diagnosis.

An outdated answer may come from stale content, bad chunking, missed retrieval, weak ranking, or unsupported generation. Stage-level traces distinguish these causes.

How do you evaluate and observe a RAG system?

RAG evaluation should measure retrieval, answer quality, and operations separately. Offline evaluation tests representative queries with expected evidence or outcomes before release. Online evaluation monitors real requests, latency, failures, and changing patterns.

Retrieval quality

Useful measures may include:

  • Recall@k
  • Precision@k
  • Mean Reciprocal Rank
  • nDCG
  • Context relevance or context recall
  • Filter fallout

Answer quality

Measure outcomes such as:

  • Groundedness or evidence support
  • Answer relevance
  • Citation correctness
  • Task completion
  • Abstention quality
  • Human review on a representative sample

Operational quality

Track:

  • End-to-end and per-stage latency
  • Token usage and cost per query
  • Timeout and error rates
  • Index freshness
  • Cache hit rate
  • Source update and deletion lag

Set targets from your workload and baseline. Public benchmarks may not transfer to a different corpus, model, user population, or latency budget.

Privacy-safe observability

Traces may include chunk IDs, scores, filters, reranker output, prompt version, citations, latency, and token counts. Because prompts and passages may contain confidential data, log deliberately.

  • Minimize or redact sensitive fields.
  • Apply retention limits.
  • Restrict access to traces.
  • Keep tenant data separated.
  • Follow the legal and security requirements that apply to the data.

How do permissions, caching, and failures affect production RAG?

Authorization must be enforced before or during retrieval

Metadata filtering is not authorization. The searchable data layer and application should enforce identity and permission rules so retrieval remains inside the user's allowed scope. Post-retrieval filtering may remove unsuitable results, but it must not be the first security boundary. Once restricted text reaches the model, filtering the final answer is too late. Retrieved content should also be treated as untrusted input. A prompt telling the model not to reveal restricted information is not a security control.

Caching requires isolation and invalidation

Caching may reduce cost and latency when requests and access scopes allow safe reuse. It can also expose data if entries are shared incorrectly. Cache keys may need to include tenant, permission scope, data and index versions, retrieval configuration, model, and prompt version. Invalidate or rebuild relevant entries when content, permissions, models, prompts, retrieval settings, or policies change. Test revocation and tenant isolation rather than assuming the key design is safe.

Safe failure behavior should be designed in advance

Define what happens when evidence is weak, a source fails, a stage times out, or citations cannot be validated. A clear abstention is often safer than a fluent guess; high-risk requests may need human review.

What are the benefits and limits of RAG?

Support for fresher external knowledge

For frequently changing facts, updating approved sources is generally more practical than retraining for every change. Freshness still depends on ingestion, indexing, and deletion handling.

Support for grounding and traceability

Retrieved passages can support evidence-based answers and claim-to-source traceability when identifiers are preserved and citations are validated throughout the RAG pipeline. Retrieval alone does not guarantee either outcome.

Replaceable and measurable components

Teams can test chunking, retrieval, ranking, models, and prompts separately. This aids diagnosis but creates more components to operate. RAG does not eliminate hallucinations. Unsupported answers may still appear when retrieval misses evidence, sources are wrong, context is incomplete, citations are generated rather than verified, or the model misuses the supplied information.

What are the most common RAG failure modes?

  • Stale or missing sources: The correct information never reaches the searchable layer.
  • Poor parsing: Tables, headings, or relationships disappear during extraction.
  • Weak chunks: Relevant information is split apart or surrounded by too much noise.
  • Inconsistent metadata: Scoping and filtering behave unpredictably.
  • Authorization mistakes: Retrieval crosses a user's permitted data boundary.
  • Low retrieval recall: The right passage does not enter the candidate set.
  • Weak ranking: The passage exists but remains below the cutoff.
  • Overpacked context: Duplicate or irrelevant text competes with useful evidence.
  • Prompt injection: Retrieved content attempts to influence model or tool behavior.
  • Unsupported generation: The model fills gaps with plausible but unverified claims.
  • Citation errors: The answer points to a source that does not support the claim.
  • Insufficient observability: The team sees a bad answer but cannot locate the failed stage.

A trace should make these failures distinguishable. Without it, several different causes look like the same model problem.

RAG architecture review checklist

Use this checklist before release:

  1. Assign an owner and refresh policy to every source.
  2. Verify parsing on difficult formats such as PDFs, tables, and code blocks.
  3. Evaluate chunking with representative queries.
  4. Check metadata coverage and consistency.
  5. Enforce authorization before or during retrieval and test with low-privilege accounts.
  6. Record the embedding model, chunking method, schema, and index version.
  7. Test whether updates and deletions propagate within the required window.
  8. Compare dense, sparse, and hybrid retrieval using the same evaluation set.
  9. Test several top-k values and measure recall, answer quality, latency, and cost.
  10. Keep reranking only when its measured gain justifies the added latency.
  11. Remove duplicate and irrelevant context before prompt construction.
  12. Test prompt-injection defenses using hostile retrieved content.
  13. Confirm that every citation refers to evidence supplied to the model.
  14. Define abstention and fallback behavior for missing or weak evidence.
  15. Run offline evaluations before release and monitor production metrics afterward.
  16. Minimize sensitive data in traces and apply a retention policy.
  17. Test cache isolation, revocation, and invalidation.
  18. Set per-stage latency and cost budgets.
  19. Create alerts for stale indexes, failed ingestion, and source drift.
  20. Define human escalation for high-risk queries.

When a team does not want to operate every data-layer component directly, GigaRAG supports ingestion, chunking, embedding, indexing, and retrieval as managed RAG infrastructure. Application teams still own system-specific authorization, retrieval strategy, ranking choices, prompt design, citation validation, and evaluation.

Frequently asked questions

What is RAG architecture?

RAG architecture is the system used to retrieve external information, add selected evidence to a model's input, and attempt to produce an evidence-supported response. It includes source preparation, indexing, retrieval, prompt construction, generation, validation, evaluation, and operations.

What are the main components of a RAG system?

The main components are knowledge sources, a searchable data layer, retrieval and orchestration, a language model, prompt construction, output validation, and evaluation. Optional components may include embeddings, a vector database, hybrid search, query rewriting, reranking, caching, and tool calls.

Does every RAG system need a vector database?

No. A RAG system can retrieve through keyword search, structured database queries, knowledge graphs, APIs, tools, dense vector search, or a combination. A vector database is useful for some workloads, but it does not define RAG.

What is the difference between RAG and an LLM?

An LLM is a model. RAG is a system pattern that connects a model to external information at query time. The surrounding retrieval, permissions, prompting, and validation logic belongs to the application architecture.

How do you evaluate a RAG architecture?

Measure retrieval, answers, and operations separately. Retrieval metrics may include Recall@k and nDCG. Answer measures may include evidence support and citation correctness. Operational measures include latency, cost, failures, and index freshness. Use representative offline tests and production monitoring.

Does RAG prevent hallucinations?

No. RAG can supply relevant evidence and make some failures easier to trace, but it does not guarantee that retrieval is correct or that the model will use the evidence properly. Safe failure behavior and validation are still required.

What is the difference between RAG and fine-tuning?

Fine-tuning primarily adapts model behavior, terminology, formats, and task patterns, and it may encode some static knowledge. RAG supplies external information at query time and is generally a better fit when facts change frequently or must remain traceable to sources. The two approaches can also be combined.

Is ChatGPT a RAG model?

The phrase “RAG model” is used in some research, but RAG is usually clearer as a system architecture in production discussions. A product may use retrieval features without the underlying language model itself becoming a distinct model category. Whether a particular ChatGPT experience uses retrieval depends on the feature and configuration, so the product name alone does not answer the architecture question.

Key takeaways

  • RAG is an end-to-end architecture, not one retrieval call.
  • Production RAG is commonly organized into an offline source-preparation workflow and an online query workflow.
  • Those workflows may connect through an index, database, graph, API, search service, or tool.
  • Retrieval quality helps but does not guarantee an evidence-supported answer.
  • Chunking, metadata, authorization, ranking, prompts, validation, evaluation, and operations all affect the result.
  • A vector database, hybrid search, and reranking are optional design choices.
  • There is no universal configuration for chunk size, top-k, retrieval method, or reranking.
  • Production RAG requires measurement, privacy-aware observability, and safe failure behavior.
All posts