Skip to main content
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

1

Install AutoGen

2

Set your OpenAI API key

Code Example

This example creates two expert agents (math and chemistry) that can be consulted by a main assistant:

Run the Example

Expected Output

For the math question:
For the chemistry question:

How It Works

  1. Specialized Agents: Creates two expert agents with specific system messages
  2. AgentTool: Wraps each expert agent as a tool that can be called by the main assistant
  3. Tool Selection: The main assistant automatically determines which expert to consult
  4. 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

AgentTool Parameters

  • 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