Skip to main content
Phoenix provides auto-instrumentation for the OpenAI Python SDK and OpenAI Node.js SDK, allowing you to trace all calls to OpenAI’s APIs including GPT models, embeddings, and function calling.
Also works with Azure OpenAI: This instrumentation is compatible with Azure OpenAI endpoints.

Installation

pip install openinference-instrumentation-openai openai

Setup

1

Set your OpenAI API key

export OPENAI_API_KEY=sk-...
2

Register Phoenix tracer

from phoenix.otel import register

# Configure the Phoenix tracer
tracer_provider = register(
  project_name="my-llm-app",  # Default is 'default'
  auto_instrument=True  # Auto-instrument based on installed packages
)
3

Use OpenAI as normal

import openai

client = openai.OpenAI()
response = client.chat.completions.create(
    model="gpt-4o",
    messages=[{"role": "user", "content": "Write a haiku about observability."}],
)
print(response.choices[0].message.content)

What Gets Traced

The OpenAI instrumentation automatically captures:
  • Chat Completions: Model, messages, temperature, tokens, latency
  • Embeddings: Model, input text, embedding dimensions
  • Function Calling: Tool definitions, function calls, responses
  • Streaming: Token-by-token streaming completions
  • Errors: Rate limits, API errors, exceptions

Advanced Examples

Function Calling

import openai
import json

client = openai.OpenAI()

# Define tools
tools = [
    {
        "type": "function",
        "function": {
            "name": "get_weather",
            "description": "Get the current weather for a location",
            "parameters": {
                "type": "object",
                "properties": {
                    "location": {"type": "string", "description": "City name"},
                    "unit": {"type": "string", "enum": ["celsius", "fahrenheit"]}
                },
                "required": ["location"]
            }
        }
    }
]

response = client.chat.completions.create(
    model="gpt-4o",
    messages=[{"role": "user", "content": "What's the weather in San Francisco?"}],
    tools=tools,
    tool_choice="auto"
)

# Phoenix captures the tool definition, selection, and execution
print(response.choices[0].message.tool_calls)

Streaming Completions

import openai

client = openai.OpenAI()

stream = client.chat.completions.create(
    model="gpt-4o",
    messages=[{"role": "user", "content": "Count to 10."}],
    stream=True,
)

for chunk in stream:
    if chunk.choices[0].delta.content:
        print(chunk.choices[0].delta.content, end="")

# Phoenix captures the full streamed response

Embeddings

import openai

client = openai.OpenAI()

response = client.embeddings.create(
    model="text-embedding-3-small",
    input="Phoenix makes observability easy",
)

print(f"Embedding dimension: {len(response.data[0].embedding)}")

Observability in Phoenix

Once instrumented, you can:
  • View all OpenAI calls in the Phoenix traces tab
  • Monitor token usage and costs per request
  • Track latency for each completion
  • Inspect prompts and responses for debugging
  • Analyze function calling patterns and tool usage
  • Debug errors like rate limits and timeouts

Resources

Example Notebook

Complete Python tutorial

OpenInference Package

View source code

Working Examples

More code examples

OpenAI Agents SDK

Instrument OpenAI Agents

Build docs developers (and LLMs) love