Pavlo Golovatyy

Software Engineer specialized in AI and ML

OpenAI Agents SDK: Build Agentic AI Apps in Python with Ease

OpenAI Agents SDK: Build Agentic AI Apps in Python with Ease

The OpenAI Agents SDK is a cutting-edge toolkit designed to make the development of agentic AI applications straightforward, flexible, and efficient. By providing a minimal yet powerful set of core primitives, the SDK enables developers to craft sophisticated workflows that harness the full potential of large language models (LLMs). This SDK represents a mature evolution of the earlier Swarm experiment, optimized for production environments and real-world use cases.

At its core, the OpenAI Agents SDK offers three foundational components:

  1. Agents: These are LLMs enhanced with specific instructions and equipped with access to tools that extend their capabilities.
  2. Handoffs: A seamless mechanism that allows agents to delegate subtasks to specialized agents, ensuring expertise-driven handling of different queries.
  3. Guardrails: Built-in validators that monitor inputs and outputs throughout the agent’s lifecycle, enabling early detection and halting of execution if any anomalies or undesired behavior occur.

When combined with the power and simplicity of Python, these primitives empower developers to design and implement complex, multi-agent workflows without steep learning curves or reliance on unfamiliar languages or frameworks. Moreover, the SDK features integrated tracing functionality, allowing detailed visualization, debugging, monitoring, evaluation, and fine-tuning of agentic flows right from the OpenAI Dashboard.

Why Choose the Agents SDK

The Agents SDK is built around two key design principles that set it apart:

  • Simplicity: It offers just enough features to build real-world, production-grade applications while keeping the number of primitives low, so new users can quickly learn and start building. This intentional minimalism means you won’t be overwhelmed by complexity, making the SDK approachable for both beginners and experienced developers alike.
  • Customizability: Despite being easy to start with, the SDK allows deep customization, giving developers control over every aspect of the agents’ behavior, workflows, and validation logic. This flexibility means you can start simple but scale up to complex, domain-specific workflows as your needs grow.

Whether you are rapidly prototyping an AI assistant or deploying a multi-agent system for production, the Agents SDK streamlines integration, enhances safety, and improves maintainability across the lifecycle of your application.

Key Features

  • Agent Loop A built-in, continuous execution loop that manages calling external tools, feeding results back to the LLM, and iterating until the conversation or task reaches a natural conclusion. This loop abstracts away the complexity of managing state and conversations, letting you focus on what your agents do rather than how they execute.

  • Python-First The SDK embraces native Python constructs for orchestrating agents and tools. This means no new domain-specific languages (DSLs) or complicated APIs—just straightforward Python coding. Developers can use familiar language features like async/await, type hints, and classes to build complex AI-driven workflows without context switching.

  • Handoffs Allows a division of labor by routing questions or subtasks to specialist agents with domain expertise. For example, a science expert agent can handle physics queries, while a literature expert handles questions about classic novels. Handoffs support complex orchestration patterns, enabling you to build scalable multi-agent ecosystems where agents cooperate seamlessly.

  • Guardrails Enables parallel validation of inputs and outputs at various stages of the workflow. If a guardrail detects an issue—such as inappropriate content, invalid input format, or policy violations—it immediately stops the flow to avoid wasting resources or generating misleading results. This safety net is essential for production-grade applications to maintain quality and compliance.

  • Function Tools Any Python function can be seamlessly converted into a tool, complete with automatic generation of Pydantic schemas for input validation and type safety. This means your existing Python code can be exposed to agents as callable tools with minimal boilerplate, accelerating integration and reducing errors.

  • Tracing Automatically records every step, including LLM generations, tool calls, handoffs, and guardrail triggers. This data can be visualized and analyzed within the OpenAI Dashboard, assisting developers in debugging, monitoring, performance evaluation, and iterative fine-tuning of their agent workflows. Tracing is indispensable for understanding agent behavior in production and spotting bottlenecks or unexpected outputs early. Note that this tracing feature is disabled for organizations enforcing Zero Data Retention (ZDR) policies to protect user privacy.

Installation & Hello World

To get started with the OpenAI Agents SDK, install the package via pip and set your OpenAI API key:

pip install openai-agents
export OPENAI_API_KEY=sk-...

Here’s a minimal example demonstrating the creation and execution of a simple assistant agent that generates a motivational quote:

from agents import Agent, Runner

agent = Agent(
    name="Motivator",
    instructions="You are an encouraging assistant who creates motivational quotes."
)

result = Runner.run_sync(agent, "Give me a motivational quote about perseverance.")
print(result.final_output)

# Sample output:
# "Perseverance is not a long race; it's many short races one after the other."

This example highlights how easily you can define an agent with custom instructions and run it synchronously to generate meaningful responses.

Quickstart: Orchestrating Multiple Agents

The SDK excels in creating multi-agent systems that specialize in different fields, managed by a central triage agent that delegates queries accordingly.

  1. Set Up Your Project Environment

    mkdir smart_assistant && cd smart_assistant
    python -m venv .env
    source .env/bin/activate
    pip install openai-agents
    export OPENAI_API_KEY=sk-...
    
  2. Define Specialist Agents

    Create agents focused on different expertise, such as a Science Expert and a Literature Expert:

    from agents import Agent
    
    science_agent = Agent(
        name="Science Expert",
        handoff_description="Handles scientific questions, especially physics and chemistry.",
        instructions="You explain scientific concepts clearly and provide detailed answers."
    )
    
    literature_agent = Agent(
        name="Literature Expert",
        handoff_description="Specializes in classic and modern literature.",
        instructions="You provide thoughtful explanations and summaries of literary works."
    )
    
  3. Configure the Triage Agent

    This agent directs user questions to the appropriate specialist based on content:

    triage_agent = Agent(
        name="Central Triage",
        instructions="Analyze the question and assign it to the Science Expert or Literature Expert accordingly.",
        handoffs=[science_agent, literature_agent]
    )
    
  4. Run the Multi-Agent Workflow

    Use async execution to query the triage agent and receive answers from the correct specialist:

    from agents import Runner
    import asyncio
    
    async def main():
        response1 = await Runner.run(triage_agent, "Explain the theory of relativity.")
        print("Science Agent:", response1.final_output)
    
        response2 = await Runner.run(triage_agent, "Who wrote 'Pride and Prejudice'?")
        print("Literature Agent:", response2.final_output)
    
    asyncio.run(main())
    

This setup demonstrates how you can create a modular and extendable system where agents collaborate seamlessly based on their specializations.

Adding Guardrails for Robustness and Compliance

Guardrails act as essential checkpoints within your agent workflows to ensure inputs and outputs are valid and policy-compliant.

Here’s an example where a guardrail detects if the question is related to sensitive medical advice, stopping the flow if detected:

from agents import Agent, InputGuardrail, GuardrailFunctionOutput, Runner
from pydantic import BaseModel

class MedicalCheckOutput(BaseModel):
    contains_medical_content: bool
    explanation: str

medical_guardrail_agent = Agent(
    name="Medical Content Checker",
    instructions="Identify if the user's question requests medical advice or contains medical content.",
    output_type=MedicalCheckOutput
)

async def medical_guardrail(ctx, agent, input_data):
    result = await Runner.run(medical_guardrail_agent, input_data, context=ctx.context)
    output = result.final_output_as(MedicalCheckOutput)
    # If medical content is detected, tripwire to halt the workflow
    return GuardrailFunctionOutput(
        output_info=output,
        tripwire_triggered=output.contains_medical_content
    )

triage_agent = Agent(
    name="Central Triage",
    instructions="Route the question to the appropriate expert, unless it involves medical advice.",
    handoffs=[science_agent, literature_agent],
    input_guardrails=[InputGuardrail(guardrail_function=medical_guardrail)]
)

This guardrail example safeguards your application by stopping flows that request sensitive advice, ensuring ethical compliance and reducing risk.

Tracing: Deep Visibility into Agent Workflows

Tracing automatically logs every interaction your agents perform, from LLM calls to handoffs and guardrail checks. This rich data is available on the OpenAI Dashboard to help you analyze, debug, and optimize your AI workflows effectively.

For teams building complex multi-agent systems, this insight is invaluable to ensure reliability and maintain performance.

Benefits & Use Cases

The OpenAI Agents SDK is ideal for a broad spectrum of applications, ranging from fast prototyping to robust, scalable deployments:

  • Rapid Development: Quickly launch intelligent agents with minimal setup.
  • Full Control: Tailor every element of the agent lifecycle, including loops, handoffs, and validation.
  • Visibility & Monitoring: Leverage tracing to monitor workflows and debug issues in real time.
  • Scalability: Build multi-agent ecosystems for use cases like education, customer support, research assistance, and more.
  • Reliability: Guardrails and strict validation ensure your applications are consistent and compliant.

Summary

The OpenAI Agents SDK offers a powerful, flexible, and intuitive framework to create agentic AI applications ready for production. Its Python-first approach, combined with primitives such as handoffs and guardrails, allows developers to build complex, domain-specific workflows that are maintainable and customizable.

Whether you want to build a single AI assistant or a network of specialized agents working collaboratively, the Agents SDK equips you with all the necessary tools to bring your AI-driven workflows to life efficiently and safely.

Try the OpenAI Agents SDK today and unlock new possibilities in agentic AI applications!