← Back to Tutorials

OpenAI Agents SDK

SDK Fundamentals

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

Basic Agent

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)

Sales Automation & Handoffs

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="...")),
    ]
)

Agents as Tools

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]
)

Safety & Guardrails

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

Deep Research Workflows

Build agents that search the web, plan research, and compile reports.

Visualization & Deployment

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()
✏️ Exercise: Build a sales outreach automation system with three agents: a Prospect Research Agent, a Personalized Outreach Agent, and a Follow-up Agent. Use handoffs to pass context between them. Add input guardrails to block PII. Deploy the final system with a Gradio chat interface.