All posts
3 min read

Production RAG architecture: hybrid retrieval, reranking and refusal

Why naive RAG fails in production and the architecture that fixes it: layout-aware ingestion, hybrid retrieval, reranking, citation grounding and an explicit refusal path.

RAGRetrievalArchitectureLLM

The canonical RAG demo is: embed documents, embed the question, take the top-k chunks, paste them into a prompt. It works for a blog search. It falls apart on real documents — scanned PDFs, exact invoice numbers, tables, and questions the corpus does not answer.

Here is the architecture that survives production.

TL;DR

  • Ingest with layout awareness. Tables, headers and scanned text are where naive parsers die.
  • Retrieve hybrid. BM25 + vectors, then fuse. Exact terms and paraphrases both matter.
  • Rerank the shortlist with a cross-encoder before generation.
  • Ground every sentence in citations and refuse when support is weak.
  • Budget latency and cost at every stage; retrieval is cheap, generation is not.

Where naive RAG breaks

  1. Exact identifiers vanish. Embeddings blur INV-2024-00871 into "an invoice-ish thing". Lexical search does not.
  2. Top-k returns near-duplicates. Five chunks of the same paragraph crowd out the one that matters.
  3. Tables and layouts get shredded. Fixed-size chunking on a financial table produces garbage.
  4. The model always answers. No context? It invents one. That is the failure users remember.

The pipeline

1. Layout-aware ingestion

Parse structure first: headings, tables, reading order, page numbers. OCR only pages that need it. Keep a pointer back to the source page so you can cite later.

2. Chunking by meaning

Chunk on section boundaries, not a fixed token count. Attach metadata (document, page, section) to every chunk. Store the original text alongside any embedding.

3. Hybrid retrieval

Run lexical (BM25) and vector search in parallel, then fuse with Reciprocal Rank Fusion:

def rrf(rank_lists, k=60):
    scores = {}
    for ranks in rank_lists:
        for rank, doc_id in enumerate(ranks):
            scores[doc_id] = scores.get(doc_id, 0) + 1 / (k + rank + 1)
    return sorted(scores, key=scores.get, reverse=True)

Lexical catches the exact number; vectors catch the paraphrase. Fusion keeps both.

4. Rerank

Take the top ~30 candidates and rerank with a cross-encoder to the top ~5. This is the single highest-leverage quality step per dollar.

5. Ground and cite

Constrain the model to answer only from provided passages, with inline citation markers. Post-check that each cited passage actually supports its claim.

6. Refuse explicitly

If retrieval confidence is low or grounding fails, return a clear "I don't have enough to answer that" plus the closest sources. Refusal precision is a metric, not an afterthought.

Budget from day one

StageTypical cost driverLever
IngestionOCROCR only low-text pages; cache
Retrievalvector DBpre-filter by metadata; batch
Rerankcross-encoder callsrerank fewer candidates
Generationtokenscap context, cache answers

On this portfolio's RAG project, the target envelope is p95 ≈ 1.4s end-to-end and ≈ $3.80 per 1k queries — with groundedness and refusal precision shown next to those numbers on the Proof section. Quality, latency and cost on one dashboard is the whole point.

What good looks like

A production RAG system is boring in the best way: it answers what it can support, cites it, and cleanly refuses the rest. It has a measured recall@k, a groundedness score, and a cost budget — and it gets re-evaluated on every change.

Want the deeper version, including the eval harness? Read how I evaluate a RAG system.

Frequently asked

Why does naive RAG fail in production?

Naive RAG (embed, top-k, stuff into prompt) fails because embeddings miss exact identifiers and numbers, top-k returns near-duplicates, and the model answers even when context is missing. Production RAG adds hybrid retrieval, reranking, citation grounding and an explicit refusal path.

What is hybrid retrieval and why does it help?

Hybrid retrieval combines lexical search (BM25) with vector similarity. Lexical search nails exact terms, IDs and numbers that embeddings blur; vectors catch paraphrases. Merging both with reciprocal rank fusion is more robust than either alone.

When should a RAG system refuse to answer?

When retrieval confidence is low or the generated answer is not supported by the retrieved passages. Refusing — and saying so — is better than a confident hallucination, especially in finance, legal and healthcare.

Keep reading

Hiring an AI Engineer who ships like this?