Skip to main content
This document covers detailed usage examples and component references for multi-agent orchestration in AgentScope.

Orchestration Paradigms

AgentScope supports two primary orchestration paradigms for building multi-agent applications:

Master-Worker Pattern

A central agent coordinates and delegates tasks to specialized workers. Best for task decomposition, routing, and hierarchical workflows.

Conversation / SOP / Workflow

Agents communicate peer-to-peer via Msg objects. Best for multi-agent discussions, simulations, and message-driven pipelines.

Master-Worker Pattern

In the master-worker pattern, a central agent (master) coordinates and delegates tasks to subordinate agents (workers). The master agent controls the execution flow and decides which worker should handle each task. Key characteristics:
  • Centralized control and decision-making
  • Explicit task delegation and routing
  • Workers typically don’t communicate directly with each other
  • Clear hierarchical structure
Use cases:
  • Task decomposition and parallel execution
  • Specialized agents for different domains (e.g., code generation, web search, data analysis)
  • Dynamic agent creation based on task requirements
  • Complex workflows requiring orchestration logic
Implementation approaches:
  • Explicit routing: Use structured output or tool calls to route tasks to different agents
  • Agent-as-tool: Wrap sub-agents as tool functions that the master agent can invoke

Conversation/SOP/Workflow Pattern

In the conversation pattern, agents communicate by broadcasting and passing Msg objects among themselves. There’s no central controller — agents interact peer-to-peer, and the execution flow emerges from their interactions. Key characteristics:
  • Decentralized communication
  • Message-driven coordination
  • Agents observe and respond to each other’s messages
  • Flexible, dynamic interaction patterns
Use cases:
  • Multi-agent discussions and debates
  • Collaborative problem-solving
  • Simulations with multiple autonomous entities
  • Scenarios requiring peer-to-peer communication
Core tools:
  • MsgHub: Automatically broadcasts messages among a group of agents
  • Pipeline: Provides structured execution patterns (sequential, fanout)

Combining Both Paradigms

A single application can use both paradigms. For example, a master agent might orchestrate multiple conversation groups, or agents within a conversation might delegate specialized tasks to worker agents.

Master-Worker Pattern

Explicit Routing

Route user queries to different downstream agents based on the query content. Approach 1: Structured Output
Approach 2: Tool Calls Wrap downstream agents as tool functions:

Agent-as-Tool

Wrap entire agents as tool functions to enable dynamic agent creation and delegation. The master agent invokes these tools to create and execute worker agents.
Key benefits:
  • Dynamic worker creation based on task requirements
  • Workers can have different capabilities and tools
  • Master agent focuses on planning and coordination
  • Workers are isolated and can run concurrently

Conversation/SOP/Workflow Pattern

MsgHub

MsgHub is an async context manager that automatically broadcasts messages among a group of agents:
Example output:
Dynamic participant management:

MsgHub Parameter Reference

Methods: How it works: When entering the context, MsgHub registers each participant as a subscriber of all other participants. When any participant generates a reply via __call__, the reply message is automatically sent to all other participants via their observe() method. On exit, all subscriptions are cleaned up.
Newly added participants (via hub.add()) will not receive previous messages — only future ones.
When enable_auto_broadcast=False, MsgHub only broadcasts via the announcement parameter and the broadcast() method. This is useful when you want fine-grained control over message routing.

Pipeline

Sequential Pipeline

Execute agents in order, passing output from one to the next.
Behavior: Equivalent to msg = await alice(msg); msg = await bob(msg); msg = await charlie(msg).

Fanout Pipeline

Distribute the same input to multiple agents and collect responses.
Returns: A list of Msg objects, one from each agent.
Choose enable_gather=True for performance (parallel I/O), or False for deterministic ordering.

Stream Printing Messages

Convert an agent’s internal print messages into an async generator for streaming to a web UI or other consumers.
How messages are identified: Messages with the same msg.id are considered the same message being updated (streaming). The content is accumulative (not delta), so each yield contains the latest full content.

ChatRoom

Internal forwarding loop: The ChatRoom maintains a central asyncio.Queue. When a ServerEvent is received from any agent, it is forwarded to the outgoing_queue (for the frontend) and broadcast to all other agents (excluding the sender, identified by agent_id). When a ClientEvent is received, it is distributed to all agents via handle_input().

Formatter Reference

For an introduction to Chat vs MultiAgent formatters and how to choose one, see Model — Formatter.

Formatter Table

All formatters are importable from agentscope.formatter.

Formatting Example

Example of how DashScopeMultiAgentFormatter transforms messages:
The system message is preserved as-is. All other messages are combined into a <history> section within a single user message, with each speaker’s name prefixed to their text.

Workflow Pattern Examples

This section provides practical code examples demonstrating how to implement common multi-agent orchestration patterns using both paradigms.

Conversation/SOP Pattern Examples

User-Agent Conversation (Chatbot)

The simplest pattern — a user and an agent take turns:
Use ChatFormatter (e.g., DashScopeChatFormatter) for user-agent conversations — it uses the role field to distinguish user and assistant. Use MultiAgentFormatter when more than two agents are involved.

Multi-Agent Discussion

When more than two agents are involved, use MultiAgentFormatter (e.g., DashScopeMultiAgentFormatter) and MsgHub:
The formatter converts multi-party history like this:
into a single user message with XML-tagged history, suitable for LLM APIs that only support user/assistant roles. Combined with MsgHub, a multi-agent discussion is simply:

Multi-Agent Debate

Multiple agents discuss a topic in rounds, with a moderator deciding when consensus is reached:

Concurrent Agents

Use asyncio.gather() to run agents concurrently:
Both agents start simultaneously and run in parallel (since LLM API calls are I/O-bound, asyncio handles them efficiently).
Combine MsgHub with sequential_pipeline or fanout_pipeline for more complex workflows.

Realtime Voice Chat Room

For realtime voice agent scenarios, ChatRoom orchestrates multiple RealtimeAgent instances sharing a session:
Unlike MsgHub (which works with text-based agents), ChatRoom handles ServerEvents and ClientEvents in the realtime voice pipeline. When one agent generates a response, ChatRoom forwards it both to the frontend and to other agents (excluding the sender).

Master-Worker Pattern Examples

Routing

Route user queries to different downstream agents based on the query content. Two approaches: Approach 1: Structured Output
Approach 2: Tool Calls Wrap downstream agents as tool functions:

Orchestrator-Workers (Handoffs)

An orchestrator decomposes tasks and dynamically creates worker agents: