Sub topic 10
Structuring an agent run as a trace
This chapter assumes you can create spans, set attributes, configure a tracer provider, and wrap a single model call with GenAI attributes. Here you will combine those pieces into one trace for a whole agent run.
The shape of an agent trace
An agent run is a loop. The model decides what to do, a tool runs, and the result goes back to the model. This repeats until the model produces a final answer. The trace should mirror that loop.
Use one root span for the whole run. Under it, add one child span per model call and one per tool call, in the order they happen. Because the spans are siblings under the root, the trace shows the loop as a sequence you can read from top to bottom.
Naming the spans
The GenAI conventions give each step an operation name. Put it in gen_ai.operation.name and at the start of the span name.
| Step | Span name | Kind | Useful attributes |
|---|---|---|---|
| Whole run | `invoke_agent support-agent` | INTERNAL | `gen_ai.agent.name` |
| Model call | `chat example-model` | CLIENT | `gen_ai.request.model`, token counts |
| Tool call | `execute_tool search_orders` | INTERNAL | `gen_ai.tool.name` |
The agent and tool spans do not leave your process, so INTERNAL fits. The model call goes over the network, so it is CLIENT.
A runnable example
This example prints spans to the console, so you can run it without a Collector. The model is a stub that asks for one tool call and then answers.
from opentelemetry import trace
from opentelemetry.sdk.resources import Resource
from opentelemetry.sdk.trace import TracerProvider
from opentelemetry.sdk.trace.export import SimpleSpanProcessor, ConsoleSpanExporter
from opentelemetry.trace import SpanKind
provider = TracerProvider(resource=Resource.create({"service.name": "support-agent"}))
provider.add_span_processor(SimpleSpanProcessor(ConsoleSpanExporter()))
trace.set_tracer_provider(provider)
tracer = trace.get_tracer("support_agent")
MAX_STEPS = 5
def fake_model(step):
if step == 0:
return {"tool": "search_orders"}
return {"answer": "You have one open order."}
def chat(step):
with tracer.start_as_current_span("chat example-model", kind=SpanKind.CLIENT) as span:
span.set_attribute("gen_ai.operation.name", "chat")
span.set_attribute("gen_ai.request.model", "example-model")
return fake_model(step)
def run_tool(name):
with tracer.start_as_current_span(f"execute_tool {name}") as span:
span.set_attribute("gen_ai.operation.name", "execute_tool")
span.set_attribute("gen_ai.tool.name", name)
return [{"id": "A-1001"}]
def run_agent(question):
with tracer.start_as_current_span("invoke_agent support-agent") as span:
span.set_attribute("gen_ai.operation.name", "invoke_agent")
span.set_attribute("gen_ai.agent.name", "support-agent")
for step in range(MAX_STEPS):
reply = chat(step)
if "answer" in reply:
span.set_attribute("agent.steps", step + 1)
return reply["answer"]
run_tool(reply["tool"])
span.set_attribute("agent.hit_step_limit", True)
return "Stopped: step limit reached."
print(run_agent("Where is my order?"))
provider.shutdown()Every span created inside run_agent shares one trace ID, and its parent is the agent span. You never pass the parent explicitly, because start_as_current_span handles it.
Common mistakes
- Starting a new root per step. Creating spans outside the agent's
withblock, for example in a background thread without copied context, produces several unconnected traces. - Hiding the loop. If you wrap all iterations in one span, you cannot see which step was slow.
- No loop bound. A runaway agent produces a huge trace. Cap the steps and record when the cap is hit, as the example does.
Exercise
In the example, why does execute_tool search_orders appear as a child of invoke_agent support-agent without any parent argument being passed?
Show answer
start_as_current_span makes the agent span the current span in the active context. Any span started inside that block, including in other functions, takes the current span as its parent.