Debug a session with traces
When an agent gives an unexpected answer, check what information reached the model. A trace records model responses and tool calls during a turn, one round of work in a session. Each recorded step is a span. A generation span shows the recorded input and output for one model response.
In the OpenAI Platform dashboard, find the session by ID in the project where it ran. Identify the turn that produced the result and open its trace. For your own tracing tool, you can export the session’s traces as shown in the diagram.
Read the spans for a turn
The Agents API traces new sessions by default. A trace can take time to appear after the turn ends. While the turn runs, follow live session events. To export traces, your organization must enable trace export and your project key must have api.traces.read or api.agents.read permission.
One response can request a tool call, and the next response can use the tool’s result. To find where a detail went missing, compare the spans for those steps.
| Span | What to look for |
|---|---|
| Agent | Which agent did the work? The span groups that agent’s model responses and tool calls and shows its instructions when recorded. |
| Tool | Check the tool name, arguments, and any recorded result or error. Did the call return the information you expected? |
| Generation | Inspect the recorded input and output of one model response. Did the model receive the tool result? Did its answer account for it? |
If the trace lacks a recorded input or result, check the saved conversation items and your application logs before concluding that the data was missing.
Example: Find out why the agent proposed changing the wrong code
Find the turn that produced the proposal
Herringbone saves the session ID and turn ID with each proposal. Even if the agent has since revised its answer, Herringbone can open the original turn in the dashboard using those IDs.
To see which steps the API recorded, Herringbone lists the session’s traces. Each trace’s id is the main agent’s turn ID. This Python program finds the matching trace and prints the span names without logging customer code or tool results.
Set OPENAI_API_KEY for a project with trace export access. Copy the program into find-proposal-trace.py and replace sess_b and turn_proxy_patch with the session and turn IDs of a completed turn in that project. With the Python package manager uv, run uv run --with openai==3.57.0 python find-proposal-trace.py.
from openai import OpenAI
session_id = "sess_b"
turn_id = "turn_proxy_patch"
client = OpenAI()
for trace in client.beta.agents.sessions.traces.list(session_id, limit=1):
if trace.id != turn_id:
continue
for resource in trace.otlp["resourceSpans"]:
for scope in resource["scopeSpans"]:
for span in scope["spans"]:
print(span["name"])
break
else:
print("No published trace for this turn. Check the IDs or try again later.")Compare the tool result with the model input
In the example trace for Customer B, execute_tool get_integration_context tells Herringbone the lookup ran. Did it return the proxy’s rules? To find out, Herringbone opens turn_proxy_patch in the dashboard and compares two spans.
- Tool. The agent requested
get_integration_context, a function Herringbone runs to look up a customer’s integration records. The result contains the app’s request fromsrc/payments.ts. It omits the proxy’s forwarding rules. - Generation. The next model input contains that same request. The model recommends changing how
src/payments.tssends the cursor.
The first gap is in Herringbone’s lookup. Herringbone includes the proxy rules in Customer B’s lookup result and sends a follow-up asking the agent to run the lookup again and revise its proposal.
Herringbone tests the revised proxy rule with two pages of sample payments. This time, the cursor reaches the API and the app displays page two. Through Herringbone’s app, the FDE alerts Customer B to the pagination change and recommends updating the proxy to forward the cursor parameter. The app already sends it.
Other findings would point to different fixes.
- No lookup call. Check the session’s tool definition and instructions for when to call the function.
- Rules in the lookup result but missing from the model input. Check that the backend returned the full result with the matching turn and call IDs.
- Rules in the model input but overlooked in the proposal. Review the instructions the session used. If you revise the saved agent’s instructions, test them in a new session.
That’s a wrap on the Agents API training! You saw how Herringbone used one agent to help three customers through the same API change. Each customer had their own session, with the tools and files the agent needed to review their integration and write up a fix. When a customer came back, the agent could pick up where it left off. And when a recommendation looked wrong, you could open the trace to see how the agent got there.
Now you can apply this training to a real use case. Explore the Agents API docs and put an agent to work in your own app!