OpenAI Agents
Auto-instrument OpenAI Agents SDK.
Risicare automatically instruments the OpenAI Agents SDK.
Python only
Installation
pip install 'risicare[openai-agents]'
# or
pip install risicare openai-agentsVersion Compatibility
Requires openai-agents >= 0.1.0.
Auto-Instrumentation
The samples on this page use await at the top level, as in a notebook. In a script, put the code in an async def main() and call asyncio.run(main()). For a sample that does not stream, you can call Runner.run_sync() instead.
import risicare
from agents import Agent, Runner
risicare.init()
agent = Agent(
name="Assistant",
instructions="You are a helpful assistant."
)
# Automatically traced (async)
result = await Runner.run(agent, "Hello!")
# Or use run_sync for synchronous usage
# result = Runner.run_sync(agent, "Hello!")What's Captured
The integration patches Runner.run / Runner.run_sync and emits one openai_agents.run/{agent} span per run. Since Python 0.16.1, each turn of the run has a span openai_agents.agent/{agent} under the run span (see A span for each turn). Handoffs, model, and step count are recorded as attributes on the run span — a handoff is not a span of its own. Since Python 0.16.0, each call of a function tool is a tool_call span (see Tools).
| Feature | Description |
|---|---|
| Agent Execution | The full Runner.run as a single openai_agents.run/{agent} span, with the name of the starting agent |
| Turns | One openai_agents.agent/{agent} span for each turn, with the name of the agent of that turn (since 0.16.1) |
| Tools | A tool_call span for each call of a function tool, and the tool names in the framework.openai_agents.tools attribute of the run span |
| Handoffs | Recorded by agent name in the framework.openai_agents.handoffs attribute, and the agent that finished the run in framework.openai_agents.final_agent (not per-handoff spans). Through 0.16.0, a handoff made with handoff() was recorded as the Python text of the Handoff object |
| LLM Calls | Traced by the OpenAI patch, for the Responses API (the default of the Agents SDK) and the Chat Completions API. They are children of the span of their turn |
| Agent of each turn | The spans of each turn carry the agent (Agent.name), handoffs and agents as tools too. After a handoff they carry the new agent. The run span has the starting agent when no agent is current (since 0.16.0) |
| Step Count | Number of agent loop steps, in framework.openai_agents.step_count |
The Responses API is traced
The OpenAI Agents SDK calls the OpenAI Responses API by default. The OpenAI patch traces responses.create and responses.parse, so the model calls of an agent run arrive as provider spans, as child spans of the span of their turn. If you call set_default_openai_api("chat_completions") (from agents), the Agents SDK uses the Chat Completions API, and those calls are traced too.
Span Hierarchy
A run of the agent triage that hands off to the agent billing:
openai_agents.run/triage (AGENT kind)
├── openai_agents.agent/triage (AGENT kind, turn 1)
│ └── openai.responses.create/{model} (provider span; the call that hands off)
└── openai_agents.agent/billing (AGENT kind, turn 2)
├── openai.responses.create/{model} (provider span)
└── openai_agents.tool/{tool_name} (TOOL_CALL kind)
With set_default_openai_api("chat_completions"), the provider spans are openai.chat.completions.create. A call of a function tool is an openai_agents.tool/{tool_name} span, a child of the span of the turn that called it.
A span for each turn
Since Python 0.16.1, each turn of a run has a span openai_agents.agent/{agent name} of kind agent, a child of the run span openai_agents.run/{starting agent}. This applies to Runner.run, Runner.run_sync and Runner.run_streamed.
- The LLM and tool spans of the turn are its children. So after a handoff, the tree shows the new agent.
- An error that passes through the turn (a model error, or a tool error with
failure_error_function=None) fails the turn span of the agent that owned it, and then the run span. A tool error that the Agents SDK gives to the model fails only the tool span. - A cancel of a turn is not an error. The span gets the cancel marker.
- The run span and the turn spans have the same kind,
agent. Tell a turn from a run by the name:openai_agents.agent/for a turn,openai_agents.run/for a run. The order of the turns is their start time. - To count runs, count the
openai_agents.run/spans, or the traces. Do not count the spans of kindagent.
One more span for each turn
Each turn is one more span, so one more event of your quota: a run of 3 turns sends 3 more spans. The tree of an Agents run changes too: the LLM and tool spans are one level deeper. Through Python 0.16.0, every LLM and tool span was a direct child of the run span, which has the name of the starting agent, so a failure after a handoff looked like a failure of the first agent.
Provider Spans
OpenAI Agents SDK instrumentation creates agent/framework-level spans. Underlying LLM calls (e.g., OpenAI) are traced separately by provider instrumentation, giving you both framework-level and LLM-level visibility.
Multi-Agent Handoffs
Agent handoffs are recorded on the run span:
from agents import Agent, Runner
sales_agent = Agent(
name="Sales",
instructions="Handle sales inquiries."
)
support_agent = Agent(
name="Support",
instructions="Handle support requests."
)
# Define the specialists first, and pass the Agent objects.
# A list of names makes no handoff.
triage_agent = Agent(
name="Triage",
instructions="Route to the appropriate specialist.",
handoffs=[sales_agent, support_agent]
)
# Handoffs are recorded in the framework.openai_agents.handoffs attribute,
# and the agent that finished the run in framework.openai_agents.final_agent —
# not as separate child spans.
result = await Runner.run(triage_agent, "I want to buy something")Tools
Each call of a function tool is a span of kind tool_call (since Python 0.16.0). It is named openai_agents.tool/{name}. With content capture on, it has tool.input and tool.output. The tool names are also in the framework.openai_agents.tools attribute of the run span.
from agents import Agent, Runner, function_tool
@function_tool
def get_weather(location: str) -> str:
"""Get weather for a location."""
return f"Weather in {location}: Sunny, 72°F"
agent = Agent(
name="Weather Assistant",
tools=[get_weather]
)
# The call of get_weather is an openai_agents.tool/get_weather span.
result = await Runner.run(agent, "What's the weather in Paris?")A tool that raises makes an error span. The trace has an error also when the Agents SDK gives the error to the model as the output of the tool and the run ends ok. So the trace goes to the trace.error webhook. Before Python 0.16.0, a tool had no span, and no span marked the tool as failed.
Only a function tool has a tool span. A hosted tool (web search, file search) runs at OpenAI and has none. A handoff, and the computer, shell and apply-patch tools, have none.
A guardrail tripwire ends the run span with the status error. Since 0.16.0, error.message names the guardrail and its kind. Since 0.16.0, each failed span has one exception record (before, a failed span could have two). A run has one record for each span that failed, so a failed run can have more than one record.
Context Variables
Not captured in this release. The integration does not record the context that
you pass to Runner.run().
from agents import Agent, Runner
agent = Agent(
name="Personalized Assistant",
instructions="Greet the user by name. User name: {user_name}"
)
result = await Runner.run(
agent,
"Hello!",
context={"user_name": "Alice"}
)
# The context is not recorded on the span in this releaseStreaming
Runner.run_streamed is traced. It has a run span over all turns of the run. The span ends when the run ends, with the error or the cancel marker.
from openai.types.responses import ResponseTextDeltaEvent
result = Runner.run_streamed(agent, "Write a story")
async for event in result.stream_events():
if event.type == "raw_response_event" and isinstance(event.data, ResponseTextDeltaEvent):
print(event.data.delta, end="", flush=True)