Skip to main content

Command Palette

Search for a command to run...

Building Multi-Agent Systems with OpenAI Agents SDK (Step-by-Step Guide)

Published
4 min read

Introduction

Modern AI applications are no longer powered by a single monolithic model. Instead, multi-agent systems are becoming the standard — where specialized AI agents collaborate, each with their own responsibilities, tools, and decision boundaries.

The OpenAI Agents SDK makes this architecture clean, structured, and production-ready.

In this blog, you will learn:

  • What the OpenAI Agents SDK is

  • How agents differ from traditional chatbots

  • How to build 2 collaborating agents

  • How each agent uses at least 2 tools

  • How agents hand off tasks to each other

By the end, you’ll have a working multi-agent system.


🧠 What Is the OpenAI Agents SDK?

The Agents SDK allows you to define AI agents with:

  • Clear instructions

  • Typed outputs (Pydantic models)

  • Tool access

  • Agent-to-agent handoff

  • Guardrails & validation

Think of agents as AI microservices.


🏗 Architecture Overview

We’ll build a Research + Summary AI System:

Agents

  1. Research Agent

    • Searches topics

    • Extract key points

  2. Writer Agent

    • Summarizes

    • Generates blog-ready text

Tools

Each agent will use 2 tools:

AgentTool 1Tool 2
Research Agentweb_search()extract_keywords()
Writer Agentsummarize_text()format_blog()

📦 Installation

pip install openai-agents pydantic python-dotenv

🔐 Environment Setup

export OPENAI_API_KEY="your_api_key_here"

🛠 Tool Definitions

Tool 1: Web Search (Mock Example)

def web_search(query: str) -> str:
    return f"Search results for '{query}': AI agents, task delegation, tool usage, orchestration."

Tool 2: Keyword Extraction

def extract_keywords(text: str) -> list:
    return ["AI Agents", "OpenAI SDK", "Multi-Agent", "Tools"]

Tool 3: Summarization

def summarize_text(text: str) -> str:
    return f"Summary: {text[:120]}..."

Tool 4: Blog Formatter

def format_blog(summary: str) -> str:
    return f"""
### Final Blog Summary

{summary}

✅ Generated using OpenAI Agents SDK
"""

📐 Output Models (Structured Responses)

from pydantic import BaseModel
from typing import List

class ResearchOutput(BaseModel):
    findings: str
    keywords: List[str]

class BlogOutput(BaseModel):
    blog_text: str

🤖 Agent 1: Research Agent (Uses 2 Tools)

from agents import Agent

research_agent = Agent(
    name="Research Agent",
    instructions="""
    You research the given topic.
    Use web_search to gather information.
    Extract important keywords.
    """,
    tools=[web_search, extract_keywords],
    output_type=ResearchOutput,
)

Tools Used

  • web_search

  • extract_keywords


✍️ Agent 2: Writer Agent (Uses 2 Tools)

writer_agent = Agent(
    name="Writer Agent",
    instructions="""
    You convert research into a clean blog summary.
    Summarize the content and format it.
    """,
    tools=[summarize_text, format_blog],
    output_type=BlogOutput,
)

Tools Used

  • summarize_text

  • format_blog


🔁 Agent Orchestration (Runner)

from agents import Runner
import asyncio

async def run_agents():
    # Step 1: Research
    research_result = await Runner.run(
        research_agent,
        "Explain OpenAI Agents SDK and multi-agent systems"
    )

    print("🔍 Research Output:", research_result.final_output)

    # Step 2: Writing
    writer_input = research_result.final_output.findings
    blog_result = await Runner.run(
        writer_agent,
        writer_input
    )

    print("📝 Blog Output:")
    print(blog_result.final_output.blog_text)

asyncio.run(run_agents())

📤 Sample Output

🔍 Research Output:
Findings: OpenAI Agents SDK enables structured multi-agent workflows...
Keywords: ['AI Agents', 'OpenAI SDK', 'Multi-Agent', 'Tools']

📝 Blog Output:

### Final Blog Summary

Summary: OpenAI Agents SDK allows developers to build structured AI agents...

✅ Generated using OpenAI Agents SDK

🔐 Why Use Agents Instead of a Single LLM?

FeatureSingle LLMAgents SDK
Role separation
Tool governance
Structured output
Agent handoff
Production readiness⚠️

🧩 Real-World Use Cases

  • RAG Systems (Retriever → Answer Agent)

  • Auto DevOps (Planner → Executor → Verifier)

  • Customer Support Bots

  • Data Analysis Pipelines

  • Autonomous AI Workflows


🚀 Best Practices

✔ Keep agents small & focused
✔ Give each agent clear tools
✔ Use typed outputs
✔ Avoid one giant agent
✔ Log agent decisions


🏁 Conclusion

The OpenAI Agents SDK enables you to build modular, scalable, production-grade AI systems using:

  • Multiple agents

  • Explicit tools

  • Clean orchestration

  • Safe handoffs

This approach mirrors how real engineering teams work, but with AI.