Building Production-Ready RAG Systems
By Sairam Kumar Reddy Veereddy · AI/ML Engineer
Tutorials on Retrieval-Augmented Generation (RAG) make it look trivial: take a PDF, split it every 500 characters,
embed it with OpenAI text-embedding-3-small, query ChromaDB, and prompt GPT-4.
In production—especially in highly regulated environments like pharmacy clinical formularies or insurance underwriting—that basic setup fails catastrophically. Dense embeddings lose exact entity matches (such as NDC drug codes or policy numbers), standard chunking splits critical tables across borders, and the LLM generates convincing answers supported by non-existent citations.
Here are the architectural patterns required to transform toy RAG into a production-grade enterprise system.
The Enterprise RAG Pipeline
flowchart TD
Query[User Query] --> Pre[Query Decomposition & Expansion]
subgraph Retrieval [Two-Pronged Hybrid Retrieval]
Pre -->|Lexical Match| BM25[BM25 Keyword Search / Elastic]
Pre -->|Dense Vector| Vector[ChromaDB / Pinecone Vector Store]
end
BM25 --> Recip[Reciprocal Rank Fusion RRF]
Vector --> Recip
Recip --> Rerank[Cross-Encoder Re-Ranking Model]
Rerank --> Context[Top 5 High-Precision Chunks]
Context --> Guard[Citation Attribution & Guardrails]
Guard --> LLM[LLM Foundation Model]
LLM --> VerifiedAnswer[Audited Answer with Exact Citations]
1. Hybrid Retrieval: Why Dense Vectors Alone Fail
Vector embeddings represent semantic concepts well, but perform poorly on keyword specificity. If a doctor queries "Dosage for Metformin 500mg ER vs IR", a dense vector model might retrieve chunks discussing Metformin side effects or Glipizide dosages because their embeddings are nearby in latent space.
Production RAG must implement Hybrid Retrieval: pairing BM25 inverted index search (for exact keyword, drug, and code matches) with dense vector search (for conceptual semantics). We combine their ranked outputs using Reciprocal Rank Fusion (RRF):
def reciprocal_rank_fusion(dense_results, lexical_results, k=60):
"""Combines dense vector and BM25 rank scores without requiring score normalization."""
rrf_scores = {}
for rank, doc in enumerate(dense_results):
doc_id = doc["id"]
rrf_scores[doc_id] = rrf_scores.get(doc_id, 0) + (1 / (k + rank + 1))
for rank, doc in enumerate(lexical_results):
doc_id = doc["id"]
rrf_scores[doc_id] = rrf_scores.get(doc_id, 0) + (1 / (k + rank + 1))
# Sort docs by highest combined reciprocal rank
sorted_docs = sorted(rrf_scores.items(), key=lambda item: item[1], reverse=True)
return sorted_docs
2. Cross-Encoder Re-Ranking: The Quality Multiplier
Initial retrieval returns the top 25 candidate chunks. Feeding all 25 chunks into an LLM prompt inflates token costs and introduces the "lost in the middle" attention degradation phenomenon.
We insert a fast Cross-Encoder (such as bge-reranker-large or Cohere Rerank)
that computes full token-level cross-attention between the query and candidate chunk. This winnows 25 candidates
down to the 5 highest-relevance passages with high precision.
3. Enforcing Verifiable Citation Anchors
An enterprise RAG response is useless unless a human auditor can verify every claim.
We mandate that the LLM cite specific chunk IDs in brackets (e.g. [Doc-3, Page 14]).
A post-generation validation step verifies that every cited chunk was indeed in the prompt and contains
the supporting semantic tokens.