Sub topic 15
Evaluating retrieval, grounding and agent behavior
This chapter assumes you have built the agent loop from earlier chapters, can read basic Python, and know what a retrieved chunk and a tool call are. An agentic RAG system can fail in three separate places. If you score only the final answer, you can't tell which place failed.
Three layers to measure
| Layer | Question | Typical metric |
|---|---|---|
| Retrieval | Did the search tool return the passages that contain the answer? | Recall@k, hit rate |
| Grounding | Is every claim in the answer supported by the passages it cites? | Supported-claim rate, citation validity |
| Agent behavior | Did the model search when it should, stop in time, and stay inside its budget? | Tool-call count, step count, skip accuracy |
Build a small test set
Write 20 to 50 questions by hand. For each one, record the ids of the passages that contain the answer, and whether retrieval is needed at all. Include greetings or general-knowledge questions, so you can check that the agent skips search. Include a few questions the documentation cannot answer, so you can check that it admits the gap.
Score retrieval from the trace
Log every tool call with its query and the returned ids. Then compute recall over all the calls in a run. The agent may search several times, so a gold passage counts as found if any call returned it.
def retrieval_recall(gold_ids, tool_calls):
# tool_calls: [{"query": str, "returned_ids": [str, ...]}, ...]
if not gold_ids:
return None # no retrieval expected, so recall is undefined
found = set()
for call in tool_calls:
found.update(call["returned_ids"])
return len(found & set(gold_ids)) / len(gold_ids)
def citation_validity(cited_ids, tool_calls):
# Cited ids must come from passages the agent actually saw.
seen = set()
for call in tool_calls:
seen.update(call["returned_ids"])
if not cited_ids:
return 0.0
return len(set(cited_ids) & seen) / len(set(cited_ids))
calls = [
{"query": "rate limit", "returned_ids": ["d1", "d7"]},
{"query": "rate limit burst", "returned_ids": ["d3"]},
]
print(retrieval_recall(["d3", "d9"], calls)) # 0.5
print(citation_validity(["d3", "d99"], calls)) # 0.5Score grounding
Citation validity only shows that a cited id exists. It does not show that the passage supports the claim. For that, split the answer into claims and check each one against its cited passage, either by hand on a sample or with a second model call used as a judge. Spot-check the judge against human labels before you trust it.
Score agent behavior
Read the trace for patterns that the answer alone hides:
- Skip accuracy: the share of no-retrieval questions where the agent made zero calls.
- Missed searches: factual questions answered with no tool call.
- Repeated queries: the same query sent twice, which suggests the agent is not using earlier results.
- Budget hits: runs that ended on the step limit instead of a final answer. A high rate points to weak stopping rules or poor retrieval.
- Honest gaps: for unanswerable questions, whether the answer says the evidence is missing.
Exercise
An agent answers a question correctly, but retrieval recall for that run is 0 and the answer cites a passage id that no tool call returned. Which layer failed, and what does it suggest?
Show answer
Grounding failed, and retrieval did too. The correct answer probably came from the model's own memory, and the citation was invented. The answer score alone would have hidden this, which is why you score each layer separately.