FullStack AI Hub
Studio
AI & LLM Systems · 02. Inference & Retrieval
10 min
6 steps
Intermediate

RAG & Vector Retrieval Pipelines

Architect chunking, hybrid BM25 + HNSW vector search, cross-encoder reranking, and fresh incremental indexing.

AI & LLM Systems02. Inference & RetrievalLesson 2 of 2
10 minIntermediate6 steps

RAG & Vector Retrieval Pipelines

Architect chunking, hybrid BM25 + HNSW vector search, cross-encoder reranking, and fresh incremental indexing.

Recall@5
68%
Retrieval P95
95 ms
Citation Acc
74%
Tokens/Query
3.2k
top-5 chunksSearch UsersEmbedderVector DBLLMGenerator
User Query
Embedding / Index Write
Rerank / LLM Synthesis
Cited Grounded Answer
Hallucination / Stale Hit
In This LessonStep 1 of 6
What You Will Master
  • ✓Identify why dense-only vector search misses exact product IDs, acronyms, and fresh edits
  • ✓Combine BM25 lexical search with HNSW vector search via Reciprocal Rank Fusion (RRF)
  • ✓Boost top-5 precision with a fast cross-encoder reranker before LLM generation
  • ✓Keep document chunks fresh and permission-filtered with event-driven ingestion

Architectural Trade-Off Comparison Matrix

PatternBest ForKey Trade-Off
Dense Vector (HNSW)Conceptual similarity and paraphrased natural-language questionsWeak on exact SKU codes, error codes, and rare proper nouns
Sparse Lexical (BM25)Exact keyword, API symbol, and compliance clause matchingMisses synonyms unless expanded or paired with embeddings
Hybrid RRF + RerankerProduction enterprise RAG requiring high recall and grounded citationsAdds 40–90 ms rerank latency on the top-50 candidate set
Metadata Pre-FilteringMulti-tenant ACL enforcement (only search docs the user can read)Overly selective filters can degrade HNSW graph traversal recall

Step-by-Step Architectural Breakdown (6 Steps)

Full architectural walkthrough transcript and state transitions for RAG & Vector Retrieval Pipelines.

STEP 01 · SETUPJump to step →

Baseline Embed-and-Retrieve Pipeline

Retrieval-Augmented Generation grounds an LLM in private or up-to-date documents without retraining weights. A query is embedded into a vector, matched against an **HNSW vector index**, and injected into the prompt context.

idea:RAG quality is bounded by retrieval recall—if the right paragraph never reaches the prompt, the smartest LLM cannot cite it.

EmbeddingsHNSW IndexTop-K RetrievalContext Window
STEP 02 · PROBLEMJump to step →

Where Naive Dense Retrieval Fails

Ask a dense-only index for `ERR_CONN_4092` or a newly renamed policy, and semantic embeddings return vaguely related troubleshooting docs. Worse, naive fixed-size 500-character chunking slices tables and headers apart.

watch:Stuffing 20 noisy chunks into the prompt triggers "lost in the middle" attention decay and drives up hallucination rate.

Chunk Boundary LossSemantic DriftLost in the MiddleACL Leakage
STEP 03 · SOLUTIONJump to step →

Hybrid Retrieval with Reciprocal Rank Fusion

Fan every query out in parallel to both a **BM25 lexical index** (for exact IDs and names) and an **HNSW vector index** (for semantic intent), applying tenant ACL pre-filters at both stores.

A lightweight **Reciprocal Rank Fusion (RRF)** combiner merges the ranked lists without fragile score-scale normalization.

good:Hybrid BM25 + Vector retrieval routinely lifts recall by 15–25 points over either engine alone.

Hybrid SearchBM25Reciprocal Rank FusionACL Pre-Filter
STEP 04 · TECHNIQUEJump to step →

Two-Stage Cross-Encoder Reranking

Bi-encoder embeddings compress a whole paragraph into one vector independently of the query. A **Cross-Encoder Reranker** scores each `(query, chunk)` pair with full cross-attention over the top 50 RRF hits, keeping only the crispest top 5.

idea:Passing 5 razor-sharp reranked chunks costs 4× fewer LLM input tokens and beats passing 20 unranked chunks on grounded accuracy.

Cross-EncoderBi-EncoderPrecision@5Context Compression
STEP 05 · TRADE-OFFJump to step →

Event-Driven Chunking & Index Freshness

Nightly batch re-indexing leaves enterprise RAG 24 hours stale whenever a wiki page or pricing table changes. Instead, push document diffs onto an **ingestion queue**, re-chunk only changed sections with content hashes, and upsert both BM25 and Vector indexes atomically.

watch:Always track document version IDs so deleted or superseded chunks are tombstoned immediately—stale chunks silently poison answers.

Incremental IndexingContent HashingTombstone DeletesSemantic Chunking
STEP 06 · RECAPJump to step →

Production RAG System Recap

Production RAG is a search engineering discipline first and a prompting problem second. Combine **ACL-aware hybrid retrieval**, **cross-encoder reranking**, **event-driven indexing**, and inline citation checks.

good:Evaluate Retrieval Recall@K separately from Generation Faithfulness so you always know which half of the pipeline needs work.

Retrieval EvaluationFaithfulnessHybrid RAGCitation Grounding

Related Production Deep-Dives & Simulators

Included In Free Course
Free · Verifiable Credential

FS-102LLM Systems Foundations

Build a connected mental model from tokens through retrieval evaluation security and operations.

24 concepts~18h self-paced
Start Free Course
Weekly Engineering Dispatch

Get the weekly systems teardown

One technical note per week covering inference bottlenecks, retrieval harnesses, and commit-pinned teardowns.