Seeing what it did
When an agent gives a strange answer, the question is always the same: what did it actually do? Everything you need is already on the result.
for item in result.new_items:
if item.type == "tool_call_item":
print(f"asked for {item.raw_item.name}({item.raw_item.arguments})")
elif item.type == "tool_call_output_item":
print(f"tool said {item.output}")
elif item.type == "message_output_item":
print(f"replied {ItemHelpers.text_message_output(item)}")print("what the run actually did")
print("-" * 46)
for item in result.new_items:
if item.type == "tool_call_item":
print(f"asked for {item.raw_item.name}({item.raw_item.arguments})")
elif item.type == "tool_call_output_item":
print(f"tool said {item.output}")
elif item.type == "message_output_item":
print(f"replied {ItemHelpers.text_message_output(item)}")
print("-" * 46)
print("model calls:", len(result.raw_responses))Eleven lines and you can read a run like a transcript. This is the first thing to write when an agent starts behaving oddly, and it needs nothing but the result you already have.
The three questions it answers
| Question | What to look at |
|---|---|
| Why did it pick that tool? | the arguments it sent, and that tool's description |
| Why did it not use a tool? | whether the tool was in the list at all |
| Why is the answer wrong? | what the tool actually returned, above the reply |
Most agent bugs are one of those three, and all three are visible in the transcript above. The model is rarely the problem; the description it was given usually is.
The hosted version
OpenAI also records runs for you, with timing and token counts, in a dashboard. It is on by default and this course turns it off, because it needs a key and there is nothing to send from a browser.
from agents import set_tracing_disabled
set_tracing_disabled(True) # what this course doesOn your own machine, with a key in the environment, take that line out and every run appears at platform.openai.com under Traces, with the same items you printed above plus how long each took and what it cost.
- Print
len(result.raw_responses)and compare it with the item count. - Add a second tool call and read the transcript again.
- Print
item.raw_itemfor a tool call and see the exact JSON the model produced.
You understood something today that you didn't yesterday.