Skip to content
AgenticAIWiki
  1. Learn
  2. Part 1
  3. 7. Hallucination and grounding
  4. 7.4

Lesson 7.4 · 4 of 4 · 4 min read · Reviewed 24 Nov 2026

What an eval is and why you need one

By the end, you can

  • Explain what an eval is.
  • Use evals to measure hallucination and grounding.

This is a short preview. Part 3 covers evalsEval: A repeatable test that measures how well an AI system performs on a set of cases with defined success criteria. in depth.

The parts of an eval

test cases  ->  your system  ->  grader  ->  scores
(inputs and      (prompt,         (code,      (per criterion,
 expectations)    model, tools)    model,      compared with
                                   or human)   a target)

A grounding eval

Case Expectation Grader
“Refund window for shoes?” Says 30 days; cites pol-4.2 Code checks the citation; model grader checks support
“What’s the CEO’s phone number?” Says it’s not available Model grader: refusal is correct
“Return worn sale shoes?” Combines two rules correctly Model grader with reference answer

Scores to track: share of supported claims, citation accuracy, correct refusals.

When to run it

  • Before shipping a prompt or model change
  • After changing retrieval (chunking, index, rerankerReranking: Re-scoring an initial set of retrieved results with a more accurate model to put the best ones first.)
  • On a schedule, to catch drift (chapter 67)

Start small

Ten good cases are enough to start (chapter 16). Add every hallucinationHallucination: Output that sounds confident but is false or unsupported by the provided sources. you find in production as a new case.

Key points

  • An eval is a repeatable test: inputs, expected behaviour, and a way to score outputs.
  • For grounding, evals check that claims are supported, citations are valid and unanswerable questions get honest replies.
  • Run evals whenever you change prompts, models or retrieval, so you know if things got better or worse.
Hands-on labRuns in the Playground

Build a small grounding eval

Score two prompt versions on the same fixed cases so you can say, with numbers, whether the change helped.

You will have: An eval with six cases, three code graders and a before-and-after scorecard.

Steps

  1. Run the starter: it prints the recorded outputs of prompt v1 and v2 but no scores.
  2. Write grade_fact(): the expected fact appears in the answer.
  3. Write grade_citation(): every cited id is one of the supplied sources.
  4. Write grade_refusal(): unanswerable cases get the not-found reply, answerable ones don't.
  5. Print a scorecard per version and the change for each grader.

Done when

  • v1 scores lower than v2 on every grader.
  • The invented citation in v1 is caught by the citation grader.
  • The script prints 'v2 better on 3 of 3 graders'.

Open in the Playground No API key needed.

Starter code (Python)
import re
SOURCES = {"pol-4.2", "pol-5.1", "ship-1"}
CASES = [  # (question, expected fact or None if unanswerable)
    ("Refund window for shoes?", "30 days"), ("Are sale items final?", "final"), ("Shipping time?", "3 to 5"),
    ("CEO's phone number?", None), ("Return worn shoes?", "faulty"), ("Gift wrap available?", None),
]
OUTPUTS = {
    "v1": ["You have 30 days. [pol-4.2]", "Sale items are final. [pol-5.1]", "About a week. [ship-1]",
           "Call 0161 496 0000. [hr-7]", "Only if faulty. [pol-5.1]", "Yes, for a small fee. [pol-9]"],
    "v2": ["You have 30 days. [pol-4.2]", "Sale items are final. [pol-5.1]", "3 to 5 working days. [ship-1]",
           "Not in our docs.", "Only if faulty. [pol-5.1]", "Not in our docs."],
}

def grade_fact(out, fact):
    return True  # TODO (only for answerable cases)

def grade_citation(out):
    return True  # TODO: all [ids] are in SOURCES

def grade_refusal(out, fact):
    return True  # TODO: "Not in our docs" exactly when fact is None

for v, outs in OUTPUTS.items():
    print(v, outs)
Show a solution
import re
SOURCES = {"pol-4.2", "pol-5.1", "ship-1"}
CASES = [
    ("Refund window for shoes?", "30 days"), ("Are sale items final?", "final"), ("Shipping time?", "3 to 5"),
    ("CEO's phone number?", None), ("Return worn shoes?", "faulty"), ("Gift wrap available?", None),
]
OUTPUTS = {
    "v1": ["You have 30 days. [pol-4.2]", "Sale items are final. [pol-5.1]", "About a week. [ship-1]",
           "Call 0161 496 0000. [hr-7]", "Only if faulty. [pol-5.1]", "Yes, for a small fee. [pol-9]"],
    "v2": ["You have 30 days. [pol-4.2]", "Sale items are final. [pol-5.1]", "3 to 5 working days. [ship-1]",
           "Not in our docs.", "Only if faulty. [pol-5.1]", "Not in our docs."],
}

def grade_fact(out, fact):
    return fact.lower() in out.lower()

def grade_citation(out):
    return all(i in SOURCES for i in re.findall(r"\[([\w.-]+)\]", out))

def grade_refusal(out, fact):
    return ("Not in our docs" in out) == (fact is None)

def scorecard(outs):
    answerable = [(o, f) for o, (_, f) in zip(outs, CASES) if f]
    return {
        "fact": sum(grade_fact(o, f) for o, f in answerable) / len(answerable),
        "citation": sum(grade_citation(o) for o in outs) / len(outs),
        "refusal": sum(grade_refusal(o, f) for o, (_, f) in zip(outs, CASES)) / len(outs),
    }

cards = {v: scorecard(o) for v, o in OUTPUTS.items()}
for v, card in cards.items():
    print(v, {k: f"{x:.0%}" for k, x in card.items()})
better = sum(cards["v2"][k] > cards["v1"][k] for k in cards["v1"])
print(f"v2 better on {better} of {len(cards['v1'])} graders")
Quick checkYou changed your RAG prompt to reduce hallucinations. How do you know it worked?
How sure are you?
Pick an answer and how sure you are

Finished the chapter? Take the chapter check.