Technical Reference · Foundational Knowledge

RAG: Hybrid Retrieval, Grounded Generation, Evaluation, and Security

RAG as pipeline engineering—indexes, hybrid retrieval, faithfulness, and ACLs—not a single prompt pattern.

Core Subject: retrieval-augmented generation
Curriculum: Enterprise AI Reference
Knowledge Graph: 111 Connected Guides

Retrieval-augmented generation (RAG) couples a retrieval system to a generator (typically a large language model) so answers can be conditioned on documents that live outside the model’s parameters (often prepared by document intelligence pipelines). The generator proposes language; the corpus and index are the system of record for facts that must stay fresh, private, or attributable. RAG is a pipeline discipline—ingestion, indexing, retrieval, assembly, generation, citation, evaluation—not a single prompt trick. Vector index vendors such as Pinecone appear in many stacks, but the corpus contract matters more than the brand.

This guide owns the retrieval+generation split, chunking and index design, hybrid retrieval, grounded-answer evaluation, and RAG security/ACL. Tool permission loops and kill switches belong in AI agents. Vector database product internals are a neighbor; here indexes matter only insofar as they serve retrieval quality and operations. LLM pretraining and alignment stacks are out of scope.

Why parametric memory fails as a system of record

Regulatory and contractual documents change on schedules the training cutoffs cannot track. RAG makes “what is true for us today” an indexing problem. That is an operational win only if freshness and ACLs are first-class—otherwise you have moved the hallucination into the retrieval layer.

Parametric memory—the weights—compresses training-time text. It cannot be reliably updated per customer document, cannot prove which file justified a claim, and cannot enforce document-level ACLs. Enterprises that treat a chat model as a knowledge base discover stale answers, cross-tenant leakage risk, and un-auditable citations.

Hallucination is not only “making things up”; it includes fluent answers that ignore retrieved evidence or invent citations. RAG reduces some failure modes by supplying evidence, but it introduces others: retrieving the wrong passage, assembling conflicting snippets, or citing a chunk the model did not use. Retrieval is necessary, not sufficient.

How retrieval changes the generation pipeline

Failure attribution should be stage-tagged in logs: parse_empty, chunk_orphan, index_lag, retrieve_miss, rerank_drop, context_truncated, generate_unfaithful, cite_mismatch. Without stage tags, teams argue about prompts while the index is a week behind.

Production RAG pipeline from ingestion through hybrid retrieval, rerank, and cited generation
RAG quality is pipeline quality—from ingestion and hybrid retrieval through rerank and citations.

A production RAG path is roughly: ingest → parse → chunk → embed/index → retrieve (often hybrid) → rerank → assemble context → generate → cite → log. Skipping stages shows up as mysterious quality loss. Each stage has its own metrics and owners. Treating “the LLM” as the only component hides whether the bug is a parser, a chunker, an index lag, or a prompt.

Online serving must respect timeouts: retrieval and rerank budgets are part of the latency SLO. Offline indexing must respect freshness SLOs: how long after a document lands until it is retrievable? Publish both.

Ingestion, parsing, and provenance

OCR and table extraction deserve dedicated quality bars when documents are scans. A beautiful embedding index on garbled text retrieves garbage confidently. Sample parse outputs in review queues for high-value corpora.

Ingestion converts PDFs, HTML, tickets, and wikis into text and structured fields with provenance: source URI, version, modified time, ACL principals, and checksum. Parsing failures (multicolumn PDFs, scanned pages, broken tables) become retrieval failures later. Prefer retaining layout hints when they matter for tables and headings.

Provenance must survive chunking. Every chunk carries parent document ID, offsets, and ACL. Without that, citations and permission filters cannot work. Delete and update flows need tombstones so stale chunks do not linger after a document is revoked.

Chunking strategies and stable document IDs

Choose chunk size from retrieval eval curves, not folklore (e.g., “512 tokens”). Different corpora peak differently: API docs vs narrative policies vs code. Keep a chunking config version next to the embedding model version.

Chunking trades context completeness against retrieval precision. Tiny chunks retrieve precisely but starve the generator; huge chunks retrieve noisily and waste context window. Structure-aware chunking (headings, sections, semantic boundaries) usually beats fixed token windows alone. Overlap can help continuity but multiplies storage and can dominate rankings with near-duplicates.

Stable document and chunk IDs enable safe reindexing. If IDs change every rebuild, feedback loops and citation analytics break. Version chunks when content changes; keep historical versions if audits require reconstructable answers.

Embeddings and approximate nearest neighbor indexes

Dimensionality, M/ef parameters in HNSW-style indexes, and quantization (e.g., scalar/product quantization) all move the recall–latency–memory frontier. Document the operating point you accepted. Revisit it when corpus size grows by an order of magnitude; yesterday’s parameters become today’s silent quality regression.

Query embedding must use the same instruction prefix conventions the document embedding used if the model family requires asymmetric instructions. Mismatched prefixes are a frequent “we upgraded the model and recall collapsed” cause.

Recall@k of the ANN index against exact neighbors on a sample should be monitored after compaction and rebalancing. Silent recall loss looks like “the model got worse” when the index parameters drifted.

Embedding models map text to vectors so semantically similar passages land nearby. Approximate nearest neighbor (ANN) indexes trade exactness for speed at corpus scale. Index choice (HNSW, IVF variants, etc.) is an ops decision constrained by recall@k targets, memory, and rebuild time.

Embedding model changes are breaking changes: you generally must re-embed the corpus. Mixing vectors from different models in one index silently destroys retrieval. Track embedding model version beside index version.

Sparse, dense, and hybrid fusion

Analyze failures by token type: if misses are mostly identifiers, boost sparse weight or add keyword fields; if misses are paraphrases, inspect embedding domain fit. Fusion weights are product knobs, not one-time constants.

Sparse lexical retrievers (BM25-class) excel at exact tokens: SKUs, error codes, names. Dense retrievers excel at paraphrase. They fail differently, so hybrid retrieval is the practical default for enterprise corpora.

Hybrid search fuses sparse and dense candidate sets—via rank fusion (e.g., RRF), weighted scores, or learned fusion—before reranking. Measure gains on your queries; hybrid is not magic (AI search owns ranking without grounded generation) if the corpus is tiny or purely conversational paraphrases.

Retriever Strengths Weaknesses Watch-outs
Sparse (BM25-like) Exact tokens, interpretability Paraphrase misses Analyzer/language config
Dense ANN Semantic paraphrase Weak on rare tokens Model/index version coupling
Hybrid + rerank Coverage across failure modes Latency/cost Fusion weights need eval

Query rewriting and routing

Conversational follow-ups need coreference (“that API” → concrete name) before retrieval. Naively embedding the latest short utterance loses context. Maintain a retrieval query separate from the user-visible chat turn when needed.

User queries are often underspecified. Rewriting expands acronyms, adds product context, or splits multi-hop questions. Routing sends queries to different corpora or tools (docs vs tickets vs code). Bad rewrites inject wrong constraints; log both raw and rewritten queries for debugging.

HyDE-style hypothetical document embeddings can help some semantic gaps and hurt others—especially when the hypothesis invents entities. Treat rewrite strategies as experiments with offline metrics, not defaults.

Reranking and context assembly

Hard context caps force triage: drop lowest rerank scores first, but protect mandatory policy snippets when the domain requires them. Explicit “must-include” documents for certain query classes beat hoping the retriever finds them.

Reranking reorders a candidate pool with a cross-encoder or similar model that sees query and passage together. It improves precision@k at extra latency. Retrieve broadly, rerank narrowly, then assemble the final context under a token budget.

Assembly policies matter: diversity versus redundancy, mandatory citation spans, table preservation, and ordering (relevance vs document order). Stuffing duplicate near-matches wastes the window. Conflicting passages should be retained when the task is adjudication, not silently dropped.

Citations versus faithfulness

Design citation UX for skeptics: show the quote span, the document title, timestamp, and ACL-safe link. If users cannot verify quickly, citations become decoration. For internal tools, deep-link to the exact paragraph in the source system when possible.

Generator instructions should prefer abstention over speculative glue between unrelated chunks. Models will eagerly connect dots; your product may need them to refuse connections that are not entailed.

Automatic faithfulness checkers help at scale but disagree with humans on edge cases. Use them as triage, and keep human review on high-impact corpora. Never advertise “grounded” without a defined faithfulness test.

A citation is a pointer; faithfulness is whether the answer is supported by the cited evidence. Systems can cite correctly while answering incorrectly, or answer correctly while citing irrelevant chunks. Evaluate both. Prefer quote-level or span-level citations when stakes are high.

UI that shows sources without guaranteeing usage encourages false trust. When evidence is insufficient, the system should abstain or ask a clarifying question rather than improvise.

Split evaluation: retrieval quality and grounded answers

When building golden sets, label supporting chunks, not only final answers. Otherwise you cannot tell retrieve_miss from generate_unfaithful. Include “unanswerable” items to train abstention.

Split metrics: retrieval (recall@k, nDCG, MRR on labeled relevant chunks) versus generation (answer correctness, faithfulness, citation precision). End-to-end only scores hide whether to fix the index or the prompt. Maintain golden question sets with labeled supporting chunks; include adversarial and ACL cases.

Online metrics: deflection rate, thumbs-down reasons, citation click-through, and escalation to humans. Tie eval to freshness: a correct answer against an outdated corpus is still an operational miss.

Security: ACL, tenancy, and retrieval injection

Defense layers: pre-retrieval ACL filters, post-retrieval ACL recheck, output DLP for secrets, and rate limits on export-like queries. Red-team with curious insiders and malicious documents. Assume some retrieved content is hostile.

Test with two tenants’ documents that share near-identical wording; verify retrieval never crosses. Add canary documents with injection payloads and assert that tool-enabled wrappers do not obey them. Security eval is part of RAG eval.

Enforce ACLs at retrieval time, not only at generation time. If unauthorized chunks enter the context, the model may leak them. Filter by principal before ranking finalizes. Multi-tenant indexes need hard tenancy boundaries—shared ANN indexes without filters are a breach waiting to happen.

Retrieval injection places malicious instructions inside documents to hijack generation or tool-using wrappers. Treat retrieved text as untrusted data. When RAG feeds an agent, combine this page’s ACL rules with the agent guide’s tool gateway rules.

Latency and cost per successful answer

Adaptive retrieval—skip rerank for easy FAQ hits, deepen retrieval for low-confidence cases—can cut cost if confidence is calibrated. Uncalibrated confidence will skip retrieval when it was most needed.

Cost drivers: embedding queries, ANN probes, sparse search, rerank inferences, and generator tokens (often dominated by context size). Optimize for cost per successful grounded answer, not cost per call. Caching frequent queries, smaller rerank pools, and adaptive retrieval depth help.

Latency budgets should allocate milliseconds to each stage. A beautiful reranker that blows the SLO will be disabled in production—measure it honestly.

Freshness operations and corpus changelogs

Access revocations must be as fast as content updates. A deleted permission with a live chunk is a vulnerability. SLO on revocation propagation belongs beside freshness SLO.

Corpus changelogs record adds, updates, deletes, and ACL changes. Index pipelines should be idempotent and observable. Lag dashboards matter as much as answer quality dashboards. Hot documents (policies, pricing) may need priority paths or cache busting.

Graph and agentic RAG only when measured

If you do add agentic retrieval hops, inherit step caps and tool permissions from agent engineering. Unbounded “search again” loops are a cost and injection hazard.

Graph-augmented retrieval and multi-hop agentic retrieval can help complex questions—and can thrash cost without gains. Add them when offline eval shows clear lifts on multi-hop slices, with budgets inherited from agent control practices. Do not deploy graph theater for FAQ corpora that hybrid retrieval already solves.

Build versus buy for RAG platforms

Exit criteria for vendors should include export of chunks with provenance, ACL filter semantics you can test, and the ability to run your golden sets. Lock-in without eval portability is how quality regresses invisibly after a contract renewal.

Buy when connectors, ACL integrations, and ops tooling dominate your scarcity. Build when corpus formats, ranking needs, or compliance boundaries are differentiating. Either way, you still own evaluation, ontology of sources, and security reviews. Platforms do not absolve you of golden-set discipline.

Operational playbook: from pilot to production RAG

Pilots often succeed on a clean wiki subset and fail on the real union of Drive folders, ticketing exports, and PDFs with scans. Production readiness means: connectors with ACL, parse quality bars, index lag SLOs, golden sets with labeled chunks, stage-tagged logging, and a revocation path. If any of those are missing, you still have a demo.

Staff the pipeline like a search service. Someone owns ingestion health, someone owns ranking quality, someone owns generator prompts and abstention behavior. Blaming “the model” for a parse failure wastes months. Weekly failure triage should sample retrieve_miss and generate_unfaithful separately and assign owners.

Change management needs dual control for prompt and index config changes that affect high-impact corpora. A one-line rewrite prompt can move money-adjacent answers. Treat ranking and prompt configs as release artifacts with rollback.

Customer-facing RAG should expose uncertainty: partial evidence, multiple candidate policies, and clear “not in corpus” states. Hidden best-effort answers train users to overtrust. Product copy should match the faithfulness definition you actually measure.

Corpus design patterns that change retrieval difficulty

Boilerplate-heavy corpora (repeated headers, legal footers) pollute dense retrieval unless you strip or down-weight templates. Near-duplicate policies across regions require metadata filters (jurisdiction, product line) before similarity search. Code and API corpora need analyzers that preserve punctuation and identifiers—default English stemmers can destroy signal.

Mixed-language corpora need explicit language detection and either multilingual embeddings or per-language indexes. Silently embedding everything with an English-centric model produces uneven recall that looks like random quality.

Accessioning strategy matters: bulk historical dumps versus ongoing CDC. Bulk loads need backfill verification; CDC needs ordering and exactly-once chunk upserts. Both need checksums. Without checksums, “silent truncate” bugs ship incomplete documents that still look indexed.

Finally, keep a living threat and quality model for RAG: top retrieve_miss intents, top unfaithful answer patterns, top ACL near-misses, and index lag incidents. Review them on a fixed cadence. RAG systems drift when corpora, permissions, and products change—even if the generator weights do not.

When comparing vendors or in-house stacks, demand the same golden-set protocol: identical questions, identical labeled chunks, identical faithfulness rubric, and identical ACL test users. Marketing charts that omit retrieval stage metrics are not comparable. Prefer systems that export stage logs you can join to your own analytics.

Context window growth does not retire RAG. Larger windows still need selection, ACLs, freshness, and cost control. Dumping an entire corpus into a long context is not a retrieval strategy; it is a temporary demo tactic that fails on permissions, latency, and distraction from irrelevant text.

For agent-mediated RAG, keep retrieval outputs typed as untrusted observations and let the agent runtime own tool permissions and step caps. Crossing those concerns—letting retrieved text authorize actions—recreates prompt-injection paths that neither a good index nor a polite system prompt will close. The clean split is: this guide for evidence pipelines; the agents guide for control loops.

Measure “time to trustworthy answer” as a product metric: includes retrieval, generation, and human verification when required. Optimizing only token latency encourages brittle short contexts. Optimizing only exhaustive retrieval encourages costly overfetch. The operating point is a deliberate trade among risk, spend, and speed.

References and further reading

Technical Clarifications

Frequently Asked Questions

Operational and architectural questions regarding retrieval-augmented generation.

Does RAG eliminate hallucinations?

No. RAG reduces some unsupported answers by supplying evidence, but it can still retrieve the wrong passage, assemble conflicts, or generate claims not entailed by citations. Measure faithfulness and retrieval quality separately.

Why is hybrid retrieval often the default?

Sparse retrievers catch exact tokens (SKUs, error codes, names) while dense retrievers catch paraphrases. They fail differently; fusion plus reranking usually covers enterprise corpora better than either alone.

How should we choose chunk size?

Choose from retrieval evaluation curves on your corpus and queries, version the chunking config, and prefer structure-aware boundaries over a single folklore token length.

How do we prevent cross-customer leaks?

Enforce ACLs at retrieval time (and recheck post-retrieval), isolate tenancy boundaries in the index path, test with near-duplicate cross-tenant documents, and treat revocation lag as a security SLO.

Knowledge Graph Continuation

Related Architectural Concepts

Continue exploring adjacent systems, infrastructure, and governance models in this subject domain.