AI & Automation
Building Reliable RAG Applications: Retrieval, Evaluation and Oversight
Retrieval-augmented generation is easy to demo and harder to run in production. This guide covers retrieval quality, evaluation, guardrails and the human oversight that make RAG systems dependable.
Kodvexa Engineering · · 8 min read
Retrieval-augmented generation (RAG) pairs a language model with a search step over your own content. Instead of relying on what the model learned during training, the application retrieves relevant passages from documents, tickets or knowledge bases and asks the model to answer using them. It is one of the most practical ways to apply generative AI to business data, and a working prototype can be built in an afternoon.
The gap between that prototype and a system people can trust is where most of the effort lies. Answers that are confidently wrong, citations that do not support the claim, and quality that quietly degrades as content changes are all common. This article walks through the areas that make the biggest difference to reliability.
Most RAG failures are retrieval failures
When a RAG system gives a poor answer, the instinct is to blame the model or rewrite the prompt. In practice, the cause is often that the right information was never retrieved, or was retrieved alongside so much irrelevant text that the model could not use it well. A model cannot ground its answer in a passage it never saw. Improving retrieval is usually the highest-value work.
Prepare content deliberately
How documents are split into chunks shapes everything downstream. Chunks that are too small lose context; chunks that are too large dilute relevance and consume the model's context window. Splitting along the natural structure of the content, such as headings, sections or individual policy clauses, tends to work better than fixed character counts.
- Keep useful metadata with each chunk: source document, section title, date, owner and access permissions
- Prepend the document and section title to each chunk so that it makes sense in isolation
- Remove boilerplate such as repeated headers, navigation text and legal footers before indexing
- Handle tables and scanned documents explicitly, as naive text extraction often scrambles them
- Re-index on a schedule or on change events so that answers reflect current content
Combine search techniques
Vector search is good at matching meaning, but it can miss exact terms such as product codes, error numbers or names. Keyword search is the reverse. Hybrid retrieval, which runs both and merges the results, is a sensible default for business content. Adding a re-ranking step, where a dedicated model scores the top candidates against the question, often improves precision further at modest cost.
Metadata filters matter too. If a user asks about the current leave policy, filtering to current documents before ranking is more reliable than hoping the newest version scores highest.
Enforce access control at retrieval time
A RAG system can expose information that a user would not be allowed to open directly. The safest pattern is to store permissions alongside each chunk and filter by the current user's entitlements as part of the retrieval query, before anything reaches the model. Relying on the model to withhold restricted content is not a security control.
Build an evaluation set before you tune
Without measurement, every prompt change or chunking tweak is a guess, and improvements in one area can hide regressions in another. An evaluation set is a collection of realistic questions, each paired with the documents that should be retrieved and a reference answer or the key facts a good answer must contain.
Start small. Fifty to a hundred questions written with subject matter experts, drawn from real user queries where possible, are enough to reveal patterns. Include questions that should be refused because the content does not contain an answer, as these test whether the system admits uncertainty.
Measure retrieval and generation separately so that you know where problems originate:
- Retrieval recall: did the expected source appear in the top results?
- Retrieval precision: how much of what was retrieved was actually relevant?
- Faithfulness: is every claim in the answer supported by the retrieved text?
- Answer relevance: does the response address the question that was asked?
- Appropriate refusal: does the system decline when the answer is not in the sources?
Retrieval metrics can be computed directly. Faithfulness and relevance are harder; many teams use a second model as a grader, but its judgements should be checked against human ratings on a sample before you rely on them. A simple retrieval check is a good first step:
def recall_at_k(eval_set, retrieve, k=5):
hits = 0
for case in eval_set:
results = retrieve(case["question"], top_k=k)
retrieved_ids = {r.doc_id for r in results}
if retrieved_ids & set(case["expected_doc_ids"]):
hits += 1
return hits / len(eval_set)
baseline = recall_at_k(eval_set, retrieve_vector_only)
candidate = recall_at_k(eval_set, retrieve_hybrid_reranked)
print(f"recall@5 baseline={baseline:.2f} candidate={candidate:.2f}")Run the evaluation in your delivery pipeline whenever prompts, models, chunking or retrieval settings change. Treat a drop in scores like a failing test that needs investigation before release.
Add guardrails around the model
Guardrails are checks that sit before and after the model call. They do not make a system perfect, but they reduce the chance of obvious failures reaching users.
- Instruct the model to answer only from the supplied context and to say so when the context is insufficient
- Require citations to specific chunks and verify that cited chunks were actually retrieved
- Set a minimum retrieval relevance score below which the system declines rather than guesses
- Screen inputs and outputs for personal data, offensive content or topics outside the intended scope
- Limit answer length and format so that responses stay focused and easy to check
Design for human oversight
The right level of oversight depends on the consequences of a wrong answer. An internal search assistant for engineering documentation can tolerate occasional misses. A system that drafts responses to customers, summarises clinical notes or interprets contracts should keep a qualified person in the loop who reviews output before it is acted on.
Make review easy. Show the sources next to each answer so that users can verify claims quickly, and provide a simple way to flag answers as wrong or unhelpful. Those flags are valuable: route them to a review queue, and add confirmed failures to the evaluation set so the same problem is caught automatically in future.
Operate it like any other production system
RAG applications need the same operational discipline as other software, plus a few additions. Log each request with the retrieved chunk identifiers, model version, prompt version, latency and token usage, taking care to handle personal data in logs appropriately. This makes it possible to reproduce a bad answer and understand why it happened.
- Version prompts and retrieval configuration alongside application code
- Pin model versions and re-run evaluations before upgrading
- Monitor cost per request and set budgets, as context size drives spend
- Watch for content drift, such as new document types that the chunking strategy handles poorly
A practical path to production
Start with a narrow, well-defined use case and a content set you understand. Build the evaluation set early, fix retrieval before tuning prompts, enforce permissions in the retrieval layer and keep people involved where errors matter. Expand scope only when the measurements show the system is ready. This approach is slower than shipping the first demo, but it produces something people will continue to use once the novelty has worn off.