Skip to main content
For agents built on the higher-level OpenAI Agents SDK instead of the plain client, see OpenAI Agents SDK. Install the integration extra:

Usage

Call patch_openai_client() once after creating your OpenAI client. All subsequent client.chat.completions.create() calls are traced automatically. No changes to individual API calls are needed. Works with both openai.OpenAI and openai.AsyncOpenAI.
Async client:

What gets traced

By default, each non-streaming chat.completions.create() call produces its own trace.
Calls made with stream=True are passed through untraced. Safely wrapping a chunk iterator without disrupting the caller’s own consumption of it needs different handling than a single request/response call, so streaming isn’t covered by this integration yet.
The raw OpenAI SDK has no built-in concept of “tool call” or “retrieval”; those only exist as plain Python code around your chat.completions.create() calls, so the patch can’t see them on its own. Use tracer.trace_tool_call() and tracer.trace_retrieval() to record them manually so they show up in the trace’s performance summary - see the Anthropic integration’s tool-use example for the same pattern (identical API, different client).

Multi-call agentic loops

Like the Anthropic integration, wrap a multi-call tool-use loop in with tracer.trace(...) to collapse every chat.completions.create() call made inside it into one trace instead of one trace per call:
This works because patch_openai_client() checks tracer.current_span on every call: if a span is active on the current thread, the call is attached to it as an "LLM Call N" step; otherwise it sends its own trace as usual.

patch_openai_client() reference

Calling patch_openai_client() on an already-patched client is a no-op; it is safe to call multiple times.
Call tracer.flush() before your process exits in scripts or one-shot jobs. In long-running servers it is not required: traces drain automatically in the background.