Multi-Agent Coordination

Multi-agent systems enable specialized agents to work together by processing events in parallel and communicating through custom events and agent handoffs.

Pattern Overview

This pattern involves:

  • Speaking Agent: Only one agent can send voice responses to the user at a time
  • Background Agents: Process events and emit insights without speaking
  • Custom Events: Enable communication between agents
  • Agent Handoffs: Transfer speaking rights between agents

Key Components

Events

  • Custom Events: Define communication between agents
  • AgentHandoff: Transfer speaking rights to another agent
  • Built-in Events: All agents can process user input and system events

Nodes

  • Speaking Node: Authorized to send AgentResponse events to the user
  • Background Nodes: Process events and emit insights for other agents
  • Specialized Nodes: Each focuses on specific tasks or domains

Routes

  • Parallel Processing: Multiple agents process the same user input
  • Event Broadcasting: Custom events flow between agent bridges
  • Authorization Control: Only authorized agent can respond to user

Example: Sales with Background Lead Analysis

1from pydantic import BaseModel
2
3# Custom events for inter-agent communication
4class LeadIdentified(BaseModel):
5 company_name: str
6 contact_info: dict
7 interest_level: str
8
9class CompanyResearch(BaseModel):
10 company_name: str
11 industry: str
12 revenue: str
13 decision_makers: list
14
15async def setup_multi_agent_system(system: VoiceAgentSystem):
16 # Main sales agent (speaking)
17 sales_node = SalesNode(system_prompt="You are a helpful sales representative.")
18 sales_bridge = Bridge(sales_node)
19 system.with_speaking_node(sales_node, bridge=sales_bridge)
20
21 # Background lead extraction agent
22 leads_node = LeadExtractionNode()
23 leads_bridge = Bridge(leads_node)
24 system.with_node(leads_node, leads_bridge)
25
26 # Background research agent
27 research_node = CompanyResearchNode()
28 research_bridge = Bridge(research_node)
29 system.with_node(research_node, research_bridge)
30
31 # Route user input to all agents
32 sales_bridge.on(UserTranscriptionReceived).map(sales_node.add_event)
33 leads_bridge.on(UserTranscriptionReceived).map(leads_node.add_event)
34
35 # Sales agent responds to user
36 (
37 sales_bridge.on(UserStoppedSpeaking)
38 .stream(sales_node.generate)
39 .broadcast()
40 )
41
42 # Background lead analysis
43 (
44 leads_bridge.on(UserStoppedSpeaking)
45 .stream(leads_node.generate)
46 .broadcast()
47 )
48
49 # Research agent processes identified leads
50 research_bridge.on(LeadIdentified).map(research_node.add_event)
51 (
52 research_bridge.on(LeadIdentified)
53 .stream(research_node.generate)
54 .broadcast()
55 )
56
57 # Sales agent receives research insights
58 sales_bridge.on(CompanyResearch).map(sales_node.add_context)

Agent Handoff Example

1# Transfer conversation to technical specialist
2class TechnicalSupportRequest(BaseModel):
3 issue_type: str
4 complexity: str
5
6# Sales agent identifies need for handoff
7async def process_context(self, context):
8 if technical_question_detected:
9 yield AgentHandoff(
10 target_agent="technical_support",
11 reason="Customer has technical questions about implementation"
12 )
13
14# Set up handoff routing
15tech_bridge.on(TechnicalSupportRequest).map(
16 lambda _: AgentHandoff(target_agent="technical_support")
17).broadcast()

Best Practices

  1. Clear Separation: Each agent should have distinct responsibilities
  2. Custom Events: Define typed events for inter-agent communication
  3. Background Processing: Use non-speaking agents for analysis and insights
  4. Contextual Handoffs: Transfer agents based on conversation context
  5. Authorization Management: Only one agent should speak at a time

Common Use Cases

  • Sales with Research: Sales agent with background lead analysis and research
  • Support with Escalation: General support with specialist handoffs
  • Multi-lingual: Language detection with appropriate agent routing
  • Form Filling: Conversation agent with form validation agent
  • Analysis Pipeline: Multiple analysis agents feeding insights to main agent

This pattern enables sophisticated agent coordination while maintaining clear conversation flow and user experience.