RAG Architecture Deep Dive
Deep dive into RAG architecture: context management, quality optimization, ranking algorithms, and production patterns.
What is a RAG architecture?
A retrieval-augmented generation (RAG) architecture is the complete system that finds evidence for a question, places a bounded set of that evidence in a model prompt, and decides whether the resulting answer is safe to release.
In plain language: the model does not search the whole knowledge base itself. A retrieval system selects a small, authorized evidence packet first. The model must answer from that packet or admit that the evidence is insufficient.
The core invariant is no evidence, no grounded claim. A fluent answer is not proof that retrieval found the right source, that the user may see it, or that the source is current.
Recall
Find the evidence
Retrieve answer-bearing chunks before generation begins
Precision
Spend context well
Rerank and deduplicate so weak candidates do not consume the prompt
Policy
Enforce access
Apply tenant, identity, and freshness filters before evidence reaches the model
Grounding
Release with proof
Cite supported claims and refuse when evidence is incomplete
Separate the offline and online systems
RAG has two connected paths. The offline path turns source documents into searchable units. The online path turns a user question into an evidence-backed answer. Mixing their responsibilities makes failures difficult to diagnose.
A question earns its context
Every handoff has a different contract: ingestion preserves meaning and policy; retrieval finds candidates; generation uses only the final evidence packet.
Offline
Prepare the corpus
Parse structure, create stable chunks, attach source identity and access metadata, then build lexical and vector indexes.
Online
Plan the query
Resolve conversation references, decompose only when needed, and construct mandatory authorization and freshness filters.
Recall
Retrieve broadly
Run lexical, vector, or hybrid retrieval to gather a candidate set larger than the final context.
Precision
Select evidence
Rerank, deduplicate, enforce the token budget, and preserve source provenance for each selected passage.
Grounding
Generate or refuse
Answer from selected evidence, attach citations, and stop when required facts are missing or conflicting.
Keep ownership explicit
- Source owners define authoritative records, retention, and access policy.
- The ingestion pipeline preserves structure, lineage, policy metadata, and index freshness.
- The retrieval service owns query planning, filtering, candidate recall, and ranking.
- The answer service owns context assembly, citation mapping, refusal, and response policy.
- The evaluation system measures retrieval and generation separately so one cannot hide the other's failure.
Add complexity only for a measured retrieval failure
Simple default
Single-query RAG
Run one bounded query, select evidence, and generate once. Prefer this when questions are direct and the corpus uses consistent terminology.
Vocabulary mismatch
Hybrid retrieval
Combine exact-term and semantic signals. This helps when identifiers need lexical precision while paraphrases need vector recall.
Long structure
Hierarchical retrieval
Route through document or section summaries, then retrieve detailed passages from the selected parents. Use it when document structure carries meaning.
Multi-part questions
Multi-query retrieval
Decompose a question into bounded subqueries and merge evidence. Use it only when one query repeatedly misses required facts.
Agentic retrieval is not a quality switch. Planning calls add latency, cost, and new failure modes. Start with the smallest pipeline that passes a representative evaluation set, then add a stage for a named failure such as low exact-term recall, missing multi-hop facts, or poor ranking.
Pack the index and prompt into explicit budgets
Chunk size, overlap, embedding width, candidate count, and final evidence count affect different resources. Use the lab to change the corpus and query envelope. Watch raw vector storage, prompt usage, and reranking pressure move independently.
Design chunks as retrievable evidence units
A chunk should be large enough to preserve one useful idea and small enough to rank independently. There is no universal token count or overlap percentage. Tune the strategy against labeled questions from the actual corpus.
Preserve meaning before optimizing size
- Split on document structure such as headings, paragraphs, list boundaries, tables, and code blocks before falling back to token limits.
- Store stable source and chunk IDs, document version, section path, effective time, tenant, and access-control attributes with every chunk.
- Keep the original text available for display and citation even if cleaned text is used for embedding.
- Avoid copying headers or overlap into every chunk when it causes near-duplicate candidates to crowd out distinct evidence.
- Re-index deterministically so a source update can replace its previous chunks rather than creating silent duplicates.
Use parent context without flooding the prompt
Retrieve the smallest answer-bearing passage, then attach a bounded parent heading or neighboring passage only when it resolves an ambiguous reference. Parent-child retrieval can recover structure without ranking an entire long document as one unit.
Diagnose retrieval before blaming the model
The candidate set determines what the generator can know. Select a query, change the retrieval mode, and remove policy controls in the lab. The release verdict changes when the top evidence is irrelevant, stale, unauthorized, or incomplete.
Treat retrieval as a funnel, not one score
Lexical and vector retrieval produce candidate sets; reranking only reorders the candidates it receives. A reranker cannot recover a relevant passage that initial retrieval missed.
1 Policy
Apply hard filters
Constrain tenant, identity, document state, language, and effective time before candidates can enter the evidence path.
2 Candidate set
Retrieve for recall
Use lexical search for exact identifiers, vectors for semantic similarity, or both when the workload contains both query shapes.
3 Evidence set
Rerank for precision
Spend the expensive relevance model on a bounded candidate pool, then remove duplicates and passages that do not add a required fact.
4 Token budget
Assemble the prompt
Reserve answer and instruction tokens, order evidence deliberately, and retain source-to-passage mappings for citations.
Do not confuse these signals
- Similarity estimates how closely a chunk matches a query representation; it does not prove factual correctness.
- Reranker score orders the supplied candidates; score scales are model-specific and should not be treated as universal confidence.
- Citation presence shows that a source was attached; claim-level support still needs evaluation.
- Model confidence cannot repair missing, unauthorized, or stale evidence.
Build the context as an evidence contract
The final prompt should contain only what the answer service is allowed to use. Retrieved text is external data, not trusted instructions.
Before generation
- Preserve the system and application instructions outside the retrieved-data boundary.
- Label every passage with a stable source ID and delimit retrieved content from instructions.
- Drop unauthorized, deleted, expired, malformed, or untraceable passages.
- Detect conflicts across authoritative sources and route them to a refusal or resolution policy.
- Reserve enough tokens for the answer, citations, and safety instructions instead of filling the window with retrieval output.
After generation
- Map each material claim to supporting passage IDs.
- Reject citations that do not entail the attached claim.
- Return an explicit insufficient-evidence state when required facts are absent.
- Log source IDs, index version, query plan, filters, candidate ranks, selected passages, and policy version for replay.
Authorization must happen in retrieval, not only in the final prompt. Once forbidden text enters model context, response filtering cannot reliably undo the disclosure.
Evaluate the two systems separately
An end-to-end answer score is useful, but it cannot tell whether retrieval failed or generation ignored good evidence. Maintain a versioned evaluation set with questions, required facts, acceptable source IDs, access context, and time context.
Retrieval evaluation
- Recall at k: did the candidate set contain an acceptable answer-bearing source?
- Precision at k: how much of the selected evidence was actually useful?
- Fact coverage: did the final evidence packet contain every fact required by the question?
- Policy leakage rate: did any unauthorized or expired passage enter candidates, selected evidence, or logs?
- Freshness lag: how long after a source change until retrieval reflects the approved version?
Generation evaluation
- Groundedness: are material claims supported by the supplied passages?
- Answer completeness: does the response cover the required facts without inventing missing ones?
- Citation correctness: does each citation point to the passage that supports its claim?
- Refusal correctness: does the system stop when evidence is absent, conflicting, or forbidden?
Operate for stale indexes, partial dependencies, and unsafe content
- Index lag: expose source and index versions, alert on ingestion delay, and keep deletion updates on a short, measured path.
- Search degradation: define whether lexical-only or vector-only fallback is acceptable for each query class; do not silently change semantics.
- Reranker timeout: use a bounded fallback ranking and record that the precision stage was skipped.
- Embedding migration: dual-write or rebuild into a versioned index, evaluate at matched traffic, and switch with rollback available.
- Duplicate evidence: deduplicate by source lineage and semantic similarity before context assembly.
- Indirect prompt injection: treat instructions inside retrieved documents as untrusted content and restrict any downstream tools independently.
- Cross-tenant leakage: enforce policy filters in the retrieval request and include adversarial identity cases in every release evaluation.
- Missing evidence: return a typed refusal with the missing fact or unavailable source class instead of asking the model to guess.
Primary documentation behind the design
- Microsoft's RAG overview separates classic and agentic retrieval, explains token and security constraints, and recommends measuring retrieval choices against the workload.
- Azure AI Search chunking guidance explains why large documents must be partitioned for embedding and generation input limits.
- Azure hybrid ranking documentation describes rank fusion and keeps hybrid and semantic reranker scores conceptually separate.
- Amazon Bedrock reranking documentation defines reranking as reordering an existing candidate list rather than searching the corpus again.
- NIST's adversarial machine-learning taxonomy includes indirect prompt injection through external sources used by RAG applications.
Product defaults and APIs change. Treat these sources as implementation references and verify the current limits of the service and models you deploy.