Sub topic 13
Tracing and evaluating multi-agent runs
This chapter assumes you have read the chapters on handoffs, orchestrators, and turn limits. You should be comfortable with Python and JSON. It shows how to see what a multi-agent run did and how to judge whether it did it well.
Why single logs are not enough
A multi-agent run is a chain of decisions. A triage agent hands off to a billing agent. An orchestrator calls a researcher and a writer. If the final answer is wrong, you need to know which step went wrong. The final text alone does not tell you.
A trace is the full record of one run. It is made of spans. A span is one timed step, such as a model call, a tool call, or a handoff. Each span records its parent, so spans form a tree.
What to record
For each span, keep these fields:
- A run ID shared by every span in the run.
- A span ID and a parent span ID.
- The agent name.
- The span type: model, tool, or handoff.
- Start time and duration.
- Inputs and outputs, or a truncated copy of them.
- Token counts and any error.
Record handoffs as their own spans, with the source and target agent. Without them, you cannot tell why the active agent changed.
A minimal tracer
This tracer wraps the agent loop from earlier chapters. It writes one JSON line per span.
import json
import time
import uuid
from contextlib import contextmanager
class Tracer:
def __init__(self):
self.run_id = str(uuid.uuid4())
self.spans = []
self._stack = []
@contextmanager
def span(self, kind: str, agent: str, **data):
span = {
"run_id": self.run_id,
"span_id": str(uuid.uuid4()),
"parent_id": self._stack[-1] if self._stack else None,
"kind": kind,
"agent": agent,
"data": data,
"error": None,
}
start = time.perf_counter()
self._stack.append(span["span_id"])
try:
yield span
except Exception as exc:
span["error"] = repr(exc)
raise
finally:
span["ms"] = round((time.perf_counter() - start) * 1000, 1)
self._stack.pop()
self.spans.append(span)
tracer = Tracer()
with tracer.span("handoff", "triage", target="billing"):
pass
with tracer.span("tool", "billing", name="refund") as s:
s["data"]["output"] = "refund issued"
for s in tracer.spans:
print(json.dumps(s))Wrap each model call, tool call, and handoff in tracer.span(...). Because a worker run started by as_tool happens inside the orchestrator's tool span, its spans nest under it automatically.
Evaluating runs
Tracing shows what happened. Evaluation scores it. Check three levels:
| Level | Question | Example check |
|---|---|---|
| Outcome | Is the final answer correct? | Compare to an expected answer, or use a grader model. |
| Trajectory | Did the agents take a sensible path? | Expected agent sequence, no repeated tool calls. |
| Step | Did each agent do its own job? | Router picked the right target for a labeled message. |
Start with step-level tests for routing, because they are cheap and deterministic. Build a small set of labeled messages and check which agent the triage agent chooses. Then add trajectory checks, such as "a refund request must reach the billing agent within three turns". Use a grader model for outcomes only when exact matching is not possible, and spot-check its scores by hand.
Common errors
- Logging only the final answer. You lose the failing step.
- Storing full inputs with secrets. Traces often contain customer data. Redact or truncate before writing.
- Testing once. Model output varies. Run each test case several times and report a pass rate.
- Ignoring cost. Include token counts so a change that doubles tokens shows up in your evaluation.
Exercise
A refund request ends with a wrong answer. The trace shows triage handed off to the billing agent, and billing called a lookup_order tool that returned the wrong order. Which evaluation level would have caught the problem earliest, and why?
Show answer
Step-level evaluation. A test of the lookup_order call with a known order ID would fail at that step. Outcome checks only report that the final answer is wrong, and the trajectory (triage to billing) looked correct.