Lesson 4 of 9 · 27m
RAG Architecture That Survives Production
Design ingestion, indexing, retrieval, grounding, and citation as separate pipeline stages with measurable behavior at each boundary.
Retrieval-augmented generation fails in a way that feels unfair: the demo works beautifully because the demo corpus is small, clean, and written to match the questions. Production corpora are versioned, contradictory, duplicated, permissioned, stale, and full of edge cases. RAG is not a vector database with a prompt attached. It is a content pipeline whose output the model is allowed to consult.
Draw the pipeline before choosing tools
source systems -> extraction -> normalization -> chunking
-> embedding/indexing -> query rewriting -> retrieval
-> reranking -> context packing -> generation -> citation validationEach stage has independent failure modes. If you cannot inspect each boundary, every bad answer becomes a vague complaint that "the AI hallucinated." Usually it did not hallucinate in isolation; the pipeline retrieved the wrong thing, omitted the right thing, or packed context badly.
Ingestion is a data engineering problem
PDF extraction loses tables. HTML extraction captures navigation. Tickets contain personal data. Wikis contain obsolete pages with confident titles. Normalize documents into a durable intermediate representation:
{
"doc_id": "kb_1842",
"source": "help_center",
"title": "Refunds for annual plans",
"version": "2026-11-12",
"acl": ["support", "customer:enterprise"],
"text": "Annual plans may be refunded within 30 days...",
"checksum": "sha256:..."
}Do not index what you cannot authorize. ACLs must travel with chunks, not just parent documents, because retrieval returns chunks.
Chunk for retrieval units, not token counts
Chunking by a fixed token count is easy and often wrong. A good chunk is a coherent answer unit: a policy section, a procedure, an API reference entry, or a table plus its caption. Overlap helps preserve continuity, but large overlap creates duplicate evidence and wasted context.
chunks = split_markdown(
document.text,
split_on=["##", "###"],
max_tokens=700,
overlap_tokens=80,
)Keep structural metadata: headings, source URL, document version, effective date, product area, and permissions. Metadata filters often improve retrieval more than another embedding model upgrade.
Retrieval is recall; reranking is precision
Vector search finds semantically plausible material, but lexical search still matters for IDs, error codes, exact product names, and version numbers. Hybrid retrieval is the boring winner:
SELECT chunk_id, text, source_url,
ts_rank_cd(text_tsv, plainto_tsquery('english', 'refund annual plan')) AS bm25
FROM chunks
WHERE acl && ARRAY['support']
ORDER BY embedding <=> $1
LIMIT 20;Then rerank the union of vector and keyword candidates. Retrieve wide, rerank narrow, pack only what the answer can use. Ten mediocre chunks are not better than three precise ones.
Grounding means bounded claims
The generation prompt should say what happens when evidence is missing: answer with a bounded uncertainty statement or escalate. Require citations that map claims to chunk IDs. Validate that cited chunks exist and actually contain the answer span or close paraphrase. Citation formatting is easy; citation truth is the feature.
Field notes
- If users ask about exact error codes, add lexical search before buying a fancier embedding model.
- Deleted source documents must delete chunks and cache entries too.
- Re-indexing is a migration: version embedding model, chunker, and normalization logic.
- Contradictory documents need precedence rules; the model should not be your document governance system.
- A successful retrieval set can still produce a bad answer if the packing order hides the decisive passage.
Was this lesson helpful?