Building a Production-Ready RAG Application
01 // The Technical Problem
Standard RAG setups run simple vector lookups and feed the top results to the LLM. In production, this approach fails because vector similarity does not guarantee semantic relevance. The database may return text fragments that contain similar words but do not answer the question, leading to generic or incorrect model responses.
02 // Background & Engineering Constraints
To improve accuracy, we must implement a multi-stage search pipeline. When a query is sent, the system should rewrite it for search optimization, query the database using a hybrid vector-keyword algorithm, rerank the retrieved chunks, and filter context before sending it to the LLM.
03 // Architecture Solution & Implementation
We construct an optimized RAG pipeline. We implement BM25 keyword matching alongside vector similarity search to capture both exact matches and semantic meaning. We pass the retrieved segments to a cross-encoder model to rerank them by relevance, ensuring only the best context is sent to the LLM.
We write database search functions in Python. We use pgvector for similarity queries and BM25 for keyword search. We merge the results using Reciprocal Rank Fusion (RRF), run a Cohere reranking model, and inject the top 3 segments into the LLM system prompt.
Data Flow Pipeline
- 1User Query → LLM Query Rewriter API
- 2Database Search → Vector similarity search + BM25 keyword match
- 3Merge Pool → Reciprocal Rank Fusion (RRF) calculation
- 4Reranker Engine → Cross-Encoder reranks chunks by relevance
- 5Context Filter → Top 3 chunks injected into LLM system prompt
Implementation Snippet (python)
def reciprocal_rank_fusion(vector_results, keyword_results, k=60):
scores = {}
# Run Reciprocal Rank Fusion on merged search pools
for rank, item in enumerate(vector_results):
scores[item.id] = scores.get(item.id, 0) + 1.0 / (rank + k)
for rank, item in enumerate(keyword_results):
scores[item.id] = scores.get(item.id, 0) + 1.0 / (rank + k)
return sorted(scores.items(), key=lambda x: x[1], reverse=True)Engineering Takeaways & Pitfalls
- •Implementing a reranking step increases final response accuracy by up to 40% while reducing model input tokens.
- •Always implement an embeddings cache. Storing vectors for common questions avoids redundant API calls and reduces latency.
- •Set up strict token limit parameters. Too much context degrades LLM output quality.
Frequently Asked Technical Questions
What is hybrid search in RAG?
Hybrid search combines dense vector retrieval (for semantic meaning) with sparse keyword matching (BM25, for exact terms like serial codes), improving overall search accuracy.
How do I handle document updates in RAG?
We use a hashing function on files. When a document is modified, we recalculate its hash, clear the old chunks from the database, and inject the new vectors.
Ashish Verma
Author & Systems LeadLead AI Systems Engineer & Co-Founder
Specializes in high-accuracy RAG architectures, hybrid search, and vector database optimization.
Authoritative Engineering References
Cohere Rerank Cross-Encoder Architecture
Cross-encoder models to rank semantic search retrieval chunks with high precision.
LlamaIndex Advanced Retrieval & Hybrid Search Strategies
Production architecture for hybrid vector + BM25 keyword retrieval.
PostgreSQL pgvector HNSW Benchmark Studies
Hierarchical Navigable Small World (HNSW) indexing for millisecond vector lookups.
Related Engineering Case Studies
AI Assistant for Business Documents
A possible solution retrieves relevant passages and presents answers with source references for review.
From Spreadsheets to Intelligence
A possible solution brings selected sources into a dashboard with clear definitions and data-quality checks.
Further Reading in this Engineering Domain
RAG vs Fine-Tuning: Which Should Your Business Use?
An architectural guide comparing RAG and fine-tuning: evaluate database lookups vs weight updates across data freshness, hallucination, and compute cost.
How AI Search Is Changing SEO
Analyze how AI-powered search engines (Perplexity, Gemini, SearchGPT) index websites, and learn how to optimize entity schemas and sitemaps.
Build This Architecture With SOCHYEAH
SEVA Public Information Directory
Discover verified Indian central and state government schemes for MSME tech upgradation, cold-storage, solar energy, and healthcare coverage.
Need Help Designing Production Systems?
Schedule a technical architecture call with our lead engineering team to review system requirements and deployment roadmaps.
CONTACT OUR LEAD ENGINEERS