communication-flows.mdx 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. ---
  2. title: "Communication Flows"
  3. description: "Understanding communication flows in Agency Swarm."
  4. icon: "comments"
  5. ---
  6. <Note>
  7. Always start with one agent. Never build two agents simultaneously unless you know exactly what you're doing. The more agents you have, the harder it becomes to train them on specific tasks.
  8. </Note>
  9. ## Orchestration Patterns
  10. Agency Swarm supports flexible orchestration patterns. Unlike all other frameworks, communication flows in Agency Swarm are **uniform**, meaning they can be defined in any way you want.
  11. | Pattern | Description | Reliability | Autonomy | Costs | When to Use |
  12. |---------|-------------|-------------|----------|-------|-------------|
  13. | **Handoff** | Control transfers completely to another agent who continues the conversation | Very High | Low | Lower | Sequential workflows where each step requires tight feedback from the user |
  14. | **Orchestrator-Worker** | One agent assigns tasks to multiple agents and compiles their responses | Lower (mitigated with guardrails) | Very High | Higher | Complex multi-step tasks with independent not-very-high stakes steps |
  15. _Scroll table to the right to see the full description of each pattern._
  16. **Examples**:
  17. - **Handoff**: Creative script writing with planner and script writer agents - requires tight user feedback at each stage to refine the story direction
  18. - **Orchestrator-Worker**: News aggregation and research from various sources - quality of individual sources isn't critical, main agent filters and compiles them
  19. ## Handoff Pattern
  20. Control transfers completely to another agent who takes over the interaction. The receiving agent gets full conversation history and continues from there.
  21. ```python
  22. from agency_swarm import Agent
  23. from agency_swarm import Handoff
  24. # Triage agent routes to specialists
  25. triage_agent = Agent(
  26. name="TriageAgent",
  27. instructions="Assess customer inquiries and route to appropriate specialist",
  28. )
  29. billing_specialist = Agent(
  30. name="BillingSpecialist",
  31. description="Handles billing inquiries, payment issues, and invoice questions",
  32. instructions="Resolve customer billing issues with full context from previous conversation",
  33. )
  34. technical_specialist = Agent(
  35. name="TechnicalSpecialist",
  36. description="Handles technical support and troubleshooting",
  37. instructions="Provide technical support with full conversation history",
  38. )
  39. agency = Agency(
  40. triage_agent,
  41. communication_flows=[
  42. (triage_agent, billing_specialist, Handoff), # Handoff to billing
  43. (triage_agent, technical_specialist, Handoff), # Handoff to technical
  44. ]
  45. )
  46. ```
  47. The TriageAgent transfers control completely to the appropriate specialist, who receives full conversation history and continues the interaction. Control does not return to TriageAgent.
  48. ## Orchestrator-Worker Pattern
  49. One agent assigns tasks to multiple other agents and compiles their responses. Agents use other agents as specialized tools through the `SendMessage` mechanism. Control always returns to the orchestrator after each delegation.
  50. ```python
  51. # Portfolio Manager orchestrates investment research
  52. portfolio_manager = Agent(
  53. name="PortfolioManager",
  54. instructions="Orchestrate investment research by gathering data, delegating analysis, and compiling recommendations",
  55. tools=[fetch_market_data],
  56. )
  57. risk_analyst = Agent(
  58. name="RiskAnalyst",
  59. instructions="Specialize in investment risk analysis",
  60. tools=[analyze_risk_factors],
  61. output_type=RiskAssessment, # Structured output
  62. )
  63. report_generator = Agent(
  64. name="ReportGenerator",
  65. instructions="Generate professional investment reports",
  66. tools=[create_report],
  67. )
  68. agency = Agency(
  69. portfolio_manager, # Entry point and orchestrator
  70. communication_flows=[
  71. (portfolio_manager, risk_analyst), # Can request risk analysis
  72. (portfolio_manager, report_generator), # Can request report generation
  73. ]
  74. )
  75. ```
  76. The Portfolio Manager delegates to specialists, receives their structured responses, and compiles them into a final investment decision.
  77. ## Understanding Communication Flows
  78. Communication flows are defined using tuples in the `communication_flows` parameter:
  79. ```python
  80. from agency_swarm import Agency
  81. from agency_swarm import Handoff
  82. agency = Agency(
  83. ceo, dev, # Entry points - agents that can communicate with users
  84. communication_flows=[
  85. (ceo, dev, Handoff), # CEO can transfer the user to the developer
  86. (ceo, dev), # CEO can assign tasks to developer
  87. (dev, va) # developer can assign tasks to virtual assistant
  88. ]
  89. )
  90. ```
  91. Each tuple `(sender, receiver)`, `(sender > receiver)`, or `(receiver < sender)` defines a directional communication path. Entry points are agents that can communicate with users.
  92. In the example above, the CEO can transfer the user to the Developer and the developer will proceed to interact with the user directly.
  93. At the same time, the CEO can assign tasks to both Developer and Virtual Assistant, so they will run in parallel in different threads and come back with their results to the CEO.
  94. ## Under the Hood
  95. Agency Swarm uses a `SendMessage` tool for inter-agent communication. Defining communication flows adds recipients to this tool.
  96. For advanced customization, see [Custom Communication Flows](/additional-features/custom-communication-flows/overview).