Multi-agent Orchestration through OpenAI’s Swarm – A Hands-on Guide

OpenAI's Swarm framework explores multi-agent orchestration, showcasing simple routines and handoffs in action.

OpenAI recently released Swarm, an open-source, lightweight, experimental framework that explores multi-agent orchestration interfaces. Swarm is currently in the experimental phase and is not intended to be used in production; therefore, it has no official support. Its primary goal is to show the routines and handoffs and how they can be used to orchestrate multiple agents simply and easily. This article explores OpenAI’s Swarm through a practical application

Table of Contents

  1. Understanding Swarm
  2. Core-components of Swarm
  3. Key features and benefits of Swarm
  4. Hands-on implementation of Swarm 

Understanding Swarm

OpenAI’s Swarm is an open-source multi-agent orchestration framework that enables users to build and manage multi-agent systems through the usage of routines and handoffs. These notions help the users build and execute multi-agent systems through a holistic approach rather than using independent capabilities and instructions that are difficult to encode into a single prompt. Swarm is powered by OpenAI’s Chat Completions API and is stateless between calls. 

Swarm is used by instantiating a Swarm client which is nothing more than a simple OpenAI client. This client uses the run() function which is analogous to the chat.completions.create() function in the OpenAI Chat Completions API. The run() function of the client takes messages and returns messages without saving state between calls. 

Swarm’s client.run() implements a loop that gets a completion from the current agent in execution, executes the tool calls and append results, switches agents if required, updates context variables and returns the response. Once the run is finished, it will return a response containing the updated state. 

Core-components of Swarm

Swarm makes agent coordination and execution lightweight and easily testable through two primitive abstractions – Agents and Handoffs. The agent is composed of instructions with a set of functions and can choose to hand off a conversation to another agent as required. The agent performs the tasks whereas the handoffs enable one agent to transfer control to another agent, enabling agent communication based on the context of the task. 

The key agent fields used in Swarm are shown below: 

Agent Fields in Swarm (Source)

Instructions, on the other hand, are directly converted into the system prompt of a conversation. Only the instructions of an active agent will be present at any given time. If there is a handoff, the system prompt will change but the chat history will persist. A sample agent, for the Google search task, is shown below: 

Another important notion in Swarm is known as Routines which are predefined in natural language as a list of instructions along with the functions required to execute them. This allows the simplification of workflows through manageable steps, enabling a high degree of control. A sample routine, showcasing intelligent searching, is shown below: 

A handoff can be used for transferring control from one agent to another within the Swarm framework. A sample handoff transference is showcased below: 

Key features and benefits of Swarm

Swarm is still in an experimental phase and lacks features corresponding to memory, advanced agentic deployments and integrations. But, it possesses certain unique features enlisted below: 

  1. Execution of multiple-agents in a controlled manner 
  2. Handling diverse and independent capabilities
  3. Managing multi-step processes where the agents can handle different tasks and the handoffs enabling seamless coordination. 
  4. AI applications involving multiple agents working together towards achieving a common goal is very simple and easy using Swarm framework. 

Hands-on implementation of Swarm

In this section, we will build a simple web data analyst using Swarm, Firecrawl and SerpAPI. 

Pre-requisites: 

  1. OpenAI API Key
  2. Firecrawl API Key
  3. SerpAPI API Key

Step 1: Installing and importing the necessary libraries – 

Step 2: Loading the environment having the required APIs – 

Step 3: Initialising the APIs – 

Step 4: Defining functions for agents – 

Step 5: Defining handoffs for context variable updations – 

Step 6: Defining the agents – 

Step 7: Testing the Swarm through REPL on command line. 

Output (in Terminal):

We can see the Swarm framework integrated with Firecrawl and SerpAPI was able to scrape and analyse the website, generating proper results in JSON format as per the agent definitions. 

Final Words

OpenAI’s educational framework Swarm is open-sourced under MIT License enables users to work alongside and enhance its capabilities. This is a great step towards fostering a collaborative environment and enables active expansion in the field of multi-agent orchestration. Swarm lets users develop multi-agent orchestrations with a huge efficiency and comfortability based on the lightweight and controllable features demonstrated in the framework. 

References

  1. Link to Code
  2. OpenAI Swarm GitHub Repo
  3. OpenAI Cookbook – Orchestrating Agents using Swarm
Picture of Sachin Tripathi

Sachin Tripathi

Sachin Tripathi is the Manager of AI Research at AIM, with over a decade of experience in AI and Machine Learning. An expert in generative AI and large language models (LLMs), Sachin excels in education, delivering effective training programs. His expertise also includes programming, big data analytics, and cybersecurity. Known for simplifying complex concepts, Sachin is a leading figure in AI education and professional development.

The Chartered Data Scientist Designation

Achieve the highest distinction in the data science profession.

Elevate Your Team's AI Skills with our Proven Training Programs

Strengthen Critical AI Skills with Trusted Generative AI Training by Association of Data Scientists.

Our Accreditations

Get global recognition for AI skills

Chartered Data Scientist (CDS™)

The highest distinction in the data science profession. Not just earn a charter, but use it as a designation.

Certified Data Scientist - Associate Level

Global recognition of data science skills at the beginner level.

Certified Generative AI Engineer

An upskilling-linked certification initiative designed to recognize talent in generative AI and large language models

Join thousands of members and receive all benefits.

Become Our Member

We offer both Individual & Institutional Membership.