BXUP Track progress & test yourself — free
QA & Testing · Playbook · Advanced · ~4 min read
QA & TESTING · PLAYBOOK · ADVANCED

Evaluate Agent Trajectories — Catch the Right Answer on the Wrong Path

An agent that answers correctly after looping through eleven redundant tool calls, or after reading data it had no business touching, passes every final-answer test you own. Trajectory evaluation asserts on the path — the sequence of steps — and catches the false positives that final-response testing structurally cannot see.

Tier: Advanced · Workflow area: Testing AI systems — judges, agents & robustness

This playbook adds a trajectory layer to an agent test bench: expected-path specs, loop detection, and step-efficiency budgets, evaluated over traces from Langfuse or Arize Phoenix.

When to use

USE THIS WHEN

The path carries risk or cost

Multi-step agents where extra steps cost real money or latency. Compliance contexts where how the answer was produced matters (which sources were consulted, whether a scoped tool was bypassed). Final-answer suites are green but users report slow, expensive or erratic runs. Loop incidents have already occurred.

SKIP IT WHEN

The path is trivial

Single-call or fixed-pipeline features with no branching — tool-call asserts (previous playbook) cover it. You have no trace capture yet — instrument first; trajectory testing without step-level traces is archaeology without a dig site.

Prerequisites

  • Step-level tracing: every tool call, its arguments and result recorded in order (Langfuse, Phoenix, or structured logs with a run ID).
  • The tool-call suite from the previous playbook passing — trajectory bugs are only diagnosable once individual calls are trustworthy.
  • A set of scenarios where you can write down what a good path looks like.

The workflow

  1. Choose the evaluation mode per scenario — write it down. Three modes, in increasing strictness, and most suites need all three somewhere:

    • Any-order set: these tools must all be used, sequence free. For research-style tasks.
    • In-order subsequence: these steps must appear in this order; other steps may interleave. The workhorse mode.
    • Exact path: the full sequence is determined. Only for genuinely deterministic workflows — exact-path asserts on open-ended tasks produce permanent false failures.
  2. Write expected-path specs as data, not code. A YAML spec per scenario keeps them reviewable by non-programmers:

    scenario: refund-request-existing-order
    mode: in-order-subsequence
    must_include:
      - order_lookup
      - refund_policy_check
      - refund_create
    must_not_include:
      - customer_pii_export
    max_steps: 6
    max_repeats_per_tool: 2
    

    The must_not_include line is where compliance requirements become executable — tools the agent may have access to but must not touch on this path.

  3. Pull trajectories from traces and assert against specs.

    def tool_sequence(trace):
        return [s.name for s in trace.spans if s.type == "TOOL"]
    
    def test_trajectory(spec, trace):
        seq = tool_sequence(trace)
        assert is_subsequence(spec.must_include, seq), f"path was {seq}"
        assert not set(spec.must_not_include) & set(seq)
        assert len(seq) <= spec.max_steps
        for tool, n in Counter(seq).items():
            assert n <= spec.max_repeats_per_tool, f"{tool} called {n}x"
    

    The repeat-count assert is your loop detector: an agent stuck re-calling order_lookup with the same failing arguments is the classic runaway, and max_repeats_per_tool: 2 catches it long before a token-budget blowout.

  4. Add a step-efficiency budget and track it as a metric, not just a gate. Median and p95 steps per scenario, recorded per run:

    refund-request-existing-order (n=10 runs)
    pass rate: 10/10 final answer correct
    steps: median 4, p95 9      <- was p95 5 last release
    FLAG: 2 runs took 9 steps — order_lookup retried 4x on
          malformed date arg before self-correcting
    

    This run is the poster child for the playbook: every final answer correct, and a real regression (a flapping self-correction loop) visible only in the path.

  5. Score path quality with a judge only where rules cannot reach. Whether interleaved extra steps were reasonable is a judgment call — a calibrated LLM judge over the trajectory ("were any steps redundant given the results already retrieved?") is legitimate here. Keep it advisory or calibrate it before it gates (see the judge playbook); the deterministic spec asserts remain the gate.

  6. Run under repetition and gate with pass^k thinking. Paths vary run to run at nonzero temperature. Repeat each scenario 5–10 times; gate on "all runs satisfy the spec" for compliance scenarios (must_not_include violations are one-strike), and on rates (≥ 90%) for efficiency budgets.

  7. Feed incidents back as specs. Every production loop or forbidden-tool incident becomes a scenario with a spec that would have caught it. The spec file grows into your agent's behavioral contract.

Warning

A trajectory violation with a correct final answer is still a failing test. The one-in-ten run that bypasses refund_policy_check and happens to get it right is exactly the run that will burn you at scale — do not let green answers argue you out of red paths.

Variations

  • Langfuse agent evals: trajectories come from nested observations; run the spec asserts in a scheduled job over sampled production traces, not only in CI — trajectory drift after model updates is common and silent.
  • Arize Phoenix: strong span-level view for developing specs — replay a trace, decide what the good path was, write the spec from it; its built-in trajectory evals can layer on top.
  • Final-response + trajectory pairing: report the four cells (answer right/wrong × path clean/dirty). Right-answer-wrong-path and wrong-answer-clean-path are different bug classes; the pairing table routes them.

Quality check

  • Every scenario declares its mode (set / subsequence / exact) with a sentence on why.
  • Loop detection present via repeat caps on every spec, not just suspected scenarios.
  • must_not_include used for real constraintsat least the PII/scope rules your compliance story claims.
  • Step budgets tracked over time with p95, so efficiency regressions show as trends before they fail gates.
  • Specs live in version control and reviewers can read them without reading the harness.

Pitfalls

  • Exact-path everywhere. Over-constrained specs fail on every harmless variation and get deleted within a month. Reserve exact mode for genuinely fixed workflows.
  • Testing trajectories before tool calls are solid. A wrong-parameter bug shows up as a weird path; you will debug routing when the fault is extraction. Order matters.
  • Single-run verdicts. One clean path proves one sample. Compliance asserts need all-runs verification.
  • Ignoring cost in the budget. Steps are a proxy; a 4-step path with two 80k-token context stuffings is worse than a 6-step lean one. Record tokens per run alongside steps.
Key Takeaway:

Final-answer testing grades the destination; trajectory testing grades the driving. Agents are judged in production on both — a suite that only checks arrivals will certify an agent that speeds through every red light it meets.

This is the free reading view. Open BXUP to track your progress, run the hands-on labs, and prove it with original, exam-pattern assessments.
© Anvi Innovate · BXUP — Be Exponentially Upskilled · BXUP is an independent learning platform, not affiliated with or endorsed by AWS, Microsoft, Google, NVIDIA, IBM, Salesforce, Databricks, Oracle or Anthropic. All trademarks belong to their respective owners. · bxup.in