Skip to main content

RAG Agent Evaluation

This example shows a common pattern: evaluating a RAG agent across a dataset of question-answer pairs, checking for both hallucination and pass.

The Setup

You have a RAG agent that:
  1. Takes a user question
  2. Retrieves relevant documents
  3. Generates an answer grounded in those documents
You want to evaluate:
  • Hallucination — Is the answer grounded in the retrieved documents?
  • Pass — Does the answer match the expected reference?

The Eval

What’s Happening

Case datasetcases= creates one eval run per row. Each row sets ctx.input and ctx.reference automatically. Target function — The run_agent target runs before the eval body, populating ctx.output and storing source docs in metadata. Storing context for analysis — We save the retrieved documents to ctx.metadata. This shows up in the results JSON and Web UI, so you can debug retrieval issues. Multiple scoring criteria — We use ctx.store(scores=...) for hallucination (a named score) and assert for pass (the default score). Both appear in your results. LLM-as-judge — The hallucination_judge and pass_judge are placeholder functions representing whatever LLM judge you’re using (OpenAI, Anthropic, your own prompts, etc.).

Running It

Example Results

After running, you’ll have a JSON file with structured results:
Your coding agent can read this JSON, analyze patterns, identify which questions have retrieval issues, and suggest improvements—all without leaving the terminal.