Tutorial 4

AI agent evaluation

The system can loop. A math agent reads an expression, decides whether it needs a tool, calls add, subtract, multiply, or divide, and only then emits a number. The metric never asks an LLM whether that number is right.

The system Tool loop

The agent stays in the loop until it can emit a final number.

Expression(2 + 3) * (4 - 1)Math agentneed a tool?add / sub / mul / divFinal number
The dashed arrow is the tool result returning to the agent. Smoke-test with python -m ragas_examples.agent_evals.agent.

The dataset expression + expected

Each row is an expression and the number it should produce. No rubric, because the answer is a float.

dataset = [
    {"expression": "(2 + 3) * (4 - 1)", "expected": 15},
    {"expression": "5 * (6 + 2)", "expected": 40},
    {"expression": "10 - (3 + 2)", "expected": 5},
]

The metric Numeric tolerance

A non-LLM metric. Within 1e-5 of the expected value scores 1; anything else, including an error string, scores 0.

@numeric_metric(name="correctness")
def correctness_metric(prediction: float, actual: float):
    if isinstance(prediction, str) and "ERROR" in prediction:
        return 0.0
    result = 1.0 if abs(prediction - actual) < 1e-5 else 0.0
    return MetricResult(
        value=result,
        reason=f"Prediction: {prediction}, Actual: {actual}",
    )

@numeric_metric returns a float, unlike the pass/fail metrics on the earlier pages. The reason string still records both numbers, so a 0 tells you how far off the agent was.

The experiment Keep the tool log

A wrong number is not enough. The log shows which tools the agent called on the way there.

@experiment()
async def run_experiment(row):
    prediction = math_agent.solve(row["expression"])
    correctness = correctness_metric.score(
        prediction=prediction.get("result"),
        actual=row["expected"],
    )
    return {
        "expression": row["expression"],
        "expected_result": row["expected"],
        "prediction": prediction.get("result"),
        "log_file": prediction.get("log_file"),
        "correctness": correctness.value,
    }

Key takeaways

  • End to end: python -m ragas_examples.agent_evals.evals.
  • If the task has a checkable answer, do not spend an LLM judge on it. Tolerance, exact match, and schema checks belong in code.
  • Keep log_file. Agent failures are usually a bad tool call, not a bad final sentence.