12. Evaluating AI systems
How to know whether an AI system works. Test sets, retrieval metrics (recall@K, precision@K, MRR, NDCG, hit rate), generation metrics, LLM-as-a-judge, agent and operational metrics, and regression testing.
If you cannot measure it, you cannot improve it, and with LLMs your intuition from a few manual tries is especially misleading: outputs vary, improvements on one question break another, and a confident answer can be wrong. Evaluation (“evals”) replaces vibes with numbers.
The evaluation loop
- Define success for your use case (correct facts? cited? concise? safe? under 3 seconds?).
- Build a test set of realistic cases with expected properties.
- Run the system on every case and score with metrics.
- Change one thing (prompt, chunking, model, reranker) and run again.
- Compare case by case: what improved, what regressed?
- Keep the test set as a regression suite and grow it with every bug you find.
Building a test set
- Start small: 20–50 cases catch most regressions. Grow over time.
- Cover typical questions, hard ones (multi-hop, ambiguous), edge cases, unanswerable questions (the right answer is I don’t know) and adversarial inputs (injection attempts).
- For each case, record what you can: a reference answer, expected sources (which document or section should be retrieved), required facts (
must_include), forbidden content (must_not_include), expected tools. - Version the dataset: a comparison is valid only on the same version.
Retrieval metrics
Given the top K retrieved chunks and the set of relevant (expected) ones:
| Metric | Question it answers | Formula (intuition) |
|---|---|---|
| Recall@K | Did we find the relevant chunks? | relevant found in top K / all relevant |
| Precision@K | How much of what we returned is relevant? | relevant in top K / results returned (≤ K) |
| Hit rate@K | Did we find at least one? | 1 if any relevant in top K, else 0 |
| MRR (mean reciprocal rank) | How high is the first relevant result? | mean of 1 / rank of first relevant (1, ½, ⅓, …) |
| NDCG@K (normalised discounted cumulative gain) | Are relevant results near the top? | sum of relevance / log2(rank + 1), normalised by the ideal ordering |
For RAG, recall@K is usually the most important: if the evidence is not retrieved, the answer cannot be grounded.
Generation metrics
| Metric | Measures | Needs |
|---|---|---|
| Required facts / contains | Key facts or phrases present | Expected phrases |
| Token F1 | Word overlap with a reference (precision and recall of tokens) | Reference answer |
| Exact match | Identical to the reference (good for short answers) | Reference |
| Citation validity | Cited sources exist | Citations |
| Citation source recall | The expected sources were cited | Citations + expected sources |
| Correctness (judge) | Same meaning as the reference | Reference + judge |
| Groundedness / faithfulness (judge) | Every claim supported by the retrieved passages | Passages + judge |
| Answer relevance (judge) | Actually answers the question | Judge |
Lexical metrics (F1, exact match) are cheap and objective but miss paraphrases. Judge metrics capture meaning but are estimates.
LLM-as-a-judge
Using a model to grade outputs against a rubric (Zheng et al., 2023, Judging LLM-as-a-Judge). Practical rules:
- Forced structure: make the judge call a tool like
submit_grade(score 1–5, reasoning). - Clear rubric: define what 1, 3 and 5 mean.
- Low temperature, a capable judge model, ideally different from the model being judged.
- Store everything: judge model, prompt version, rubric, reasoning.
- Known biases: position bias, verbosity bias (longer looks better), self-preference.
- Validate the judge against a few human labels.
- Label results as estimates, and treat a judge failure as not applicable, never as zero.
Agent and safety metrics
- Tool use: were the expected tools called? (Trajectory evaluation goes further: right tools, right order, no wasted calls.)
- Task success for end-to-end goals.
- Forbidden content absent; refusal correctness on out-of-scope or harmful requests; injection attack success rate.
Operational metrics
Success rate, latency (report mean and p50/p95: averages hide slow tails), tokens, cost and retries per case. A change that improves quality by 1 % and doubles cost may not be worth it.
Comparing versions honestly
- Same dataset version, same K, same judge; otherwise the comparison is invalid.
- Look at per-case changes, not just averages: a mean can stay flat while three cases regress.
- Small datasets show direction, not proof; there is noise between runs. Re-run when differences are small.
- Keep a baseline and compare every change against it.
Offline vs online evaluation
- Offline: test sets before release (this chapter).
- Online: in production: user feedback (thumbs up/down), A/B tests, monitoring of latency, cost and failure rates, sampling traces for review. Feed real failures back into the offline test set.
Try it in AgenticMind
The Evaluation Lab: versioned datasets (form editor, JSON import/export), 18 versioned evaluators (recall@K, precision@K, MRR, NDCG@K, hit rate, required facts, reference F1, citation validity, citation source recall, correctness and groundedness judges, forbidden content, tool use, success, latency, tokens, cost, retries), every case a real traced run, baselines, and comparisons that flag non-comparable runs. Try it with examples/datasets/employee-handbook-qa.json on Advanced RAG. See Evaluation Lab.
Key takeaways
Evaluate with a versioned test set and metrics, not with a few manual tries. Measure retrieval (recall@K first, then MRR and NDCG) separately from generation (required facts, citations, judged correctness and groundedness). LLM judges are useful estimates with biases: structure, store and validate them. Compare per case, on the same dataset version, against a baseline.

