
Divya Krishnamurthy
Mar 15, 2026 · 11 min read
It takes about 15 lines of Python code to set up a working vector search and LLM response pipeline. However, once you deploy that to thousands of users searching through real business PDFs, user satisfaction tanks. Queries get irrelevant context, hallucinations skyrocket, and LLM token costs spiralled out of control.
Click through the different components of a production-grade RAG architecture below to review the code implementation, primary failure modes, and best-practice solutions.
Instead of raw character splits which sever sentences, we parse documents into sections, sub-headings, and tables. Small sentences are indexed but mapped to their larger parent context.
Character-limit splits causing search tokens to fetch fragmented sentences.
Parent-Child relation index stores child vectors but returns parent nodes to LLM.
# Use LlamaIndex HierarchicalNodeParser
from llama_index.core.node_parser import HierarchicalNodeParser
node_parser = HierarchicalNodeParser.from_defaults(
chunk_sizes=[2048, 512, 128]
)
nodes = node_parser.get_nodes_from_documents(documents)
Standard chunking cuts text at arbitrary character limits (e.g. 500 characters). This splits crucial sentences or structures in half. We moved to a Hierarchical Node parser: we split documents into larger sections, parse sub-points, and link them. The vector database searches the small chunks but feeds the larger parent context to the LLM.
To filter out noise, we combine vector search (semantic) with BM25 (keyword search), and pass the top results through Cohere's re-ranking engine. This guarantees only high-relevance paragraphs are passed to GPT-4:
from cohere import Client
import numpy as np
co = Client("your-api-key")
def search_and_rerank(query, vector_db, top_k=20, final_top_n=3):
# 1. Fetch semantic and keyword candidates
semantic_results = vector_db.vector_query(query, limit=top_k)
keyword_results = vector_db.keyword_query(query, limit=top_k)
# Merge unique documents
candidates = list({doc.id: doc for doc in (semantic_results + keyword_results)}.values())
texts = [doc.text for doc in candidates]
# 2. Re-rank utilizing Cohere Rerank
rerank_response = co.rerank(
model="rerank-english-v3.0",
query=query,
documents=texts,
top_n=final_top_n
)
# 3. Compile high-relevance context
final_docs = []
for hit in rerank_response.results:
final_docs.append(candidates[hit.index])
return final_docsWe deployed NeMo Guardrails to sanitize user input. If a user asks for recipes inside a corporate database portal, the guardrail rejects the request immediately. This alone decreased unnecessary LLM costs by 28% and eliminated brand safety risks.
Divya Krishnamurthy
Mar 15, 2026
11 min read
Mobile App Development