MODULE 02 · AI & LLM SYSTEMS ENGINEERING

Production RAG, Hybrid Search & Reranking

Why naive chunk-and-cosine retrieval breaks at scale — architecting Parent-Child chunking, ACL pre-filtered HNSW + BM25 Reciprocal Rank Fusion, Cross-Encoder reranking with graceful degradation, and grounded citation verification.

Act I · Scope, Retrieval Math & Latency Budgets

Hybrid Index Sizing & End-to-End Retrieval Envelope

Step 1 of 5
Corpus Vector Footprint
10M Chunks (~43 GB)

1536-dim FP16 vectors (~30.7 GB) + HNSW graph (~12 GB); INT8 scalar quantization cuts RAM to ~16 GB with 3x oversampling.

Retrieval Budget
< 180 ms P95

End-to-end retrieval budget: 25ms embed + 35ms parallel HNSW/BM25 + 90ms Cross-Encoder rerank + 30ms ACL & assembly.

Hybrid Recall
Recall@50 ≥ 95%

Hybrid Dense + BM25 RRF fusion lifts exact-match SKU, error-code, and acronym recall by +18–26% over pure vector search.

Citation Grounding
≥ 98% Grounded

Strict inline [chunk_id] citation verification with automatic refusal when top Cross-Encoder score < 0.45.

Act II · Wire Protocols, Chunk & Dual-Index Contracts

Hybrid Query Wire Contract & Parent-Child Dual-Index Schema

Step 2 of 5
Hybrid Retrieval & Grounded Response (POST /v1/rag/query)
Parent-Child Chunk & Dual-Index Schema
Why Pre-Filtering ACLs Is Mandatory: Post-filtering top-K vector results starves recall (e.g., 48 of 50 candidates belong to restricted groups, leaving only 2 chunks) and leaks tenant existence via timing. Always apply acl_tags bitmasks inside HNSW & BM25 traversal.
Act III · Interactive RAG Pipeline, Live Ranking Inspector & Chaos Switchboard

Hybrid HNSW + BM25 Fusion, Reranking & Degradation Lab

HYBRID + RERANK
Step 1 / 6
Retrieval P95
142 ms
Budget < 180 ms
Top-5 Faithfulness
98.6%
SLO ≥ 98.0%
Context Packing
5 / 50 Chunks · 3.8K tok
Edge-first [0,2,4,3,1]
Pipeline Mode
HYBRID + RERANK
embed-v3 · ACL Pre-Filter
RAG Pipeline Topology · Hybrid Search & RRF Fusion
QUERY EMBED
search Query & Embedder embed-v3 · 1536d FP16 ERR_4092 · 25ms hub Dense HNSW Index Cosine ANN · Pre-ACL TOP-50 · 32ms text_fields Sparse BM25 Index Exact Lexical · Pre-ACL EXACT HIT · 18ms merge RRF & ACL Gate 1/(60+r) · Ver Guard FUSED TOP-50 tune Cross-Encoder 120ms Breaker TOP-5 · 86ms auto_awesome Context & LLM Edge Pack · [C-ID] GROUNDED 98.6%
QUERY EMBED

Loading RAG simulator...

Invariant active
Live Candidate Ranking & RRF Inspector
Query: "Fix ERR_4092 gateway retry limit"
Chunk ID & Title Dense Rank BM25 Rank RRF 1/(60+r) Rerank Score Status
Chaos Switchboard
0 Active
Event Telemetry Console
Act IV · Zero-Dependency Python 3.12+ Blueprints

Standard-Library Python RAG Core & Production Stack Mapping

Auto-Synced to Simulator
Production Stack Mapping Strip 6 RAG Primitives
Vendor-Agnostic Primitive Role in Pipeline Production Systems
Structure-Aware Parent-Child Chunker Preserves headings/tables, embeds small child spans, retrieves rich parent sections Docling, Unstructured, Tree-sitter
Dense Vector Index (HNSW / DiskANN) Sub-35ms semantic ANN lookup with INT8/FP16 quantization & ACL bitmask Qdrant, Milvus, pgvectorscale, Turbopuffer
Sparse Lexical Index (BM25 / SPLADE) Exact match for error codes, SKUs, acronyms, and rare domain identifiers OpenSearch, Elasticsearch, Vespa, Tantivy
Hybrid Fusion & Pre-Filter ACL Gate Merges scale-invariant ranks via RRF (k=60) & enforces embedding version parity Vespa, Qdrant Query API, Custom RRF Service
Cross-Encoder Reranker Joint query-document self-attention scoring Top-50 → Top-5 with 120ms breaker Cohere Rerank 3.5, BGE-Reranker-v2-M3, HuggingFace TEI
Citation & Faithfulness Verifier Validates inline [chunk_id] entailment and enforces low-score refusal NLI Cross-Encoder, LettuceDetect, Custom Span Verifier
Act V · Quality / Latency / Cost Tradeoffs & Production SLOs

Retrieval Architecture Tradeoffs & Production Targets

Step 5 of 5
Tradeoff 01 Security & Recall

Pre-Filtering vs. Post-Filtering ACLs

Pre-Filtered HNSW/BM25

Evaluates acl_tags bitmasks during graph traversal—preserving full Top-50 recall and 0.00% cross-tenant leakage.

Post-Filter After ANN

Simple to bolt on, but if 48 of 50 neighbors belong to restricted teams, recall collapses to 2 chunks and leaks timing metadata.

Verdict Always enforce Pre-Filtered ACLs inside the index; for tiny tenants (<1% of corpus), fall back automatically to filtered brute-force scan.
Tradeoff 02 Chunk Granularity

Fixed-Window vs. Parent-Child Hierarchical Chunking

Parent-Child Hierarchy

Embeds crisp 256-token child spans for high-precision retrieval, then expands to the 1,024-token parent section for complete LLM reasoning.

Fixed 512-Token Window

Trivial to code, but slices Markdown tables, code blocks, and runbook steps in half—causing incomplete or hallucinated citations.

Verdict Decouple indexing unit (small child chunk) from generation unit (parent section) and deduplicate via SHA-256 content hashes.
Tradeoff 03 Precision vs. Latency

Bi-Encoder ANN vs. Two-Stage Cross-Encoder Reranking

Two-Stage + Breaker

Fast Bi-Encoder + BM25 narrows 10M chunks → Top-50 in 35ms; Cross-Encoder scores full token-to-token attention on Top-50 in 86ms.

Bi-Encoder Only

Fastest (~35ms), but independent embeddings miss subtle negation, version numbers, and multi-hop relevance—letting decoys into Top-5.

Verdict Always run Two-Stage Reranking wrapped in a strict 120 ms timeout breaker that degrades gracefully to RRF-only ordering.
Tradeoff 04 RAM Cost vs. Recall

FP16 HNSW vs. INT8/Binary Quantization + NVMe Rescoring

INT8/Binary + Rescore

Cuts 10M-vector RAM from ~43 GB down to ~16 GB (INT8) or ~4 GB (Binary); oversampling 3x and rescoring FP16 from NVMe retains >98% recall.

Uncompressed FP16 RAM

Zero quantization loss and lowest latency, but RAM costs scale linearly at ~$300–$500/mo per 10M chunks in high-memory clusters.

Verdict Use INT8 scalar quantization + 3x oversample rescoring as the default above 5M chunks to cut cluster RAM by ~62%.
Production Observability SLOs
Enterprise RAG Targets
P95 Retrieval Latency < 180 ms Embed + Hybrid + Rerank
Hybrid Recall@50 ≥ 95.0% Dense HNSW + BM25 RRF
Citation Faithfulness ≥ 98.0% Inline [chunk_id] verified
Cross-Tenant ACL Leakage 0.00% Pre-filtered index bitmask