Skip to main content

RAG Architecture

1. Executive Summary​

Retrieval-Augmented Generation (RAG) grounds large language model responses in external knowledge retrieved at query time, reducing hallucinations and enabling answers over private corpora without full model retraining. A production RAG system spans ingestion (chunking, embedding, indexing), retrieval (dense vector, sparse BM25, hybrid), reranking, context assembly, and generation with careful prompt engineering and evaluation.

Architecturally, RAG is a distributed data pipeline plus inference path: vector databases (Pinecone, Weaviate, pgvector, OpenSearch k-NN) scale similarity search; embedding models must version with index; freshness requires incremental indexing and staleness SLAs. Principal architects address security (tenant isolation in indexes), cost (embedding and retrieval at scale), and correctness (recall vs precision, attribution).

This chapter covers mechanisms, guarantees, failure modes, and interview-ready design patterns for enterprise knowledge assistants.

Production RAG failures rarely stem from vector database choice—they stem from bad chunking, missing ACL propagation, and absence of eval harnesses. Treat retrieval quality as a data engineering metric (recall@k, MRR) and generation quality separately (faithfulness). Staff cross-functional owners for ingest, retrieval, and generation tiers.

2. Why This Topic Matters​

RAG is the default enterprise AI pattern—interviewers ask:

  • Chunking strategy? — Size, overlap, structure-aware splits.
  • Vector vs keyword search? — Hybrid when both matter.
  • When RAG fails? — Wrong retrieval, context overflow, stale docs.
  • Evaluation? — Recall@k, faithfulness, human eval loops.
  • Multi-tenant index design? — Namespace isolation vs filtered search.

"We embedded everything in Pinecone" without ingestion discipline produces demos, not production systems.

Principal-level panels increasingly ask for end-to-end architecture: ingest SLA, retrieval latency budget, faithfulness metrics, and tenant isolation proof—not a diagram of three boxes labeled embed, search, generate. Budget time to whiteboard eval metrics and ACL filters in every RAG design review. Treat stale indexes as production incidents with defined freshness SLOs.

3. Problems Being Solved​

ProblemRAG approach
LLM lacks private knowledgeRetrieve relevant chunks at query time
Hallucination on factsGround generation in retrieved context
Frequent knowledge updatesRe-index without retraining model
Long documents exceed contextChunk + retrieve top-k
Attribution / complianceCite source chunks in response
Cost of huge context windowsRetrieve only relevant passages

Workload fit matrix​

Use caseRAG fitCaveat
Internal policy Q&AStrongAccess control on chunks
Customer support KBStrongFreshness SLA
Code search assistantStrongStructure-aware chunking
Real-time market dataWeakNeeds tools/APIs not static index
Tasks requiring reasoning onlyWeakMay not need retrieval
Highly structured SQL analyticsWeakText-to-SQL separate pattern

4. Assumptions and System Model​

AssumptionImplication
Chunks represent retrievable factsChunking quality dominates recall
Embedding model fixed per indexRe-embed on model change
Similarity ≈ relevanceReranking often required
Context window boundedTop-k and compression tradeoffs
Sources may updateIncremental index + tombstones

Safety: Retrieved content must respect ACLs—never leak cross-tenant chunks. Liveness: Stale index degrades answer quality without hard failure.

5. Essential Terminology​

TermDefinition
EmbeddingDense vector representation of text
ChunkIndexed text segment with metadata
Vector databaseANN index for similarity search
BM25Sparse lexical retrieval scoring
Hybrid searchCombine dense + sparse scores
RerankerCross-encoder scoring query-doc pairs
Top-kNumber of chunks retrieved
MMRMaximal Marginal Relevance—diversity in results
FaithfulnessAnswer supported by retrieved context
GraphRAGKnowledge graph enhanced retrieval [research/product evolving]

6. Core Mechanism​

6.1 RAG pipeline overview​

Figure 1: Offline indexing path separate from online retrieval-generation loop.

6.2 Hybrid retrieval​

Figure 2: Hybrid retrieval improves recall when queries contain exact identifiers or rare terms.

6.3 Multi-tenant index isolation​

Figure 3: Metadata filters or separate namespaces enforce tenant isolation at retrieval.

7. Step-by-Step Walkthrough​

Walkthrough A: Document ingestion​

  1. PDF uploaded to object storage; trigger extraction (text + layout).
  2. Structure-aware chunking: 512 tokens, 64 overlap, preserve headings in metadata.
  3. Embed with text-embedding-3-large equivalent; store vector + doc_id, page, acl_group.
  4. Upsert to vector index; catalog registers lineage in governance system.

Walkthrough B: Query execution​

  1. User asks: "What is our parental leave policy in Germany?"
  2. Query embedding computed; hybrid search returns 20 candidates.
  3. Cross-encoder reranker scores top 5.
  4. Prompt template inserts chunks with [Source: HR-DE-2024] tags.
  5. LLM generates answer; post-process validates citations exist in context.

Walkthrough C: Incremental update​

  1. HR updates policy PDF; webhook fires re-ingestion.
  2. Delete old chunks by doc_id; insert new version chunks.
  3. Eventual consistency: 1–5 min index lag documented in SLA.
  4. Cache invalidation for related prefix caches in LLM gateway.

Walkthrough D: Failure—wrong retrieval​

  1. User reports incorrect answer about expense limits.
  2. Trace: retrieval returned outdated expense-2022 chunk ranked above expense-2025.
  3. Fix: boost recency metadata; re-ingest with version field; add eval case.

Walkthrough E: Parent-child chunking for manuals​

  1. Technical manual ingested with heading-aware splits—each chunk retains section_path metadata.
  2. Retrieval returns subsection chunks; LLM cites section_path in answer.
  3. User navigates to source PDF page via stored page_number metadata.
  4. Eval verifies citation section_path exists in retrieved set—automated faithfulness check.
  5. Reduces hallucinated section references common with naive fixed-size chunks.

Walkthrough F: Query routing in advanced RAG​

  1. Classifier determines query type: factual lookup vs summarization vs comparison.
  2. Factual: hybrid search k=10, rerank to 3, low temperature generation.
  3. Comparison: multi-query retrieval (LLM generates 3 sub-queries), union results, dedupe.
  4. Summarization: retrieve broader k=30, map-reduce summarize chunks before final answer.
  5. Platform logs routing decision for offline eval of classifier accuracy.

RAG quality metrics (operational)​

MetricDefinitionTarget direction
recall@kRelevant doc in top kHigher
MRRMean reciprocal rankHigher
FaithfulnessAnswer supported by contextHigher
Citation accuracyCited chunk IDs validHigher
Latency p99End-to-endLower per SLO
Index freshness lagTime since last ingestLower

8. Invariants and Guarantees​

PropertyGuarantee
ACL enforcementRetrieved chunks ⊆ caller-authorized set
Citation integritySources map to indexed chunk IDs
Index versioningEmbedding model version tracked
Idempotent ingestionSame doc_id replace semantics

RAG does not guarantee factual correctness—only that model sees specified context. Faithfulness is eval metric, not theorem.

9. Failure Scenarios​

FailureBehaviorMitigation
Low recallLLM hallucinates or abstains poorlyHybrid search, reranker, query expansion
Context overflowTruncated prompt loses infoCompress chunks; increase k selectively
Stale indexWrong answersFreshness SLA; CDC ingestion
Embedding model changeIndex incompatibleBlue/green re-index
Poisoned documentMalicious content in answersAuth on upload; content scanning
Cross-tenant leakCritical security incidentFilter tests; separate indexes
Slow ANN at scaleHigh p99 latencySharding; HNSW tuning; cache hot queries

10. Performance Characteristics​

DimensionBehavior
IngestionBatch embed bound by GPU/API throughput
RetrievalSub-100ms ANN at millions of vectors [tuned]
RerankingAdds 50–200ms for cross-encoder on top-k
End-to-end latencyRetrieval + LLM TTFT
Index sizeMemory/disk scales with dimensions × vectors

11. Scalability Limits​

  • Billions of vectors require sharded indexes and approximate recall tradeoffs.
  • Wide metadata filters slow ANN if poorly indexed.
  • Massive chunks reduce retrieval precision.
  • Real-time ingestion vs batch—write amplification on index.
  • Multi-language corpora need language-aware embeddings.

12. Operational Considerations​

  • Eval harness: golden questions with expected source IDs.
  • Monitor recall@k, MRR, faithfulness score, latency per stage.
  • Version embedding models in index metadata.
  • Dead letter queue for failed ingestions.
  • Capacity plan: embedding API QPS vs batch jobs.
  • Run red team tests for ACL bypass via prompt injection.
  • Re-index playbook tested quarterly including embedding model version bump drill.
  • Ingestion DLQ reviewed weekly; poison documents quarantined with steward notification.
  • Chunk size histogram monitored—bimodal distribution signals bad chunking strategy.
  • Hybrid search weights tuned monthly on held-out eval set; log fusion parameters in metadata.

13. Security Considerations​

  • Document-level ACL propagated to chunk metadata.
  • Filter mandatory on every query—never optional post-filter.
  • Sanitize retrieved HTML before prompt injection.
  • Audit what chunks were retrieved per query (metadata only if PII concern).
  • Encrypt vectors at rest if policy requires [often metadata more sensitive].

14. Cost Considerations​

  • Embedding cost at ingest scale—cache chunk hashes to skip unchanged.
  • Vector DB hosting vs managed SaaS.
  • LLM tokens—retrieved context increases input tokens.
  • Reranker GPU—optional tier for quality-sensitive paths.
  • Re-index on model change—budget full rebuild.

Chunking strategy comparison​

StrategyBest forRisk
Fixed token windowGeneral proseSplits tables/code badly
Recursive characterMixed docsMay lose structure
Heading-awareManuals, policiesRequires clean headings
Semantic chunkingHeterogeneous corporaHigher ingest cost
Parent-document retrieverLong coherent sectionsTwo-stage retrieval complexity

Principal architects pilot chunking on golden question set before full corpus ingest—cheap eval prevents expensive re-index.

Hybrid search score fusion​

Reciprocal Rank Fusion (RRF) combines dense and sparse rankings without calibrating score scales: score = Σ 1/(k + rank_i) with k≈60 common default [verify tuning]. Weighted linear fusion requires score normalization—fragile across query types. Document fusion choice in platform standards; A/B test recall@k on held-out queries.

RAG vs fine-tune decision guide​

SignalPrefer RAGPrefer fine-tune
Knowledge changes weekly✓
Style/format consistency✓
Private docs with ACL✓
Proprietary reasoning pattern✓
Must cite sources✓
Low latency no retrieval✓

Many production systems combine both—RAG for facts, light fine-tune for tone and tool-use format.

15. Production Implementations​

Case study: Enterprise HR assistant (illustrative)​

Context​

50k employees; 12k policy documents; SOC2 requirements.

Architecture​

S3 → Lambda extract → chunk → embed (batch) → OpenSearch k-NN. Gateway enforces HR role ACL filter. Reranker on GPU sidecar. GPT-4 class model with 8k retrieved context budget.

Eval​

200 golden QAs; weekly regression; faithfulness > 92% target [internal metric].

Extended operations narrative​

Initial launch used pure vector search—recall failed on internal SKU queries until hybrid BM25 added. ACL penetration test found missing filter on admin API path; fixed before external audit. Re-index took 18 hours when embedding model upgraded—blue/green index cutover now standard playbook. Legal required citation of section_path in every HR answer—post-processor validates citation IDs against retrieved set.

Tradeoffs​

ChoiceTradeoff
OpenSearch vs PineconeOps vs managed
512-token chunksPrecision vs context fragmentation
Hybrid searchIndex complexity vs recall

16. Alternatives and Tradeoffs​

ApproachWhen
Fine-tuning onlyStatic knowledge; high train cost
Long context onlySmall corpus; expensive tokens
RAGDynamic/private knowledge
Tool calling / APIsLive structured data
Knowledge graph RAGRich entity relationships

17. Common Misconceptions​

MisconceptionReality
"RAG eliminates hallucination"Reduces; does not eliminate
"Bigger k always better"Noise overwhelms context
"Same embedding for ingest and query"Must use same model version
"Vector search finds exact matches"Semantic not lexical—use hybrid
"One chunk size fits all"Code, tables, prose differ

18. Principal Architect Perspective​

  1. Invest in ingestion and eval more than prompt tweaks.
  2. Hybrid retrieval default for enterprise text.
  3. ACL at index metadata—non-negotiable for multi-tenant.
  4. Track embedding model version like schema version.
  5. Separate offline index from online serving SLOs.

RAG is a data product with freshness, ACL, and quality SLOs—not a vector database science project. Principals who own outcomes invest in golden evals and ingestion before debating embedding model benchmarks. Treat re-indexing as a schema migration with communication, dual-run periods, and rollback plans.

Operating playbook (first 90 days)​

Days 1–30: Build golden eval set (50–200 QAs) with expected source chunk IDs. Establish chunking strategy on pilot corpus.

Days 31–60: Deploy hybrid retrieval + reranker; measure recall@k and faithfulness weekly. Enforce ACL filters in integration tests.

Days 61–90: Automate incremental ingest with freshness SLO. Run red-team tests for cross-tenant retrieval bypass.

19. Architecture Review Exercise​

Scenario: Single shared index for all tenants; ACL checked after LLM response generation.

Findings: Retrieval leak risk; fix mandatory pre-retrieval filter; penetration test.

20. Whiteboard Explanation​

"Documents are split into chunks, embedded into vectors, and stored in an ANN index. At query time, we embed the question, retrieve top-k similar chunks—often hybrid with keyword search for exact matches—rerank with a cross-encoder, then stuff the best passages into the LLM prompt with citation markers. The model generates an answer grounded in that context. Ingestion runs asynchronously when docs change; we version embeddings and enforce tenant filters on every search. Quality depends on chunking and recall; we measure with golden datasets and faithfulness checks, not vibes."

Principal addendum: Enforce ACL at retrieval. Hybrid + rerank for enterprise corpora. Invest in eval and ingestion before prompt tuning.

21. Interview Questions​

  1. What is RAG? — Retrieve context then generate answer.
  2. Chunking tradeoffs? — Size vs precision vs context limit.
  3. Dense vs sparse retrieval? — Semantic vs lexical matching.
  4. Why reranker? — Cross-encoder precision on top-k.
  5. Hybrid search fusion? — Weighted or RRF score combination.
  6. ACL in RAG? — Metadata filters at retrieval time.
  7. Embedding model change impact? — Full re-index required.
  8. Faithfulness metric? — Answer supported by context.
  9. When RAG insufficient? — Live data, reasoning-only tasks.
  10. GraphRAG concept? — Graph structure augments retrieval [verify product claims].
  11. Context window management? — Top-k, compression, summarization.
  12. Incremental indexing? — Upsert/delete by doc_id.
  13. Eval recall@k? — Relevant doc in top k results.
  14. Poisoning attack? — Malicious docs in corpus.

Scoring rubric (principal)​

DimensionStrongWeak
PipelineIngest + retrieve + eval"Embed and ask"
SecurityPre-retrieval ACLPost-hoc check
QualityHybrid + rerank + metricsPrompt-only
OpsVersioning, freshnessStatic demo

Extended scoring notes​

Principal bar: Names hybrid retrieval, reranker, and eval metrics in first two minutes. ACL at retrieval mandatory. Weak hire: "We use Pinecone" with no ingestion or freshness story.

  1. ANN vs exact search? — Approximate tradeoff recall/latency.
  2. Chunk size too large symptom? — Low precision in context.
  3. GraphRAG when? — Entity-heavy corpora [verify product fit].

22. Interview Follow-Ups​

  1. Design RAG for 10M documents, 1k QPS. — Sharded index, cache, async embed, gateway.
  2. User asks about doc uploaded 30s ago. — Ingestion lag SLA; streaming index optional.
  3. Improve recall on SKU queries. — Hybrid BM25 + metadata exact match field.
  4. Prove tenant isolation. — Filter injection tests; separate namespaces.
  5. Cost reduce 50%. — Smaller embed model, fewer k, cache embeddings, compress context.

Additional principal scenarios​

Scenario: Legal requires deletion of user data from RAG index. Answer: Lineage + metadata filter on user_id; delete chunks; run compaction; verify with search probe; document in erasure ticket.

Scenario: Retrieval returns correct chunk but LLM hallucinates anyway. Answer: Lower temperature; require citation format; faithfulness classifier; eval gap may be generation not retrieval—tune both stages independently.

Scenario: Embedding model vendor deprecates model version. Answer: Blue/green re-index plan; dual-run period; freeze ingest during cutover; communicate freshness gap in SLA.

22A. Production Low-Level Design (Interview + Game Day)​

Enterprise RAG platform with hybrid retrieval and ACL-enforced index. Production LLD and Game Day Standard.

Control vs data plane​

PlaneComponents
ControlIngest pipeline, embedding model version registry, eval harness, catalog ACL sync
DataVector index (ANN), BM25 sparse index, reranker, LLM gateway

Step-by-step query path:

StepActionFailure mode
1Gateway extracts tenant_id from JWT401 unauthenticated
2Filtered ANN + BM25 hybridEmpty if ACL too strict—monitor
3Cross-encoder rerank top-kLatency budget ~100ms
4Prompt with citation tagsContext overflow if k too large
5Post-gen citation validationHallucinated cite flagged
6Audit log chunk IDs retrievedCross-tenant alert

Ingest contract​

FieldValue
Chunking512 tokens, 64 overlap, structure-aware
Metadatadoc_id, tenant_id, acl_groups, version
Freshness SLAIndex lag < 5 min p99 after doc update
Re-indexBlue/green on embedding model change

One SLO​

RAG quality ops: faithfulness > 92% on golden set [internal metric]; retrieval p99 < 300ms; zero cross-tenant chunk in integration tests.


22B. Game Day Playbooks​

#DrillInjectDetectMitigateVerify
GD-1Stale indexPause ingest 30mFreshness SLO burnResume ingest; priority re-indexGolden QA passes
GD-2ACL bypass attemptQuery without tenant filterSecurity test failureFix gateway; block API pathFilter mandatory
GD-3Wrong retrieval dominatesBoost outdated doc versionEval regression on SKU queriesRecency boost + re-ingestrecall@k recovers

GD-2 — Cross-tenant retrieval leak​

23. Strong Answer Example​

Question: "How do you prevent cross-tenant data leakage in a shared vector index?"

Answer this as a 45-minute security design using the PRACTICE framework. Below is how a strong student sounds aloud—ACL at retrieval, not post-generation hope.


Student interview script (PRACTICE)​

P — Problem framing​

"Shared vector index for cost efficiency—but one retrieval miss leaks another tenant's chunks into LLM context. Post-generation filtering is too late; logs and intermediate steps may already expose data."

R — Requirements​

TypeRequirement
FRHybrid retrieval; citations; multi-tenant index
NFRZero cross-tenant chunks in prod traces
SecurityMandatory metadata filters from JWT
OpsPen tests; audit chunk IDs per request

A — Assumptions​

AssumptionImplication
ANN without filter scans global spaceFiltered ANN required
Clients cannot set tenant_idGateway injects from auth
Highest isolation tenants existDedicated namespace option

C — Capacity​

Tenants (say) → 500 on shared index
Chunks → 10M with tenant_id + acl_groups on every row
Query QPS → 1K; filter reduces candidate set pre-ANN
Pen test → weekly bypass attempts in CI

T — Topology​

"Every chunk has tenant_id + acl_groups. Gateway injects filters from JWT—filtered approximate nearest neighbor. Integration tests attempt bypass. Sensitive tenants get dedicated index. Audit retrieved chunk IDs; alert on ACL mismatch."

I — Invariants​

"No query without filter. No ACL propagation skip in ingest. Embedding model version pinned like schema version."

E — Evolution​

PhaseAdd
M1Mandatory filters + tests
M2Audit + alerting
M3Dedicated indexes for tier-0 tenants

Closing line​

"Metadata on every chunk, filter at retrieval, gateway-enforced JWT claims, pen tests, and audit—shared index only when isolation is proven, not assumed."

Scoring signals​

StrongWeak
Pre-retrieval ACLPost-hoc output filter only
Filtered ANN"Pinecone namespaces" without enforcement
Audit chunk IDsTrust SDK defaults

24. Weak Answer Example​

Weak: "We use Pinecone namespaces per customer and trust the SDK."

Red flags: No filter enforcement story; no eval; no ingestion ACL propagation.

25. Hands-On Exercise​

Lab: labs/lab-015-rag-platform/ — ingest + hybrid retrieval on :8105

cd labs/lab-015-rag-platform
python3 -m venv .venv && source .venv/bin/activate
pip install -r requirements.txt
pytest tests/ -v
docker compose -p lab015 -f docker/docker-compose.yml up --build -d
chmod +x scripts/demo_rag.sh && ./scripts/demo_rag.sh
StepEndpointWhat happens
1POST /v1/documentsChunk + embed markdown corpus
2GET /v1/documentsList ingested docs + chunk counts
3POST /v1/queryHybrid vector + BM25 retrieval
4ResponseAnswer + citations from top chunks
5ACL filterTenant-scoped retrieval (bypass attempt fails)

Swagger: http://localhost:8105/docs

Engineer guide: how the local stack works​

  1. Ingestion pipeline — chunk overlap, embedding, vector store insert with metadata ACL.
  2. Hybrid retriever — dense ANN + sparse BM25 fusion for acronym/keyword queries.
  3. Reranker — cross-encoder re-scores top-k before LLM context assembly.
  4. Citation grounding — answer must reference retrieved chunk IDs (faithfulness check).
  5. Latency breakdown — embed, retrieve, rerank, generate stages timed separately.

Build-from-scratch exercise (optional)​

  1. Build minimal RAG: chunk markdown files, pgvector, local LLM.
  2. Compare pure vector vs hybrid with BM25 on acronym queries.
  3. Add reranker; measure MRR on 20 test questions.
  4. Simulate ACL filter; attempt bypass.
  5. Measure end-to-end latency breakdown per stage.

26. Knowledge Check​

  1. RAG two phases? (Retrieve + generate.)
  2. Chunk overlap purpose? (Context continuity.)
  3. BM25 is? (Sparse lexical retrieval.)
  4. Reranker type? (Cross-encoder typically.)
  5. ANN stands for? (Approximate nearest neighbor.)
  6. Faithfulness means? (Answer grounded in context.)
  7. Re-index trigger? (Embedding model change.)
  8. Hybrid search why? (Lexical + semantic recall.)
  9. ACL filter when? (Before/during retrieval.)
  10. recall@k measures? (Relevant in top k.)
  11. MMR purpose? (Diverse retrieval results.)
  12. Citation integrity? (Sources map to chunk IDs.)
  13. Stale index symptom? (Wrong factual answers.)

27. Flashcards​

FrontBack
RAGRetrieval-Augmented Generation
EmbeddingDense vector text representation
ChunkIndexed text segment
Vector databaseANN similarity search store
BM25Lexical sparse retrieval
Hybrid searchDense + sparse combined
RerankerRe-scores top candidates
FaithfulnessAnswer supported by context
Top-kNumber of retrieved chunks
Metadata filterACL enforcement at retrieval

28. Cheat Sheet​

RAG PIPELINE
Ingest: chunk → embed → index
Query: embed → retrieve → rerank → prompt → LLM

QUALITY LEVERS
Chunking, hybrid search, reranker, eval golden set

SECURITY
ACL metadata on every chunk; filter at retrieval

OPS
Embed model versioning, freshness SLA, re-index playbook

PRINCIPAL ANCHORS
ACL filter at retrieval
Hybrid for enterprise text
Eval golden set first
Chunking drives recall
Faithfulness ≠ hallucination-free
Re-index on embed model change
Ingestion is data product
Citation validation post-gen

30. References​

Primary sources​

  • Lewis, P., et al. (2020). Retrieval-Augmented Generation for Knowledge-Intensive NLP Tasks. NeurIPS.
  • OpenSearch k-NN, Pinecone, Weaviate documentation — ANN index implementations.
  • Anthropic/OpenAI retrieval guides — implementation patterns.
  • BEIR benchmark — retrieval evaluation methodology.

Principal study path​

Continue with LLM Serving and Model Gateways for generation tier, Agent Platform Architecture for RAG-as-tool patterns, Data Governance and Lineage for document ACLs, and Distributed Caching for query cache layers. Principal interviews often ask end-to-end latency breakdown from embed through first token.

Distinction​

ClaimType
RAG formal problemLewis et al. paper
ANN recall/latencyIndex parameters—empirical
GraphRAG capabilitiesVerify Microsoft/product docs