OpenAIAdapter extracts telemetry from OpenAI responses — model name, token counts, and cost in USD — and passes them directly into agentvista.record() or a traced run. The adapter works with both the Chat Completions API (client.chat.completions.create) and the Responses API (client.responses.create), auto-detecting the response shape. Cost is calculated automatically from the model name using AgentVista’s built-in pricing table.
Setup
Usage examples
- Basic
- With tracing
- Responses API
Use
The adapter automatically detects whether the response uses
adapter.extract() on any OpenAI response and unpack the result directly into agentvista.record().adapter.extract() returns a dict with any of these keys present:| Key | Type | Description |
|---|---|---|
model | str | Model ID returned by the API (e.g. "gpt-4.1") |
input_tokens | int | Prompt tokens (or input_tokens from the Responses API) |
output_tokens | int | Completion tokens (or output_tokens from the Responses API) |
total_tokens | int | Sum of input and output tokens |
cost_usd | float | Total cost in USD, rounded to 6 decimal places |
prompt_tokens / completion_tokens (Chat Completions) or input_tokens / output_tokens (Responses API) and normalizes both into the same output shape.Supported models
Cost calculation is automatic for the following OpenAI models. If your model is not listed,
Models that support prompt caching (
cost_usd will be absent from the extracted telemetry; all other fields (tokens, model name) are still captured.| Model | Input (per M tokens) | Output (per M tokens) |
|---|---|---|
gpt-5.4 | $2.50 | $15.00 |
gpt-5.4-mini | $0.75 | $4.50 |
gpt-5.4-nano | $0.20 | $1.25 |
gpt-5 | $0.625 | $5.00 |
gpt-4.1 | $2.00 | $8.00 |
gpt-4.1-mini | $0.40 | $1.60 |
gpt-4.1-nano | $0.10 | $0.40 |
gpt-4o | $2.50 | $10.00 |
gpt-4o-mini | $0.15 | $0.60 |
o3 | $2.00 | $8.00 |
o4-mini | $1.10 | $4.40 |
gpt-5.4, gpt-5.4-mini, gpt-5.4-nano, gpt-5, gpt-4.1, gpt-4.1-mini, gpt-4.1-nano, gpt-4o, gpt-4o-mini, o3, o4-mini) automatically detect cached token counts from the response and apply the correct cached read rate when calculating cost.