Sub topic 2
Context Propagation and Parent-Child Spans
This chapter assumes basic Python, functions, context managers, and familiarity with calling an LLM API. It also assumes you know that a span records one operation inside a trace.
Context propagation and parent-child spans
Context propagation carries the current trace position through function calls, threads, and service boundaries. A span started while another span is current becomes its child. This creates a tree that shows both order and duration.
For an agent, the top-level span can represent one user request. Model calls, retrieval operations, and tool calls become child spans. A child span should describe one meaningful operation rather than an arbitrary code block.
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 call_model(messages):
return {"text": "I found your order.", "input_tokens": 18, "output_tokens": 6}
def run_agent(question):
with tracer.start_as_current_span("invoke_agent support-agent") as agent_span:
agent_span.set_attribute("gen_ai.operation.name", "invoke_agent")
agent_span.set_attribute("gen_ai.agent.name", "support-agent")
result = call_model([{"role": "user", "content": question}])
orders = search_orders("c-42")
return result["text"], orders
print(run_agent("Where is my order?"))Because start_as_current_span makes the new span current inside the with block, the tool span is automatically a child of the agent span.
The GenAI semantic conventions
Semantic conventions are standard attribute names that make telemetry comparable across applications. GenAI conventions identify operations, models, agents, tools, and usage:
| Attribute | Meaning |
|---|---|
| `gen_ai.operation.name` | Operation such as `chat` or `execute_tool` |
| `gen_ai.request.model` | Model requested by the application |
| `gen_ai.usage.input_tokens` | Tokens sent to the model |
| `gen_ai.usage.output_tokens` | Tokens returned by the model |
Convention versions can change, so pin the version used by your instrumentation and backend.
Instrumenting model calls and recording usage
Create a client span around each model request. Set the model name and operation before the call. Add token counts after a successful response. Record exceptions and mark the span as failed when the request raises an error.
Token attributes let you identify expensive prompts, unusually long responses, and retries that multiply cost.
Structuring agent and tool traces
A useful trace might have this shape:
If an agent loops, create a child span for each iteration or model decision. This exposes runaway loops and shows which tool call caused a delay.
Exercise
Where should a tool-call span appear when the agent invokes the tool inside its active span?
Show answer
It should appear as a child of the agent span, because the agent span is the current context when the tool span starts.