# Agents API

> For the complete documentation index, see [llms.txt](/llms.txt). Markdown versions of documentation pages are available by appending `.md` to the page URL.

## 7. Debug a session with traces



When an agent gives an unexpected answer, check what information reached the model. A [trace](https://developers.openai.com/api/docs/guides/agents-api/tracing#read-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](https://developers.openai.com/api/docs/guides/agents-api/tracing#generation) shows the recorded input and output for one model response.

In the [OpenAI Platform dashboard](https://platform.openai.com/logs?api=agents), find the session by ID in the project where it ran. [Identify the turn that produced the result](https://developers.openai.com/api/training/agents-api/5-inspect-session-results-and-artifacts.md) and open its trace. For your own tracing tool, you can [export the session’s traces](https://developers.openai.com/api/docs/guides/agents-api/tracing#export-session-traces) as shown in the diagram.



### Export and inspect a turn’s trace

1. **Request traces for the session** (Your app → OpenAI Agents API, `GET /v1/agents/sessions/sess_b/traces`)

   **Your app**: Uses a project API key with permission to read traces.

   **OpenAI Agents API**: Finds traces available for this session.

   **Query parameters**: `limit=1&order=asc`

2. **Read the page of traces** (OpenAI Agents API → Your app, HTTP response)

   **Your app**: Selects the trace whose `id` matches the main agent’s turn ID.

   **OpenAI Agents API**: Returns a page of traces in OpenTelemetry Protocol (OTLP) format.

   **Response JSON**: `{"object":"list","data":[{"id":"turn_proxy_patch","object":"agent.session.trace","session_id":"sess_b"}],"last_id":"turn_proxy_patch","has_more":true}`

3. **Compare the tool result with the next model response** (Your app → application records, Application operation)

   **Your app**: Checks whether the model received the information needed to answer.

   **Example inspection notes**: `Tool result: client code only` `Next model input: client code only` `Model output: client-only patch` `First gap: lookup omitted proxy rules`



### Read the spans for a turn

The Agents API traces new sessions by default. A [trace can take time to appear](https://developers.openai.com/api/docs/guides/agents-api/tracing#when-traces-are-ready) after the turn ends. While the turn runs, [follow live session events](https://developers.openai.com/api/docs/guides/agents-api/observability#follow-events-and-inspect-session-history). 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](https://developers.openai.com/api/docs/guides/agents-api/tracing#agent) | Which agent did the work? The span groups that agent’s model responses and tool calls and shows its instructions when recorded.    |
| [Tool](https://developers.openai.com/api/docs/guides/agents-api/tracing#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](https://developers.openai.com/api/docs/guides/agents-api/sessions/events#fetch-items-and-turns) and your application logs before concluding that the data was missing.




### Example: Find out why the agent proposed changing the wrong code



When Herringbone starts returning payments one page at a time, it asks
    Customer B’s [AI forward-deployed
    engineer](https://developers.openai.com/api/training/agents-api.md#use-case-ai-forward-deployed-engineer) to review the integration.
    The customer’s app sends requests through a proxy, a service that forwards
    them to Herringbone’s API. To fetch page two, the request must include the
    cursor, a value Herringbone returned with page one.
  

  

    The agent proposes changing the app, but Herringbone’s test shows that the
    app sends the cursor and the proxy drops it. Before notifying Customer B,
    Herringbone investigates why the agent recommended changing code that
    already worked.



#### 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](https://developers.openai.com/api/docs/guides/agents-api/tracing#open-a-trace) 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`](https://developers.openai.com/api/docs/guides/agents-api/quickstart#prerequisites) 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`.

find-proposal-trace.py

```python
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.

1. **Tool.** The agent requested `get_integration_context`, a [function Herringbone runs](https://developers.openai.com/api/training/agents-api/3-send-tasks-and-handle-tool-calls.md) to look up a customer’s integration records. The result contains the app’s request from `src/payments.ts`. It omits the proxy’s forwarding rules.
2. **Generation.** The next model input contains that same request. The model recommends changing how `src/payments.ts` sends 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](https://developers.openai.com/api/docs/guides/agents-api/sessions#continue-or-steer-the-work) 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](https://developers.openai.com/api/docs/guides/agents-api/tools/functions#define-a-function) 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](https://developers.openai.com/api/docs/guides/agents-api/tools/functions#return-the-result).
- **Rules in the model input but overlooked in the proposal.** Review the instructions the session used. If you [revise the saved agent’s instructions](https://developers.openai.com/api/docs/guides/agents-api/configuration#update-a-saved-agent), test them in a new session.



**Ask your coding agent:**

```text
Help me debug an unexpected Agents API result. Ask what I expected and which turn produced the result. Compare the tool call and its result with the next generation's recorded input and output. Cite the first span that shows a discrepancy. If the trace is incomplete, tell me which application record to check. Suggest a test to confirm the fix.

Read this lesson for context: https://developers.openai.com/api/training/agents-api/7-debug-a-session-with-traces.md
```

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](https://developers.openai.com/api/docs/guides/agents-api/overview) and put an agent to work in your own app!
