Sub topic 7
The GenAI semantic conventions
This chapter assumes you can write basic Python and have used an LLM chat API. It also assumes you know what a span is, how to set attributes on it, and how the SDK exports spans. Those topics come from earlier chapters. Here the focus is on what to name things.
Why conventions exist
A span with the attribute model = "gpt" is only useful to the person who wrote it. If one team writes model, another writes llm.model_name, and a third writes ai.model, no shared dashboard or query can work across them.
OpenTelemetry semantic conventions are an agreed set of names and meanings for span names, attributes, and metrics. The GenAI conventions cover generative AI systems: model calls, agents, tools, and token usage. If you follow them, any backend that understands them can show your model calls, group by model, and sum tokens without custom mapping.
The GenAI conventions have been under active development. Some names have changed between versions, and parts are still marked experimental. Check the version of the specification that your instrumentation library targets before you rely on an exact name.
The shape of a GenAI span
Every GenAI span follows the same pattern. It has a required operation name, a provider, and a span name built from the operation and a target. Request attributes describe what you asked for. Response attributes describe what came back. Usage attributes describe the tokens used.
Operation names and span names
The attribute gen_ai.operation.name says what kind of work the span represents. Common values are:
| Value | Meaning |
|---|---|
| `chat` | A chat completion call |
| `text_completion` | A legacy text completion call |
| `embeddings` | An embedding request |
| `invoke_agent` | A run of an agent |
| `execute_tool` | A call to a tool |
The span name is the operation followed by the target, separated by a space. For a model call the target is the model name, so you get chat example-model. For an agent it is the agent name, as in invoke_agent support-agent. For a tool it is the tool name, as in execute_tool search_orders. The names are low in cardinality. They never contain the prompt or a user ID, which keeps the list of distinct span names small enough for a backend to group on.
Request, response and usage attributes
Request attributes record the parameters you sent. Set them before the call, so they exist even if the call fails.
gen_ai.request.model: the model you asked for.gen_ai.request.temperature,gen_ai.request.top_p,gen_ai.request.max_tokens: sampling parameters.
Response attributes record what the provider returned. Set them after the call.
gen_ai.response.model: the model that actually served the request. Providers often resolve an alias such aslatestto a dated version, so this can differ from the request value.gen_ai.response.id: the provider's ID for the completion, useful when you open a support ticket.gen_ai.response.finish_reasons: an array of strings such asstoporlength. Alengthreason means the output was cut off by the token limit.
Usage attributes are gen_ai.usage.input_tokens and gen_ai.usage.output_tokens. A later chapter covers token accounting in detail.
Provider naming
Attributes that identify the provider have changed across versions of the conventions. Older releases used gen_ai.system. Newer releases use gen_ai.provider.name. If you work with an existing library, look at which one it emits. If you mix old and new names, a query written for one will miss spans from the other.
The same caution applies to every attribute in this chapter. Pin the version of your instrumentation package, and read its release notes before you upgrade.
A span that follows the conventions
The following function builds on the pattern from the previous chapters. It sets request attributes before the call and response attributes after. It runs as shown if you have the OpenTelemetry SDK installed. Without an exporter configured, the spans are created but not sent anywhere.
from opentelemetry import trace
from opentelemetry.trace import SpanKind, Status, StatusCode
tracer = trace.get_tracer("support_agent")
def call_model(messages, model, temperature):
# Stand-in for a real LLM client call.
return {
"text": "Hello!",
"response_model": f"{model}-2026-01-15",
"response_id": "resp-123",
"finish_reason": "stop",
"input_tokens": 12,
"output_tokens": 3,
}
def traced_chat(messages, model="example-model", temperature=0.2):
with tracer.start_as_current_span(f"chat {model}", kind=SpanKind.CLIENT) as span:
span.set_attribute("gen_ai.operation.name", "chat")
span.set_attribute("gen_ai.provider.name", "example-provider")
span.set_attribute("gen_ai.request.model", model)
span.set_attribute("gen_ai.request.temperature", temperature)
try:
result = call_model(messages, model, temperature)
except Exception as exc:
span.record_exception(exc)
span.set_status(Status(StatusCode.ERROR, str(exc)))
span.set_attribute("error.type", type(exc).__name__)
raise
span.set_attribute("gen_ai.response.model", result["response_model"])
span.set_attribute("gen_ai.response.id", result["response_id"])
span.set_attribute("gen_ai.response.finish_reasons", [result["finish_reason"]])
span.set_attribute("gen_ai.usage.input_tokens", result["input_tokens"])
span.set_attribute("gen_ai.usage.output_tokens", result["output_tokens"])
return result["text"]
print(traced_chat([{"role": "user", "content": "Hi"}]))Three details matter here:
- The span kind is
CLIENTbecause your process is calling a remote service. - The
error.typeattribute holds a short, stable class name. Backends use it to group failures, so avoid putting the full error message there. finish_reasonsis a list even when there is only one value, because some APIs return several choices.
Agent and tool attributes
The conventions also define attributes for agents and tools, which you saw in the earlier agent examples.
gen_ai.agent.nameandgen_ai.agent.ididentify the agent on aninvoke_agentspan.gen_ai.tool.nameidentifies the tool on anexecute_toolspan.gen_ai.tool.call.idlinks a tool span to the tool call requested by the model, when the API provides an ID.
Those attributes let you filter for "all runs of this agent" or "all calls to this tool" with one query.
Content attributes and opt-in
The conventions also describe how to record prompts and responses, either as span attributes or as events. Because prompts often contain personal or confidential data, these are opt-in. Instrumentation libraries normally leave them off unless you set a flag. The chapter on capturing prompts and responses safely covers what to record and what to redact.
Common mistakes
- Putting variable text in the span name.
chat: summarize this invoice for Danacreates a new span name for every request. Usechat example-model. - Skipping the response model. If you only record the request model, you cannot tell which model version produced a regression.
- Using a string for token counts. Token attributes are integers. A string value breaks sums and averages in most backends.
- Inventing near-duplicate names. Writing
llm.tokens.innext togen_ai.usage.input_tokenssplits your data across two fields. Use the standard name and add custom attributes only for facts the conventions do not cover, under your own prefix such asapp.. - Mixing convention versions. As described above, provider attributes differ between releases.
Exercise
A request asks for the model alias "example-model-latest". The provider answers with a dated model version. Which attribute holds each value, and why record both?
Show answer
The alias goes in genai.request.model and the dated version goes in genai.response.model. Recording both shows which concrete model version served the request, so you can link a change in quality or latency to a silent version change.
Exercise
A teammate names spans like "chat Summarize order 8841 for Dana". What is wrong, and what should the span name and attributes be?
Show answer
The span name contains variable and possibly personal data, so it creates a distinct name per request and leaks content. The name should be "chat <model>", for example "chat example-model", with gen_ai.operation.name set to "chat". Any request-specific detail belongs in attributes, and content should be recorded only if opted in.