OpenAI Agents SDKopenai-agents 0.22 · Python 3.10+
1
Curious builder0 XP earned · 300 to level 2
0 daysFinish a lesson to begin
Badge collection0 of 6 unlocked
25 small wins to finish your pathNext lesson

Running the agent

You have an agent, a tool and a model. Runner is the thing that puts them together and keeps going until there is an answer.

One trip round the loop
You ask. Runner.run takes the agent and your question.Step 1 of 4

Putting the three together

python
support = Agent(
    name="Support",
    instructions="Answer using the tools you have. Be brief.",
    model=PretendModel(),
    tools=[lookup_order],
)

This is the first time an agent has been given a model and a tool. Nothing has run yet, it is still a description.

python
result = await Runner.run(support, "Where is order A17?")

One line, and everything happens inside it. Runner.run is asynchronous, which is why there is an await, and the panel can run that directly.

Read the three items

The output ends with a list of what happened, and it is worth reading slowly.

ItemWhat it was
ToolCallItemThe model asking for lookup_order. Not an answer, a request.
ToolCallOutputItemYour function's return value, put back into the conversation.
MessageOutputItemThe model again, this time with words, having seen the tool's answer.

Those three items are the agent loop. Every framework you read about is that shape with more comfort on top, and once it makes sense, agents make sense.

Break it on purpose
Take tools=[lookup_order] out and run it again. The model has nothing to ask for, so you get one item instead of three, and the answer comes straight from the model.
Try it yourself
  • Remove the tool and count the items.
  • Ask something without A17 in it and see which rule the model follows.
  • Print result.final_output only, and nothing else.

This is what real progress feels like.