OpenAI Agents SDK, agents, handoffs and guardrails
The OpenAI Agents SDK is OpenAI's lightweight framework for building agents, the production successor to their earlier Swarm experiment. It deliberately has very few concepts: agents, tools, handoffs, guardrails, sessions and tracing, plus newer additions such as sandbox agents for file and shell work and realtime agents for voice. It is Python first (openai-agents, on 0.24 as of October 2026) with a JavaScript and TypeScript version as well. It works best with OpenAI models, but it can call other providers too.
Why it matters
Many agent frameworks are big because they try to cover everything. The Agents SDK takes the opposite approach: a small set of primitives that compose well, with Python functions as the glue. That makes it a good fit when you want:
- Multi agent routing without a graph. A triage agent can hand off to a billing agent or a refunds agent. Handoffs are exposed to the model as tools named like
transfer_to_<agent_name>, so routing is just tool calling. - Guardrails that run alongside the agent. Input and output guardrails validate what goes in and comes out, and a tripwire stops the run immediately. Tool guardrails can check individual tool calls.
- Built-in memory. Sessions (for example
SQLiteSession) store conversation history and prepend it on each run, so you do not manage message lists by hand. - Tracing on by default. Every run produces a trace you can view in the OpenAI dashboard, or export to other backends through trace processors.
- Hosted tools. With OpenAI's Responses API models you can attach
WebSearchTool,FileSearchTool,CodeInterpreterTool,HostedMCPToolandImageGenerationTool, which run on OpenAI's side.
How it works
The Runner drives the loop. Each turn it calls the current agent's model; if the model calls a tool, the runner executes it and loops; if it calls a handoff, the runner switches the current agent and continues; if it produces a final output, the run ends.
Rendering diagram…
Two patterns for multi agent work are supported. Handoffs transfer control: the new agent owns the conversation from then on. Agents as tools (agent.as_tool(...)) keep a manager agent in control and call specialists like functions. I default to agents as tools when I want a single voice for the user, and handoffs when specialists really should take over.
Reference architecture
A customer support setup with a triage agent, two specialists, a guardrail and session storage:
Rendering diagram…
Code
A triage agent with a tool, a handoff, a guardrail and a session. Import paths follow the current SDK docs, where decorators live in agents.decorators (the older function_tool name is still exported from agents).
import asyncio
from pydantic import BaseModel
from agents import (
Agent,
GuardrailFunctionOutput,
InputGuardrailTripwireTriggered,
RunContextWrapper,
Runner,
SQLiteSession,
TResponseInputItem,
)
from agents.decorators import input_guardrail, tool
@tool
def order_status(order_id: str) -> str:
"""Look up the shipping status of an order."""
return f"Order {order_id} shipped yesterday."
refunds_agent = Agent(
name="Refunds agent",
instructions="Handle refund requests. Ask for the order id if missing.",
)
class OffTopic(BaseModel):
is_off_topic: bool
reasoning: str
topic_checker = Agent(
name="Topic checker",
instructions="Decide if the message is unrelated to orders, shipping or refunds.",
output_type=OffTopic,
)
@input_guardrail
async def on_topic(
ctx: RunContextWrapper[None], agent: Agent, input: str | list[TResponseInputItem]
) -> GuardrailFunctionOutput:
result = await Runner.run(topic_checker, input, context=ctx.context)
return GuardrailFunctionOutput(
output_info=result.final_output,
tripwire_triggered=result.final_output.is_off_topic,
)
triage_agent = Agent(
name="Triage agent",
instructions="Answer order questions with tools. Hand off refunds.",
tools=[order_status],
handoffs=[refunds_agent],
input_guardrails=[on_topic],
)
async def main():
session = SQLiteSession("customer-42")
try:
r1 = await Runner.run(triage_agent, "Where is order 1042?", session=session)
print(r1.final_output)
r2 = await Runner.run(triage_agent, "I want my money back for it", session=session)
print(r2.last_agent.name, r2.final_output)
except InputGuardrailTripwireTriggered:
print("Sorry, I can only help with orders.")
asyncio.run(main())
The second run remembers "it" means order 1042 because of the session, and r2.last_agent tells you which agent ended up answering. If a tool requires human approval, the run returns result.interruptions; you approve them on result.to_state() and run again with that state.
Trade-offs and pitfalls
- Few primitives means you build the rest. There is no graph engine. For long, branching workflows with durable checkpoints you will either write orchestration code or pair it with a workflow engine.
- Best experience on OpenAI. Hosted tools and some features need the Responses API. Other providers work, but you lose those extras.
- Guardrails cost latency and tokens. An LLM based guardrail is another model call per turn. Use cheap models or deterministic checks where you can, and know which guardrails run in parallel with the agent and which block it.
- Handoff history grows. When an agent hands off, the new agent sees prior conversation by default. Use handoff input filters to trim what it receives.
- Pre-1.0 versioning. The SDK is at 0.x, so minor versions can include breaking changes. Pin and read release notes.
- Tracing data goes to OpenAI by default. Check that fits your data policy, or disable or redirect it.
When to pick it: you mainly use OpenAI models, you want a small, readable framework, you need simple multi agent routing with guardrails, or you want hosted web search, file search and code execution without running them yourself.
When not to: you need heavy provider neutrality, durable human approval flows over hours or days, or a large integration catalogue. LangGraph, Pydantic AI or a workflow engine may fit better.
Checklist
- Start with one agent and tools; add handoffs only when a specialist really needs to own the conversation.
- Prefer agents as tools when one agent should stay in charge.
- Add input guardrails for scope and safety, output guardrails for format and policy.
- Use a session store keyed by conversation id instead of passing message lists.
- Filter handoff inputs to control context size.
- Review traces for every new agent before shipping.
- Pin the SDK version.