OpenAI Swarm: A Hands-On Guide to Multi-Agent Systems

Nibedita Dutta Last Updated : 16 Dec, 2024
9 min read

OpenAI Swarm – launched in 2024, is an experimental framework designed to simplify the orchestration of multi-agent systems for developers. It aims to streamline the coordination of AI agents through scalable and user-friendly mechanisms, making it easier to manage interactions within complex workflows. As an open-source project available on GitHub, Swarm offers developers the opportunity to explore its features, experiment with its functionality, and contribute to its ongoing development. For machine learning professionals, it provides a powerful yet accessible tool to build and scale agent-based systems without the need for advanced orchestration skills.

openai swarm

Learning Objectives

  • Introduction to the framework and its core components.
  • Highlighting the standout features of OpenAI Swarm.
  • Distinguishing OpenAI Swarm from other multi-agent systems like Autogen and CrewAI.
  • Practical applications where OpenAI Swarm can be utilized effectively.
  • Demonstrating how to build a simple Brand Product Researcher using Wikipedia data with OpenAI Swarm.

This article was published as a part of the Data Science Blogathon.

What is OpenAI Swarm?

OpenAI Swarm is a tool that helps manage multiple AI agents working together. It makes it easy to control and customize how these agents communicate and work on tasks. Think of it like a team of robots that can pass work between each other to get things done. Swarm is especially helpful when you need different agents to handle different parts of a task, or when the environment changes and agents need to make decisions based on that. It’s like having a team that can quickly adapt to solve problems.

  • Agents in Swarm. In Swarm, every AI agent has its own job or role, like a “Sales Agent,” and a list of tasks it can perform. The functions needed for the agents are automatically organized into JSON structures, which makes it easy for the agents to work together and get things done without any problems.
  • Handoffs to Other Agents. Swarm lets agents pass work to each other based on what’s happening in the conversation or specific rules. When one agent finishes its task, it can hand over to the next agent, making sure the work keeps going smoothly and each task is handled by the right agent.
  • Context Variables. Swarm uses something called context variables to help agents remember and share important information with each other. These variables give the agents a starting point and are updated as the conversation goes on, making sure everything stays consistent and makes sense.

Key Features of OpenAI Swarm

  • Multi-Agent Coordination: Swarm allows multiple AI agents to work together and handle different tasks, enabling efficient teamwork.
  • Customizable Roles: Each agent can be assigned a specific role (e.g., “Sales Agent,” “Support Agent”), making it easy to define their tasks and responsibilities.
  • Dynamic Handoffs: Agents can smoothly pass tasks to each other based on the conversation flow or specific conditions, ensuring work is completed by the right agent.
  • Context Sharing: Swarm uses context variables to help agents share important information with each other, ensuring consistency throughout a conversation or task.
  • Scalable Design: The framework is built to scale, allowing developers to manage complex systems with many agents efficiently.
  • Open-Source: Swarm is open-source and available on GitHub, making it accessible for developers to explore, experiment with, and contribute to its development.
  • Easy Integration: Swarm is designed to be simple to use and integrate with other systems, making it easier for developers to build multi-agent applications.

OpenAI Swarm vs Other AI agents

Coordination Between Agents

Agents in CrewAI are structured with defined roles and responsibilities. Each agent is assigned a specific “Task” object that outlines the type of work it can perform, providing clarity and specificity in agent functions. 

Swarm allows for more flexible agent behavior without imposing strict task limits. Agents can act independently and do not require a centralized manager as required in CrewAI, promoting a decentralized approach to task execution.

Autogen provides even more flexibility by emphasizing on dynamic collaboration among agents that can adjust their roles based on real-time task demands. Agents can work in pairs or groups, making the collaboration more fluid and adaptable compared to the more structured approach of OpenAI Swarm.

Memory Management

Swarm maintains persistent context by storing information through context_variables across agent interactions. Autogen provides a similar memory object to track relevant data for agent communication. CrewAI differentiates itself with an advanced memory object that manages both short- and long-term memory, automatically generating embeddings for key terms and memories.

Tools Utilized

Swarm defines functions with docstrings, which are useful for general purposes but can be challenging when detailed parameter descriptions are needed. Autogen uses function annotations to simplify customization of agent capabilities by specifying parameters. CrewAI allows agents to utilize tools from its own toolkit or Langchain, offering good compatibility.

AutoGen is particularly strong in code generation and managing intricate multi-agent programming workflows. On the other hand, OpenAI Swarm and CrewAI are designed to be user-friendly, making them perfect for beginners in multi-agent AI, as they don’t require complicated setups.

Also Read: How to Build AI Agents Using “Tool Use”?

Use Cases of OpenAI Swarm

Some of the potential use cases of a open AI Swarm could be the following –

  • Virtual Customer Support: Specialized agents can efficiently tackle distinct types of customer queries, such as resolving billing issues or addressing technical glitches, while routing complex concerns to the appropriate expert.
  • Smart Personal Assistance: Autonomous agents can collaborate to handle everyday responsibilities, including managing schedules, sending reminders, and drafting email responses, offering comprehensive support for daily activities.
  • Real-time Data Workflows: In dynamic data environments, agents can seamlessly handle various stages such as gathering, analyzing, and generating insights from data streams, ensuring a streamlined pipeline.
  • Enhanced Retail Interactions: Swarm agents can work together in e-commerce settings to assist with customer inquiries, recommend suitable products, and process returns, creating a unified and efficient customer journey.

Simple Product Researcher using OpenAI Swarm – Python Implementation

          ┌─────────────────────────────────┐
│ SWARM ORCHESTRATOR │
│ - Receives user input │
│ - Initializes context_variables │
│ - Selects entry agent │
│ (wiki_agent) based on task │
└───────┬─────────────────────────┘
│ CONTEXT_VARIABLES SET
v
┌─────────────────────────────────┐
│ WIKI_AGENT │
│ Instructions: "Retrieve brand │
│ info from Wikipedia." │
└───────┬─────────────────────────┘
│ CALL FUNCTION: wikipedia_lookup(report_text)
v
┌─────────────────────────────────┐
│ WIKIPEDIA_LOOKUP() │
│ - Retrieves Wikipedia summary │
│ - Returns summary to WIKI_AGENT │
└───────┬─────────────────────────┘
│ SUMMARY RETURNED
v
┌─────────────────────────────────┐
│ WIKI_AGENT │
│ - Updates context_variables │
│ with retrieved summary │
│ - Handover to product_agent │
└───────┬─────────────────────────┘
│ CALL FUNCTION: transfer_to_product_agent()
v
┌─────────────────────────────────┐
│ PRODUCT_AGENT │
│ Instructions: "List key products │
│ from the retrieved summary only."│
└───────┬─────────────────────────┘
│ READ FROM context_variables (summary)
v
┌─────────────────────────────────┐
│ PRODUCT_AGENT (PROCESSING) │
│ - Extracts products from summary │
│ - Formats them into bullet points│
└───────┬─────────────────────────┘
│ RETURN FINAL RESULT
v
┌─────────────────────────────────┐
│ SWARM ORCHESTRATOR │
│ - Receives final product list │
│ - Returns response to user │
└───────┬─────────────────────────┘
│ RESPONSE
v
┌─────────────────────────────────┐
│ USER │
│ Receives bullet-pointed product │
│ list as final answer. │
└─────────────────────────────────┘

Step 1. Install the Library

!pip install git+https://github.com/openai/swarm.git 
!pip install wikipedia

We would be using the wikipedia library for this use case and hence would install it at this stage as well.

Step 2. Define the OpenAI API key

import os
os.environ['OPENAI_API_KEY']=''

Step 3. Import necessary Python Libraries

from swarm import Swarm, Agent
import wikipedia

We would be using the wikipedia API for extracting information for this usecase. Hence, we install the wikipedia library as well.

Step 4. Defining the Functions


def transfer_to_product_agent():
    return product_agent 

def wikipedia_lookup(context_variables):
    try: 
      print("context variable",context_variables["report_text"])
      summ = wikipedia.page(context_variables["report_text"]).summary
      print("SUMM",summ)
      return summ
    except: return None
    

transfer_to_product_agent() – This function returns the product_agent object, which is defined in the next step (Step 5). The role of this function is to facilitate transferring control to a specialized agent for generating or handling summaries.

wikipedia_lookup() – This function attempts to retrieve a summary of a Wikipedia page based on the provided context_variables.

Step 5. Defining the Agents

product_agent = Agent(
    name="Product Agent",
    instructions="LIST NAME OF the key PRODUCTS BY THE BRAND ONLY FROM THE RETRIEVED WIKIPEDIA INFORMATION in Bullet points. ONLY USE THE RETRIEVED INFORMATION to list the key products.DO NOT USE INFORMATION FROM OUTSIDE",
)


wiki_agent = Agent(
    name="Agent",
    instructions="""You are a helpful agent that answers user queries by finding and analysing information from Wikipedia.
                    You will be given a BRAND NAME and you must retrieve it's entry on Wikipedia and then hand over to the Summary Agent.""",
    functions=[wikipedia_lookup, transfer_to_product_agent],
)

The provided code defines two agents, product_agent and wiki_agent, with specific roles and responsibilities

product_agent

  • Purpose: Focuses on identifying and listing the key products associated with a brand, strictly using information retrieved from Wikipedia.
  • Definition:
    • The Agent object is initialized with:
      • name: “Product Agent”, representing its identity.
      • instructions: Detailed guidance specifying its role.
    • The instructions enforce strict usage of retrieved Wikipedia information and prohibit reliance on external data.
    • Output Format: Key products are listed as bullet points.

wiki_agent

  • Purpose: Handles Wikipedia lookups for brand-related queries and transfers the information to another agent for further processing.
  • Definition:
    • The Agent object is initialized with:
      • name: “Agent”, identifying its function.
      • instructions: Specifies its role as a helpful assistant using Wikipedia to answer user queries.
        • It retrieves information about a brand and hands it off to the next step (
    • Functions:
      • wikipedia_lookup: A previously defined function that retrieves the summary of a Wikipedia page based on input.
      • transfer_to_product_agent: Likely transfers control to product_agent, enabling it to list the products after Wikipedia data is retrieved.

How it Works Together?

1. A user query provides a brand name.

2. `wiki_agent`:

– Searches for the brand on Wikipedia using the `wikipedia_lookup` function.

– Hands over the retrieved information to `product_agent` (or a similar processing agent).

3. `product_agent`:

– Extracts the product information from the Wikipedia content.

– Lists the products in bullet points.

Step 6. Execute the Multi-agent System

client = Swarm()
# Run summary agent
text = "philips"
response = client.run(
    agent=wiki_agent ,
    messages=[{"role": "user", "content": text}],
    context_variables={"report_text": text}
)
print(response.messages[-1]["content"])

client.run: The method run is called on the client object, indicating it’s responsible for sending a query to the AI agent and returning a response.

Parameters:

  1. agent: Specifies the agent that will handle the query.
  2. messages=[{“role”: “user”, “content”: text}]:
    This defines a list of messages, with one dictionary item representing a message from the user (“role”: “user”). The “content” key contains the text input (“philips” in this case).
  3. context_variables={“report_text”: text}:
    Additional context variables passed to the agent for processing. Here, it provides the same text as “report_text”. This might be used by the agent for customization or additional logic during the response generation.

Output

"

As we can see from the output, all the key products of the Philips brand have been listed here. The key product information, as instructed in the agentic system, is retrieved from the wikipedia page. We can see that in the output text fetched from the wikipedia page.

Output from Wikipedia Page

 Output From Wikipedia
Output From Wikipedia

Handover of Agents 

#Fetch the name of the last executed agent
response.agent.name
"

As we can see from the image above, the last executed agent was “Summary agent”, even though we ran the client on the “wiki_agent”. This means internally transfer to the Summary agent was done post extraction of information from Wikipedia on the “Philips” brand. The Summary agent, as instructed, gave us the list of the key products of “Philips” from the extracted information.

Conclusions

In conclusion, OpenAI Swarm provides a robust framework for managing and coordinating multiple AI agents effectively. Its ability to assign specific roles, utilize JSON-based task structures, and enable seamless task handoffs ensures efficiency in handling complex workflows. The use of context variables further enhances collaboration by maintaining consistency and continuity among agents. Swarm’s adaptability makes it an invaluable tool for dynamic environments requiring flexible and intelligent task management. This innovation positions it as a versatile solution for streamlining multi-agent interactions.

Key Takeaways

  • OpenAI Swarm efficiently manages multiple AI agents by assigning specific roles and tasks to each agent. Tasks are structured using JSON, enabling smooth collaboration and ensuring each agent knows its responsibilities.
  • Swarm facilitates seamless handoffs between agents based on conversational flow or predefined rules. The use of context variables ensures agents maintain and share updated information, enabling adaptive and consistent problem-solving.
  • Autogen excels in generating code and efficiently handling complex workflows involving multiple agents. In contrast, OpenAI Swarm and CrewAI prioritize simplicity and accessibility, making them ideal for beginners exploring multi-agent AI systems, as they avoid intricate configurations.
  • OpenAI Swarm showcases its versatility by enabling collaborative AI agents to handle diverse tasks, from virtual customer support and personal assistance to real-time data workflows and enhanced e-commerce interactions, ensuring efficiency and adaptability in dynamic scenarios.

The media shown in this article is not owned by Analytics Vidhya and is used at the Author’s discretion.

Frequently Asked Questions

Q1. What is OpenAI Swarm, and how does it function?

A. OpenAI Swarm is a system designed to manage multiple AI agents working collaboratively. Each agent is assigned a specific role and tasks, organized in JSON structures, which helps streamline their coordination and ensures clarity in their responsibilities.

Q2. How does OpenAI Swarm handle communication between agents?

A. Swarm facilitates smooth transitions between agents through handoff functions guided by conversational flow or predefined rules. Context variables play a key role in maintaining and updating shared information, ensuring all agents work with accurate and consistent data.

Q3. What sets OpenAI Swarm apart from other multi-agent AI tools?

A. While tools like Autogen excel in handling complex workflows and code generation, OpenAI Swarm prioritizes simplicity and accessibility. This makes it an excellent choice for beginners, as it requires minimal configuration and offers straightforward agent management.

Q4. What are some practical use cases for OpenAI Swarm?

A. OpenAI Swarm is versatile, enabling applications such as virtual customer support, smart personal assistants, real-time data workflows, and enhanced retail experiences. It ensures efficiency by delegating tasks to specialized agents and adapting to dynamic scenarios.

Q5. What is the role of context variables in Swarm?

A. Swarm uses context variables to enable agents to share and update important information. This ensures that all agents involved in the task or conversation maintain consistency and work with the most current data.

Nibedita completed her master’s in Chemical Engineering from IIT Kharagpur in 2014 and is currently working as a Senior Data Scientist. In her current capacity, she works on building intelligent ML-based solutions to improve business processes.

Responses From Readers

Clear

We use cookies essential for this site to function well. Please click to help us improve its usefulness with additional cookies. Learn about our use of cookies in our Privacy Policy & Cookies Policy.

Show details