Skip to main content

Authors:

Yiran Wu

PhD student at Pennsylvania State University

Mark Sze

Software Engineer at AG2.ai

As of version 0.9, we have merged the experimental Swarm functionality into the core framework, creating a new way to run group (multi-agent) chats. The new group chat contains all of the functionality available in Swarm (and more).Updating to the new group chat from your Swarm code is straightforward, see this guide on how to do it.The Swarm code has now been deprecated but will still function with some minor changes. See this guide to update your current Swarm code to work with v0.9 and up. We would advise updating your code to work with the new group chat with the Swarm code being deprecated.
AG2 now provides an implementation of the swarm orchestration from OpenAI’s Swarm framework, with some additional features! Background: the swarm orchestration is a multi-agent collaboration where agents execute tasks and are responsible for handing them off to other agents. Here are the key features of the swarm orchestration:
  • Hand-offs: Agents can transfer control to another agent, enabling smooth and direct transitions within workflows.
  • Context variables: Agents can dynamically update a shared memory through function calls, maintaining context and adaptability throughout the process.
Besides these core features, AG2 provides:
  • Simple Interface for Tool Call/Handoff Registration: When creating a swarm agent, you can pass in a list of functions to be registered directly. We also provide a simple method to register handoffs.
  • Transition Beyond Tool Calls: We enable the ability to automatically transfer to a nominated swarm agent when an agent has completed their task. We will extend this to allow other transitions in the future (e.g., use a function to determine the next agent ).
  • Built-in human-in-the-loop: Adding a user agent (UserProxyAgent) to your swarm will allow swarm agents to transition back to the user. Provides a means to clarify and confirm with the user without breaking out of the swarm.
This feature builds on GroupChat, offering a simpler interface to use swarm orchestration. For comparison, see two implementations of the same example: one using swarm orchestration and another naive implementation with GroupChat (Legacy).

Handoffs

Before we dive into a swarm example, an important concept in swarm orchestration is when and how an agent hands off to another agent. Providing additional flexibility, we introduce the capability to define an after-work handoff. Think of it as the agent’s next action after completing their task. It can be to hand off to another agent, revert to the user, stay with the agent for another iteration, or terminate the conversation. The following are the prioritized handoffs for each iteration of the swarm.
  1. Agent-level: Calls a tool that returns a swarm agent: A swarm agent’s tool call returns the next agent to hand off to.
  2. Agent-level: Calls a pre-defined conditional handoff: A swarm agent has an OnCondition handoff that is chosen by the LLM (behaves like a tool call).
  3. Agent-level: After work hand off: When no tool calls are made it can use an, optional, AFTER_WORK handoff that is a preset option or a nominated swarm agent.
  4. Swarm-level: After work handoff: If the agent does not have an AFTER_WORK handoff, the swarm’s AFTER_WORK handoff will be used.
In the following code sample a ConversableAgent named responder has:
  • Two conditional handoffs registered (OnCondition), specifying the agent to hand off to and the condition to trigger the handoff.
  • An after-work handoff (AFTER_WORK) nominated using one of the preset options (TERMINATE, REVERT_TO_USER, STAY). This could also be a swarm agent.
You can specify the swarm-level after work handoff when initiating the swarm (here we nominate to terminate):

Creating a swarm

  1. Define the functions that can be used by your ConversableAgents.
  2. Create your ConversableAgents.
  3. For each swarm agent, specify the handoffs (transitions to another agent) and what to do when they have finished their work (termed After Work).
  4. Optionally, create your context dictionary.
  5. Call initiate_swarm_chat.

Example

This example of managing refunds demonstrates the context handling, swarm and agent-level conditional and after work hand offs, and the human-in-the-loop feature.
And here’s the output, showing the flow of agents.

Notes

  • Behind-the-scenes, swarm agents are supported by a tool execution agent, that executes tools on their behalf. Hence, the appearance of Tool Execution in the output.
  • Would you like to enhance the swarm feature or have found a bug? Please let us know by creating an issue on the AG2 GitHub.

For Further Reading