Agent Evals: Trajectory vs Outcome Scoring

RoadmapsAI Engineering

Overview

Evaluating an agent based on the sequence of tool calls and actions it took (trajectory), not just the final text it returned (outcome).

Agents can arrive at the right answer through luck, hallucinations, or dangerous tool loops. Outcome evaluation hides these catastrophic failures.

Where used: Evaluating agent loops, Test suites for agentic applications, Continuous integration for LLM architectures

Why learn this

Code walkthrough

def evaluate_agent_run(trace_object):
    # BAD: Outcome-only evaluation passes even if the path was chaotic
    assert 'Booked' in trace_object.final_output

    # GOOD: Trajectory evaluation inspecting the exact sequence
    tool_names = [step.tool_name for step in trace_object.steps]
    
    # A bad trace might be: ['search', 'search', 'hallucinated_api', 'book']
    # We must explicitly forbid loops and hallucinations
    assert len(tool_names) == 2, f'Expected 2 tool calls, got {len(tool_names)}'
    assert tool_names[0] == 'search_flights'
    assert tool_names[1] == 'book_flight'
    
    # Assert the environmental impact actually occurred
    assert db.get_flight_status() == 'Booked'

Focus: Outcome evaluation only checks the final string ('Booked'), which would pass an agent that hallucinated 15 failed database queries before accidentally succeeding. Trajectory evaluation asserts the exact sequence of tools used and verifies the environment state. Helper names are placeholders — the control flow is the point.

Common mistakes

Recall questions

Understanding checks

An agent evaluates a complex database query. It gets the answer right 1 out of 5 times. If we use `pass@5`, the score is 100%. If we use `pass^5`, the score is 0%. Which metric provides a safer evaluation for deploying this autonomous agent to production, and why?

`pass^5` is safer. Autonomous agents can cause harm or incur massive costs if they fail or loop infinitely 80% of the time. `pass^k` measures the required consistency.

Agents have compounding error rates due to their multi-step nature, meaning consistency is critical.

What is the flaw in this trajectory evaluation?

It checks the first tool call but doesn't limit or check the total number of tool calls. The agent could have hallucinated 50 tool calls after the first one before eventually outputting 'Success'.

Trajectory evaluation must ensure the agent didn't take unnecessary or dangerous steps, which requires validating the entire tool call sequence.

Practice tasks

Write a Trajectory Evaluator

Write a Python function `eval_trajectory(trace)` that takes a trace object (which has a `.steps` list of strings representing tool names). The function should return True only if the agent called 'search' exactly once, followed by 'execute' exactly once, and made no other tool calls.

Continue learning

Previous: Evaluation & Test Sets

Previous: Observability & Tracing

Return to AI Engineering Roadmap