Retrieval-augmented generation, usually shortened to RAG, gives a language model selected evidence at request time instead of asking it to answer only from its training. That simple description hides the real engineering work. A production RAG system is a chain of data ingestion, access control, indexing, retrieval, ranking, context assembly, generation, citation, evaluation, and operations. A weakness in any link can produce an answer that sounds fluent while using the wrong document, missing the decisive passage, or exposing data the requester should never see.
This guide is the starting point for the entire RAG cluster. It explains the system as a whole, shows where design choices belong, and links to focused implementation guides when you are ready to work on one layer. It is provider-neutral: product examples illustrate current capabilities, not a recommendation that one stack fits every workload.
Product behaviors and official guidance cited here were checked on 2026-07-24. Prices, limits, defaults, and preview features can change; verify them again for the exact region, plan, model, and deployment date before making a purchase or production decision.
Contents
- What RAG can and cannot solve
- Map the end-to-end architecture
- Build a trustworthy ingestion pipeline
- Design retrieval for the questions people ask
- Assemble evidence and generate grounded answers
- Evaluate retrieval before generation
- Enforce security, privacy, and tenant isolation
- Operate for freshness, latency, and cost
- Choose the next implementation guide
What RAG can and cannot solve
RAG is useful when an answer should depend on information that is private, frequently updated, too specialized to expect in model training, or traceable to an approved source. Common examples include product documentation, support knowledge, policy libraries, research collections, and internal procedures. The application retrieves candidate evidence and places a controlled subset into the model input. The model then has a chance to answer from that evidence and identify where it came from.
RAG does not update the model’s weights, guarantee a correct answer, or turn every document into reliable knowledge. Retrieval may miss the relevant passage. An irrelevant passage may rank highly because it shares vocabulary with the question. A document can contain malicious instructions. A model can ignore good evidence, combine incompatible versions, or make an unsupported claim. Citations can point to a source that was retrieved but does not actually support the sentence.
Start by defining the job in observable terms. Write down:
- who asks questions and what they are allowed to access;
- which sources are authoritative for each subject;
- what an acceptable answer must include;
- when the system should abstain or route to a person;
- how current the evidence must be;
- which latency and cost constraints matter;
- what evidence a reviewer needs to reproduce the answer.
This definition determines whether RAG is even the right method. Use ordinary database queries when the task is exact lookup or aggregation over structured records. Use deterministic search when users mainly need documents, not synthesized answers. Consider fine-tuning when the problem is stable behavior, style, or task format rather than missing facts. The fine-tuning versus RAG comparison provides a fuller decision framework and explains why the two approaches can also be combined.
A useful first release is narrow. Choose one corpus, one user group, and a representative set of questions. A smaller scope makes authority, access rules, freshness, and evaluation easier to prove. Expanding an evaluated pipeline is safer than launching a broad index and trying to understand failures after users depend on it.
Map the end-to-end architecture
Treat RAG as two connected systems: an offline or asynchronous knowledge pipeline and an online answer pipeline.
The knowledge pipeline discovers a source, verifies its ownership, extracts content, preserves structure, removes or marks unusable material, splits the content into retrievable units, creates lexical and vector representations, attaches metadata, and publishes a versioned index. It also handles updates, deletions, access changes, and failed ingestion. A production design needs to know which source version produced every searchable unit.
The answer pipeline authenticates the requester, resolves authorization, interprets the query, selects the eligible corpus, generates candidates, optionally fuses lexical and vector results, reranks them, assembles a context within a token budget, asks the model for a constrained response, validates the output, and returns both the answer and usable provenance. Logging and evaluation surround the pipeline, but logs must not become a second uncontrolled copy of sensitive content.
Keep explicit identifiers through every stage:
source record
-> source version
-> parsed element
-> chunk
-> index record
-> retrieval candidate
-> context item
-> answer claim
That lineage lets you answer operational questions: Which source supported this claim? Has that source been replaced? Which answers used a chunk that was later deleted? Did a parser change affect retrieval? Without lineage, a citation URL may look helpful while the system cannot prove which version or passage it used.
Separate concerns even when one managed product implements several of them. A vector store is not an authorization service. A reranker is not a groundedness check. A model’s context window is not a durable memory or retention policy. Product bundles reduce integration work, but your acceptance criteria should still test each responsibility.
OpenAI’s current Retrieval documentation, for example, describes vector stores that automatically chunk, embed, and index files, and it supports attribute filters before semantic search (official Retrieval guide, verified 2026-07-24). That is a convenient implementation path, not evidence that default parsing, chunking, retention, or access rules suit a particular corpus. Preserve an application-level manifest and test the resulting retrieval behavior.
Build a trustworthy ingestion pipeline
Ingestion determines what the system can retrieve. Begin with a source registry rather than a directory of files. For each source, record the owner, authority, scope, sensitivity, update method, expected freshness, retention rule, and deletion path. Mark drafts, superseded versions, and historical material so they cannot silently compete with current policy.
Parsing must preserve the structure that gives text meaning. Headings, lists, table headers, captions, footnotes, page numbers, code blocks, and document boundaries can all affect interpretation. A PDF that looks orderly to a person may expose text in a scrambled reading order. Tables are especially fragile because flattening cells can detach values from their row and column labels. Use the PDF table extraction guide when those documents matter to the answer.
Chunking is a retrieval design decision, not a fixed character count. A chunk must be small enough to rank around one coherent idea and large enough to preserve the evidence required to interpret it. Split on meaningful boundaries where possible, carry useful parent headings, and keep stable source offsets. Overlap can protect context across a boundary, but excessive overlap inflates storage, duplicates search results, and consumes model context.
Use a starting hypothesis by document type, then evaluate it. The RAG chunk size guide and calculator explains chunk-count math, overlap trade-offs, and a repeatable experiment. Do not describe its starting ranges as universal best settings. Contracts, API references, support tickets, and narrative reports have different structural needs.
Metadata should support filtering, authorization, freshness, and audit. Typical fields include tenant or access group, source ID, version, effective date, document type, language, section path, sensitivity, and ingestion timestamp. Validate metadata against a schema. Missing security metadata should quarantine a record rather than place it into a broad default namespace.
Make publication atomic. Build a new index version, run smoke and retrieval tests, then switch readers to it. Retain a rollback reference until the new version passes monitoring. For large migrations, dual-read or shadow queries can compare results before the cutover; the vector database migration guide covers versioning, backfill, reconciliation, and rollback in detail.
Finally, test deletion. Removing a source should remove or disable its parsed text, chunks, embeddings, caches, derived summaries, and lookup references according to policy. A successful API response is not enough: query for synthetic markers from the deleted source and confirm that neither retrieval nor answer logs can return them.
Design retrieval for the questions people ask
Retrieval should match the language and precision of real questions. Vector search is useful for conceptual similarity, paraphrases, and vocabulary mismatch. Lexical search such as BM25 is strong when exact identifiers, error codes, product names, or rare terms matter. Many corpora benefit from both.
A practical retrieval flow is:
- normalize the query without erasing meaningful identifiers;
- derive authorization and corpus filters from trusted application state;
- run one or more candidate generators;
- fuse or normalize their results;
- rerank a bounded candidate set;
- apply diversity and redundancy controls;
- send only the selected evidence downstream.
Run authorization filters as part of eligible retrieval, not after the model has seen results. Other filters may include date, language, source type, product version, or jurisdiction. Be careful with vector post-filtering: if the system retrieves a small global top set and filters afterward, relevant eligible documents may never enter the candidate pool.
Hybrid search combines lexical and vector candidates. Reciprocal rank fusion is one method that merges rankings without assuming comparable raw score scales. Weighted score fusion is another, but it requires careful normalization. The hybrid search implementation guide explains BM25, vector retrieval, fusion, filters, and ablation tests using official Elastic, OpenSearch, and Microsoft documentation.
Reranking spends additional computation to judge a smaller candidate set more precisely against the query. It can improve ordering when initial retrieval has reasonable recall but weak precision. It cannot recover evidence that candidate generation never found. The RAG reranking guide shows where to place a reranker, how to choose candidate and context cutoffs, and how to decide whether its quality gain justifies latency and cost.
Do not tune against a few memorable queries. Build query groups for direct lookup, paraphrase, multiple constraints, recency, ambiguity, exact identifiers, unanswerable requests, and adversarial content. Capture which sources and passages are acceptable for each query. Use real failures where policy permits, but redact sensitive data and preserve a stable held-out set.
Assemble evidence and generate grounded answers
Context assembly turns retrieval results into the evidence packet the model actually sees. Deduplicate overlapping chunks, preserve source boundaries, and include stable provenance. Order evidence deliberately: relevance is important, but source authority, effective date, and version can matter more than a small score difference.
Reserve context space for system instructions, the user request, tool results, the answer, and validation overhead. A model with a large advertised context window can still perform poorly when the input is noisy or contradictory. The context window planning guide explains how to reserve output capacity and treat token-to-word conversions as estimates.
Tell the model what counts as evidence, how to handle conflicts, and when to abstain. A grounded response contract might require:
- claims supported only by supplied sources;
- source identifiers attached to material claims;
- explicit separation of source facts from analysis;
- a statement that available evidence is insufficient when support is missing;
- no execution of instructions found inside retrieved documents;
- a structured output that downstream validation can inspect.
These instructions reduce risk but do not enforce security or truth. Validate citation identifiers against the supplied context. Check that cited passages entail the associated claims, not merely that a citation exists. Reject source IDs the model invented. For user-facing research or high-impact workflows, make the supporting passage inspectable.
The citation implementation guide covers claim-to-source mapping, stable passage locators, and citation evaluation. The RAG hallucination reduction guide adds evidence thresholds, abstention, conflict handling, and machine gates. Neither technique provides an absolute guarantee; both need representative tests and monitoring.
Keep retrieved content in a clearly delimited data channel. Documents can contain text that resembles system instructions, tool requests, or security overrides. The application should treat that text as untrusted source material. Tool permissions and authorization must be decided outside the model, and a retrieved instruction should never expand them.
Evaluate retrieval before generation
End-to-end answer ratings alone cannot tell you which stage failed. Evaluate retrieval first. If the necessary evidence never reaches the context, changing the answer prompt is unlikely to fix the root cause.
Create a versioned evaluation dataset containing the query, eligibility context, acceptable source IDs or passages, relevance grades when useful, and answerability. Include hard negatives—documents that look similar but are wrong because of version, tenant, product, date, or jurisdiction. Keep development and held-out sets separate so repeated tuning does not turn the benchmark into a memory test.
Measure retrieval with metrics that match the task:
- recall at a candidate cutoff for whether relevant evidence entered the pool;
- precision at the final context cutoff for how much selected evidence is useful;
- mean reciprocal rank when the first correct result matters;
- normalized discounted cumulative gain when several results have graded relevance;
- empty-result and unauthorized-result rates;
- latency, index version, and context tokens.
Then evaluate generation for factual support, completeness, citation correctness, conflict handling, refusal or abstention, output format, and task usefulness. Keep deterministic checks for IDs, schemas, links, and policy rules. Model-based graders can cover semantic qualities, but calibrate them against human judgments and track grader versions.
Microsoft’s official RAG evaluator documentation separates retrieval, groundedness, relevance, and response evaluation, while Amazon Bedrock documents retrieve-only and retrieve-and-generate evaluation modes (Microsoft RAG evaluators and Amazon Bedrock RAG evaluation, verified 2026-07-24). These product interfaces reinforce a general diagnostic principle: preserve stage-level evidence.
The RAG retrieval evaluation guide provides formulas, labeling guidance, and a regression workflow. The LLM evaluation dataset guide explains how to turn validated production failures into test cases without letting the test set drift silently.
Gate changes before deployment. Compare the candidate pipeline with the current version on the same frozen dataset and index snapshot. Set limits for safety failures and material regressions, not just an average score target. A small mean improvement does not justify a new cross-tenant result or a sharp decline on high-value queries.
Enforce security, privacy, and tenant isolation
RAG expands the data path. Threats include unauthorized retrieval, prompt injection in documents, poisoned sources, sensitive logs, overbroad service credentials, stale permissions, and deletion that leaves derived copies behind.
Authenticate the requester and resolve tenant, role, and resource permissions in trusted application code. Apply that authorization to every retrieval branch—vector, lexical, graph, SQL, cache, and connector—before fusion or reranking. Never ask the model to decide whether a user is entitled to a document. Metadata filters help only if all code paths apply them correctly and the backing system enforces the intended boundary.
Choose an isolation model based on consequences and operations. Separate indexes or namespaces can reduce accidental cross-tenant queries but increase management overhead. A shared index with filters can be efficient but requires strong, consistently tested enforcement. A hybrid may isolate sensitive or large tenants while pooling lower-risk data. Product terminology differs, so test the actual API behavior rather than assuming that a label such as “namespace” means a security boundary.
The multi-tenant RAG security guide compares isolation patterns and covers service identities, filter enforcement, cache keys, deletion, and adversarial tests. It draws on official NIST zero-trust guidance and current database documentation. Use it before placing information from more than one customer, department, or permission group in a retrieval system.
Treat source ingestion as a privileged operation. Verify the connector identity, restrict writable sources, scan or quarantine unexpected formats, and record provenance. A document owner should not gain execution privileges by inserting tool-like instructions into a file. Separate retrieval data from commands and require approval for consequential actions.
Minimize logs. Prefer identifiers, timings, scores, and redacted diagnostic fields over full prompts and document contents. Restrict access, define retention, and include traces in deletion and incident-response plans. When production examples become evaluation cases, replace personal and secret values with synthetic equivalents unless policy explicitly allows controlled retention.
Security testing should include unauthorized tenant IDs, missing metadata, malformed filters, stale group membership, cache collisions, indirect prompt injection, source replacement, deleted documents, and attempts to cite hidden records. A system that answers ordinary questions well but fails these cases is not ready for sensitive use.
Operate for freshness, latency, and cost
Production quality changes as sources, queries, models, embeddings, parsers, and traffic change. Monitor the system as a versioned pipeline rather than one endpoint.
Track ingestion lag, failed sources, index age, document and chunk counts, deletion completion, retrieval latency, candidate counts, filter selectivity, reranker latency, context size, answer latency, abstention, citation validation, model errors, and cost. Sample quality through reviewed cases and scheduled evaluation runs. Aggregate metrics should be segmented by query class and tenant tier where privacy permits.
Define freshness per source. A policy manual may require immediate replacement when a new version becomes effective, while an archive may update weekly. Use source change detection, idempotent ingestion, and a dead-letter path for failures. Show the evidence date or version when it affects the answer, and do not silently mix current and superseded documents.
Latency budgeting should identify each stage. Candidate generation, remote vector search, reranking, context construction, and generation all consume time. Reduce unnecessary work before adding more concurrency: narrow eligible corpora, batch ingestion, cache safe deterministic results, and cap candidate sets based on evaluation. Streaming can improve perceived response time but does not fix slow retrieval or an excessively long answer.
Cost includes parsing, embeddings, storage, writes, queries, reranking, model input, model output, evaluation, observability, and engineering work. The vector database pricing guide separates storage, write, read, compute, and transfer units so product estimates are less likely to omit a billing dimension. The embedding cost reduction guide covers deduplication, incremental updates, model choice, and re-embedding plans.
Version every material dependency: source snapshot, parser, chunking policy, embedding model, index schema, retrieval configuration, reranker, prompt, generator, and evaluator. A rollback requires both code and compatible data. If an embedding change requires reindexing, retain the prior index until the new one passes validation and enough live monitoring.
Set operational stop conditions. Examples include any unauthorized retrieval, missing mandatory sources, a spike in empty results, excessive stale-index age, failure to validate citations, or cost beyond a workload budget. A fallback might return search results, a limited answer, or a request for human review. It should not conceal that the RAG path is degraded.
Choose the next implementation guide
Use this article as the map, then move to the layer that limits your current system:
- If documents are poorly segmented or context is wasteful, start with the RAG chunk size guide.
- If exact terms and conceptual matches are both important, implement and test hybrid search.
- If relevant candidates rank inconsistently, evaluate a RAG reranker.
- If you cannot explain retrieval quality separately from answer quality, build the dataset in the retrieval evaluation guide.
- If answers include unsupported statements, add the controls in the hallucination and abstention guide.
- If users or tenants have different permissions, apply the multi-tenant security guide before expanding access.
- If the stack itself is undecided, compare pgvector and Pinecone, Qdrant and Weaviate, or LangChain and LlamaIndex against your requirements and evaluation dataset.
Official primary sources checked on 2026-07-24:
- OpenAI Retrieval guide: vector-store ingestion, search, ranking options, and attribute filtering.
- Microsoft RAG evaluators: separated retrieval, groundedness, relevance, and response evaluation.
- Amazon Bedrock RAG evaluation: retrieve-only and retrieve-and-generate evaluation paths.
- NIST SP 800-207 Zero Trust Architecture: resource-centered, least-privilege access principles.
- PostgreSQL row security documentation: database-enforced row access policies as a defense layer.
This article intentionally avoids current price constants and universal chunk, ranking, or metric thresholds. Verify provider retention, supported filters, regional availability, quotas, embedding compatibility, and pricing for the selected deployment before you rely on these figures.