OpenAI Agents SDK
Track every OpenAI Agents SDK run with cost, latency, token usage, and tool call data. Register AgentMetricsProcessor once and it hooks into the SDK's tracing pipeline, covering all agents in the process.
Requirements
- Python 3.10 or later
openai-agents0.0.5 or later
1. Install
Shell
pip install agentmetrics-openai-agents
2. Register the processor
Call register() once at startup, before running any agents. It registers the tracing processor globally. No code changes needed.
Python
import os
from agentmetrics_openai_agents import register
register(
api_key=os.environ["AGENTMETRICS_API_KEY"],
agent_id="my-openai-agent",
base_url="http://localhost:8099",
)
Then run your agents normally:
Python
from agents import Agent, Runner
agent = Agent(name="my-agent", instructions="You are a helpful assistant.")
result = await Runner.run(agent, "summarize this document")
Manual registration
If you prefer to manage the processor instance directly, use AgentMetricsProcessor with the SDK's add_trace_processor:
Python
from agents.tracing import add_trace_processor
from agentmetrics_openai_agents import AgentMetricsProcessor
add_trace_processor(
AgentMetricsProcessor(
api_key=os.environ["AGENTMETRICS_API_KEY"],
agent_id="my-openai-agent",
base_url="http://localhost:8099",
)
)
Singleton guard
register() is idempotent. Calling it multiple times registers the processor only once, even across multiple modules.
What gets tracked
The processor receives OpenAI Agents SDK trace and span events:
| Span type | What is captured |
|---|---|
LLMSpanData | Input tokens, output tokens, cache read tokens, model name |
FunctionSpanData | Tool call count, tool name, tool errors |
HandoffSpanData | Handoff counted as a tool call |
| Trace error | Failure status and error message |
One run summary is emitted per trace when on_trace_end fires.
Configuration
| Parameter | Default | Description |
|---|---|---|
api_key | required | Your AgentMetrics API key |
agent_id | "openai-agent" | Fallback label if trace name is not set |
base_url | "http://localhost:8099" | AgentMetrics backend URL |