The OpenAI Agents SDK provides a lightweight, Python-native way to build agents. Core classes: Agent, Runner, and Trace.
pip install openai-agents python-dotenv
from agents import Agent, Runner
agent = Agent(
name="Assistant",
instructions="You are a helpful assistant.",
model="gpt-4o"
)
result = Runner.run_sync(agent, "What is the capital of France?")
print(result.final_output)
Create specialized sales agents and use handoffs to route between them.
from agents import Agent, Runner, handoff
triage_agent = Agent(
name="Triage Agent",
instructions="Route to the right sales agent.",
handoffs=[
handoff(Agent(name="Product Agent", instructions="...")),
handoff(Agent(name="Pricing Agent", instructions="...")),
]
)
One agent can call another agent as a tool within its own workflow.
from agents import Agent, function_tool
@function_tool
def search_knowledge_base(query: str) -> str:
"""Search the knowledge base for information."""
return f"Results for: {query}"
agent = Agent(
name="Support Agent",
tools=[search_knowledge_base]
)
Add input and output guardrails to prevent misuse.
from agents import Guardrail
class PIIGuardrail(Guardrail):
async def check_output(self, text: str):
# Check for PII patterns
if "@" in text and ".com" in text:
return False, "Output contains email address"
return True, None
Build agents that search the web, plan research, and compile reports.
Use Gradio to add a chat UI, then deploy to HuggingFace Spaces.
import gradio as gr
def chat(message, history):
result = Runner.run_sync(agent, message)
return result.final_output
gr.ChatInterface(chat, title="AI Agent").launch()