liveBook · Manning

Trajectory

As explained in Build an AI Agent (From Scratch)

An execution trace is a chronological record of events and intermediate activity produced during program or agent execution.

A trajectory is the path an agent takes toward a goal: its intermediate reasoning steps, tool-call sequence, and final response. An execution trace records that path for a single request. A correct answer alone cannot show whether the agent followed a reasonable, efficient route or succeeded despite a mistake. section 10.2.1 section 10.1.1

A trace can be broken into spans and events. Spans represent work such as an LLM call, tool invocation, memory read, or planning step. Events mark occurrences within that work, such as an exception or the start of a retry. Metrics show measurable behavior; logs record details; traces connect the activity into a request-wide flow. section 10.1.1

Use trajectories at two levels:

  • Debug one run: inspect the trace to find where reasoning or tool use went wrong, rather than judging only the final answer. section 4.8.2
  • Evaluate many runs: collect traces for representative requests, review recurring failures, and turn the most important patterns into evaluation criteria. section 10.2 section 10.2.1

A plausible answer can hide a bad path

Suppose a tool call fails, but the agent claims it succeeded and continues to a plausible answer. Reviewing the trace exposes the first error: tool-result hallucination. Likewise, an agent may reach the right answer only after unnecessary calls or backtracking. Both cases require examining the path, not just the output. section 10.2 section 10.2.1

Listing 4.34 Debugging with execution traces
from scratch_agents.utils import display_trace

agent = create_gaia_agent(models[0], mcp_tools)
result = await agent.run(
    "If Eliud Kipchoge could maintain his marathon pace, "
    "how many thousand hours to reach the Moon?"
)

print(f"Answer: {result.output.final_answer}")
print(f"Steps: {result.context.current_step}")

display_trace(result.context)

This Python example runs a tool-enabled agent, prints its answer and step count, then passes the result’s execution context to display_trace() for inspection. section 4.8.2

How Build an AI Agent (From Scratch) covers it

How deep it goes
Defines request-wide traces, spans, and events, then gives a trace-inspection call and a trace-based evaluation workflow.
Examples
Shows a Python display_trace() call and examines failed tool calls, unnecessary calls, and backtracking.
What you can do after
Diagnose/debug with it by locating failures in a run, then evaluate/understand recurring path errors across representative requests.
How it connects
Connects trajectories to observability, tool calls, error analysis, failure taxonomies, and evaluation rubrics.

Also explained in

Compare how 19 books explain trajectory →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Build an AI Agent (From Scratch). The section links point to the parts of the book it draws on.