Build AI agents with OpenAI Agents SDK
The OpenAI Agents SDK is a minimal, opinionated framework for building agents on top of GPT models. It bundles tool calling, handoffs between agents, and tracing.
Prerequisites
- +Python 3.10+
- +OpenAI API key with model access
- +pip
Step-by-Step
- 1
Install the SDK
The package is openai-agents. It depends on openai>=1.x and pydantic.
pip install openai-agents - 2
Define a basic agent
An Agent is name + instructions + tools + model. Keep instructions short and operational.
from agents import Agent, Runner triage = Agent( name='Triage', instructions='Route the user request to the right specialist agent.', model='gpt-4o-mini' ) - 3
Add tools
Tools are typed Python functions. The SDK auto-generates JSON schemas from your signatures.
from agents import function_tool @function_tool def get_weather(city: str) -> str: """Return current weather for a city.""" return fetch_weather(city) - 4
Set up handoffs
Handoffs let one agent delegate to another. The SDK handles the transcript reset.
weather_agent = Agent(name='Weather', instructions='You answer weather questions.', tools=[get_weather]) triage.handoffs = [weather_agent] - 5
Run with Runner
Runner.run is the entry point. It returns a result object with output, messages, and trace metadata.
result = Runner.run_sync(triage, 'What is the weather in Tokyo?') print(result.final_output) - 6
Add tracing
Set OPENAI_AGENTS_DISABLE_TRACING=0 and view runs in the OpenAI dashboard. Critical for debugging in prod.
Common Pitfalls
- !Skipping type hints on tools breaks schema generation.
- !Too many handoff candidates confuses the triage agent.
- !Running without tracing in prod means flying blind.
Agent Hub
Run, monitor, and orchestrate your agent fleet from one dashboard. Built for multi-agent teams.
What's Next
- ->Add guardrails to validate inputs and outputs.
- ->Persist conversation state for multi-turn use.
