OpenAI Agents SDK: How to Add Tools (Function Calling Quickstart)
Updated 2026-11-25
The OpenAI Agents SDK (openai-agents on PyPI) is a lightweight framework for tool-using agents. It runs the tool loop for you, turns Python functions into tools from their type hints, and supports handoffs between agents. If you have not built a raw tool loop yet, read Build your first AI agent in Python first. Attach hosted tools with Connect an AI agent to an MCP server after you configure a server such as Notion MCP or GitHub MCP.
Prerequisites
- Python 3.10+
OPENAI_API_KEYset in your environment
Steps (tool calling with @function_tool)
1. Install
pip install openai-agents
Note the import name is agents, not openai_agents.
2. Define tools with @function_tool
# app.py
from agents import Agent, Runner, function_tool
@function_tool
def get_order_status(order_id: str) -> str:
"""Look up the shipping status of an order by its ID."""
fake_db = {"A100": "shipped, arriving Friday", "A101": "processing"}
return fake_db.get(order_id, "order not found")
@function_tool
def refund_policy() -> str:
"""Return the store's refund policy."""
return "Refunds within 30 days of delivery for unused items."
The docstring becomes the tool description and the type hints become the JSON Schema.
3. Create and run the agent
support = Agent(
name="Support agent",
instructions="You help customers with orders. Use tools; never invent order data.",
tools=[get_order_status, refund_policy],
)
result = Runner.run_sync(support, "Where is order A100, and can I return it?", max_turns=6)
print(result.final_output)
max_turns caps the loop; exceeding it raises MaxTurnsExceeded.
4. Run async (for web servers)
import asyncio
async def main():
result = await Runner.run(support, "Status of A101?")
print(result.final_output)
asyncio.run(main())
5. Add a handoff to a specialist agent
billing = Agent(name="Billing agent", instructions="Handle billing and refund questions.",
tools=[refund_policy])
triage = Agent(name="Triage", instructions="Route billing questions to the Billing agent.",
handoffs=[billing], tools=[get_order_status])
print(Runner.run_sync(triage, "I was charged twice").final_output)
6. Use MCP servers as tools
from agents.mcp import MCPServerStdio
async def main():
async with MCPServerStdio(params={"command": "python", "args": ["server.py"]}) as server:
agent = Agent(name="Weather", instructions="Answer weather questions.", mcp_servers=[server])
print((await Runner.run(agent, "Weather in Oslo?")).final_output)
See Build an MCP server in Python for server.py.
Common errors
ModuleNotFoundError: No module named 'agents': install openai-agents in the same virtualenv you run from. Also check you don’t have a local folder named agents/ shadowing the package.
MaxTurnsExceeded: the agent looped. See Fix: LangGraph recursion limit and Fix: AI agent stuck in a loop.
RuntimeError: This event loop is already running (Jupyter): use await Runner.run(...) instead of run_sync.
Tracing warnings without an OpenAI key: if you use another provider, disable tracing with set_tracing_disabled(True) from agents.
FAQ
How do I add a tool in the OpenAI Agents SDK?
Decorate a Python function with @function_tool. The SDK reads the type hints, runs the tool loop, and you call it with a sync or async runner.
What limits a runaway agent?
max_turns. Set it on the run so a tool loop cannot continue without a bound.
Does this guide cover handoffs?
It includes a minimal billing handoff in step 5. For handoff() callbacks, input_type, and filters, see OpenAI Agents SDK handoffs.
Related guides
- Build your first AI agent in Python (raw tool loop before the SDK)
- Connect an AI agent to an MCP server (HostedMCPTool from Python)
- GitHub MCP server (issues and repos as MCP tools)
- OpenAI Agents SDK handoffs (multi-agent routing)
- Build an AI agent that triages Gmail (Gmail tools in a Python agent)
- Fix: LangGraph recursion limit (when
max_turnsis not enough) - Fix: CrewAI tool not found (another multi-agent stack)
- What is an AI agent? (concepts behind tool calling)