A retrieval-augmented generation (RAG) demo is easy to build. You split some documents into chunks, embed them, store the vectors, and pass the closest matches to a language model. On a handful of clean documents, the answers look impressive.

Production is a different problem. Real documents are messy, real questions are vague, permissions matter, and people act on the answers. This guide covers what has to change, in the order I would tackle it.

Decide whether you need RAG at all

RAG lets a model answer from retrieved text instead of relying only on what it learned in training. It is a good fit when people ask natural-language questions over a large body of changing text, and when answers need to point to a source.

It is the wrong tool in two common cases:

  • The material is small. If the content fits comfortably in a prompt, include all of it. There is nothing to retrieve.
  • The question has an exact answer in a database. “What is the status of order 4471?” needs a query, not semantic search. Semantic similarity is the wrong way to find a specific record.

Many useful assistants combine these approaches, using a structured query for facts and retrieval for explanations.

Treat ingestion as the foundation

Most teams spend their time on the model and the vector database. In my view, ingestion deserves far more attention, because retrieval cannot find what was destroyed during parsing.

The questions to answer early are:

  • Can you parse it properly? Tables, multi-column PDFs and scanned pages frequently come out scrambled. Check real output by eye before building anything on top of it.
  • Does each chunk make sense alone? A chunk reading “the limit is 30 days” is useless without knowing which policy it belongs to. Keep headings and document titles with each chunk.
  • What metadata do you need? Source, date, product, customer and access rules all have to be captured at ingestion, because you will filter on them later.
  • What happens when a document changes? A real pipeline has to handle updates and deletions, or the system will confidently quote a policy you withdrew last year.

There is no universally correct chunk size. Chunks should follow the structure of your documents, and the size should be tested using your evaluation set instead of copied from a tutorial.

Use hybrid retrieval

Vector search finds text with similar meaning, so it copes well when a user’s wording differs from the document’s. It is weaker on exact terms: product codes, error numbers, names and acronyms. Keyword search has the opposite profile.

Combining the two, and then reranking the merged results, is a sensible default for most systems. Anthropic’s write-up on contextual retrieval gives a useful reference point: in its tests, adding context to chunks and combining embeddings with BM25 keyword search reduced failed retrievals by 49%, and by 67% with reranking added. Your results will depend on your data, but the direction is consistent with why hybrid search is so widely used.

Start with the database you already run

If you already use PostgreSQL, pgvector is usually the simplest starting point. Your vectors sit beside the application data and permissions, and there is no new system to operate. A dedicated vector database becomes worthwhile when scale or specialised search features justify the extra operational work.

One behaviour catches people out. With approximate indexes, pgvector applies filters after the index scan. Its documentation gives the example of a condition that matches 10% of rows: with HNSW’s default settings, only about four rows will match on average. If you filter heavily, for example by customer, look at its iterative index scans and test the results.

Enforce access control during retrieval

For a multi-tenant product, this is the requirement that cannot fail. If another customer’s document reaches the prompt, it can reach the answer, and asking the model not to reveal it is not a security control.

-- The tenant filter comes from the authenticated session,
-- never from anything the user or the model supplied.
SELECT id, content
FROM document_chunks
WHERE tenant_id = $1
ORDER BY embedding <=> $2
LIMIT 20;

Filters belong in the retrieval query, and their values should come from your authentication layer. Test this directly: write automated tests that attempt to retrieve another tenant’s content, and run them on every change.

Treat retrieved text as untrusted too. Documents can contain instructions aimed at the model, which is why prompt injection sits first in the OWASP Top 10 for LLM applications. This matters even more when the model can also call tools.

Evaluate retrieval and answers separately

Without evaluation, every change is a matter of opinion. When an answer is bad, you need to know whether the system failed to find the right passage or found it and answered badly anyway. These have different fixes, so measure them separately.

Start with a modest test set of real questions, ideally taken from actual users or support tickets, along with the documents that should answer them. Fifty good examples will teach you more than a thousand synthetic ones. Then track:

  • Retrieval: Did the correct passage appear in the top results? Tools such as Ragas provide metrics like context precision and context recall.
  • Answer quality: Is the answer supported by the retrieved text, and does it address the question? Ragas calls these faithfulness and response relevancy.
  • Refusals: Does the system say it doesn’t know when the answer isn’t in your documents? Include unanswerable questions in your test set, because they are the ones most likely to produce invented answers.

Run this in CI, so that a change to a prompt, a chunking rule or a model produces a score you can compare. Automatic metrics are not perfect, so have someone who knows the subject review a sample as well.

Design the interface around being wrong

No RAG system is correct every time, so the interface should help people notice when it isn’t.

  • Show citations that link to the source passage, so answers can be checked.
  • Let the system say it doesn’t know. A confident wrong answer does far more damage than an honest gap.
  • Collect feedback and review it. Each bad answer is a candidate for your test set.

Operate it like any other production system

Once live, a RAG system needs the same care as the rest of your software, plus a few additions:

  • Trace each request through query, retrieved chunks, prompt and answer. You can’t debug a bad answer without seeing what the model was given.
  • Watch cost and latency per feature. Prompt size is the main driver, so limit how much context you send, and consider prompt caching for large repeated content.
  • Plan for re-embedding. Changing embedding model means re-processing your documents, so version your embeddings and be able to rebuild the index.
  • Stream responses so that slow generation feels acceptable to users.

A sensible order of work

  1. Collect real questions and build a small evaluation set.
  2. Get ingestion right on a representative set of documents.
  3. Build hybrid retrieval with access control from the start.
  4. Add generation with citations and a clear way to decline.
  5. Measure, fix the largest failure category, and repeat.
  6. Add tracing, cost tracking and an update pipeline before launch.

If you have a prototype that isn’t holding up on real data, or you are planning a system like this, see how I approach RAG and LLM development, or get in touch to talk it through.

Conclusion

The order of work is the advice: decide whether you need retrieval, then get retrieval right, then evaluate answers separately, then design for being wrong. Teams that skip to the model spend the rest of the project debugging retrieval they never measured.

Frequently asked questions

How do I know whether I need RAG at all?

If the answer has to come from documents that change and that the model was never trained on, you need retrieval. If the knowledge is small and stable, putting it in the prompt is simpler and easier to check.

Should I evaluate retrieval and answers separately?

Yes. A wrong answer can come from retrieving the wrong passage or from misreading the right one, and those are different fixes. Measuring them together tells you the system is wrong but not where.

What is the most common production failure?

Permissions. A demo retrieves from everything; production has to retrieve only what the person asking is allowed to see, and that filter has to live in the query rather than after it.

How do I keep RAG cost under control?

Cache embeddings, retrieve fewer chunks, and send less context per request. Retrieval quality usually improves when you send fewer, better chunks, so the cheaper configuration is often the better one.