Lesson 3 of 5 · Evaluating Agents
Trajectory Evaluation
Evaluate an AI agent's trajectory: in-order and any-order matching, tool call precision and recall, forbidden orders, argument checks and LLM judges.
- 7 min read
- Advanced
- Reviewed
- Explain why the path to a result matters, not just the result
- Compare trajectories with exact, in-order and any-order matching
- Measure precision and recall of tool calls
- Check rules about order, repetition and arguments
- Use a model to judge trajectories that can't be checked by rules
The right answer for the wrong reasons
An agent refunds an order, and the refund is correct. Test passed? Not if it issued the refund without checking the refund policy. This time the order happened to be eligible; next time it won't be.
Trajectory evaluation catches problems that outcome checks miss:
- Unsafe paths: skipping a required check, or doing something risky that happened to work.
- Wasted effort: redundant calls, loops, needless steps that cost time and money.
- Lucky successes: right outcome, wrong reasoning, likely to fail on the next similar task.
- Where things went wrong in failed runs, so you know what to fix.
Comparing against a reference
Write a reference trajectory for each test task: the tool calls a good run would make. Then compare actual runs with it, at different levels of strictness:
# The reference trajectory for "Refund order 881 if it's eligible", and three agents' actual tool calls
reference = ["get_order", "check_refund_policy", "issue_refund", "send_email"]
runs = {
"agent A": ["get_order", "check_refund_policy", "issue_refund", "send_email"],
"agent B": ["get_order", "get_customer", "check_refund_policy", "issue_refund", "send_email"],
"agent C": ["issue_refund", "get_order", "send_email", "get_order"],
}
FORBIDDEN_ORDER = [("check_refund_policy", "issue_refund")] # the first must happen before the second
def exact(actual):
return actual == reference
def in_order(actual):
"""Reference calls all appear, in the same order (extra calls in between are allowed)."""
it = iter(actual)
return all(step in it for step in reference)
def precision_recall(actual):
hits = len(set(actual) & set(reference))
return hits / len(set(actual)), hits / len(set(reference))
def violations(actual):
found = []
for before, after in FORBIDDEN_ORDER:
if after in actual and (before not in actual or actual.index(before) > actual.index(after)):
found.append(f"{after} without {before} first")
repeats = sorted({c for c in actual if actual.count(c) > 1})
if repeats:
found.append(f"repeated: {', '.join(repeats)}")
return found
for name, actual in runs.items():
p, r = precision_recall(actual)
print(f"{name}: exact={exact(actual)!s:<5} in_order={in_order(actual)!s:<5} "
f"precision={p:.2f} recall={r:.2f} problems={violations(actual) or 'none'}")- Exact match is the strictest: same calls, same order, nothing extra. Only agent A passes, which is too strict for agent B, whose extra
get_customercall is harmless. - In-order match checks that the reference calls appear in order, allowing extra calls in between. Agent B passes. (The demo's trick:
step in itsearches an iterator, which moves forward as it searches, so each reference call must appear after the previous one.) - Precision and recall treat calls as a set, ignoring order. Agent B's precision is 0.80 because one of its five distinct calls wasn't in the reference. Agent C's recall is 0.75 because it never checked the policy.
- Rule checks find what the scores hide. Agent C issued a refund before looking at anything and repeated a call. Its precision of 1.00 looks perfect; its trajectory is dangerous.
Checking arguments
The right tool with the wrong arguments is still wrong. Check the arguments that matter: the refund amount equals the order total, the email goes to the customer on the order, the date is the one the user asked for. Compare values exactly where you can (amounts, IDs, dates) and loosely where text can vary (a search query just needs to mention the right product).
Judging whole trajectories
Some qualities can't be checked with rules: Was the reasoning sound? Did the agent ask a sensible clarifying question? Did it give up too early? For these, show a model the task, the trajectory and a rubric, and ask for narrow yes-or-no judgements:
- Did the agent gather the information needed before acting?
- Did every action follow from the information it had at the time?
- Did it handle tool errors sensibly (retry, try an alternative, or report)?
- Did it stop when the task was done?
The same rules apply as for any LLM judge: give it the facts, keep criteria narrow, and check its grades against a sample graded by people.
Common mistakes
Write an in-order trajectory check: the agent must make the expected calls in the expected order, but may add other calls in between.
reference = ["search_flights", "hold_seat", "pay"]
runs = [
["search_flights", "get_weather", "hold_seat", "pay", "send_email"],
["hold_seat", "search_flights", "pay"],
]
def in_order(actual):
# Return True if every reference call appears in actual, in the same order
# (other calls may come in between). Hint: walk through actual, keeping a
# position in reference that moves forward each time you find the next call.
pass
for actual in runs:
print(in_order(actual))
Agent C's tool calls have a precision of 1.00 but it refunded before checking the policy. Which check caught the problem?
Summary
- A trajectory is the sequence of steps and tool calls behind a result; checking it catches unsafe, wasteful and lucky runs.
- Compare with a reference using exact, in-order or any-order matching, chosen per task.
- Precision shows unwanted calls; recall shows skipped ones.
- Rules check order, forbidden actions and repeats; argument checks catch the right tool used wrongly.
- LLM judges with narrow rubrics assess reasoning that rules can't.
Next: agent observability and tracing.
Finished reading? Tick it off to track your progress.