Send traces with OpenInference
Send traces from applications instrumented with OpenInference.
This guide sends traces from an application instrumented with OpenInference, which has instrumentations for OpenAI, Anthropic, LangChain, LlamaIndex, DSPy and other libraries. Tracing reads the model, tokens and messages of LLM spans, and the input and output of chain, tool and retriever spans.
Before you start
You need an API key with the traces:write scope. Create one under Settings in the dashboard, or run div login.
Send traces
-
Install the OpenTelemetry SDK, the OTLP exporter and the instrumentation for your library. This example uses OpenAI’s.
pip install opentelemetry-sdk opentelemetry-exporter-otlp-proto-http openinference-instrumentation-openai -
Set the exporter’s endpoint and key.
export OTEL_EXPORTER_OTLP_ENDPOINT=https://api.divergentlabs.xyz/api/otlp export OTEL_EXPORTER_OTLP_HEADERS="Authorization=Bearer%20$DIVERGENT_API_KEY" export OTEL_SERVICE_NAME=my-app -
Set up the tracer provider and instrument the library.
from opentelemetry import trace from opentelemetry.sdk.trace import TracerProvider from opentelemetry.sdk.trace.export import BatchSpanProcessor from opentelemetry.exporter.otlp.proto.http.trace_exporter import OTLPSpanExporter from openinference.instrumentation.openai import OpenAIInstrumentor provider = TracerProvider() provider.add_span_processor(BatchSpanProcessor(OTLPSpanExporter())) trace.set_tracer_provider(provider) OpenAIInstrumentor().instrument(tracer_provider=provider) -
Run your application, then list the traces.
div traces --since 10m
Sessions and users
OpenInference’s using_session and using_user context managers set session.id and user.id, which tracing reads:
from openinference.instrumentation import using_session, using_user
with using_session(session_id), using_user(user_id):
client.chat.completions.create(model="gpt-4o-mini", messages=messages)
Keep content out of traces
Set OPENINFERENCE_HIDE_INPUTS=true and OPENINFERENCE_HIDE_OUTPUTS=true.