Artificial Intelligence • 3 min read

Evaluating RAG and AI Agents

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 kk retrieved results. Precision@k asks what fraction of those kk 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 k=4k=4, precision is 2/4=0.52/4=0.5 and recall is 2/3≈0.672/3\approx0.67. At k=1k=1, 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 criterionExample failure
Task completionSays event created, but no event exists
Tool correctnessCalls delete instead of update
Permission complianceReads data outside authorized scope
State consistencyRepeats a payment after timeout
RecoveryCannot resume after a failed tool call
EfficiencyMakes 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.

References

Related articles in

Image preview

Fit