The OpenAI Agents SDK
Agentic AI Foundations · Lesson 10 of 20 · 1:03:02
YouTube loads only after you press play. This chapter opens at its timestamp in the full course. Watch on YouTube ↗
Learning with these videos? Subscribe for new courses and exam-style questions.
Subscribe on YouTube (opens in a new tab)In this lesson
Prepare for the Oracle Agentic AI Foundations Associate exam (1Z0-1157-26). In this lesson: the six concepts of the Agents SDK, agents and the runner (Runner.run, run_sync, and max_turns), function tools, and tracing. Includes three original practice questions, with Fairhaven Furniture, a fictional furniture seller, as the example.
Study notes
The text of this lesson, to read at your own pace. Information as of November 2026.
The loop, for free
Fairhaven's second team picks the Agents SDK for its support agent: it will need several steps, tools, and later, several agents. The SDK promises to run the loop for them. Here's how its pieces fit together.
Six concepts
The Agents SDK is a Python framework for agents, tools, and multi-agent systems, built on the Responses API, and you install it with pip, as the OpenAI agents package. An agent combines a model, instructions, and tools, and the runner executes it: it runs the loop of model calls and tool calls until the job is done. Tools are functions that the agent can call. Handoffs pass control from one agent to another, and guardrails validate inputs and outputs, two topics for lesson eleven. And tracing logs every step, so you can see what the agent did.
Agents and the runner
In Python, you import Agent and Runner. An agent has a name, like a job title, instructions, like a job description, and optionally a model and tools. Runner.run_sync runs the agent and waits for the result, and await Runner.run does the same asynchronously. Under the hood, the runner calls the Responses API, runs the tools that the model asks for, and loops. And result.final_output holds the agent's answer.
Inside the loop
Each turn, the runner calls the model with the current input. If the model asks for tools, the runner runs them, adds the results, and loops again, and if it asks for a handoff, the runner switches to the other agent. The output is final when the model returns text of the expected type, with no tool calls. And if a run goes past max_turns, the runner raises a MaxTurnsExceeded exception, so a stuck agent cannot loop forever.
Function tools
The SDK has three kinds of tools. Hosted tools come from OpenAI, like web search, file search, and code interpreter, and you add them to the agent's list of tools. Function tools are your own Python functions with the function_tool decorator: the SDK builds the schema from the type hints, and the docstring becomes the description that the model sees. Agents as tools let a manager agent call other agents, with as_tool, a pattern for lesson eleven. The rule of thumb: start with a hosted tool when one exists, and write a function tool when you need your own logic.
Tracing
Tracing is built into the SDK, and it's on by default. Each run produces a trace, made of spans: the model calls, the tool calls, the handoffs, and the guardrails, each with its start and end time. You read them in OpenAI's Traces dashboard, to debug during development and to monitor in production. When data must not leave your systems, you can turn tracing off, globally with an environment variable, or for a single run. So when an answer looks wrong, the trace shows which tool the agent called, with which arguments, and what came back.
Recap
Let's recap. The Agents SDK is built on the Responses API, around agents, the runner, tools, handoffs, guardrails, and tracing. An agent has a name, instructions, a model, and tools, the runner runs its loop, and result.final_output holds the answer, while max_turns stops a stuck run. Tools are hosted by OpenAI, written as function tools, or other agents used as tools. And tracing, on by default, logs every step as spans, for debugging and monitoring.
Quick checks for this lesson
The 3 questions of this lesson, as plain text. The button above runs them with shuffled choices and the reason behind every choice.
In a test, Fairhaven’s agent keeps calling tools, and never produces a final answer. What stops the run?
- The trace, once it gets too long
- A line in the instructions asking it to stop
- A handoff from the agent to the runner
- The max_turns limit, with an exception
Show the answer
The max_turns limit, with an exception
When a run goes past max_turns, the runner raises a MaxTurnsExceeded exception, so a stuck agent cannot loop forever. “The trace, once it gets too long” logs what happened, but it does not stop the run. “A line in the instructions asking it to stop” asks the model to behave, and nothing guarantees that it will. And “A handoff from the agent to the runner” is not how handoffs work: a handoff passes control to another agent, not to the runner.
Fairhaven’s agent must check stock in the company’s own warehouse system, which no hosted tool covers. What should the developer add?
- A function tool for its own system
- The hosted web search, to find it online
- The hosted file search, over manuals
- More detail in the agent’s instructions
Show the answer
A function tool for its own system
When no hosted tool fits, a function tool wraps your own logic, here a call to the warehouse system. “The hosted web search, to find it online” searches the public internet, where Fairhaven's stock is not published. “The hosted file search, over manuals” searches files uploaded to OpenAI, not a live system. And “More detail in the agent’s instructions” would change how the agent behaves, but give it no access to the stock data.
Fairhaven’s agent gave a customer the wrong delivery date. The developer wants to see which tools it called, with which arguments, and what came back. Where should they look?
- In the instructions of the agent
- In the docstring of each tool
- In the run’s trace, its spans
- In the max_turns setting
Show the answer
In the run’s trace, its spans
The trace logs every model call and tool call as spans, with arguments and results, so the developer can see where the date went wrong. “In the instructions of the agent” sets the agent's behavior, but logs nothing about a run. “In the docstring of each tool” describes each tool to the model, not how it was used. And “In the max_turns setting” only limits how many turns a run can take.