How OpenAI’s SWARM Simplifies Multi-Agent Systems

LARRUZ walks through OpenAI's Swarm, a lightweight framework for multi-agent systems built on two ideas: agents (system prompt plus functions) and handoffs (passing control between them). A triage-to-sales demo runs in 30 lines of code, but Swarm keeps state management and memory on you.
Chapters
On The Friday evening, OpenAI released Swarm, which is their lightweight multi agent orchestration framework. It's their take on how to build multi agent systems using OpenAI models. And I must say their implementation is extremely clean without any abstractions compared to some of the other multi agent frameworks that we have seen. The closest I have seen to something like this is going to be Transformers Agents 2.0 which is a multi agent framework from Hugging Face, but I think it's not being used enough. Now keep in mind it's not an official product from OpenAI, it's just their take on how you can build multi agent systems and that's why they are not going to be reviewing PRs and issues. But this package is released under MIT license and there are two main concepts which they are trying to highlight. One is handoff and the other one is routines. Think of routines as agents and handoff is basically transferring execution from one agent to another agent. There is a detailed blog post orchestrating agents routines and hands off which basically explains how Swarm was created. And I'll highly recommend everyone to go through this blog post. It explains the whole concept in a lot more details. Now in this video I'm going to walk you through some of the concepts. I'll also show you a few code examples of how you can set this up. But in a later video we're going to have a more detailed deep dive. And I also really like the their definition of agents because I think people over complicates the concepts of agents. Now according to OpenAI, an agent simply encapsulates a set of instructions with a set of functions. So basically it's an LLM with a system prompt that has access to a set of functions that it can use to execute some user instructions. Now how do you use this? It's dead simple. So there are two concepts. You will first need to import Swarms, which is the main orchestration framework and then agent. As I said, agent has two parts. One is system instructions and then a set of instructions that it can execute. Now you can define an agent like this. So you provide agent name or Persona, then the system instruction along with the functions that it can use. So in this case there is only one function which is transfer to Agent B. Then we have another agent which is Agent B. In this case we only provide a system instruction. Now you execute this exactly the same way you would do a chat completion API endpoint. But then whenever we call Agent A, then Agent A can use this transfer to agentb function which is essentially function calling to Transfer control or handoff control to Agent B. And you only need to understand these two concepts. The concept of routines, routines is basically your agent. And the second is that you can transfer control to another agent just using a function call and that is going to be your handoff. The focus of the Swarm is agent coordination and execution in a highly controllable and easily testable way. And if you think about the way they have designed their multi agent framework system, it's basically a state machine with some conditionals. And then there are just two abstractions in this whole framework as we discussed. One is agents and the other one is handoffs. And as you can see, it's not an official OpenAI product by just looking at spelling mistakes in this GitHub repo. Now, why build this multi agent framework? So Swarm is lightweight, scalable and highly customizable by design. And since it's a state machine, you can really customize it the way you want. Now, it's very different from the assistance API that is a hosted solution from OpenAI. But here it's optimal for developers who want full transparency and fine, gain control over contact steps and tool calls. And this will run on the client side, much like the chat completion API. Now, unlike some of the other multi agentic frameworks, this is not a full fledged solution. There is no memory. If you want to add memory, you'll have to implement it your own way. But the idea is basically you have triage assistant or a triage triage agent. And then depending on the solution that you want to implement, you can have multiple agents. Each of these agents can have multiple different tools, but each of the agent is doing one specific task and depending on your need, you just transfer the control to that specific agent. The repo provides a lot more details which I'm going to cover in a separate video. Now, the way it's set up, it's very similar to a simple function calling. So here's an example of how function calling works for different LLMs. When the user query comes in, an LLM has access to a set of different tools. Then depending on the user query, the LLM can determine or decide which tool to use. Now, in this case, instead of tools or function, it can be another agent. So let's say your user query comes in and it needs to call another agent. It can just pick this from a list of tool calls or function calls, but that function call is going to transfer execution to another agent and then you get response from that agent and this whole loop continues. In order to understand this, they have provided a few examples in the repo. We are going to specifically look at this example of us setting up basic triage to hand off to the right agent. So this accepts a user input and chooses whether to respond directly or triage request to sales or refund agents. So here is how they have implemented this whole system. And probably close to, I would say like 30, 35 lines of code. If you remove all the spaces. There are three different agents. One is the triage agent which determines which agent is best suited to handle users request and transfer the conversation to that agent. So this is basically the master agent that receives a user input. Then there is another agent which is a sales agent. So be super enthusiastic about selling bees. And then there is a third agent which is your refund agent which help the user with the refund. If the reason is that it was too expensive, offer the user some sort of discount. Right now this is the only agent that has access to two different functions directly. One is process refund and the second one is apply discount. And you can see here are those two functions that the refund agent has access to. But since we want to have a continuous conversation between the user and the triage agent. So there are two or three different other functions. One is transfer back to triage agent. So this is a function that we are going to make available to all of the to the sales agent and the refund agent. Because at some point they'll have to transfer control back to the triage agent. Then there is another function, transfer to sales and transfer to refund. Now the way it's set up is that the triage agent initially did not include any functions. But here we are adding these two functions. So depending on the nature of the conversation, it will either transfer the control or hand off the control to sales agent or refund agent. Now for the sales agent it needs to transfer controls back to the triage agent at some point. So we append this function. So essentially it's going to have access to a single function and that is going to be how do you transfer control to the triage agent. Now the refund agent already has two functions, but we also append this transfer back to triage agent as another possible function. And the main code looks something like this. So there is a run demo loop which is basically an infinite while loop that is receiving input from the user and then having a conversation with the triage agent. It's a very well thought out approach. You can create a state machine and you can define the points at which you will need to transfer control to another or sub state machine and basically you can implement that in the code. These LLMs are now smart enough to decide when to transfer control to another sub agent, and I personally think of this as an extension of the function calling or tool user usage that most of us are already familiar with. But in this case instead of the tools, now you're going to be transferring control to another agent and that agent is going to have its own set of tools that it can use. And if you want to understand how to how this whole thing is built, they have this highly detailed blog post on it. You can use this blog post to create your own frameworks. You don't even have to use Swarm if you don't need to. And it's great to see that OpenAI is open sourcing some of their research or at least design patterns, even though if the models are not open source. I'll probably create more detailed videos once I get to place play with the framework, but it looks very interesting. I hope they will continue building or at least adding to this, but let's see where this takes us. Anyways, I hope you found this video useful. Thanks for watching and as always, see you in the next one.










