Day 5, 30 minutes · beginner
Why it matters
Build a small agent in Python with the OpenAI Agents SDK: two specialist agents, run once with handoffs and once as tools of a manager agent. In lesson 3 you wrote the tool loop yourself: send, run the calls, send back, stop after four rounds. That is fine for one helper. Once you want several helpers that each know one job, the loop code grows fast. OpenAI's Agents SDK is a Python library that runs that loop for you.
Today Hedda's desk gets two specialists: a lessons agent and a prices agent. You run them in the two ways the SDK offers. First with a handoff, where a front desk agent passes the question on and the specialist answers. Then as tools of a manager agent, which keeps control and writes one reply. You finish with both versions working and a clear idea of which one to pick.
What the SDK does
An agent in the SDK is a model plus instructions, a name and tools. The Runner runs it in a loop, as the SDK docs describe it: call the model; if it gives a final answer, stop; if it asks for a handoff, switch to the other agent; if it asks for tool calls, run them and go again. max_turns is the stop: past that many turns the run raises an error, the same job MAX_ROUNDS did in lesson 3.
Tools are plain Python functions with the @tool decorator, imported from agents.decorators as in the SDK's quickstart. The SDK builds the tool definition from the function name, the type hints and the docstring, so write those carefully: they are what the model reads. In desk_agents.py the tools wrap the same pool_data.py functions as lesson 3, so both versions use the same data.
Set the model on every agent, as desk_agents.py does with model=MODEL. Then you know which model, and which price, every step uses.
Two ways to work together
The SDK quickstart puts the choice as one question: who should own the final answer?
Handoff. The front desk agent gets the specialists in handoffs. Each specialist has a handoff_description that tells the front desk when to pick it. When the front desk hands off, the specialist takes over the conversation and writes the answer. result.last_agent tells you who that was.
Use it when one specialist can fully answer a question on its own, like a phone line that puts you through.
Agent as a tool. The manager gets the specialists as tools, made with as_tool. It asks each one a question, gets the answer back as a tool result, and writes the final reply itself. The docs say a nested run does not get the manager's conversation automatically, only the question the manager passes on.
Use it when one question needs several specialists, or when you want one voice and one set of rules in every reply.
A worked example
"When are the beginners lessons on Wednesday?" goes to the front desk. It hands off to the lessons agent, which calls get_lessons and answers: 16:00. The run prints Answered by: Lessons agent.
"Is there room in a Saturday lesson for improvers, and what does a block cost?" goes to the manager. It asks the lessons agent and the prices agent, then writes one reply. The run prints Answered by: Front desk manager. That is more model calls than one agent would make, so it costs more.
Tracing
The SDK records each run as a trace: which agent ran, which tools were called, what came back. The docs say tracing is on by default and the traces appear on the Traces page of the OpenAI dashboard. That is very handy for finding out why an agent did something. It also means run data is sent to your OpenAI organization. You can turn it off with the environment variable OPENAI_AGENTS_DISABLE_TRACING=1. With the made-up data of this course, leave it on and look.
The steps
- Install the Agents SDK.
- Make two specialist agents with their tools.
- Run them with a handoff from a front desk agent.
- Run them as tools of a manager agent.
- Read both traces in the dashboard.
- Write down when you would use each pattern.
Pick your tool below and follow the steps.
Common mistakes
- Leaving out
model=and getting a model you did not choose. - Vague docstrings on tools. The model only knows what you wrote there.
- One giant agent with every tool, instead of small specialists.
- No
max_turns, or a very high one. - Copying old examples from before the Assistants API was shut down. The Agents SDK runs on the Responses API.
Deeper dive
Today's actions
Ticks are not saved without an accountDo it in OpenAI API
OpenAI API
- Turn on your virtual environment and run
pip install openai-agents. The package is imported asagents. This lesson was tested with version 0.26.2: check yours withpip show openai-agents. - Create
desk_agents.py. It turns the three pool functions into agent tools and makes two specialist agents, each with its own instructions and tools.import json from agents import Agent from agents.decorators import tool import pool_data from config import MODEL @tool def get_lessons(day: str, level: str | None = None) -> str: """List the swimming lessons on one day of the week, optionally for one level.""" return json.dumps(pool_data.get_lessons(day, level)) @tool def get_spaces_left(lesson_id: str) -> str: """Get the number of free places in one lesson, by lesson id.""" return json.dumps(pool_data.get_spaces_left(lesson_id)) @tool def get_price(ticket_type: str) -> str: """Get the price in euros of adult_swim, child_swim or lesson_block.""" return json.dumps(pool_data.get_price(ticket_type)) lessons_agent = Agent( name="Lessons agent", handoff_description="Questions about swimming lessons, times and free places.", instructions="Answer from your tools only. Keep it under 60 words.", model=MODEL, tools=[get_lessons, get_spaces_left], ) prices_agent = Agent( name="Prices agent", handoff_description="Questions about ticket and lesson prices.", instructions="Answer from your tool only. Prices are in euros. Keep it under 60 words.", model=MODEL, tools=[get_price], ) - Create
handoff_desk.py. The front desk agent only decides which specialist takes over the question.import asyncio from agents import Agent, Runner from config import MODEL from desk_agents import lessons_agent, prices_agent front_desk = Agent( name="Front desk", instructions="Hand each question about Otterbrook Community Pool to the right specialist.", model=MODEL, handoffs=[lessons_agent, prices_agent], ) async def main() -> None: result = await Runner.run(front_desk, "When are the beginners lessons on Wednesday?", max_turns=6) print(result.final_output) print("Answered by:", result.last_agent.name) if __name__ == "__main__": asyncio.run(main()) - Run
python handoff_desk.py. You should see the answer andAnswered by: Lessons agent: after the handoff, the specialist owns the reply. - Create
manager_desk.py. Here the manager keeps control and calls the two specialists as tools.import asyncio from agents import Agent, Runner from config import MODEL from desk_agents import lessons_agent, prices_agent manager = Agent( name="Front desk manager", instructions=( "Answer questions about Otterbrook Community Pool. Use your tools for facts, " "then write one friendly reply under 80 words. Never guess a time or a price." ), model=MODEL, tools=[ lessons_agent.as_tool( tool_name="ask_lessons_agent", tool_description="Ask about lesson times, levels and free places.", ), prices_agent.as_tool( tool_name="ask_prices_agent", tool_description="Ask about ticket and lesson prices.", ), ], ) async def main() -> None: question = "Is there room in a Saturday lesson for improvers, and what does a block cost?" result = await Runner.run(manager, question, max_turns=8) print(result.final_output) print("Answered by:", result.last_agent.name) if __name__ == "__main__": asyncio.run(main()) - Run
python manager_desk.py. You should see one combined reply andAnswered by: Front desk manager. - Open
platform.openai.com/tracesand find both runs. Click through the steps: which agent ran, which tools were called and what they returned. - Write
agent-notes.txtwith two lines: when a handoff fits better, and when an agent as a tool fits better, using these two runs as your example.
Note: Tracing is on by default: run data goes to the Traces page of your OpenAI organization. To turn it off, set OPENAI_AGENTS_DISABLE_TRACING=1 in your environment. max_turns stops a run that goes on too long. Every agent sets its model, so nothing falls back to a default you did not choose.
Paid planNeeds prepaid API credit. The OpenAI API is billed per use, separately from any ChatGPT plan.
Create a free account to save your progress
Free, and no payment details. Your ticks and finished lessons are kept, on every device.
Create a free account I already have an accountCreate a free account to save your progress. Sign in or sign up