Skip to main content
The OpenAI SDK gives you direct access to the Responses API and tool calling, which is all you need to build an agent: a loop where the model requests a tool, you run it and feed the result back, and repeat until the model returns a final answer. Because you build the loop yourself, its structure lives in your code, so you have to tell the tracer where the agent boundaries are. This guide builds that loop and makes it observable in Arize AX. You auto-instrument the OpenAI SDK to capture every model call, then add a few manual spans so the agent run and each tool execution appear in the trace too.
Plain SDK or the Agents SDK? Reach for the OpenAI Agents SDK when you want handoffs, guardrails, and sessions out of the box, and its instrumentor traces all of that for you. See the OpenAI Agents SDK guide. Use the plain OpenAI SDK when you want a minimal dependency footprint and explicit control over the loop, which is what this guide covers.

What you’ll build

You will build a small math-solving agent that answers arithmetic questions by calling a solve_equation tool, then trace it end to end. By the end of this guide, you will be able to:
  • Build a tool-calling agent on the OpenAI SDK using a manual reasoning loop.
  • Auto-instrument OpenAI SDK calls so every model request and response is captured.
  • Add manual AGENT and TOOL spans so the full agent trajectory, not just the LLM calls, is visible.
  • Read the resulting trace tree in Arize AX to debug and evaluate agent behavior.
This guide takes about 20 minutes and assumes you are comfortable with Python or TypeScript and have made an OpenAI API call before.

Get your Arize credentials

Create a tracing project from Projects → New Tracing Project. The setup page shows the credentials you need: copy your Space ID and click Create API Key to generate a key. Save the key somewhere safe. You’ll plug both into whichever path you choose below.
New Tracing Project setup page showing Space ID and Create API Key

Requirements

1

Install

2

Set up tracing

First, export your credentials so the code below can read them from the environment:
Tracing has two parts, and this step wires up the first: auto-instrumentation for the OpenAI SDK. register() configures an OpenTelemetry tracer provider that exports spans to Arize AX, and OpenAIInstrumentor patches the SDK so every Responses API call emits an LLM span with the prompt, response, tool calls, and token usage. You do not have to write any span code for the model calls themselves.
3

Build the agent

Now build the agent loop. Auto-instrumentation captures the model calls, but it cannot see the parts that live in your code: the overall agent run and the tool executions. Those run outside the OpenAI SDK, so no span is emitted for them at all. This is the second part of tracing: you add manual spans to fill the gaps.Two manual spans do the job:
  • An AGENT span wraps the whole loop and becomes the root of the trajectory. Every model call and tool execution nests underneath it.
  • A TOOL span wraps each tool execution, capturing its arguments and result.
Because the manual spans and the auto-instrumented LLM spans share the same tracer provider, they stitch together into a single trace automatically.
4

Run the agent

The entry point imports the instrumentation module first. This guarantees tracing is configured before the OpenAI client is used, so no early spans are missed.
Run it with python example.py. You should see:
5

View the trace in Arize AX

  1. Open your Arize AX space and select project openai-sdk-agent-example.
  2. Within ~30 seconds you will see a trace shaped like the trajectory itself: a Math Solver Agent span (AGENT) at the root, an LLM span for the first Responses API call that returns the tool request, a solve_equation span (TOOL) holding the arguments and result, and a second LLM span where the model turns the tool result into the final answer.
  3. Select any span to inspect its inputs, outputs, and token usage.
The manual AGENT and TOOL spans are what make this trace readable as an agent run. Without them you would still see the two LLM calls, but nothing would tie them together or show the tool execution between them. This is the combine auto and manual instrumentation pattern applied to an agent loop.

Next steps

OpenAI Agents SDK

Build the same kind of agent with the OpenAI Agents SDK and let its instrumentor trace handoffs and tools for you.

Evaluate agents

Score tool-call accuracy, tool results, and goal completion once your agent is emitting traces.

Manual instrumentation

Go deeper on span kinds, attributes, sessions, and agent trajectories.