Auto-instrument OpenAI with OpenInference
OpenInference is a set of OpenTelemetry instrumentations specific to AI libraries. It hooks the OpenAI client and emits a span for every chat completion, with attributes for model, prompt tokens, completion tokens, and the messages themselves. You write zero tracing code to get started.
from phoenix.otel import register
from openinference.instrumentation.openai import OpenAIInstrumentor
def init_tracing(project_name: str = "observability-course") -> None:
tracer_provider = register(
project_name=project_name,
endpoint="http://localhost:6006/v1/traces",
auto_instrument=False,
)
OpenAIInstrumentor().instrument(tracer_provider=tracer_provider)Two lines of real work. register wires the OTel exporter at the Phoenix endpoint, and the OpenAI instrumentor hooks every chat.completions.create call.
from session import EcommerceSession
from observability.tracing import init_tracing
def main() -> None:
init_tracing() # register tracer + instrument OpenAI
session = EcommerceSession()
print(f"[main] Session ID: {session.session_id}")
# ... rest of CLICall init_tracing exactly once at process start. Every OpenAI call anywhere in the codebase is now traced, no edits to the tool code.
Run the demo with make demo. Open Phoenix, pick your project, and you will see one trace per turn with an LLM span inside. Click a span to see the messages, the model name, the usage tokens, and the exact latency.
Quiz: Quiz
Loading practice…