Listen to this Post

Introduction:
In traditional software engineering, testing is deterministic – you assert an expected output against an actual one, and the test either passes or fails. Early generative AI applications inherited this paradigm: input-output prompt testing was sufficient because the model’s behavior was largely stateless. However, autonomous AI agents have shattered this model entirely. A single user prompt can trigger dozens of internal reasoning steps, API calls, dynamic retrievals, and state mutations across multi-turn conversations. The most dangerous trap in agent engineering is that an agent can produce a correct final answer while failing silently along the execution path – using the wrong tool, sending malformed parameters, hallucinating actions that never occurred, or corrupting state that only manifests later. To fix this, we must shift from final-output validation to Trajectory Evaluation – scoring the full execution path across two main failure pillars: Agent Task Success (behavioral and goal alignment) and Tool Use Quality (syntactic and schema precision).
Learning Objectives:
- Understand why traditional input-output testing is insufficient for autonomous AI agents and the concept of trajectory evaluation.
- Identify the two primary failure pillars in agent execution: behavioral misalignment and tool-use quality issues.
- Learn practical implementation techniques for trajectory evaluation using open-source frameworks, LLM-as-judge paradigms, and automated evaluation flywheels.
1. Understanding Trajectory Evaluation: Beyond the Final Payload
Trajectory evaluation means scoring the entire execution path an AI agent takes rather than just its final output. This requires capturing the full execution tree of your agent – every tool selected, every document retrieved, and every reasoning step the model took. Without this visibility, you are debugging blind.
The core insight is that an agent can reach a correct answer through a completely broken path. Consider two agents that both return “The weather in SF is 80 degrees and sunny.” Agent A calls `get_weather(city=”SF”)` once and returns the result. Agent B calls get_weather(location="San Francisco"), fails to parse the response, calls get_temperature(city="SF"), gets a numeric value, hallucinates the unit, calls get_conditions(city="SF"), and finally assembles the answer. Both final answers are identical, but one trajectory is efficient and correct; the other is inefficient, error-prone, and may have corrupted internal state.
Step-by-Step Guide to Capturing Trajectories:
- Instrument your agent to log every step: user messages, agent thoughts, tool calls, tool outputs, and agent responses.
- Structure each trajectory as a list of OpenAI-style messages or LangChain BaseMessage objects.
- Store trajectories with metadata: timestamp, agent version, cost, latency, and token usage.
- Implement a tracing layer that captures not just what the agent did, but why – the reasoning behind each tool selection.
Code Example (Python – Trajectory Logging):
from datetime import datetime
import json
class TrajectoryLogger:
def <strong>init</strong>(self, agent_id, session_id):
self.agent_id = agent_id
self.session_id = session_id
self.steps = []
self.start_time = datetime.now()
def log_step(self, step_type, content, metadata=None):
self.steps.append({
"timestamp": datetime.now().isoformat(),
"step_type": step_type, "user_message", "agent_thought", "tool_call", "tool_output", "agent_message"
"content": content,
"metadata": metadata or {}
})
def log_tool_call(self, tool_name, arguments, result):
self.log_step("tool_call", {"name": tool_name, "arguments": arguments})
self.log_step("tool_output", result)
def get_trajectory(self):
return {
"agent_id": self.agent_id,
"session_id": self.session_id,
"start_time": self.start_time.isoformat(),
"end_time": datetime.now().isoformat(),
"steps": self.steps,
"total_steps": len(self.steps)
}
- The Two Failure Pillars: Agent Task Success and Tool Use Quality
The post identifies two primary failure pillars that trajectory evaluation must address:
Pillar 1: Agent Task Success (Behavioral & Goal Alignment)
- Hallucinations: The agent claims it lacks tools it actually has, or hallucinates unperformed actions.
- Instruction Violations: The agent violates negative constraints, over-punts (asks unnecessary permission), or stops execution halfway.
- Tool Output Handling: The agent fails to parse valid API return payloads.
Pillar 2: Tool Use Quality (Syntactic & Schema Precision)
- Parameter Errors: Passing `string “50”` instead of
int 50, or swapping parameter mappings. - Selection & Syntax: Selecting the wrong tool or passing malformed JSON syntax.
- Tool Hallucinations: Inventing fake function names or parameter keys that don’t exist.
Step-by-Step Guide to Evaluating These Pillars:
- Define rubrics for each failure type. For tool use, create static schema validators that check parameter types, required fields, and JSON syntax.
- Implement adaptive rubric metrics that automatically generate scoring criteria based on the agent’s configuration and user prompt.
- Use LLM-as-judge to evaluate behavioral alignment – provide the trajectory and ask the judge to score instruction compliance, hallucination presence, and goal completion.
- Calculate composite scores that blend task completion, step efficiency, and tool selection accuracy into a single 0–1 score.
Code Example (LLM-as-Judge Trajectory Evaluation using agentevals):
from agentevals.trajectory.llm import create_trajectory_llm_as_judge, TRAJECTORY_ACCURACY_PROMPT
Initialize the trajectory evaluator
trajectory_evaluator = create_trajectory_llm_as_judge(
prompt=TRAJECTORY_ACCURACY_PROMPT,
model="openai:o3-mini"
)
Your agent's trajectory as a list of OpenAI-style messages
trajectory = [
{"role": "user", "content": "What is the weather in SF?"},
{"role": "assistant", "content": "", "tool_calls": [{"function": {"name": "get_weather", "arguments": json.dumps({"city": "SF"})}}]},
{"role": "tool", "content": "It's 80 degrees and sunny in SF."},
{"role": "assistant", "content": "The weather in SF is 80 degrees and sunny."}
]
Run the evaluation
eval_result = trajectory_evaluator(outputs=trajectory)
print(eval_result)
Output: {'key': 'trajectory_accuracy', 'reasoning': '...', 'score': True}
3. Automated Trajectory Evaluation Frameworks
Several open-source frameworks have emerged to operationalize trajectory evaluation:
- AgentEvals (LangChain): Provides readymade trajectory evaluators using LLM-as-judge, with support for both Python and TypeScript. Installation:
pip install agentevals. - agent_trajectory_evaluation: A unified framework supporting both LLM-as-judge (TRACE) and ground-truth comparison (GRACE-style) evaluation paradigms. Supports OpenAI, Gemini, Claude, and Bedrock.
- Strands Evaluation: A comprehensive framework for AI agents and LLM applications, covering output validation, multi-agent interaction analysis, and trajectory evaluation.
- AgentRx: An automated, domain-agnostic diagnostic framework that pinpoints the critical failure step in a failed agent trajectory.
- AgentLens: Diagnoses and compares coding agents by evaluating both the final repository state and the decisions made along the way.
Step-by-Step Guide to Implementing agent_trajectory_evaluation:
from agent_trajectory_evaluation import LLMConfig
from agent_trajectory_evaluation.unified import UnifiedEvaluator
Configure the LLM evaluator
config = LLMConfig(
provider="openai",
model="gpt-4o-mini",
api_key="YOUR_OPENAI_API_KEY"
)
Initialize the unified evaluator
evaluator = UnifiedEvaluator(llm_config=config)
Your agent's trajectory (list of steps)
trajectory = [
{"step": 1, "action": "thought", "content": "User wants weather in SF"},
{"step": 2, "action": "tool_call", "name": "get_weather", "arguments": {"city": "SF"}},
{"step": 3, "action": "tool_output", "content": "80°F, sunny"},
{"step": 4, "action": "response", "content": "The weather in SF is 80°F and sunny."}
]
Evaluate using TRACE (LLM-as-Judge, no ground truth)
result = evaluator.evaluate_trace(trajectory)
print(result)
Returns metrics: Efficiency, Hallucination, Adaptivity, InstructionError
4. The Evaluation Flywheel: Evolutionary Prompt Engineering
The post mentions a “5-stage eval flywheel and evolutionary prompt engineering” as the automated solution. This concept represents a continuous improvement loop where evaluation results drive prompt optimization, which in turn improves agent performance, generating new evaluation data.
The 5-Stage Flywheel:
- Data Collection: Capture production trajectories with full telemetry – every tool call, reasoning step, latency, and cost metric.
- Dataset Construction: Curate evaluation datasets from real traffic, including edge cases and adversarial trajectories.
- Effect Evaluation: Run trajectory evaluations using both LLM-as-judge and ground-truth comparison methods.
- Evolution Asset Harvesting: Extract successful trajectories, optimal tool selections, and effective reasoning patterns.
- Prompt Evolution: Use optimization algorithms (like GEPA, MIPROv2) to mutate and evaluate prompts, instructions, and few-shot examples – not model weights.
Step-by-Step Guide to Building an Evaluation Flywheel:
- Deploy trajectory logging in production to collect real user interactions.
- Build a golden dataset of “ideal” trajectories for your key use cases.
- Run offline evaluations against this dataset after every agent update.
- Use failure clustering to identify systematic failure patterns – for example, the agent consistently fails on multi-step tool sequences or specific parameter types.
- Automate prompt optimization by treating the evaluation score as a fitness function for evolutionary algorithms.
Code Example (Pseudo-code for Flywheel Automation):
class EvaluationFlywheel:
def <strong>init</strong>(self, agent, evaluator, prompt_optimizer):
self.agent = agent
self.evaluator = evaluator
self.prompt_optimizer = prompt_optimizer
self.trajectory_store = []
def collect_trajectory(self, user_input, agent_output):
trajectory = self.agent.get_trajectory()
self.trajectory_store.append(trajectory)
return trajectory
def evaluate_batch(self, trajectories):
results = []
for traj in trajectories:
score = self.evaluator.evaluate(traj)
results.append({"trajectory": traj, "score": score})
return results
def evolve_prompts(self, evaluation_results):
Identify low-scoring trajectories
failures = [r for r in evaluation_results if r["score"] < 0.7]
Extract failure patterns
patterns = self.cluster_failures(failures)
Optimize prompts based on patterns
new_prompts = self.prompt_optimizer.optimize(patterns)
return new_prompts
def run_cycle(self):
1. Collect
recent_trajectories = self.trajectory_store[-100:]
2. Evaluate
results = self.evaluate_batch(recent_trajectories)
3. Evolve
new_prompts = self.evolve_prompts(results)
4. Deploy
self.agent.update_prompts(new_prompts)
5. Practical Implementation with Google Cloud Agent Platform
Google Cloud’s Gemini Enterprise Agent Platform provides built-in trajectory evaluation capabilities. After running an evaluation, the platform offers diagnostic tools to identify root causes at three levels: aggregate trends in dashboards, semantic failure clusters, and per-trajectory fine-grained logic paths.
Using the Vertex AI SDK for Trajectory Evaluation:
from vertexai import evals, types Run an evaluation result = client.evals.evaluate( dataset=eval_dataset, metrics=[ types.RubricMetric.FINAL_RESPONSE_QUALITY, types.RubricMetric.TOOL_USE_QUALITY, types.RubricMetric.HALLUCINATION, types.RubricMetric.SAFETY, ], ) Visualize aggregate and per-case results in your notebook result.show()
The visualization includes summary metrics (average scores and pass rates) and per-case results that can be expanded to inspect detailed findings, including rubric judgments with natural language reasoning.
Accessing Failure Clusters Programmatically:
Generate failure clusters
clusters = client.evals.get_failure_clusters(evaluation_id=eval_id)
for cluster in clusters:
print(f"Failure Pattern: {cluster.pattern}")
print(f"Affected Cases: {cluster.case_ids}")
print(f"Suggested Fix: {cluster.recommendation}")
- Linux and Windows Commands for Agent Evaluation Infrastructure
Linux Commands for Trajectory Logging and Analysis:
Monitor agent logs in real-time
tail -f /var/log/agent/trajectories.log | jq '.steps[] | {step_type: .step_type, content: .content}'
Aggregate trajectory statistics
cat /var/log/agent/trajectories.log | jq '.total_steps' | awk '{sum+=$1; count++} END {print "Average steps: " sum/count}'
Find trajectories with tool call failures
grep -l '"tool_call"."error"' /var/log/agent/trajectories/.json
Calculate cost per trajectory
cat trajectories.json | jq '.cost' | awk '{sum+=$1; count++} END {print "Average cost: $" sum/count}'
Windows PowerShell Commands:
Parse JSON trajectories and extract tool calls
Get-Content .\trajectories.json | ConvertFrom-Json | Select-Object -ExpandProperty steps | Where-Object { $_.step_type -eq "tool_call" }
Calculate success rate
$trajectories = Get-Content .\trajectories.json | ConvertFrom-Json
$successful = ($trajectories | Where-Object { $_.success -eq $true }).Count
$total = $trajectories.Count
Write-Host "Success rate: $($successful/$total100)%"
Export trajectory summary to CSV
Get-Content .\trajectories.json | ConvertFrom-Json | Select-Object agent_id, session_id, total_steps, cost, success | Export-Csv -Path trajectory_summary.csv
7. Mitigating Judge Variance with Bayesian Calibration
As noted in the comments, the LLM-as-judge models scoring these trajectories are themselves noisy judges, not oracles. A Bayesian layer treating each rubric score as a noisy observation and updating a belief about the true failure rate helps separate genuine trajectory drift from judge variance.
Step-by-Step Guide to Implementing Bayesian Calibration:
- Run multiple evaluations of the same trajectory using different judge models or different prompts.
- Treat each score as an observation with associated confidence.
- Maintain a prior belief about the agent’s true performance based on historical data.
- Update beliefs using Bayesian inference after each evaluation run.
- Use the posterior distribution to make decisions about deployment or rollback.
Code Example (Simplified Bayesian Calibration):
import numpy as np from scipy.stats import beta class BayesianJudge: def <strong>init</strong>(self, prior_alpha=1, prior_beta=1): self.alpha = prior_alpha self.beta = prior_beta def update(self, score, confidence=0.8): Convert score to pseudo-counts self.alpha += score confidence self.beta += (1 - score) confidence def get_posterior_mean(self): return self.alpha / (self.alpha + self.beta) def get_credible_interval(self, confidence=0.95): lower = beta.ppf((1 - confidence) / 2, self.alpha, self.beta) upper = beta.ppf((1 + confidence) / 2, self.alpha, self.beta) return (lower, upper)
What Undercode Say:
- Key Takeaway 1: Evaluating AI agents solely on final answers is dangerously insufficient – an agent can produce a correct output while following a completely broken execution path, including wrong tool selection, malformed parameters, and hallucinated actions.
-
Key Takeaway 2: Trajectory evaluation must become the standard for agentic AI, scoring the full execution path across two pillars: Agent Task Success (behavioral alignment, instruction compliance, hallucination detection) and Tool Use Quality (syntactic precision, parameter correctness, tool selection accuracy).
Analysis: The shift from deterministic testing to trajectory evaluation represents a fundamental paradigm change in how we think about quality assurance for AI systems. Traditional software testing assumes that if the output is correct, the path to that output is irrelevant. For autonomous agents, this assumption is dangerously false. The agent’s execution path matters not just for efficiency, but for safety, cost, and reliability. A trajectory that recovers from errors with bad state drift may produce a correct answer today but fail catastrophically tomorrow. The emergence of open-source frameworks like AgentEvals, agent_trajectory_evaluation, and Google Cloud’s built-in trajectory evaluation tools signals that the industry is waking up to this reality. The next frontier is automating the evaluation flywheel – using evolutionary prompt engineering to continuously improve agent behavior based on trajectory-level feedback.
Prediction:
- +1 Trajectory evaluation will become a mandatory compliance requirement for enterprise AI agents within the next 18 months, similar to how SOC 2 and ISO 27001 became standards for data security.
-
+1 Open-source trajectory evaluation frameworks will converge around a common telemetry format, enabling cross-platform agent benchmarking and interoperability.
-
-1 Organizations that continue to rely on final-output testing will face increasing incidents of agent-induced production failures, with silent state corruption being the most dangerous and hardest-to-detect class of errors.
-
+1 The evaluation flywheel approach will evolve into fully autonomous agent improvement pipelines, where trajectory-level feedback drives continuous prompt optimization without human intervention.
-
-1 The complexity of trajectory evaluation will create a significant skills gap, with many teams lacking the expertise to implement proper trajectory logging and analysis, leading to a bifurcation between organizations that can build reliable agents and those that cannot.
-
+1 Bayesian calibration of LLM judges will become a standard practice, reducing false positives and false negatives in automated evaluation and increasing trust in AI agent deployment decisions.
▶️ Related Video (72% Match):
https://www.youtube.com/watch?v=4u64WEuQHYE
🎯Let’s Practice For Free:
🎓 Live Courses & Certifications:
Join Undercode Academy for Verified Certifications
🚀 Request a Custom Project:
Secure, high-velocity infrastructure and disruptive technological engineering. Contact our engineering team for high-tier development and proprietary systems:
[email protected]
💎 Smart Architecture | 🛡️ Secure by Design | ⭐ Trusted by Thousands
IT/Security Reporter URL:
Reported By: Edong186 If – Hackers Feeds
Extra Hub: Undercode MoN
Basic Verification: Pass ✅


