OpenAI Agents SDK
The OpenAI Agents SDK has its own tracing interface. The OpenInference instrumentation for it turns agent runs, model calls, tool calls and handoffs into OpenTelemetry spans that Traza reads natively: OpenInference’s span kinds tell it which spans are model, tool and agent steps.
pip install openai-agents openinference-instrumentation-openai-agents \
opentelemetry-sdk opentelemetry-exporter-otlp-proto-http
import os
from agents import Agent, Runner, function_tool
from openinference.instrumentation import using_session
from openinference.instrumentation.openai_agents import OpenAIAgentsInstrumentor
from opentelemetry.exporter.otlp.proto.http.trace_exporter import OTLPSpanExporter
from opentelemetry.sdk.resources import Resource
from opentelemetry.sdk.trace import TracerProvider
from opentelemetry.sdk.trace.export import BatchSpanProcessor
provider = TracerProvider(resource=Resource.create({"service.name": "support-agent"}))
provider.add_span_processor(
BatchSpanProcessor(
OTLPSpanExporter(
endpoint="https://api.trazadb.com/v1/traces",
headers={"Authorization": f"Bearer {os.environ['TRAZA_API_KEY']}"},
)
)
)
OpenAIAgentsInstrumentor().instrument(tracer_provider=provider)
@function_tool
def search_orders(order_id: str) -> str:
return "shipped on Oct 14"
agent = Agent(name="support", instructions="Answer order questions.", tools=[search_orders])
with using_session(session_id="sess-42"):
result = Runner.run_sync(agent, "Where is order 1042?")
provider.shutdown()
using_session puts session.id on every span inside it, so the
conversation is one run for Guard and Replay.
Guarding an Agents SDK run
To stop a run before its next model call, ask the
pre-call check from a run hook or a
wrapper around your tools, and raise when the verdict is stop. An ingest
key is enough to ask.
Replaying it
A replay serves chat completions, and the Agents SDK calls the OpenAI
Responses API by default, which a replay answers with 400. For runs you mean
to replay, have the SDK use chat completions, both when recording and when
replaying:
from agents import set_default_openai_api
set_default_openai_api("chat_completions")
Then a replay is two environment variables: point OPENAI_BASE_URL at the
replay’s OpenAI base URL and set OPENAI_API_KEY to a read key. If a
recording’s messages do not compare exactly with what the SDK sends on replay,
create the replay in loose mode. See Replay, including what
to do about tools, which run live.