How to Build a Custom RAG Pipeline for Document Intelligence
Retrieval-Augmented Generation (RAG) has become the default architecture for letting AI systems answer questions from your company's private documents. The concept is simple: instead of hoping a language model memorized your data, you retrieve the relevant passages at query time and hand them to the model as context. But here's what nobody tells you until you've built one: a naive RAG pipeline — chunk the PDFs, embed the chunks, do a vector search — works beautifully in demos and fails embarrassingly in production. This guide walks through how to build a RAG pipeline that actually holds up against real enterprise documents: multi-page tables, scanned contracts, financial statements, and 200-page technical manuals.
Why Naive RAG Fails on Real Documents
The standard tutorial approach splits documents into fixed 500-token chunks and embeds each one. Three problems immediately surface with real corporate data:
- Tables get shredded. A financial table split mid-row loses its column headers. When someone asks "what was Q3 revenue for the northeast region?", the retrieved chunk contains numbers with no labels, and the model confidently hallucinates an answer.
- Context boundaries destroy meaning. A clause in a contract that says "notwithstanding the above" is worthless without the paragraph it modifies. Fixed-size chunking regularly severs these dependencies.
- Scanned documents are invisible. A shocking percentage of enterprise knowledge lives in scanned PDFs with no text layer. Without an OCR and layout-analysis stage, your pipeline simply cannot see them.
The Architecture That Works: Five Stages
Stage 1: Layout-Aware Ingestion
Before you chunk anything, you need to understand document structure. We run every document through a layout analysis model that classifies regions: headers, body text, tables, figures, footnotes, and captions. For scanned documents, this stage combines OCR with visual layout detection. Multi-modal vision models have made this dramatically better — they can parse a complex table as a table, preserving row-column relationships, rather than as a soup of numbers.
Stage 2: Semantic Chunking with Structural Metadata
Instead of fixed-size splits, chunk along semantic boundaries: sections, clauses, table units. Critically, every chunk carries metadata — document title, section hierarchy, page number, and document date. A chunk from "Master Services Agreement > Section 8: Liability > 8.2 Indemnification" retrieves very differently than an anonymous text fragment. Tables get special treatment: we serialize them with headers repeated per logical unit, and store a natural-language summary of what the table shows alongside the raw data.
Stage 3: Hybrid Retrieval
Pure vector search misses exact identifiers — invoice numbers, part codes, legal citations — because embeddings blur precise strings into fuzzy semantics. Production pipelines combine three signals:
- Dense vector search for conceptual similarity ("termination conditions" matching "grounds for ending the agreement").
- Keyword search (BM25) for exact matches on codes, names, and numbers.
- Metadata filtering to scope by document type, date range, or department before ranking even begins.
Results from all three are merged with reciprocal rank fusion, which is simple to implement and consistently outperforms any single retriever.
Stage 4: Reranking
First-pass retrieval optimizes for recall — casting a wide net of 30-50 candidate chunks. A cross-encoder reranker then scores each candidate against the actual query and keeps the top 5-8. This two-stage approach routinely improves answer accuracy by 15-25 percentage points over embedding similarity alone, and it's the single highest-leverage upgrade if your pipeline already exists.
Stage 5: Grounded Generation with Citations
The final stage prompts the model to answer strictly from the retrieved context and cite which chunk supports each claim. Citations aren't cosmetic — they're your hallucination alarm. If the model can't point to a source passage, the answer gets flagged rather than delivered. For regulated industries (finance, healthcare, legal), this audit trail is what makes the system deployable at all.
Evaluation: The Step Everyone Skips
You cannot improve what you don't measure. Before going live, build a golden dataset of 100-200 real question-answer pairs sourced from the people who actually use these documents. Then track three metrics on every pipeline change:
- Retrieval hit rate: Is the passage containing the answer in the retrieved set? If not, no amount of prompt engineering will save you.
- Answer faithfulness: Does the generated answer actually follow from the retrieved context, or did the model improvise?
- Answer correctness: Does it match the golden answer as judged by a human or a strong evaluation model?
In our client work, teams that skip evaluation ship pipelines that feel fine and quietly erode user trust with a 10-15% wrong-answer rate. Teams that measure get that under 2%.
Practical Costs and Timelines
A production-grade document intelligence system for a corpus of 10,000-100,000 documents typically takes 6-12 weeks to build and tune, with the bulk of effort going into ingestion quality and evaluation — not the flashy generation layer. Ongoing costs are dominated by embedding refreshes and inference, usually a few hundred to a few thousand dollars monthly depending on query volume. Compare that against the analyst hours currently spent manually digging through documents: our insurance client cut claims document review from 9 days to under 3 hours — the full breakdown is in our case studies.
Production Concerns That Bite Later
A few engineering realities that rarely make it into tutorials but determine whether your pipeline survives contact with an enterprise environment:
- Access control at retrieval time. Not everyone should be able to query everything. If HR documents and board minutes live in the same index as the employee handbook, your retrieval layer must enforce per-user permissions before ranking — filtering after generation is too late, because the model has already seen the restricted content. Carry the source system's ACLs into chunk metadata and filter on them as a hard precondition.
- Freshness and re-indexing. Documents change. A pipeline that answers from last quarter's price list is worse than no pipeline, because it's confidently wrong. Build incremental re-indexing triggered by document updates rather than monthly full rebuilds, and stamp every answer with the source document's last-modified date so users can judge staleness themselves.
- Conflicting sources. Real corpora contain contradictions — a 2024 policy superseded by a 2026 revision, or two departments with different versions of the same procedure. Naive retrieval happily blends both into one confident, wrong answer. Practical mitigations: prefer newer documents in ranking, mark superseded documents explicitly during ingestion, and prompt the model to surface disagreements between sources rather than resolve them silently.
- Cost control under scale. Reranking 50 candidates per query with a cross-encoder is cheap at 100 queries a day and noticeable at 50,000. Cache frequent queries, batch embedding refreshes off-peak, and use smaller rerankers where your evaluation data shows the accuracy delta is negligible.
None of these are exotic problems, but every one of them is much cheaper to design for upfront than to retrofit after users have lost trust in wrong answers.
When to Build vs. When to Buy
Off-the-shelf "chat with your docs" tools are fine for casual internal search over clean text documents. You need a custom pipeline when any of these apply: your documents are heavily tabular or scanned, wrong answers carry regulatory or financial risk, you need row-level access control over who can retrieve what, or the system must integrate into existing workflows rather than living in a separate chat window. That's precisely the gap our AI data intelligence and custom AI development teams specialize in.
If you're evaluating a document intelligence project and want a second opinion on your architecture — or a working proof-of-concept against your actual documents — get in touch. We're happy to pressure-test your approach before you commit engineering months to it.