Sub topic 11
Tracing tool calls
This chapter assumes you can run the SDK setup from earlier chapters and understand parent-child spans. A tool call is a function the agent runs on the model's behalf, such as a database lookup or an HTTP request. Tool calls are often where agents slow down or fail, so each one should get its own span.
What a tool span records
The GenAI semantic conventions define an execute_tool operation. A tool span should follow these rules:
- Name the span
execute_tool <tool name>, for exampleexecute_tool search_orders. - Set
gen_ai.operation.nametoexecute_tool. - Set
gen_ai.tool.nameto the tool's name. - Leave the span kind as the default
INTERNALwhen the tool is a local function. UseCLIENTif the tool calls a remote service and you want that call shown as an outbound request.
Because start_as_current_span makes the new span current, any tool span started inside the agent span becomes its child.
from opentelemetry import trace
tracer = trace.get_tracer("support_agent")
def search_orders(customer_id):
with tracer.start_as_current_span("execute_tool search_orders") as span:
span.set_attribute("gen_ai.operation.name", "execute_tool")
span.set_attribute("gen_ai.tool.name", "search_orders")
return [{"id": "A-1001", "customer": customer_id}]
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")
orders = search_orders("c-42")
return f"Found {len(orders)} order(s) for your question: {question}"The resulting trace
The agent span is the root. Each tool call appears as a child, and its duration shows how much of the run it used.
Recording failures
A tool that raises an exception should mark its span as an error, the same way the model call span does. The agent may catch the exception and carry on, so without an error status on the tool span the trace looks healthy even though a step failed.
from opentelemetry import trace
from opentelemetry.trace import Status, StatusCode
tracer = trace.get_tracer("support_agent")
def lookup_refund(order_id):
raise TimeoutError("refund service did not respond")
def traced_refund(order_id):
with tracer.start_as_current_span("execute_tool lookup_refund") as span:
span.set_attribute("gen_ai.operation.name", "execute_tool")
span.set_attribute("gen_ai.tool.name", "lookup_refund")
try:
return lookup_refund(order_id)
except Exception as exc:
span.record_exception(exc)
span.set_status(Status(StatusCode.ERROR, str(exc)))
raiseThe raise at the end lets the agent decide how to recover. If it retries, each attempt should produce its own tool span so the trace shows how many tries occurred.
Arguments and results
Tool arguments and results can contain customer data. Treat them like prompt content and do not record them by default. If you need them for debugging, capture them only in non-production environments or after redaction. Record small, safe facts instead, such as the number of rows returned.
Common mistakes
- Starting the tool span outside the agent span's context, which produces a separate root trace.
- Catching an exception inside the tool and returning an error string without setting the span status.
- Using a different span name for every call, such as including an order ID. Put variable values in attributes, not in the name.
Exercise
An agent catches a tool's exception and retries twice before succeeding. How should the trace show this, and why does setting the error status matter?
Show answer
The trace should contain three execute_tool spans under the agent span. The first two have ERROR status and a recorded exception, and the third has no error. Without the error status on the failed attempts, the trace looks like three successful calls, and you cannot see the failures or the extra latency from the retries.