A document assistant can retrieve the wrong paragraph but generate a polished answer. An AI agent can select the right tool but use the wrong arguments. Measuring only the final sentence hides where failures begin. RAG and agent evaluation separates components and tests the complete workflow.
Evaluate retrieval before generation
A RAG pipeline typically retrieves candidate passages and passes some to a generator. Build test questions with known relevant passages so retrieval can be assessed directly.
Recall@k asks what fraction of relevant passages appear in the first retrieved results. Precision@k asks what fraction of those results are relevant. MRR rewards placing the first relevant result near the top; NDCG handles graded relevance and rank. The most suitable metric depends on whether one passage or several pieces of evidence are required.
Worked example
Suppose four passages are retrieved, with relevance labels [1, 0, 1, 0], and the collection has three relevant passages in total.
At , precision is and recall is . At , precision is 1. These numbers describe retrieval, not whether the generated answer is correct.
Evaluate the generated answer
Check factual correctness, relevance to the question, and groundedness: whether claims are supported by the retrieved source content. Also check citation accuracy. A citation is not valid merely because the linked document exists; it must actually support the statement.
Test systems on cases where retrieval returns incomplete or contradictory evidence. The expected behavior may be to express uncertainty or decline to answer, rather than invent a confident response.
Evaluate agents as sequences of actions
For an agent that can search files and create calendar events, the final chat message is insufficient. Measure whether it selected the authorized tools, passed valid arguments, used the appropriate order, respected approvals, and left external systems in the intended state.
| Agent criterion | Example failure |
|---|---|
| Task completion | Says event created, but no event exists |
| Tool correctness | Calls delete instead of update |
| Permission compliance | Reads data outside authorized scope |
| State consistency | Repeats a payment after timeout |
| Recovery | Cannot resume after a failed tool call |
| Efficiency | Makes excessive tool calls for a simple task |
Use sandboxed test environments whenever possible. Never test destructive agent actions against important real accounts merely to see what happens.
Construct trace-level tests
Create a test case with an initial state, user objective, permitted actions, required approvals, and an expected end state. Store tool-call traces and independently verify the result.
# Illustrative test of an agent execution record, not a real calendar API.
trace = [
{'tool': 'check_availability', 'status': 'ok'},
{'tool': 'create_event', 'status': 'ok'},
]
expected_order = ['check_availability', 'create_event']
actual_order = [step['tool'] for step in trace]
print('Correct tool order:', actual_order == expected_order)
A correct tool sequence does not prove the event was created at the right time. Verify external state and permissions too.
Diagnose failures systematically
If a RAG answer is wrong, inspect whether the source lacked the answer, the chunking lost context, retrieval missed evidence, reranking displaced it, or the generator ignored it. For agents, inspect planning, tool selection, argument validation, observed tool results, and final reporting.
Use both offline regression tests and production monitoring. Include adversarial inputs, prompt injection in retrieved documents, conflicting instructions, and permission-boundary checks.
Related concepts
- Retrieval-Augmented Generation (RAG)
- AI Agents and Tool-Using Workflows
- Evaluating Generative AI Systems
- AI Security Threats and Defenses
References
Related articles in
- AI Agents and Tool-Using Workflows — Learn agent goals, tool invocation, planning, state, permissions, and how to evaluate multi-step AI workflows.
- AI Development Tools: Git, Jupyter, and APIs — Learn how Git, Jupyter notebooks, Python environments, and APIs fit together in reproducible, secure artificial intelligence workflows.
- How AI Is Changing Theoretical Computer Science — AI is beginning to help researchers explore algorithms, mathematical structures, and formal proofs. Here is what it can do, what still needs human judgment, and why verification matters.