All posts

AI engineering · · 4 min read

How to Build a RAG System That Works in Production

A practical guide to retrieval-augmented generation: document ingestion, chunking, hybrid search, reranking, evaluation, and the failure modes that demos never show.

A retrieval-augmented generation (RAG) demo takes an afternoon: split some PDFs, embed them, put them in a vector database, and ask questions. A RAG system that a support team or sales team relies on every day takes a lot more care. The gap between the two is almost entirely in retrieval quality and evaluation, not in which LLM you pick.

This guide covers the decisions that matter most when you take RAG to production.

1. Start with the questions, not the documents

Before touching any data, collect 50 to 100 real questions that users will ask, along with where the correct answer lives. Get them from support tickets, Slack threads, or by sitting with the team for an hour.

This list becomes your evaluation set. Without it, every change you make is a guess. With it, you can measure whether a new chunking strategy actually helps.

2. Ingestion is where most quality is lost

Garbage in, garbage out applies more to RAG than almost anything else. Common problems:

  • PDF extraction mangles tables and multi-column layouts. Check what your parser actually outputs before you embed anything.
  • Boilerplate drowns the signal. Headers, footers, navigation menus, and legal disclaimers repeated on every page end up dominating search results. Strip them.
  • Stale and duplicate documents. If three versions of the same policy exist, the model will happily quote the old one. Track a source ID and a version for every document, and replace content on update instead of appending to it.

3. Chunking: structure beats fixed sizes

Splitting text every 500 tokens is the default in most tutorials, and it is rarely the best choice. Better options:

  • Split on document structure. Use headings, sections, and list boundaries so each chunk is about one thing.
  • Keep context with the chunk. Prepend the document title and section path, for example "Refund Policy > Enterprise customers > Annual plans". This one change often improves retrieval more than switching embedding models.
  • Use modest overlap, around 10 to 15 percent, so sentences that cross a boundary are not lost.
Content typeGood chunking approach
Help center articlesOne chunk per section under each heading
Contracts and policiesOne chunk per clause, with the clause number kept
Meeting transcriptsTime windows aligned to speaker turns
Product catalogsOne chunk per product, with structured fields as text

4. Use hybrid retrieval

Dense vector search is good at meaning and bad at exact terms. It will struggle with an error code like E-4012, a SKU, or a person's name. Keyword search (BM25) is the opposite.

In production we almost always run both and merge the results, typically with reciprocal rank fusion:

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

candidates = reciprocal_rank_fusion([
    vector_search(query, top_k=30),
    keyword_search(query, top_k=30),
])

Most vector databases, including Qdrant, Pinecone, and PostgreSQL with pgvector, now support hybrid search in some form.

5. Rerank before you generate

Retrieval should be generous, around 20 to 50 candidates. Generation should be selective, around 3 to 8 chunks. A cross-encoder reranker in between scores each candidate against the actual question and keeps only the best. It is one of the cheapest, most reliable quality improvements available.

6. Make the model cite, and make it say "I don't know"

The generation prompt should:

  • Answer only from the provided context.
  • Cite the source of each claim, so the UI can link to the original document.
  • Say clearly when the context does not contain the answer, instead of guessing.

Then check it. A simple automated test asks questions that are deliberately not in the knowledge base and verifies the system declines to answer.

7. Evaluate retrieval and answers separately

When an answer is wrong, you need to know whether retrieval failed (the right chunk was never found) or generation failed (the right chunk was found, but the model ignored or misread it). Track both:

  • Retrieval: for each evaluation question, is the correct source in the top-k results? (recall@k)
  • Answers: is the answer correct, grounded in the retrieved context, and properly cited? An LLM-as-judge with a clear rubric works well here, spot-checked by a person.

Run the evaluation set on every change to chunking, prompts, or models, the same way you would run unit tests.

8. Security and access control

If different users are allowed to see different documents, enforce that at retrieval time, by filtering on metadata such as team, customer, or role, not by asking the model to hold back. Anything that reaches the prompt can end up in the answer.

Also check your model provider's data retention terms. Most enterprise API tiers offer zero-retention options, so your documents are not stored or used for training.

9. Monitor it like any other production system

Log every query with the retrieved chunks, the answer, latency, and cost. Add a thumbs up or down in the UI. Review the low-rated answers weekly. In our experience, most problems come from a handful of missing or badly formatted documents, and they are easy to fix once you can see them.

A realistic timeline

For a focused use case, such as an internal knowledge assistant over a few thousand documents, a production-ready first version usually takes two to four weeks. That is about one week for ingestion and an evaluation set, one to two weeks for retrieval tuning and the application, and the rest for access control, monitoring, and rollout.

If you are planning a RAG project, our LangChain and LangGraph development team can help you design it, or book a free call to talk it through.

Topics: RAG, retrieval-augmented generation, vector databases, LLM evaluation, hybrid search

Want help applying this to your business?

Book a free 45-minute call and we'll talk through your use case and what it would take to build.

Free 45-minute call. No commitment.