Skip to content

OpenAI SDK and Agents SDK

Trace OpenAI calls and OpenAI Agents SDK runs in Python and JavaScript: model calls, tool calls, handoffs, guardrails, tokens and latency.

You build with Package
The OpenAI SDK (openai) anosys-sdk-openai
The OpenAI Agents SDK (openai-agents, @openai/agents) anosys-sdk-openai-agents

Before you start

  1. Create an endpoint of type OpenTelemetry / AI Agents in the console, then go to Collecting → Integration Options → API and click Create New API Key.
  2. Install the Anosys SDK packages.
  3. Initialize the logger in your code before making OpenAI calls.

OpenAI SDK Integration

Use this when you're calling the OpenAI API directly (chat completions, embeddings, etc.).

Install

pip install anosys-sdk-openai
npm install anosys-sdk-openai

Initialize and Run

import os
from openai import OpenAI
from anosys_sdk_openai import AnosysOpenAILogger

# Set your API keys
os.environ["OPENAI_API_KEY"] = "YOUR_OPENAI_API_KEY"
os.environ["ANOSYS_API_KEY"] = "YOUR_ANOSYS_API_KEY"

# Initialize the Anosys logger — this auto-instruments all OpenAI calls
AnosysOpenAILogger()

# Use the OpenAI client as usual
client = OpenAI()

response = client.chat.completions.create(
    model="gpt-4o-mini",
    messages=[
        {"role": "system", "content": "You are a helpful assistant."},
        {"role": "user", "content": "Hello!"}
    ]
)

print(response.choices[0].message.content)
import { AnosysOpenAILogger } from 'anosys-sdk-openai';
import OpenAI from 'openai';

// Set your API keys as environment variables:
// OPENAI_API_KEY=your-openai-key
// ANOSYS_API_KEY=your-anosys-key

// Initialize once — instruments all subsequent OpenAI calls automatically
new AnosysOpenAILogger();

const client = new OpenAI();
const response = await client.chat.completions.create({
  model: 'gpt-4o-mini',
  messages: [
    { role: 'system', content: 'You are a helpful assistant.' },
    { role: 'user', content: 'Hello!' },
  ],
});

console.log(response.choices[0].message.content);

That's it. Every OpenAI API call made through the client is now automatically traced.

Environment variables

For production deployments, set OPENAI_API_KEY and ANOSYS_API_KEY as environment variables rather than hardcoding them. The SDK reads both from the environment automatically.


OpenAI Agents SDK Integration

Use this when you're building agents with the OpenAI Agents SDK (openai-agents). The Anosys integration plugs into the Agents SDK's tracing processor to capture agent runs, tool calls, handoffs, guardrails, and generation spans.

Install

pip install anosys-sdk-openai-agents
npm install anosys-sdk-openai-agents

Initialize and Run

import os
from agents import Agent, Runner, set_tracing_processor
from anosys_sdk_openai_agents import AnosysOpenAIAgentsLogger

os.environ["OPENAI_API_KEY"] = "YOUR_OPENAI_API_KEY"
os.environ["ANOSYS_API_KEY"] = "YOUR_ANOSYS_API_KEY"

# Set up the tracing processor
set_tracing_processor(AnosysOpenAIAgentsLogger())

# Create and run an agent
agent = Agent(
    name="Assistant",
    instructions="You are a helpful assistant."
)

result = Runner.run_sync(agent, "Hello!")
print(result.final_output)
import { AnosysOpenAIAgentsLogger } from 'anosys-sdk-openai-agents';
import { Agent, run, addTracingProcessor } from '@openai/agents';

// Register the tracing processor — captures all agent traces automatically
const logger = new AnosysOpenAIAgentsLogger();
addTracingProcessor(logger);

const agent = new Agent({ name: 'my-agent', instructions: 'You are helpful.' });
const result = await run(agent, 'Hello');
console.log(result.finalOutput);

User Context

Associate traces with user sessions for better segmentation and analysis:

1
2
3
4
5
6
7
8
def get_user_context():
    return {
        "session_id": "user-123",
        "token": "auth-token"
    }

processor = AnosysOpenAIAgentsLogger(get_user_context=get_user_context)
set_tracing_processor(processor)
1
2
3
new AnosysOpenAIAgentsLogger({
  getUserContext: () => ({ session_id: req.sessionId, token: req.token }),
});

Leave out prompts and outputs

AnosysOpenAIAgentsLogger accepts capture_inputs=False and capture_outputs=False (and capture_parameters=False for model settings) to send timing, tokens and structure only.

Span Types Captured

The Agents SDK integration captures the following span types:

Span Type Description
agent Agent execution spans with handoffs and tool lists
function Tool/function call invocations
generation LLM generation calls with token usage
guardrail Guardrail evaluation checks
handoff Agent-to-agent handoffs
response Response spans
custom Custom user-defined spans
mcp_tools MCP tool invocations

Streaming Support

Streaming is automatically detected and logged with both Python and JavaScript SDKs:

1
2
3
4
5
6
7
8
9
stream = client.chat.completions.create(
    model="gpt-4o-mini",
    messages=[{"role": "user", "content": "Write a haiku"}],
    stream=True
)

for chunk in stream:
    if chunk.choices[0].delta.content:
        print(chunk.choices[0].delta.content, end="")
1
2
3
4
5
6
7
8
9
const stream = await client.chat.completions.create({
  model: 'gpt-4o-mini',
  messages: [{ role: 'user', content: 'Write a haiku' }],
  stream: true,
});

for await (const chunk of stream) {
  process.stdout.write(chunk.choices[0]?.delta?.content || '');
}

Custom Function Logging

Log custom functions using decorators to capture inputs, outputs, and execution time:

1
2
3
4
5
from anosys_sdk_core import anosys_logger

@anosys_logger(source="my_app")
def process_response(response):
    return response.choices[0].message.content
1
2
3
4
5
6
import { anosysLogger } from 'anosys-sdk-openai';

const processResponse = anosysLogger('my_app.pipeline')(async (input) => {
  // your logic
  return result;
});

What the OpenAI SDK Captures

Data Point Description
Model name & version The exact model used for each call (e.g. gpt-5, gpt-4.1-mini)
Prompt & completion tokens Precise input/output token counts per request
Request parameters Temperature, top-p, max tokens, stop sequences, and other model settings
Tool / function calls Names, arguments, and return values of any tool calls made by the agent
Streaming Streaming responses, with final usage
Response metadata Finish reason, system fingerprint, and response ID
Errors Error type and message

Configuration Reference

Variable / Setting Description
OPENAI_API_KEY Your OpenAI API key
ANOSYS_API_KEY Your Anosys API key
ANOSYS_API_URL (Optional) Override the logging endpoint
ANOSYS_LOG_LEVEL (Optional, JS only) debug / info / warn / error / silent (default: warn)

Troubleshooting

I don't see any data in the Anosys Console
  • Verify that ANOSYS_API_KEY is set correctly and matches the key shown in your Anosys Console.
  • Make sure AnosysOpenAILogger() is called before you create the OpenAI() client.
  • For the Agents SDK, ensure set_tracing_processor() (Python) or addTracingProcessor() (JS) is called before running agents.
  • Confirm that your OpenAI calls are completing successfully (check for API errors).
  • Ensure outbound HTTPS traffic to api.anosys.ai is not blocked by a firewall or proxy.
Does this work with async and streaming calls?

Yes. The Anosys logger automatically instruments both synchronous and asynchronous OpenAI clients, including streaming responses via stream=True.

Can I use this alongside other OTLP collectors?

Yes. The Anosys logger can coexist with other OpenTelemetry instrumentation. If you need to fan out to multiple backends, place an OpenTelemetry Collector in between and configure multiple exporters there.

Which OpenAI SDK versions are supported?

The anosys-sdk-openai package supports OpenAI Python SDK v1.x and later. The anosys-sdk-openai-agents package supports the OpenAI Agents SDK.


Next Steps