Docs/Integrations/Openai Sdk

OpenAI SDK

ANTS Platform wraps the official OpenAI SDKs so every call is recorded as a generation with the model, input messages, output, token usage, cost, latency and errors. Streaming and async calls are supported.

Install

bash
pip install "ants-platform[openai]"

Set ANTS_PLATFORM_PUBLIC_KEY, ANTS_PLATFORM_SECRET_KEY and ANTS_PLATFORM_HOST as described in the Python SDK guide.

Swap the import

python
from ants_platform import AntsPlatform from ants_platform.openai import openai ants = AntsPlatform() with ants.start_as_current_observation( as_type="agent", name="support-agent", agent_name="support-agent" ): response = openai.chat.completions.create( model="gpt-4o-mini", messages=[{"role": "user", "content": "Where is my order?"}], name="draft-answer", metadata={"channel": "web"}, ) ants.flush()

from ants_platform.openai import OpenAI (and AsyncOpenAI, AzureOpenAI, AsyncAzureOpenAI) also works if you construct a client.

⚠

Make OpenAI calls inside an agent observation, as shown. A traced call with no parent observation has no agent name and raises ValueError: agent_name is required.

Extra arguments

The wrapper accepts these keyword arguments on create(...) and removes them before calling OpenAI:

ArgumentEffect
nameName of the generation (default OpenAI-generation)
metadataMetadata on the generation
trace_id, parent_observation_idAttach the generation to an existing trace or observation
ants_platform_promptLink a prompt fetched with get_prompt

To set the user, session or tags, call update_trace(user_id=..., session_id=..., tags=[...]) on the enclosing agent observation. The wrapper accepts session_id, user_id and tags arguments but does not apply them to the trace.

Traced methods: chat.completions.create, completions.create, chat.completions.parse (or beta.chat.completions.parse on older OpenAI SDK versions), responses.create and responses.parse, each in sync and async form.

What you see

Each call appears as a generation in Tracing with the model, parameters, messages, completion, input and output tokens and calculated cost. Failed calls are recorded with level ERROR and the error message.

© 2026 ANTS Platform, Inc.Docs v1.0 · Last updated June 2026