advanced26 min

Reducing Hallucination and Evaluating RAG Quality

Build honesty checks into a RAG system and measure whether it's actually working.

What you'll learn

  • Implement a minimum-relevance threshold that gates generation
  • Explain what a 'not enough evidence' response is and why it matters
  • Describe basic RAG evaluation metrics (retrieval precision/recall, groundedness)

Prerequisites

Explanation

A hallucination is when a model states something false or unsupported with the same confident tone as something true — the response sounds authoritative regardless of whether it's accurate. RAG substantially reduces this for questions the retrieved content actually covers, but it introduces a new failure mode you must explicitly defend against: what happens when retrieval finds nothing genuinely relevant, but the model tries to answer anyway using unrelated training knowledge?

The fix is a minimum relevance threshold: before generating an answer, check whether the best retrieved chunk's similarity score clears a minimum bar. If it doesn't, skip generation and return an honest "the available content doesn't cover this" response instead of guessing. This single guardrail is one of the highest-leverage things you can build into a RAG system — it converts silent failure (a confident wrong answer) into a visible, honest one.

Choosing the threshold value is itself a tuning problem: too high, and the system refuses to answer questions it could have handled; too low, and it generates from weak, barely-related context. Real systems tune this against a labeled test set of representative queries.

Evaluating a RAG system means measuring more than "does it produce plausible-looking text." Useful metrics include:

  • Retrieval precision — of the chunks retrieved, what fraction were actually relevant?
  • Retrieval recall — of all the relevant chunks that exist, what fraction did retrieval actually find?
  • Groundedness — does the generated answer's content actually trace back to the retrieved chunks, or does it introduce unsupported claims?
  • Answer relevance — does the answer actually address the question that was asked?

A practical, deterministic way to start evaluating groundedness without another AI call is to check whether key claims/numbers in the generated answer also appear in the retrieved context — a rough but automatable and testable signal, and exactly what this lesson's exercises implement.

Example

A minimum-relevance gate that decides whether to generate or return an honest fallback.

const RELEVANCE_THRESHOLD = 0.6;

function answerOrDecline(question, bestMatchScore, bestMatchText) {
  if (bestMatchScore < RELEVANCE_THRESHOLD) {
    return "I don't have enough information in the provided content to answer that confidently.";
  }
  return `Based on the available content: ${bestMatchText}`;
}

console.log(answerOrDecline("What's the refund policy?", 0.82, "Refunds within 30 days."));
console.log(answerOrDecline("What's the CEO's home address?", 0.12, "Refunds within 30 days."));

Try it yourself

Lower the threshold and see when the fallback stops triggering.

Code editor. Press Escape then Tab to leave the editor if keyboard focus becomes trapped. Press Control+Shift+M inside the editor to toggle Tab-key focus trapping.

Loading editor…

Guided exercise

Guided exercise

Write a function `hasEnoughEvidence(topScore, threshold)` returning true if topScore is greater than or equal to threshold, false otherwise.

Checks: Above threshold returns true · Below threshold returns false · plus 1 hidden check

Code editor. Press Escape then Tab to leave the editor if keyboard focus becomes trapped. Press Control+Shift+M inside the editor to toggle Tab-key focus trapping.

Loading editor…

Stuck? Get a hint.

Independent exercise

Independent exercise

Write `generateGroundedAnswer(question, bestChunk, threshold)` where bestChunk is `{ text, score }` or null. Return the literal string 'Not enough evidence to answer this question.' if bestChunk is null OR bestChunk.score is below threshold; otherwise return bestChunk.text.

Checks: Handles a null bestChunk · Handles a below-threshold score · plus 1 hidden check

Code editor. Press Escape then Tab to leave the editor if keyboard focus becomes trapped. Press Control+Shift+M inside the editor to toggle Tab-key focus trapping.

Loading editor…

Stuck? Get a hint.

Common mistakes

  • Generating an answer regardless of retrieval confidence, letting the model quietly fall back on ungrounded training knowledge.
  • Picking a relevance threshold once and never revisiting it as content or query patterns change.
  • Evaluating only 'does this look like a good answer' instead of measurable retrieval and groundedness metrics.

Knowledge check

Knowledge check

1. What should a RAG system do when no retrieved chunk clears the relevance threshold?
2. What does 'groundedness' measure in RAG evaluation?
3. What is retrieval recall?

Takeaway

A relevance threshold that gates generation is the single highest-leverage defense against confident, ungrounded answers.

Summary

A minimum relevance threshold decides whether there's enough retrieved evidence to answer at all, converting silent hallucination into an honest 'not enough evidence' response. RAG evaluation should measure retrieval precision/recall and groundedness, not just how plausible the output reads.

References

Your notes

Notes save automatically.

Finished this lesson?

Mark it complete to track your progress and schedule a future review.