Skip to content

Quickstart: OpenTelemetry OpenAI instrumentation

opentelemetry-instrumentation-openai-v2 is OpenTelemetry’s own Python instrumentation for the OpenAI client. It records each chat call as a GenAI span. This page sends those spans to Mibo and tests the tools your app calls.

  • A Mibo account with a verified email. Open the verification link in the browser you signed up with.
  • Python 3.14 and uv.
  • An OpenAI API key.

The instrumentation is a pre-release. Mibo’s ingest checks use opentelemetry-instrumentation-openai-v2 2.4b0 with opentelemetry-util-genai 0.4b0. Version 2.4b0 fails to import with opentelemetry-util-genai 1.0 or later, so pin 0.4b0. The install line pins httpx 0.28.1 because the instrumentation imports httpx, which current openai releases no longer install. Tested with Python 3.14.9 and these versions.

If you own the workspace, create an API key in Settings and copy it. Mibo shows the key only once. If you’re a workspace member, ask the owner for a key.

Terminal window
uv venv --python 3.14
uv pip install opentelemetry-instrumentation-openai-v2==2.4b0 opentelemetry-util-genai==0.4b0 \
openai httpx==0.28.1 opentelemetry-sdk==1.45.2 opentelemetry-exporter-otlp-proto-http==1.45.2

3. Configure content capture and the exporter

Section titled “3. Configure content capture and the exporter”

The instrumentation does not record prompts or replies by default. Mibo needs the user input and the final reply, so turn capture on.

Terminal window
export OTEL_SEMCONV_STABILITY_OPT_IN=gen_ai_latest_experimental
export OTEL_INSTRUMENTATION_GENAI_CAPTURE_MESSAGE_CONTENT=span_only
export OTEL_SERVICE_NAME=openai-support
export MIBO_OTLP_TRACES_ENDPOINT=https://api.mibo-ai.com/public/traces
export MIBO_API_KEY='<YOUR_MIBO_API_KEY>'
export OPENAI_API_KEY='<YOUR_OPENAI_API_KEY>'
  • span_only writes message content to span attributes. The other capture modes put content in events, which is a different export shape. Use span_only unless you have checked the others against your traces.
  • With a workspace key, Mibo routes by OTEL_SERVICE_NAME and creates an Agent on the first trace if your plan has a free slot. See trace routing.

Save the following as support.py.

import json
import os
from openai import OpenAI
from opentelemetry import trace
from opentelemetry.exporter.otlp.proto.http.trace_exporter import OTLPSpanExporter
from opentelemetry.instrumentation.openai_v2 import OpenAIInstrumentor
from opentelemetry.sdk.resources import Resource
from opentelemetry.sdk.trace import TracerProvider
from opentelemetry.sdk.trace.export import BatchSpanProcessor
provider = TracerProvider(
resource=Resource.create({"service.name": os.environ["OTEL_SERVICE_NAME"]}),
)
provider.add_span_processor(
BatchSpanProcessor(
OTLPSpanExporter(
endpoint=os.environ["MIBO_OTLP_TRACES_ENDPOINT"],
headers={"x-api-key": os.environ["MIBO_API_KEY"]},
timeout=10,
)
)
)
trace.set_tracer_provider(provider)
OpenAIInstrumentor().instrument(tracer_provider=provider)
client = OpenAI(timeout=30, max_retries=1)
TOOLS = [
{
"type": "function",
"function": {
"name": "lookup_order",
"description": "Look up shipping status by order ID.",
"parameters": {
"type": "object",
"properties": {"order_id": {"type": "string"}},
"required": ["order_id"],
},
},
}
]
def lookup_order(order_id: str) -> str:
return {"A-1042": "shipped"}.get(order_id, "unknown")
def answer(question: str) -> str:
messages: list[object] = [
{"role": "system", "content": "You are an order support assistant."},
{"role": "user", "content": question},
]
for _ in range(3):
response = client.chat.completions.create(
model="gpt-5.4-mini", messages=messages, tools=TOOLS
)
message = response.choices[0].message
if not message.tool_calls:
return message.content or ""
messages.append(message.model_dump(exclude_none=True))
for call in message.tool_calls:
result = lookup_order(**json.loads(call.function.arguments))
messages.append({"role": "tool", "tool_call_id": call.id, "content": result})
return ""
tracer = trace.get_tracer(__name__)
try:
with tracer.start_as_current_span("support_request"):
print(answer("What is the status of order A-1042?"))
finally:
provider.shutdown()

Run the script.

Terminal window
uv run --no-project python support.py

Open Traces, select the Agent by its current name, and find lookup_order under What the agent did. Select View raw trace data to inspect the GenAI attributes.

The wrapping support_request span gives the trace a root, so the whole request appears as one trace. Name it for the step your users see.

  1. Wait for the trace to finish, then click Create test from this trace.
  2. Set Name to Looks up the order and clear Checks a trace when so the test checks each new trace for this Agent.
  3. Keep the Tool call check, with Tool name lookup_order and Must call. Remove the other checks and click Save test.
  4. Run the script again. The new trace shows the result under Test results.
Data Source
User input and final reply gen_ai.input.messages and gen_ai.output.messages on the chat span
Tool calls tool_call parts in the model’s output message. Mibo creates a tool span for each call.
Tool results tool_call_response parts in the next call’s input messages
Token usage gen_ai.usage.input_tokens and gen_ai.usage.output_tokens
Model and provider gen_ai.request.model and the provider name
HTTP status and error http.response.status_code and error.type when present

Known gaps:

  • The instrumentation does not emit reasoning-token or cache-read token counts, so checks on those counts have no data.
  • Tool calls exist only as message parts. Mibo infers the tool span from the model’s output, so it records no tool error, and the span reuses the chat call’s time range.
  • A tool result appears only after your app sends it back in a later model call. A last call that requests a tool and never receives its result has no result to check.
  • Without content capture, Mibo has no input or reply to check.
Problem Likely cause Solution
ImportError when importing the instrumentation opentelemetry-util-genai 1.0 or later is installed, or httpx is missing Install opentelemetry-util-genai==0.4b0 and httpx==0.28.1.
The trace has no prompts or replies Content capture is off Set OTEL_SEMCONV_STABILITY_OPT_IN=gen_ai_latest_experimental and OTEL_INSTRUMENTATION_GENAI_CAPTURE_MESSAGE_CONTENT=span_only before the process starts.
No tool calls appear The model made no tool call, or capture is off Check gen_ai.output.messages in the raw trace data for a tool_call part.
HTTP 401 Missing, invalid, or revoked key Set MIBO_API_KEY to an active key. An expired key returns API_KEY_EXPIRED.
404 or connection error Wrong host or path Set MIBO_OTLP_TRACES_ENDPOINT to https://api.mibo-ai.com/public/traces.
HTTP 400 with “OTLP traces need the ‘service.name’ resource attribute” The export has no service.name Set OTEL_SERVICE_NAME and pass it as a resource attribute as shown above.
Export fails with a gRPC transport error The exporter uses OTLP/gRPC Use the opentelemetry.exporter.otlp.proto.http exporter.
The script prints a reply but no trace appears Spans were buffered when the process exited Call provider.shutdown() before exit.
403 AGENT_LIMIT_REACHED Your plan has reached its Agent limit See Agent limit reached.
HTTP 403 with “Monthly trace ingestion limit reached” The workspace used its monthly trace quota Wait for the next period or upgrade, then rerun the script.
HTTP 413 with “Trace payload too large (max 10 MB)” The batch exceeds 10 MB Reduce max_export_batch_size. See Payload size limits.