Introduction
Group Chat orchestration in AG2 allows multiple agents to collaborate within a shared context, enabling more dynamic and complex workflows than what’s possible with just two agents or sequential interactions.
This powerful pattern streamlines coordination when you need more than two agents working together, whether they have specialized expertise or complementary roles in a workflow.
Purpose and Benefits
While two-agent chats work well for straightforward interactions and sequential chats handle predetermined workflows, Group Chat provides several unique advantages:
- Streamlined coordination: Manage multiple agents with less code and more flexible interactions
- Specialized expertise: Each agent can focus on what it does best, creating a team of specialists
- Dynamic collaboration: Agents can interact with each other and respond to new information as it emerges
- Flexible handoffs: Control can move between agents based on context, conversation state, or explicit directions
- Shared context: All agents work within the same conversation context, maintaining continuity
- Scalable complexity: Easily add new capabilities by introducing specialized agents rather than making existing agents more complex
Core Components
The below are the core components of a Group Chat orchestration:
- Agents: Conversable agents that perform specific tasks in the workflow
- Patterns: Define how agents take turns in the conversation and how they interact with each other.
- Handoffs: Control how agents pass control to each other
- Context Variables: Shared state information accessible to all agents throughout the conversation
Step-by-Step Guide: Creating a Simple Group Chat
This example demonstrates how to build a simple Group Chat system with three agents: a triage agent, a technical support agent, and a general support agent.
These agents collaborate to assist the user with a tech-related query. We use the AutoPattern
to automatically decide which agent should respond next based on the conversation context.
1. Import Modules and Configure the LLM
We start by importing necessary modules and setting up the language model configuration.
2. Create Specialized Agents
Each agent has a focused responsibility. We configure their roles using system messages.
3. Create a User Agent
The user agent simulates a real human user participating in the group chat.
4. Define the Conversation Pattern
We use AutoPattern to let the Group Manager dynamically decide which agent should take the next turn.
5. Start the Group Chat
We kick off the conversation by calling initiate_group_chat()
method:
What Happens During Execution?
The following diagram illustrates how the triage example unfolds. The user submits a message, and based on the issue, the triage agent decides whether it’s technical or general.
We are using the AutoPattern
in this example, where the next speaker is automatically selected for based on the context of the conversation.
Complete Code Example
Here’s the complete, ready-to-run code for our enhanced financial compliance system using the Group Chat pattern:
???+ info “Complete Code Example”
How to Run This Example
- Save the code above to a file (e.g.,
triage_chat.py)
- Set your OpenAI API key in your environment variable or use your preferred model provider
- Make sure you have AG2 installed:
pip install ag2[openai]
or use your preferred model provider extra - Run the script: python
triage_chat.py
Example Output
When you run this code, you’ll see a workflow similar to this:
Next Steps
Now that you understand the basics of Group Chat, the following sections will dive deeper into:
- Tools and Functions – Understand how agents use functions to perform actions and return results, a foundation for creating interactive workflows
- Context Variables – Learn how to share and maintain state across agents during conversations, providing a shared memory for your agent team
- Handoffs and Transitions – Master how to control and customize transitions between agents, both implicitly (via context) and explicitly (via handoff mechanisms)
- Patterns – Explore various orchestration patterns available (AutoPattern, RoundRobinPattern, RandomPattern, ManualPattern) and how they influence agent behavior
This sequence allows users to first understand how agents can perform actions through tools, then how they can share state with context variables, before moving on to how they can transition between each other with handoffs.