Skip to content
mentoor

Topic 2 of 6 · 5 min read

OpenTelemetry GenAI semantic conventions

Goal: Map the fields of a model call to OpenTelemetry GenAI attributes and spot a missing or non-standard attribute in a span.

Name your span attributes with the OpenTelemetry GenAI conventions, gen_ai.*, so any compatible backend can read your traces without a custom mapping.

Check yourself · 1 of 3

Which attribute holds the number of tokens the model read, in the OpenTelemetry GenAI conventions?

Choose one answer

Why it matters

OpenTelemetry (OTel) is the vendor-neutral standard for traces, metrics and logs. Its GenAI semantic conventions say what to call the fields of an LLM call. If you invent llm_tokens_in and modelName, only your own dashboards understand them. If you write gen_ai.usage.input_tokens, Langfuse, Phoenix, Datadog and Honeycomb can all show token usage with no translation. The conventions are still marked as in development, so attribute names can change between versions; pin your instrumentation library version and read its changelog.

The core attributes

For a model call, the span carries:

  • gen_ai.operation.name: what kind of call, such as chat, embeddings or execute_tool.
  • gen_ai.provider.name: who runs the model, such as anthropic or openai.
  • gen_ai.request.model: the model you asked for. gen_ai.response.model is the one that answered, which can differ when an alias resolves to a snapshot.
  • gen_ai.usage.input_tokens and gen_ai.usage.output_tokens: the counts the provider returned.
  • gen_ai.response.finish_reasons: why generation stopped.
  • gen_ai.request.temperature, gen_ai.request.max_tokens: the settings you sent.
  • error.type: set when the call failed.

The span name is the operation and the model, such as chat claude-sonnet. Agents add gen_ai.agent.name and tool spans add gen_ai.tool.name, and gen_ai.conversation.id ties turns of one conversation together.

Content is opt-in

The prompt and the answer are not recorded by default. They live in separate attributes (gen_ai.input.messages, gen_ai.output.messages, gen_ai.system_instructions) that you switch on deliberately, because they hold user data. Many instrumentation libraries use an environment variable for this switch; check your library's documentation for its name.

Before and after

Before:

{ "name": "llm_call", "attributes": { "model": "gpt-x", "tokens": 2210, "ok": true } }

After:

{
  "name": "chat gpt-x",
  "attributes": {
    "gen_ai.operation.name": "chat",
    "gen_ai.provider.name": "openai",
    "gen_ai.request.model": "gpt-x",
    "gen_ai.usage.input_tokens": 1980,
    "gen_ai.usage.output_tokens": 230,
    "gen_ai.response.finish_reasons": ["stop"]
  }
}

The first has one lumped token number and no way to price input and output separately.

What goes wrong

  • Custom names for standard fields. Dashboards and cost tools find nothing.
  • Only a total token count. Input and output are priced differently, so cost cannot be computed.
  • Recording only the requested model. An alias moves to a new snapshot and you cannot see it in the trace.

Examples

Spotting the non-standard attribute

A span has gen_ai.request.model, gen_ai.usage.input_tokens and tokens_out. The last one is custom. Rename it gen_ai.usage.output_tokens and a cost dashboard can read it.

A tool span

{
  "name": "execute_tool lookup_order",
  "attributes": {
    "gen_ai.operation.name": "execute_tool",
    "gen_ai.tool.name": "lookup_order"
  }
}

The same naming style covers model calls and tool calls, so one filter finds both.

Key points

  • Use gen_ai.* attribute names so any OpenTelemetry-compatible backend can read your spans.
  • Record input and output tokens separately: they are priced differently.
  • Record both the requested model and the model that answered, since an alias can resolve to a new snapshot.
  • Prompt and answer content are opt-in attributes, off by default because they hold user data.
  • The GenAI conventions are still in development: pin instrumentation versions and check changelogs.

Common mistakes

  • Inventing attribute names when a standard one exists.
  • Logging a single total token count.
  • Switching on content capture everywhere without a redaction plan.
  • Assuming the attribute names never change while the conventions are in development.

Check yourself · 2 of 3

Why use standard gen_ai.* names instead of your own?

Choose one answer

Check yourself · 3 of 3

By default, are prompt and answer text recorded in GenAI spans?

Choose one answer

Practice items, not counted towards your mastery.

Start here

More for this topic