Browse docs
On this page

Send GenAI telemetry

Set up OpenTelemetry so your language model calls, agent steps and evaluation scores reach AI Observability - what to install for each model client and framework, what each attribute lights up, and how to check it is arriving.

Turning on the AI Observability add-on does not collect anything by itself. The AI pages fill in once your services send OpenTelemetry traces that describe each model call. This page covers what that takes, service by service.

What you need

For each service that calls a language model:

  1. An ingest key. Create one from Connect Your Data in the app. Any application key you already use works too.
  2. The OpenTelemetry SDK, exporting traces over OTLP/HTTP to your workspace. Traces are what matters here. A service that only sends logs or metrics shows nothing on the AI pages.
  3. GenAI instrumentation for your model client or framework. This is the part most setups are missing. The plain OpenTelemetry SDK traces your HTTP requests, but it does not know what a model call is. The instrumentation turns each call into a span that names the model and counts the tokens.
  4. A service name (OTEL_SERVICE_NAME), so usage and cost split by the feature that spent them.

If you already run an OpenTelemetry Collector, point the service at the Collector instead of at us, and make sure the Collector's traces pipeline exports to your workspace. See OpenTelemetry Collector.

What each attribute lights up

The instrumentation sets these attributes for you. The table is here so you can tell why a number is empty.

AttributeNeeded for
gen_ai.request.model or gen_ai.response.modelRequired. A span counts as a model call only when it names a model.
gen_ai.usage.input_tokens, gen_ai.usage.output_tokensToken counts, tokens per call and the cost estimate.
gen_ai.provider.nameThe provider breakdown.
gen_ai.operation.nameThe kind of call (chat, embeddings, text completion), and agent and tool steps.
gen_ai.usage.cache_read.input_tokens, gen_ai.usage.cache_creation.input_tokensPrompt cache hit rate.
gen_ai.response.finish_reasonsTruncated replies and replies a content filter stopped.
error.type and the span's error statusError rate and failures by error type.
gen_ai.agent.name, gen_ai.tool.nameNamed agent runs and tool steps. See Agents and tools.
gen_ai.conversation.idGrouping calls into conversations.
service.nameThe service breakdown.

Instrumentation that still sends the older names, gen_ai.system, gen_ai.usage.prompt_tokens and gen_ai.usage.completion_tokens, works too.

Count each call once

Every span that names a model is counted as a model call, and its gen_ai.usage.* tokens are added to the totals, even when it is nested inside another span that does the same. So put the model and the usage on exactly one span per unit of work, normally the span for the call itself. If a wrapper span (your own task.run, say) and the invoke_agent span inside it both carry gen_ai.request.model and gen_ai.usage.*, that call and its tokens are counted twice, and so is its cost.

A span that wraps model calls can still record them. Give its copies your own names, such as app.model or app.total_tokens, rather than the gen_ai.* ones. The same applies to instrumentation: instrument the framework or the model client underneath it, not both, unless you have checked that only one of them records the model and usage.

Pick your instrumentation

You call models withPythonNode.js
OpenAIopentelemetry-instrumentation-openai-v2@traceloop/instrumentation-openai
Anthropicopentelemetry-instrumentation-anthropic@traceloop/instrumentation-anthropic
AWS Bedrockopentelemetry-instrumentation-bedrock@traceloop/instrumentation-bedrock
Google Vertex AIopentelemetry-instrumentation-vertexai@traceloop/instrumentation-vertexai
LangChainopentelemetry-instrumentation-langchain@traceloop/instrumentation-langchain
LlamaIndexopentelemetry-instrumentation-llamaindex@traceloop/instrumentation-llamaindex

The OpenAI package for Python is the OpenTelemetry project's own. The others come from OpenLLMetry, an open-source set of instrumentations that follow the same conventions. OpenLIT works too, and instruments many clients from one package.

If you use a framework, instrument the framework rather than the model client underneath it. You get the agent and tool steps as well as the model calls.

Python

Install the OpenTelemetry distribution, the OTLP exporter and the instrumentation for your client:

Shell
pip install opentelemetry-distro opentelemetry-exporter-otlp-proto-http \
  opentelemetry-instrumentation-openai-v2

Then start your service through opentelemetry-instrument. No code changes are needed:

Shell
export OTEL_SERVICE_NAME=support-bot
export OTEL_EXPORTER_OTLP_ENDPOINT=https://ingest.obistack.com/ingest/otel
export OTEL_EXPORTER_OTLP_PROTOCOL=http/protobuf
export OTEL_EXPORTER_OTLP_HEADERS="Authorization=Bearer%20${LOGS_API_KEY}"
export OTEL_TRACES_EXPORTER=otlp
export OTEL_METRICS_EXPORTER=otlp
export OTEL_LOGS_EXPORTER=otlp
export OTEL_PYTHON_LOGGING_AUTO_INSTRUMENTATION_ENABLED=true

opentelemetry-instrument python app.py

Every client.chat.completions.create(...) call in app.py now produces a chat <model> span. For another client or framework, install its package from the table above instead. opentelemetry-instrument picks it up the same way.

The two logging settings send your Python logs too, which is how evaluation scores arrive. Set OTEL_LOGS_EXPORTER=none if you do not record scores and do not want your logs sent.

Node.js

Start the OpenTelemetry SDK with the instrumentations for your clients before your application loads them:

Shell
npm install @opentelemetry/sdk-node @opentelemetry/exporter-trace-otlp-proto \
  @traceloop/instrumentation-openai @traceloop/instrumentation-anthropic
JavaScript
// otel.cjs - loaded before the application with: node --require ./otel.cjs app.js
const { NodeSDK } = require("@opentelemetry/sdk-node");
const { OTLPTraceExporter } = require("@opentelemetry/exporter-trace-otlp-proto");
const { OpenAIInstrumentation } = require("@traceloop/instrumentation-openai");
const { AnthropicInstrumentation } = require("@traceloop/instrumentation-anthropic");

new NodeSDK({
  traceExporter: new OTLPTraceExporter(),
  instrumentations: [new OpenAIInstrumentation(), new AnthropicInstrumentation()],
}).start();
Shell
export OTEL_SERVICE_NAME=support-bot
export OTEL_EXPORTER_OTLP_ENDPOINT=https://ingest.obistack.com/ingest/otel
export OTEL_EXPORTER_OTLP_HEADERS="Authorization=Bearer%20${LOGS_API_KEY}"
export TRACELOOP_TRACE_CONTENT=false

node --require ./otel.cjs app.js

The SDK has to start before your code imports the model client, which is what --require does. If the client is loaded first, its calls are not traced.

TRACELOOP_TRACE_CONTENT=false keeps prompts and replies out of the spans. OpenLLMetry records them unless you set it. See Prompts and replies.

Agents and tools

An agent framework that follows the conventions also sends an invoke_agent <name> span around each run and an execute_tool <tool> span for each tool call, with gen_ai.agent.name and gen_ai.tool.name. Those spans give an agent run its named steps, and agent run success and tool failure rates are worked out from them. Model calls without them still show as runs, one per trace.

If you write your own agent loop, you can add those spans yourself with the OpenTelemetry API. Name them invoke_agent <agent> and execute_tool <tool>, set gen_ai.operation.name to invoke_agent or execute_tool, and set the agent or tool name. In Python:

Python
from opentelemetry import trace

tracer = trace.get_tracer("support-bot")

with tracer.start_as_current_span("invoke_agent support") as run:
    run.set_attribute("gen_ai.operation.name", "invoke_agent")
    run.set_attribute("gen_ai.agent.name", "support")
    reply = client.chat.completions.create(...)  # traced by the instrumentation
    with tracer.start_as_current_span("execute_tool lookup_order") as step:
        step.set_attribute("gen_ai.operation.name", "execute_tool")
        step.set_attribute("gen_ai.tool.name", "lookup_order")
        lookup_order(order_id)

Leave the model and token attributes off spans you add like this. The model call inside already carries them, so setting them again counts that call twice. See Count each call once.

Set gen_ai.conversation.id on each call when your application has a conversation or thread id, and the Agents & Tools dashboard groups calls into conversations.

Evaluation scores

Scores come from your evaluator, whether an LLM judge, a heuristic or a thumbs up from a user. For each result, emit a gen_ai.evaluation.result event on your logs pipeline, inside the trace of the call it judged where you can (see Scores that arrive later), with:

  • gen_ai.evaluation.name, such as relevance or user_feedback
  • gen_ai.evaluation.score.value, a number on your evaluator's scale
  • gen_ai.evaluation.score.label, such as relevant or thumbs_down

In Python, with logs exported as in the Python setup above:

Python
import logging

logger = logging.getLogger("evaluations")

logger.info(
    "relevance scored 0.86",
    extra={
        "event.name": "gen_ai.evaluation.result",
        "gen_ai.evaluation.name": "relevance",
        "gen_ai.evaluation.score.value": 0.86,
        "gen_ai.evaluation.score.label": "relevant",
    },
)

Write it while the call's span is current, so it joins that call's trace.

Scores that arrive later

A human review, a thumbs down from a user, or a merged pull request often comes minutes or hours after the call, when its trace has long finished. Send the score when it arrives anyway:

  • It always counts. The evaluation panels and alerts group scores by evaluation name and label, not by trace, so a score with no trace still counts. It is counted at the time it is sent, not the time of the call.
  • Add gen_ai.conversation.id if you have one. Then you can search a conversation's calls and scores together.
  • Add the trace ID if you want the score tied to the exact call it judged, so a search for that trace finds the score with the call. Keep the trace and span IDs with the reply when you make the call, and put them back when the score arrives. A conversation ID alone does not do this.

In Python, save the IDs while the call's span is current, then use them as the parent when the score is sent:

Python
from opentelemetry import trace
from opentelemetry.trace import NonRecordingSpan, SpanContext, TraceFlags

# When the call is made: keep these with the reply.
ctx = trace.get_current_span().get_span_context()
saved = {"trace_id": format(ctx.trace_id, "032x"), "span_id": format(ctx.span_id, "016x")}

# Later, when the user gives a thumbs down:
call = SpanContext(
    trace_id=int(saved["trace_id"], 16),
    span_id=int(saved["span_id"], 16),
    is_remote=True,
    trace_flags=TraceFlags(TraceFlags.SAMPLED),
)
with trace.use_span(NonRecordingSpan(call)):
    logger.info(
        "user_feedback thumbs_down",
        extra={
            "event.name": "gen_ai.evaluation.result",
            "gen_ai.evaluation.name": "user_feedback",
            "gen_ai.evaluation.score.value": 0,
            "gen_ai.evaluation.score.label": "thumbs_down",
            "gen_ai.conversation.id": conversation_id,
        },
    )

This adds no span to the finished trace. It only sets the trace and span IDs on the score.

Check it is arriving

Make one model call from your service, then run this in the Log Explorer over the last 15 minutes:

_signal=span gen_ai.request.model=* | stats count() by service.name, gen_ai.request.model

A row for your service and model means the AI pages have what they need. If nothing comes back, search _signal=span service.name=support-bot, using your own service name:

  • No spans at all: traces are not reaching your workspace. Check the endpoint, the key, and that OTEL_TRACES_EXPORTER is not none. Behind a Collector, check that the traces pipeline exports to your workspace.
  • Spans, but none name a model: the GenAI instrumentation is not loaded. Check that its package is installed in the same environment, and in Node.js that the SDK starts before the model client is imported.

Something is missing

What you seeWhy, and what to do
Calls show, but no tokens or costThe instrumentation is not recording usage. For streamed OpenAI replies, OpenAI only returns token counts when you ask for them with stream_options={"include_usage": True}.
A model's cost shows as not pricedThe model is not in the public price list the estimate uses, often a fine-tuned or self-hosted model. Its calls and tokens are still counted. See About the cost estimate.
Prompt cache hit rate is always zeroThe instrumentation does not report cache tokens, or the provider's prompt caching is not in use.
Agent runs have no named stepsNothing sends invoke_agent or execute_tool spans. Instrument the framework, or add them yourself as in Agents and tools.
Calls, tokens or cost look doubledMore than one span per call names the model or carries gen_ai.usage.*, often a wrapper or agent span around the call, or the framework and the model client both instrumented. See Count each call once.
No evaluation scoresThe events are not reaching the logs pipeline. Check that logs are exported, not set to none.
Everything is under one serviceOTEL_SERVICE_NAME is not set, or is the same for every service.