Skip to main content
AI & LLM Engineering 12 min read•Published 2026-04-02

Building Production RAG Systems: Chunking, Hybrid Search, and Hallucination Guardrails

Moving a RAG prototype from a Jupyter notebook to production requires solving semantic drift, retrieval latency, and hallucinated citations. Here is our battle-tested architecture using PostgreSQL pgvector and Next.js.

Raja Kaleemullah
Raja KaleemullahHead of Product & Engineering • Codiest Studio
Production RAG pipeline architecture with PostgreSQL pgvector and Next.js
Direct Architectural Summary

A production Retrieval-Augmented Generation (RAG) architecture combines structure-aware document chunking, hybrid lexical-plus-vector retrieval (BM25 + HNSW pgvector indexes), cross-encoder reranking, and deterministic citation validation so LLMs generate grounded, low-latency responses without hallucinating facts.

Building a basic Retrieval-Augmented Generation (RAG) demo takes less than an afternoon: split a PDF into 500-character slices, embed them into a vector database, and pass the top three cosine-similarity matches to an LLM. Yet when engineering teams deploy that exact pipeline to production users, accuracy collapses. Users ask for specific SKU codes, regulatory clauses, or multi-hop comparisons, and the system either retrieves irrelevant passages or confidently fabricates answers.

1. Why Naive Vector RAG Fails in Enterprise Production

Dense vector embeddings excel at capturing broad conceptual similarity, but they routinely fail at exact lexical matching. When a user queries an enterprise assistant for an exact error code (`ERR_SSL_VERSION_OR_CIPHER_MISMATCH`), a model number, or a person's surname, dense embeddings compress that token into a 1,536-dimensional vector where rare identifiers get diluted.

Furthermore, fixed-character chunking slices tables, code blocks, and legal clauses right down the middle—separating a pronoun or table row from the header that defines its context.

Architecture RuleNever rely on pure cosine similarity for production search. Always pair dense vector embeddings with lexical full-text search (tsvector/BM25) so exact part numbers, names, and technical terms are never missed.

2. Structure-Aware Chunking & Contextual Headers

Before generating a single vector, documents should be parsed into an Abstract Syntax Tree (AST) or structured Markdown hierarchy. Instead of splitting arbitrarily every 512 tokens, split along semantic boundaries: `H2`/`H3` sections, complete Markdown tables, and cohesive paragraphs.

To solve context loss across chunks, prepend every chunk with its ancestral document breadcrumb (e.g., `Document: SOC2 Security Policy > Section: Access Control > Subsection: MFA Requirements`) before computing the embedding. This single technique improved top-5 retrieval recall by 28% during our work on AudioFlow AI.

3. Hybrid Retrieval: Combining pgvector HNSW with BM25

You do not need a separate proprietary vector database that introduces synchronization lag with your primary transactional store. PostgreSQL with `pgvector` (using HNSW indexes) alongside native `tsvector` GIN indexes lets you enforce tenant isolation (`WHERE tenant_id = $2`), RBAC permissions, and hybrid retrieval inside a single ACID database.

Using Reciprocal Rank Fusion (RRF), you execute dense vector similarity and lexical full-text search in parallel CTEs and normalize their rank positions into a unified score.

Retrieval StrategyExact Keyword RecallSemantic Conceptual RecallP95 Query Latency (1M Chunks)Operational Complexity
Lexical Only (PostgreSQL tsvector)94%51%14 msLow (Single DB)
Dense Vector Only (pgvector HNSW)62%88%22 msLow (Single DB)
Hybrid RRF (pgvector + tsvector)95%91%29 msLow (Single SQL Query)
Hybrid RRF + Cross-Encoder Rerank98%96%115 msMedium (Reranker Model)

4. Cross-Encoder Reranking & Hallucination Guardrails

Once Hybrid RRF returns the top 25 candidate chunks, pass those candidates through a lightweight Cross-Encoder reranker to select the top 6 passages with highest relevance to the user query.

To enforce zero-hallucination output, constrain the LLM using structured JSON schema outputs requiring an array of `citations` referencing exact `chunk_id` values. If the retrieval score falls below your calibrated confidence threshold, short-circuit the LLM call and return a deterministic fallback explaining that the knowledge base does not contain verified documentation for that query.

5. Continuous RAG Evaluation: Context Precision & Faithfulness

Treat your RAG pipeline like deterministic software with automated CI/CD evaluations. Maintain a golden dataset of 100+ real user queries paired with expected ground-truth source chunks, and measure three metrics on every prompt or chunking change: Context Recall, Faithfulness (whether every claim in the answer is entailed by the retrieved chunks), and Answer Relevance.

Hybrid Reciprocal Rank Fusion (RRF) Query in PostgreSQL + pgvector
sql
WITH semantic_search AS (
  SELECT id, content, source_uri,
         ROW_NUMBER() OVER (ORDER BY embedding <=> $1::vector) AS rank_ix
  FROM knowledge_chunks
  WHERE tenant_id = $2
  ORDER BY embedding <=> $1::vector
  LIMIT 30
),
keyword_search AS (
  SELECT id, content, source_uri,
         ROW_NUMBER() OVER (ORDER BY ts_rank_cd(tsv, plainto_tsquery('english', $3)) DESC) AS rank_ix
  FROM knowledge_chunks
  WHERE tenant_id = $2 AND tsv @@ plainto_tsquery('english', $3)
  ORDER BY ts_rank_cd(tsv, plainto_tsquery('english', $3)) DESC
  LIMIT 30
)
SELECT COALESCE(s.id, k.id) AS chunk_id,
       COALESCE(s.content, k.content) AS content,
       COALESCE(s.source_uri, k.source_uri) AS source_uri,
       COALESCE(1.0 / (60 + s.rank_ix), 0.0) + COALESCE(1.0 / (60 + k.rank_ix), 0.0) AS rrf_score
FROM semantic_search s
FULL OUTER JOIN keyword_search k ON s.id = k.id
ORDER BY rrf_score DESC
LIMIT 8;

Conclusion

Production RAG is 20% model selection and 80% data engineering and information retrieval architecture. By combining structure-aware chunking, PostgreSQL hybrid RRF search, and strict citation verification, you can ship AI systems that enterprise customers actually trust.

Next Step

Building an AI-powered SaaS or enterprise RAG system?

Partner with Codiest's AI product and full-stack engineering team to architect low-latency, hallucination-free AI workflows.

More Strategic Guides from Codiest Insights