How to Build a Production RAG Application: Ingestion, Chunking, Embeddings, and Reranking
A step-by-step engineering tutorial to architecting an enterprise RAG system with hybrid sparse-dense retrieval, semantic chunking, and sub-200ms latency.
Full-Stack Developers, AI Engineers, and Startup CTOs
1. The Anatomy of Production-Grade RAG
Building a proof-of-concept RAG application with 10 lines of LangChain code takes an afternoon; engineering a production RAG application capable of sub-100ms retrieval, multi-format document ingestion, and 0% critical hallucinations requires a disciplined five-stage pipeline.
Production RAG involves: (1) Ingestion & Semantic Chunking, (2) Vector Embedding & Metadata Indexing, (3) Hybrid Retrieval (Dense Vector + BM25 Lexical Keyword), (4) Cross-Encoder Reranking, and (5) Citation-Constrained Synthesis with Semantic Caching.
2. Stage 1: Document Chunking Strategies
Naive fixed-character chunking (e.g. splitting every 500 characters) tears sentences apart and destroys contextual meaning. Choose your chunking strategy based on document structure:
| Chunking Strategy | How It Works | Ideal Document Types | Trade-Off |
|---|---|---|---|
| Fixed-Size with Overlap | Splits by token count (e.g. 512 tokens with 50-token overlap) | Unstructured narrative text, books | Can split tables and definitions across boundaries |
| Semantic Chunking | Calculates embedding distance between adjacent sentences and splits on topic shifts | Blog posts, articles, customer feedback | Higher computation overhead during ingestion |
| Parent-Child Hierarchical | Retrieves small focused child chunks (128 tokens) but passes large parent chunk (1024 tokens) to context | Technical manuals, legal contracts | Requires two-tier indexing in vector database |
| Markdown / Heading-Aware | Splits along H1, H2, and H3 headers preserving document hierarchy | API documentation, technical handbooks | Requires clean markdown or structured HTML parsing |
3. Stage 2 & 3: Hybrid Search & Cross-Encoder Reranking
Pure vector search (dense embeddings) struggles with exact alphanumeric strings, such as product SKUs, error codes, and customer names. Production systems combine Dense Vector Search with Sparse BM25 Lexical Matching (Reciprocal Rank Fusion) and pass candidates through a Cross-Encoder Reranker (Cohere Rerank or BGE-Reranker-v2).
// lib/rag/hybrid-query.ts
import { Pinecone } from "@pinecone-database/pinecone";
import { CohereClient } from "cohere-ai";
const pinecone = new Pinecone({ apiKey: process.env.PINECONE_API_KEY! });
const cohere = new CohereClient({ token: process.env.COHERE_API_KEY! });
export async function executeHybridRagQuery({
query,
tenantId,
topK = 5,
}: {
query: string;
tenantId: string;
topK?: number;
}) {
const index = pinecone.index("enterprise-knowledge");
// 1. Generate Query Vector Embedding
const embedding = await generateEmbedding(query);
// 2. Perform Tenant-Isolated Vector Search with Metadata Filter
const vectorResults = await index.query({
vector: embedding,
topK: topK * 3, // Retrieve candidate pool
filter: { tenantId: { $eq: tenantId } },
includeMetadata: true,
});
const candidateDocuments = vectorResults.matches.map((m) => ({
id: m.id,
text: String(m.metadata?.text || ""),
source: String(m.metadata?.sourceUrl || ""),
}));
// 3. Cross-Encoder Reranking for Precision
const reranked = await cohere.rerank({
model: "rerank-v3.5",
query: query,
documents: candidateDocuments.map((d) => d.text),
topN: topK,
});
// 4. Return Top Grounded Chunks with Exact Provenance
return reranked.results.map((r) => ({
...candidateDocuments[r.index],
relevanceScore: r.relevanceScore,
}));
}4. Stage 4 & 5: Citation Constraints & Token Margin Protection
To eliminate hallucinations, inject strict system instructions: 'Answer the question strictly using the provided Context. For every claim made, cite the [Source ID]. If the Context does not provide the answer, respond with: I do not have enough information to answer based on the provided documents.'
Furthermore, implement a Redis semantic caching layer: when a new user query has >0.96 cosine similarity to a recently answered query, return the cached answer instantly at sub-10ms latency with 0 token spend.
// Core Architecture Takeaways
- •Production RAG requires moving beyond naive chunking to hierarchical or semantic chunking strategies.
- •Hybrid search (Dense Embeddings + BM25 Lexical Keyword) solves vector search blind spots on SKUs and codes.
- •Cross-encoder reranking (Cohere Rerank) filters candidate pools to ensure top retrieved chunks are truly relevant.
- •Redis semantic caching protects AI profit margins by serving repeat questions at zero token cost.
References, Research Citations & Data Sources
- [1]Pinecone Systems Engineering (2025): Production Vector Search Latency and Indexing Benchmarks
- [2]Cohere AI (2024): Cross-Encoder Reranking Architecture for High-Precision Retrieval