AI Transparency
No AI events yet
POST to /v1/ai with your LLM call details.
Manual baseline (portable, vendor-neutral)
# pip install opentelemetry-sdk opentelemetry-exporter-otlp-proto-http
import json
import time
from opentelemetry import trace
from opentelemetry.exporter.otlp.proto.http.trace_exporter import OTLPSpanExporter
from opentelemetry.sdk.resources import Resource
from opentelemetry.sdk.trace import TracerProvider
from opentelemetry.sdk.trace.export import BatchSpanProcessor
from opentelemetry.trace import Status, StatusCode
resource = Resource.create({"service.name": "my-app"})
provider = TracerProvider(resource=resource)
provider.add_span_processor(
BatchSpanProcessor(
OTLPSpanExporter(endpoint="http://YOUR_SOBS_HOST/v1/traces")
)
)
trace.set_tracer_provider(provider)
tracer = trace.get_tracer("my-app.genai")
def tracked_llm_call(user_prompt: str, model: str = "gpt-4o", provider_name: str = "openai"):
start = time.monotonic()
with tracer.start_as_current_span("gen_ai.chat") as span:
span.set_attribute("gen_ai.operation.name", "chat")
span.set_attribute("gen_ai.provider.name", provider_name)
span.set_attribute("gen_ai.request.model", model)
span.set_attribute(
"gen_ai.system_instructions",
"You are a concise observability assistant.",
)
input_messages = [
{"role": "system", "content": "You are a concise observability assistant."},
{"role": "user", "content": user_prompt},
]
span.set_attribute("gen_ai.input.messages", json.dumps(input_messages, ensure_ascii=False))
try:
# ... your provider SDK/API call ...
assistant_text = "..."
tokens_in = 50
tokens_out = 120
output_messages = [{"role": "assistant", "content": assistant_text}]
span.set_attribute("gen_ai.output.messages", json.dumps(output_messages, ensure_ascii=False))
span.set_attribute("gen_ai.usage.input_tokens", tokens_in)
span.set_attribute("gen_ai.usage.output_tokens", tokens_out)
span.set_attribute("gen_ai.response.finish_reason", "stop")
span.set_attribute("gen_ai.response.latency_ms", round((time.monotonic() - start) * 1000, 1))
return assistant_text
except Exception as exc:
span.set_attribute("error.type", exc.__class__.__name__)
span.set_attribute("error.message", str(exc))
span.set_status(Status(StatusCode.ERROR, str(exc)))
raise
Auto instrumentation (open source friendly)
# Shared HTTP transport auto instrumentation (works across OpenAI, Anthropic, Groq clients using httpx)
# pip install opentelemetry-instrumentation-httpx
from opentelemetry.instrumentation.httpx import HTTPXClientInstrumentor
HTTPXClientInstrumentor().instrument()
# Provider-specific GenAI auto instrumentation (OSS)
from openinference.instrumentation.anthropic import AnthropicInstrumentor
from openinference.instrumentation.groq import GroqInstrumentor
# pip install \
# openinference-instrumentation-openai \
# openinference-instrumentation-anthropic \
# openinference-instrumentation-groq
from openinference.instrumentation.openai import OpenAIInstrumentor
OpenAIInstrumentor().instrument()
AnthropicInstrumentor().instrument()
GroqInstrumentor().instrument()
# then create your SDK clients normally (openai/anthropic/groq)
Tip: keep one manual span wrapper around your top-level agent turn even when using auto instrumentation.
Auto instrumentation captures provider/tool transport details, while the manual wrapper preserves your app-level turn context.