Sub topic 7
The GenAI semantic conventions
This chapter assumes you know what a span and an attribute are, and that you can set up the Python SDK from the earlier chapters. It covers the naming rules OpenTelemetry defines for spans that describe generative AI work.
What semantic conventions are
A semantic convention is an agreed name and meaning for a span name or attribute. Without one, one team records the model as model, another as llm.model_name, and a backend cannot tell they mean the same thing.
The GenAI conventions define names for model calls, agent runs and tool calls. If you use them, dashboards and analysis tools can read your traces without custom mapping.
Operation names
Every GenAI span carries gen_ai.operation.name. It says what kind of work the span represents.
| Operation | Meaning |
|---|---|
| `chat` | A chat completion call to a model |
| `embeddings` | A call that creates embedding vectors |
| `invoke_agent` | One run of an agent |
| `execute_tool` | One call to a tool |
Span names and kinds
The span name is the operation followed by the target. A model call is named chat example-model. An agent run is named invoke_agent support-agent. A tool call is named execute_tool search_orders.
A call to a remote model service uses SpanKind.CLIENT, because your code is the client of that service. Agent and tool spans that run inside your process are usually internal spans.
Common attributes
Attributes fall into a few groups.
- Request:
gen_ai.request.model, plus optional settings such asgen_ai.request.temperatureandgen_ai.request.max_tokens. - Response:
gen_ai.response.model(the model that actually answered, which can differ from the one you requested) andgen_ai.response.finish_reasons. - Usage:
gen_ai.usage.input_tokensandgen_ai.usage.output_tokens. - Agent and tool:
gen_ai.agent.nameandgen_ai.tool.name. - Provider:
gen_ai.provider.namenames the provider, such as the vendor of the API.
A worked example
This code builds the span name and attributes from one helper. The helper keeps names consistent across your codebase.
from opentelemetry import trace
from opentelemetry.trace import SpanKind
tracer = trace.get_tracer("support_agent")
def start_chat_span(model: str):
span = tracer.start_span(f"chat {model}", kind=SpanKind.CLIENT)
span.set_attribute("gen_ai.operation.name", "chat")
span.set_attribute("gen_ai.request.model", model)
return span
span = start_chat_span("example-model")
span.set_attribute("gen_ai.response.finish_reasons", ["stop"])
span.end()The finish reasons attribute takes a list of strings, because a request can return more than one choice.
Common errors
- Putting the model name in the span name only. Queries filter on attributes, so always set
gen_ai.request.modeltoo. - Inventing near-duplicate names such as
llm.tokens.in. Use the standard name. - Forgetting
span.end()when you usestart_span. Preferstart_as_current_spanin awithblock. - Recording
gen_ai.response.modelfrom the request value. Read it from the response.
Exercise
A tool call in your agent is named search_orders. What should the span name be, and which two attributes should it carry?
Show answer
The span name is execute_tool search_orders. It should carry gen_ai.operation.name set to execute_tool and gen_ai.tool.name set to search_orders.