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
Set ANTS_PLATFORM_PUBLIC_KEY, ANTS_PLATFORM_SECRET_KEY and
ANTS_PLATFORM_HOST as described in the Python SDK
guide.
Swap the import
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:
| Argument | Effect |
|---|---|
name | Name of the generation (default OpenAI-generation) |
metadata | Metadata on the generation |
trace_id, parent_observation_id | Attach the generation to an existing trace or observation |
ants_platform_prompt | Link 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.
Calls through Budget AI
When the Python client uses a Budget AI key and the gateway as its base URL, the gateway also records each call. From Python SDK 3.8.0, the wrapper sends the gateway the context of its own record of the call, and the gateway writes its priced generation into the agent's trace. The wrapper's record becomes a span with zero cost, so each call is counted once. With older Python SDKs and with the JS/TS wrapper, the call is recorded twice: in the agent's trace and in a separate gateway trace. See Using the SDK and Budget AI together.