AI agent observability: trace the run, not the request
AI agent observability records a whole agent run as one trace, with every model and tool call. What to record, OpenTelemetry span names, and what to alert on.
- Published
- Updated
- Reading time
- 10 min read
In short
- AI agent observability means you can replay one agent run from recorded data: each model call, each tool call, the arguments it passed, and what came back.
- Make the run the unit. Record one trace per run, with an invoke_agent span at the root and a child span for every model call and every execute_tool call.
- The failures that cost the most, such as loops, repeated identical tool calls, and runaway token use, are invisible on any single request. They only show up when you count per run.
- Give every run a hard budget for steps and tokens, enforce it in code, and alert when runs approach it.
AI agent observability is the ability to explain one agent run after the fact from recorded data. Given a run ID, you should be able to list every model call and tool call it made, in order, with the arguments, the results, the tokens, and the time each took. An agent run is a sequence of decisions, and the bad one is often several steps before the visible failure.
Request-level telemetry, covered in what to record on every request, is the foundation. Agents need one more layer on top of it: the run as a single unit, so you can see patterns that no individual request shows.
Why request logs are not enough for agents
A request log shows each model call on its own, and an agent run is many model calls that depend on each other. Every call in a failed run can return HTTP 200 with a sensible-looking response. The failure is in the sequence: the same tool called four times, a context that doubled at every step, or a run that stopped one step short.
Three questions come up in every agent incident, and request logs answer none of them:
- What did the agent do before the step that went wrong?
- How many steps, tokens, and dollars did this one run consume?
- Which runs share the same bad pattern?
Answering them needs every call in a run tied to one identifier, with parent and child relationships intact. That is what a trace gives you.
What one agent run looks like as a trace
The core of agent tracing is one trace per run. The root span represents the run. Under it, each model call is a child span and each tool execution is a child span, in the order they happened. Sub-agents get their own invoke spans nested inside the parent run, so a multi-agent task stays one readable tree.
Read the figure the way you would read an incident. Every span finished, none errored, and each model call took a normal amount of time. The run still failed: it searched for the same thing three times, paid for four model calls, and returned what the first search already gave it. Only the run-level view shows that.
Turns in a conversation are separate runs. Link them with a conversation or session identifier instead of putting a whole conversation into one trace. A trace that spans an hour of chat is hard to read, and the unit you want to compare is the run.
The OpenTelemetry names for agent spans
OpenTelemetry's GenAI conventions define spans for agents and tools, so traces from different frameworks and teams can be read by the same backend. The run is an invoke_agent span. Each tool execution is an execute_tool span. Both, like every GenAI convention, are marked as in development.
The conventions separate where the agent runs. An agent or workflow that runs inside your process gets an internal span. Calling an agent that runs as a separate service, such as an A2A endpoint or a hosted agent API, gets a client span instead, with the same operation name.
| Span | Operation name | Span name | Key attributes |
|---|---|---|---|
| Agent run | invoke_agent | invoke_agent {agent name} | gen_ai.agent.name, gen_ai.conversation.id |
| Planning step | plan | plan {agent name} | gen_ai.agent.name |
| Workflow run | invoke_workflow | invoke_workflow {agent name} | Used when your code, not the model, decides the control flow |
| Remote agent call | invoke_agent | invoke_agent {agent name} | Span kind CLIENT; for agents outside your process |
| Tool call | execute_tool | execute_tool {tool name} | gen_ai.tool.name, gen_ai.tool.call.id, error.type |
| Model call | Inference span | chat {model} | Token usage, model, finish reason |
The conventions also define run-level metrics. gen_ai.invoke_agent.duration measures a whole invocation in your process, and gen_ai.client.invoke_agent.duration times a call to a remote agent. gen_ai.invoke_agent.inference_calls and gen_ai.invoke_agent.tool_calls count model and tool calls per invocation, and gen_ai.execute_tool.duration times each tool. Those counts are the raw material for the alerts later in this article.
One rule in the agent spans spec is easy to miss. gen_ai.conversation.id should come from your application or framework, and instrumentations should not fill it with a generated ID, a trace ID, or a hash of the content. A fake conversation ID looks valid and groups nothing.
The OpenTelemetry project's 2025 post on agent observability describes the two ways this telemetry gets produced. Some frameworks build instrumentation in. Others rely on separate instrumentation libraries. Either works, but check which version of the conventions your framework emits, because names have changed between releases.
What to record on every tool call
Record the tool name, the call ID from the model, the tool type, the duration, the error type if it failed, and the size of the result. Those fields are cheap and safe to keep for every call. The arguments and the result are more useful and more dangerous, and the conventions mark both as opt-in.
The opt-in is right. Tool arguments carry whatever the model decided to send: customer IDs, search queries built from user text, sometimes credentials pulled from context. Tool results can hold entire database rows. Treat them the way the observability article treats prompt text: capture them for a sample and for failed or flagged runs, redact before export, and keep them for days.
Add two attributes of your own that the conventions do not define:
- A hash of the normalized arguments. It lets you detect a repeated identical call without storing the arguments themselves.
- A flag for soft errors. Many tools return a success status with an error message in the body. If the model then continues as if the call worked, the trace shows a success.
import { createHash } from "node:crypto";
import { trace } from "@opentelemetry/api";
const tracer = trace.getTracer("refund-agent");
export async function tracedTool(name: string, callId: string, args: unknown, run: () => Promise<string>) {
return tracer.startActiveSpan(`execute_tool ${name}`, async (span) => {
span.setAttributes({
"gen_ai.operation.name": "execute_tool",
"gen_ai.tool.name": name,
"gen_ai.tool.call.id": callId,
// Lets you spot repeats without storing the arguments.
"app.tool.args_hash": createHash("sha256").update(JSON.stringify(args)).digest("hex").slice(0, 16),
});
try {
const result = await run();
span.setAttributes({
"app.tool.result_bytes": result.length,
"app.tool.soft_error": /"error"\s*:/.test(result),
});
return result;
} finally {
span.end();
}
});
}Failures you can only see per run
This is the part of AI agent observability that request logs cannot replace. Agent failures that matter in production rarely produce an error on any single span. They show up as patterns across a run: repetition, growth, or an ending that came too early. Detect them by aggregating spans per trace, not by reading spans one at a time.
| Failure | What it looks like in a trace | How to detect it |
|---|---|---|
| Loop | The same tool with the same argument hash, called again and again | Count repeats of tool name plus argument hash per run |
| Runaway context | Input tokens grow at every model call | Track input tokens per step and the ratio of last step to first |
| Silent tool error | A tool span succeeds, but its result carries an error message | The soft-error flag, then whether the next model call acknowledged it |
| Wrong tool | A tool that does not fit the task, such as a write tool on a read request | Compare tools used with the tools expected for the task, in evaluation |
| Early stop | The run ends without a tool call the task required | Check final runs against a required-steps list in evaluation |
| Handoff loop | Two sub-agents pass the task back and forth | Count invoke spans per agent name within one run |
Runaway context deserves a closer look, because it degrades quality before it hits a limit. Each step appends tool results to the history, so input tokens per call can grow several times over a long run. The context rot article explains why answers get worse as that happens, and context engineering covers how to keep it down.
The last two rows need a judgment, not a counter. That is where agent tracing meets evaluation: a sampled run, with its full trace, is what an LLM judge or a reviewer grades.
Budgets and alerts for agent runs
Give every agent run a hard budget for steps, tokens, and wall-clock time, and enforce it in code. Then alert on the share of runs that approach the budget. An alert alone tells you about a runaway run after it has spent the money, and a budget alone hides a rising problem until users notice.
Set the budgets from data. Take the distribution of gen_ai.invoke_agent.inference_calls per agent over a normal week, and put the hard stop well above the 99th percentile. A budget at the median kills legitimate long tasks.
Then alert on these signals, per agent:
- The 95th percentile of model calls and tool calls per run.
- Runs that hit the step or token budget, as a share of all runs.
- Repeated identical tool calls per run.
- Tool error rate by tool name, including soft errors.
- Cost per run, which is the number finance will ask about.
When a run hits its budget, end it with a clear status on the root span. A run that was stopped for exceeding its budget is a different event from one that crashed, and you will want to count them separately. Budgets are what turn AI agent observability from a debugging aid into a control.
Tracing agents through a gateway
For AI agent observability, a gateway is a useful second source of spans. It sees every model call and every MCP tool call that passes through it, so it can produce most of an agent trace without changes to the agent's code. What it cannot see is work that happens inside the agent process, such as local functions and planning logic. Instrument both, and let the gateway join the trace your agent started.
Bifrost, an open-source AI gateway by Maxim AI, works this way. Its OpenTelemetry export follows the GenAI conventions and records MCP tool calls as client spans with mcp.method.name and gen_ai.tool.name. A request that arrives with a W3C traceparent header stays on the caller's trace, so the gateway's spans appear inside the agent's own run. Requests that carry an x-bf-session-id header get a session.id on their root span, and the group_traces_by_session option groups a session's requests into one trace when the caller sends no trace context.
For agents registered behind Bifrost over A2A, Agent Logs group operations by the A2A context ID and link the model and MCP calls made during each task. That linking depends on the agent sending its session and correlation headers on its outgoing calls, which is worth checking during setup rather than during an incident.
The combination is the practical answer for most teams. The framework records the plan and local steps. The gateway records the model and tool traffic with consistent names, token counts, and costs across every agent, whichever framework built it. The MCP gateway article covers the tool side in more depth.
Frequently asked questions
What is AI agent observability?
AI agent observability is the practice of recording an agent's runs so that any single run can be explained afterward. It covers each model call, each tool call with its arguments and results, token usage, cost, and timing, all linked in one trace. It differs from request-level monitoring because it treats the whole run, not one call, as the unit.
How is agent observability different from LLM observability?
LLM observability explains a single model call: the model, the input, the tokens, the latency. Agent observability links many of those calls into one run and adds tool calls between them. The questions change from "why was this response slow?" to "why did this run take eleven steps?" Per-run counts, budgets, and repeated-call detection are specific to agents.
What should you trace in an AI agent?
Agent tracing starts with the run as a root span, each model call and tool call as child spans, and each sub-agent as a nested run. On tool calls, record the name, call ID, duration, error type, and result size by default, and the arguments and results on an opt-in basis. Add a conversation ID that links runs in the same session.
How do you detect an AI agent stuck in a loop?
Hash the arguments of every tool call and count how many times the same tool and hash appear in one run. Three or more identical calls is a strong signal. Pair the detection with a hard step budget in code, so a looping run stops instead of running until it hits a timeout or a spending limit.
Does OpenTelemetry support AI agents?
Yes. The OpenTelemetry GenAI semantic conventions define an invoke_agent span for agent runs, an execute_tool span for tool calls, and metrics that count model and tool calls per invocation. They are marked as in development, which means names can still change. Pin the version your instrumentation uses and keep attribute names in one place in your code.
Should you log tool call arguments?
Not for every call by default. Tool arguments and results often contain customer data, and the OpenTelemetry conventions mark both as opt-in. Record a hash of the arguments on every call so you can still detect repeats. Capture full arguments for a sample of runs and for every failed or flagged run, redact them before export, and set a short retention period.
Sources
- 01Semantic conventions for GenAI agent spansGitHub, OpenTelemetry
- 02Client agent invocationsGitHub, OpenTelemetry
- 03Execute toolGitHub, OpenTelemetry
- 04Semantic conventions for generative AI metricsGitHub, OpenTelemetry
- 05AI Agent Observability: Evolving Standards and Best PracticesOpenTelemetry blog, 2025
- 06OpenTelemetryBifrost docs
- 07Agent gateway observabilityBifrost docs