Documentation Index Fetch the complete documentation index at: https://mintlify.com/microsoft/autogen/llms.txt
Use this file to discover all available pages before exploring further.
This example demonstrates how to create a multi-agent system where two agents communicate with each other.
What You’ll Learn
How to create multiple agents with different roles
How to use AgentTool to wrap agents as tools
How to orchestrate agent-to-agent communication
Prerequisites
Install AutoGen
pip install -U "autogen-agentchat" "autogen-ext[openai]"
Set your OpenAI API key
export OPENAI_API_KEY = "sk-..."
Code Example
This example creates two expert agents (math and chemistry) that can be consulted by a main assistant:
import asyncio
from autogen_agentchat.agents import AssistantAgent
from autogen_agentchat.tools import AgentTool
from autogen_agentchat.ui import Console
from autogen_ext.models.openai import OpenAIChatCompletionClient
async def main () -> None :
model_client = OpenAIChatCompletionClient( model = "gpt-4o" )
# Create a math expert agent
math_agent = AssistantAgent(
"math_expert" ,
model_client = model_client,
system_message = "You are a math expert." ,
description = "A math expert assistant." ,
model_client_stream = True ,
)
math_agent_tool = AgentTool(math_agent, return_value_as_last_message = True )
# Create a chemistry expert agent
chemistry_agent = AssistantAgent(
"chemistry_expert" ,
model_client = model_client,
system_message = "You are a chemistry expert." ,
description = "A chemistry expert assistant." ,
model_client_stream = True ,
)
chemistry_agent_tool = AgentTool(chemistry_agent, return_value_as_last_message = True )
# Create main assistant with access to expert tools
agent = AssistantAgent(
"assistant" ,
system_message = "You are a general assistant. Use expert tools when needed." ,
model_client = model_client,
model_client_stream = True ,
tools = [math_agent_tool, chemistry_agent_tool],
max_tool_iterations = 10 ,
)
# Ask math question
await Console(agent.run_stream( task = "What is the integral of x^2?" ))
# Ask chemistry question
await Console(agent.run_stream( task = "What is the molecular weight of water?" ))
await model_client.close()
asyncio.run(main())
Run the Example
Expected Output
For the math question:
---------- assistant ----------
[Calling math_expert...]
---------- math_expert ----------
The integral of x^2 is (x^3)/3 + C, where C is the constant of integration.
---------- assistant ----------
The integral of x^2 is (x^3)/3 + C
For the chemistry question:
---------- assistant ----------
[Calling chemistry_expert...]
---------- chemistry_expert ----------
The molecular weight of water (H2O) is approximately 18.015 g/mol.
---------- assistant ----------
The molecular weight of water is 18.015 g/mol
How It Works
Specialized Agents : Creates two expert agents with specific system messages
AgentTool : Wraps each expert agent as a tool that can be called by the main assistant
Tool Selection : The main assistant automatically determines which expert to consult
Streaming : Uses streaming for real-time output display
Key Concepts
AgentTool Wraps an agent as a tool that can be called by another agent.
System Message Defines the agent’s role, personality, and capabilities.
Tool Selection The LLM automatically decides which tool/agent to use based on the task.
Streaming Display responses in real-time as they’re generated.
Configuration Options
return_value_as_last_message: Returns only the final message from the agent (cleaner output)
description: Helps the calling agent understand when to use this tool
AssistantAgent Parameters
system_message: Defines the agent’s persona and instructions
model_client_stream: Enables streaming responses
max_tool_iterations: Maximum number of tool calls allowed
Next Steps
Tool Calling Learn how to add custom tools to agents
Swarm Pattern Implement more complex multi-agent orchestration