Tools: OpenClaw Guide Ch6: Multi-Agent Collaboration Architecture

Tools: OpenClaw Guide Ch6: Multi-Agent Collaboration Architecture

Source: Dev.to

Chapter 6: Multi-Agent Collaboration Architecture ## πŸ—οΈ Why Multi-Agent Architecture? ## Single-Agent Limitations ## Multi-Agent Advantages ## πŸ›οΈ Multi-Agent Architecture Patterns ## Pattern 1: Master-Worker ## Pattern 2: Peer-to-Peer ## Pattern 3: Layered Architecture ## 🎯 Hands-On: Building a Multi-Agent System ## Agent Role Design ## 1. Coordinator Agent ## 2. Email Manager Agent ## 3. Calendar Manager Agent ## 4. Document Processor Agent ## 5. Data Analyst Agent ## πŸ”„ Inter-Agent Communication ## 1. Message Bus Pattern ## 2. Direct Communication ## 3. Workflow Orchestration ## πŸ“ Complete Multi-Agent Configuration ## πŸš€ Deployment and Testing ## Create Agent Workspaces ## Start the System ## πŸ› οΈ Optimization Best Practices ## 1. Load Balancing ## 2. Fault Recovery ## 3. Cache Optimization ## βœ… Chapter Summary ## πŸš€ Next Steps ## πŸ“ Practice Projects ## Project 1: Intelligent Customer Service ## Project 2: Content Creation Factory ## Project 3: Enterprise Automation Platform 🎯 Learning Objective: Design and implement multi-Agent collaboration systems, master inter-Agent communication and task delegation A single Agent is powerful, but faces limitations in complex scenarios: Use Case: Clear task dispatch and control requirements Pros: Clear architecture, easy to manage Cons: Master becomes a bottleneck, limited scalability Use Case: Agents with relatively equal capabilities, flexible collaboration needed Pros: High availability, no single point of failure Cons: Complex coordination, potential conflicts Use Case: Complex enterprise applications, clear separation of concerns Pros: Structured, easy to maintain and extend Cons: Architecture complexity, communication overhead Let's build a practical multi-Agent collaboration system simulating an intelligent office assistant platform: Using OpenClaw's built-in communication mechanism: After this chapter, you should have mastered: Next Chapter: Memory and Data Management β†’ Ready to tackle more complex system integration? 🎯 Templates let you quickly answer FAQs or store snippets for re-use. Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment's permalink. Hide child comments as well For further actions, you may consider blocking this person and/or reporting abuse CODE_BLOCK: β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”‚ Master Agent β”‚ ← Coordination, task dispatch β”‚ (Controller) β”‚ β””β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ β”‚ β”Œβ”€β”€β”΄β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β” β–Ό β–Ό β–Ό β–Ό β”Œβ”€β”€β”€β”€β”€β” β”Œβ”€β”€β”€β”€β” β”Œβ”€β”€β”€β”€β” β”Œβ”€β”€β”€β”€β”€β” β”‚ Doc β”‚ β”‚Codeβ”‚ β”‚Dataβ”‚ β”‚ Web β”‚ β”‚Asst β”‚ β”‚Asstβ”‚ β”‚Analβ”‚ β”‚Srch β”‚ β””β”€β”€β”€β”€β”€β”˜ β””β”€β”€β”€β”€β”˜ β””β”€β”€β”€β”€β”˜ β””β”€β”€β”€β”€β”€β”˜ Enter fullscreen mode Exit fullscreen mode CODE_BLOCK: β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”‚ Master Agent β”‚ ← Coordination, task dispatch β”‚ (Controller) β”‚ β””β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ β”‚ β”Œβ”€β”€β”΄β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β” β–Ό β–Ό β–Ό β–Ό β”Œβ”€β”€β”€β”€β”€β” β”Œβ”€β”€β”€β”€β” β”Œβ”€β”€β”€β”€β” β”Œβ”€β”€β”€β”€β”€β” β”‚ Doc β”‚ β”‚Codeβ”‚ β”‚Dataβ”‚ β”‚ Web β”‚ β”‚Asst β”‚ β”‚Asstβ”‚ β”‚Analβ”‚ β”‚Srch β”‚ β””β”€β”€β”€β”€β”€β”˜ β””β”€β”€β”€β”€β”˜ β””β”€β”€β”€β”€β”˜ β””β”€β”€β”€β”€β”€β”˜ CODE_BLOCK: β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”‚ Master Agent β”‚ ← Coordination, task dispatch β”‚ (Controller) β”‚ β””β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ β”‚ β”Œβ”€β”€β”΄β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β” β–Ό β–Ό β–Ό β–Ό β”Œβ”€β”€β”€β”€β”€β” β”Œβ”€β”€β”€β”€β” β”Œβ”€β”€β”€β”€β” β”Œβ”€β”€β”€β”€β”€β” β”‚ Doc β”‚ β”‚Codeβ”‚ β”‚Dataβ”‚ β”‚ Web β”‚ β”‚Asst β”‚ β”‚Asstβ”‚ β”‚Analβ”‚ β”‚Srch β”‚ β””β”€β”€β”€β”€β”€β”˜ β””β”€β”€β”€β”€β”˜ β””β”€β”€β”€β”€β”˜ β””β”€β”€β”€β”€β”€β”˜ CODE_BLOCK: β”Œβ”€β”€β”€β”€β”€β” β”Œβ”€β”€β”€β”€β”€β” β”Œβ”€β”€β”€β”€β”€β” β”‚Agent│◄──►│Agent│◄──►│Agentβ”‚ β”‚ A β”‚ β”‚ B β”‚ β”‚ C β”‚ β””β”€β”€β”¬β”€β”€β”˜ β””β”€β”€β”€β”€β”€β”˜ β””β”€β”€β”¬β”€β”€β”˜ β”‚ β”‚ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ β–Ό β”Œβ”€β”€β”€β”€β”€β” β”‚Agentβ”‚ β”‚ D β”‚ β””β”€β”€β”€β”€β”€β”˜ Enter fullscreen mode Exit fullscreen mode CODE_BLOCK: β”Œβ”€β”€β”€β”€β”€β” β”Œβ”€β”€β”€β”€β”€β” β”Œβ”€β”€β”€β”€β”€β” β”‚Agent│◄──►│Agent│◄──►│Agentβ”‚ β”‚ A β”‚ β”‚ B β”‚ β”‚ C β”‚ β””β”€β”€β”¬β”€β”€β”˜ β””β”€β”€β”€β”€β”€β”˜ β””β”€β”€β”¬β”€β”€β”˜ β”‚ β”‚ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ β–Ό β”Œβ”€β”€β”€β”€β”€β” β”‚Agentβ”‚ β”‚ D β”‚ β””β”€β”€β”€β”€β”€β”˜ CODE_BLOCK: β”Œβ”€β”€β”€β”€β”€β” β”Œβ”€β”€β”€β”€β”€β” β”Œβ”€β”€β”€β”€β”€β” β”‚Agent│◄──►│Agent│◄──►│Agentβ”‚ β”‚ A β”‚ β”‚ B β”‚ β”‚ C β”‚ β””β”€β”€β”¬β”€β”€β”˜ β””β”€β”€β”€β”€β”€β”˜ β””β”€β”€β”¬β”€β”€β”˜ β”‚ β”‚ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ β–Ό β”Œβ”€β”€β”€β”€β”€β” β”‚Agentβ”‚ β”‚ D β”‚ β””β”€β”€β”€β”€β”€β”˜ CODE_BLOCK: β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”‚ UI Layer β”‚ β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€ β”‚ Business Logic Layer β”‚ β”‚ β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β” β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”‚ β”‚ β”‚Project β”‚ β”‚ Personal β”‚ β”‚ β”‚ β”‚Manager β”‚ β”‚ Assistant β”‚ β”‚ β”‚ β””β”€β”€β”€β”€β”€β”€β”€β”€β”˜ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ β”‚ β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€ β”‚ Service Layer β”‚ β”‚ β”Œβ”€β”€β”€β”€β”€β” β”Œβ”€β”€β”€β”€β”€β” β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β” β”‚ β”‚ β”‚ Doc β”‚ β”‚Emailβ”‚ β”‚Calendarβ”‚ β”‚ β”‚ β”‚ Svc β”‚ β”‚ Svc β”‚ β”‚ Svc β”‚ β”‚ β”‚ β””β”€β”€β”€β”€β”€β”˜ β””β”€β”€β”€β”€β”€β”˜ β””β”€β”€β”€β”€β”€β”€β”€β”€β”˜ β”‚ β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€ β”‚ Data Layer β”‚ β”‚ β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”‚ β”‚ β”‚Filesystemβ”‚ β”‚Database β”‚ β”‚ β”‚ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ β”‚ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ Enter fullscreen mode Exit fullscreen mode CODE_BLOCK: β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”‚ UI Layer β”‚ β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€ β”‚ Business Logic Layer β”‚ β”‚ β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β” β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”‚ β”‚ β”‚Project β”‚ β”‚ Personal β”‚ β”‚ β”‚ β”‚Manager β”‚ β”‚ Assistant β”‚ β”‚ β”‚ β””β”€β”€β”€β”€β”€β”€β”€β”€β”˜ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ β”‚ β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€ β”‚ Service Layer β”‚ β”‚ β”Œβ”€β”€β”€β”€β”€β” β”Œβ”€β”€β”€β”€β”€β” β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β” β”‚ β”‚ β”‚ Doc β”‚ β”‚Emailβ”‚ β”‚Calendarβ”‚ β”‚ β”‚ β”‚ Svc β”‚ β”‚ Svc β”‚ β”‚ Svc β”‚ β”‚ β”‚ β””β”€β”€β”€β”€β”€β”˜ β””β”€β”€β”€β”€β”€β”˜ β””β”€β”€β”€β”€β”€β”€β”€β”€β”˜ β”‚ β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€ β”‚ Data Layer β”‚ β”‚ β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”‚ β”‚ β”‚Filesystemβ”‚ β”‚Database β”‚ β”‚ β”‚ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ β”‚ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ CODE_BLOCK: β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”‚ UI Layer β”‚ β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€ β”‚ Business Logic Layer β”‚ β”‚ β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β” β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”‚ β”‚ β”‚Project β”‚ β”‚ Personal β”‚ β”‚ β”‚ β”‚Manager β”‚ β”‚ Assistant β”‚ β”‚ β”‚ β””β”€β”€β”€β”€β”€β”€β”€β”€β”˜ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ β”‚ β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€ β”‚ Service Layer β”‚ β”‚ β”Œβ”€β”€β”€β”€β”€β” β”Œβ”€β”€β”€β”€β”€β” β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β” β”‚ β”‚ β”‚ Doc β”‚ β”‚Emailβ”‚ β”‚Calendarβ”‚ β”‚ β”‚ β”‚ Svc β”‚ β”‚ Svc β”‚ β”‚ Svc β”‚ β”‚ β”‚ β””β”€β”€β”€β”€β”€β”˜ β””β”€β”€β”€β”€β”€β”˜ β””β”€β”€β”€β”€β”€β”€β”€β”€β”˜ β”‚ β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€ β”‚ Data Layer β”‚ β”‚ β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”‚ β”‚ β”‚Filesystemβ”‚ β”‚Database β”‚ β”‚ β”‚ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ β”‚ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ CODE_BLOCK: { "id": "coordinator", "name": "Coordinator", "role": "Task dispatch and coordination", "model": "anthropic/claude-sonnet-4-20250514", "systemPrompt": "You are an intelligent coordinator responsible for understanding user requests, decomposing complex tasks, and assigning them to specialized Agents...", "tools": { "allowlist": [ "sessions_send", "sessions_list", "memory_search", "memory_get", "read", "write", "message" ] } } Enter fullscreen mode Exit fullscreen mode CODE_BLOCK: { "id": "coordinator", "name": "Coordinator", "role": "Task dispatch and coordination", "model": "anthropic/claude-sonnet-4-20250514", "systemPrompt": "You are an intelligent coordinator responsible for understanding user requests, decomposing complex tasks, and assigning them to specialized Agents...", "tools": { "allowlist": [ "sessions_send", "sessions_list", "memory_search", "memory_get", "read", "write", "message" ] } } CODE_BLOCK: { "id": "coordinator", "name": "Coordinator", "role": "Task dispatch and coordination", "model": "anthropic/claude-sonnet-4-20250514", "systemPrompt": "You are an intelligent coordinator responsible for understanding user requests, decomposing complex tasks, and assigning them to specialized Agents...", "tools": { "allowlist": [ "sessions_send", "sessions_list", "memory_search", "memory_get", "read", "write", "message" ] } } CODE_BLOCK: { "id": "email-manager", "name": "Email Manager", "role": "Email processing and management", "model": "anthropic/claude-sonnet-4-20250514", "tools": { "allowlist": ["read", "write", "web_search", "message", "cron"] } } Enter fullscreen mode Exit fullscreen mode CODE_BLOCK: { "id": "email-manager", "name": "Email Manager", "role": "Email processing and management", "model": "anthropic/claude-sonnet-4-20250514", "tools": { "allowlist": ["read", "write", "web_search", "message", "cron"] } } CODE_BLOCK: { "id": "email-manager", "name": "Email Manager", "role": "Email processing and management", "model": "anthropic/claude-sonnet-4-20250514", "tools": { "allowlist": ["read", "write", "web_search", "message", "cron"] } } CODE_BLOCK: { "id": "calendar-manager", "name": "Calendar Manager", "role": "Scheduling and time management", "model": "anthropic/claude-sonnet-4-20250514", "tools": { "allowlist": ["read", "write", "cron", "web_search", "message"] } } Enter fullscreen mode Exit fullscreen mode CODE_BLOCK: { "id": "calendar-manager", "name": "Calendar Manager", "role": "Scheduling and time management", "model": "anthropic/claude-sonnet-4-20250514", "tools": { "allowlist": ["read", "write", "cron", "web_search", "message"] } } CODE_BLOCK: { "id": "calendar-manager", "name": "Calendar Manager", "role": "Scheduling and time management", "model": "anthropic/claude-sonnet-4-20250514", "tools": { "allowlist": ["read", "write", "cron", "web_search", "message"] } } CODE_BLOCK: { "id": "doc-processor", "name": "Document Processor", "role": "Document creation, editing, and management", "model": "anthropic/claude-sonnet-4-20250514", "tools": { "allowlist": ["read", "write", "exec", "web_search", "memory_search"] } } Enter fullscreen mode Exit fullscreen mode CODE_BLOCK: { "id": "doc-processor", "name": "Document Processor", "role": "Document creation, editing, and management", "model": "anthropic/claude-sonnet-4-20250514", "tools": { "allowlist": ["read", "write", "exec", "web_search", "memory_search"] } } CODE_BLOCK: { "id": "doc-processor", "name": "Document Processor", "role": "Document creation, editing, and management", "model": "anthropic/claude-sonnet-4-20250514", "tools": { "allowlist": ["read", "write", "exec", "web_search", "memory_search"] } } CODE_BLOCK: { "id": "data-analyst", "name": "Data Analyst", "role": "Data analysis and report generation", "model": "anthropic/claude-sonnet-4-20250514", "tools": { "allowlist": ["read", "write", "exec", "web_search", "canvas"] } } Enter fullscreen mode Exit fullscreen mode CODE_BLOCK: { "id": "data-analyst", "name": "Data Analyst", "role": "Data analysis and report generation", "model": "anthropic/claude-sonnet-4-20250514", "tools": { "allowlist": ["read", "write", "exec", "web_search", "canvas"] } } CODE_BLOCK: { "id": "data-analyst", "name": "Data Analyst", "role": "Data analysis and report generation", "model": "anthropic/claude-sonnet-4-20250514", "tools": { "allowlist": ["read", "write", "exec", "web_search", "canvas"] } } COMMAND_BLOCK: # message-bus.py β€” Inter-Agent message bus import asyncio import json import logging from typing import Dict, List, Callable from datetime import datetime class MessageBus: def __init__(self): self.subscribers: Dict[str, List[Callable]] = {} self.message_history: List[Dict] = [] def subscribe(self, topic: str, callback: Callable): """Subscribe to a message topic""" if topic not in self.subscribers: self.subscribers[topic] = [] self.subscribers[topic].append(callback) def publish(self, topic: str, message: Dict, sender: str = None): """Publish a message""" msg = { "id": f"msg_{len(self.message_history)}", "topic": topic, "message": message, "sender": sender, "timestamp": datetime.now().isoformat() } self.message_history.append(msg) if topic in self.subscribers: for callback in self.subscribers[topic]: try: callback(msg) except Exception as e: logging.error(f"Message delivery failed: {e}") def get_history(self, topic: str = None, limit: int = 100): """Get message history""" messages = self.message_history if topic: messages = [m for m in messages if m["topic"] == topic] return messages[-limit:] Enter fullscreen mode Exit fullscreen mode COMMAND_BLOCK: # message-bus.py β€” Inter-Agent message bus import asyncio import json import logging from typing import Dict, List, Callable from datetime import datetime class MessageBus: def __init__(self): self.subscribers: Dict[str, List[Callable]] = {} self.message_history: List[Dict] = [] def subscribe(self, topic: str, callback: Callable): """Subscribe to a message topic""" if topic not in self.subscribers: self.subscribers[topic] = [] self.subscribers[topic].append(callback) def publish(self, topic: str, message: Dict, sender: str = None): """Publish a message""" msg = { "id": f"msg_{len(self.message_history)}", "topic": topic, "message": message, "sender": sender, "timestamp": datetime.now().isoformat() } self.message_history.append(msg) if topic in self.subscribers: for callback in self.subscribers[topic]: try: callback(msg) except Exception as e: logging.error(f"Message delivery failed: {e}") def get_history(self, topic: str = None, limit: int = 100): """Get message history""" messages = self.message_history if topic: messages = [m for m in messages if m["topic"] == topic] return messages[-limit:] COMMAND_BLOCK: # message-bus.py β€” Inter-Agent message bus import asyncio import json import logging from typing import Dict, List, Callable from datetime import datetime class MessageBus: def __init__(self): self.subscribers: Dict[str, List[Callable]] = {} self.message_history: List[Dict] = [] def subscribe(self, topic: str, callback: Callable): """Subscribe to a message topic""" if topic not in self.subscribers: self.subscribers[topic] = [] self.subscribers[topic].append(callback) def publish(self, topic: str, message: Dict, sender: str = None): """Publish a message""" msg = { "id": f"msg_{len(self.message_history)}", "topic": topic, "message": message, "sender": sender, "timestamp": datetime.now().isoformat() } self.message_history.append(msg) if topic in self.subscribers: for callback in self.subscribers[topic]: try: callback(msg) except Exception as e: logging.error(f"Message delivery failed: {e}") def get_history(self, topic: str = None, limit: int = 100): """Get message history""" messages = self.message_history if topic: messages = [m for m in messages if m["topic"] == topic] return messages[-limit:] COMMAND_BLOCK: # In the Coordinator Agent async def delegate_task(task_type: str, task_data: dict): """Delegate a task to a specialized Agent""" agent_mapping = { "email": "email-manager", "calendar": "calendar-manager", "document": "doc-processor", "analysis": "data-analyst" } target_agent = agent_mapping.get(task_type) if not target_agent: return {"error": "Unknown task type"} message = f"Please handle the following task: {json.dumps(task_data)}" result = await sessions_send( sessionKey=f"agent:{target_agent}:main", message=message, timeoutSeconds=60 ) return result Enter fullscreen mode Exit fullscreen mode COMMAND_BLOCK: # In the Coordinator Agent async def delegate_task(task_type: str, task_data: dict): """Delegate a task to a specialized Agent""" agent_mapping = { "email": "email-manager", "calendar": "calendar-manager", "document": "doc-processor", "analysis": "data-analyst" } target_agent = agent_mapping.get(task_type) if not target_agent: return {"error": "Unknown task type"} message = f"Please handle the following task: {json.dumps(task_data)}" result = await sessions_send( sessionKey=f"agent:{target_agent}:main", message=message, timeoutSeconds=60 ) return result COMMAND_BLOCK: # In the Coordinator Agent async def delegate_task(task_type: str, task_data: dict): """Delegate a task to a specialized Agent""" agent_mapping = { "email": "email-manager", "calendar": "calendar-manager", "document": "doc-processor", "analysis": "data-analyst" } target_agent = agent_mapping.get(task_type) if not target_agent: return {"error": "Unknown task type"} message = f"Please handle the following task: {json.dumps(task_data)}" result = await sessions_send( sessionKey=f"agent:{target_agent}:main", message=message, timeoutSeconds=60 ) return result COMMAND_BLOCK: { "workflow": { "name": "Intelligent Email Processing", "steps": [ { "id": "email_classification", "agent": "email-manager", "action": "classify_emails", "input": "${user_input}", "output": "email_categories" }, { "id": "urgent_handling", "agent": "coordinator", "condition": "${email_categories.urgent_count} > 0", "action": "handle_urgent_emails", "input": "${email_categories.urgent_emails}" }, { "id": "schedule_check", "agent": "calendar-manager", "parallel": true, "action": "check_calendar_conflicts", "input": "${email_categories.meeting_requests}" }, { "id": "generate_report", "agent": "doc-processor", "action": "create_email_summary", "input": { "classified": "${email_categories}", "calendar": "${schedule_check.result}" } } ] } } Enter fullscreen mode Exit fullscreen mode COMMAND_BLOCK: { "workflow": { "name": "Intelligent Email Processing", "steps": [ { "id": "email_classification", "agent": "email-manager", "action": "classify_emails", "input": "${user_input}", "output": "email_categories" }, { "id": "urgent_handling", "agent": "coordinator", "condition": "${email_categories.urgent_count} > 0", "action": "handle_urgent_emails", "input": "${email_categories.urgent_emails}" }, { "id": "schedule_check", "agent": "calendar-manager", "parallel": true, "action": "check_calendar_conflicts", "input": "${email_categories.meeting_requests}" }, { "id": "generate_report", "agent": "doc-processor", "action": "create_email_summary", "input": { "classified": "${email_categories}", "calendar": "${schedule_check.result}" } } ] } } COMMAND_BLOCK: { "workflow": { "name": "Intelligent Email Processing", "steps": [ { "id": "email_classification", "agent": "email-manager", "action": "classify_emails", "input": "${user_input}", "output": "email_categories" }, { "id": "urgent_handling", "agent": "coordinator", "condition": "${email_categories.urgent_count} > 0", "action": "handle_urgent_emails", "input": "${email_categories.urgent_emails}" }, { "id": "schedule_check", "agent": "calendar-manager", "parallel": true, "action": "check_calendar_conflicts", "input": "${email_categories.meeting_requests}" }, { "id": "generate_report", "agent": "doc-processor", "action": "create_email_summary", "input": { "classified": "${email_categories}", "calendar": "${schedule_check.result}" } } ] } } CODE_BLOCK: { "gateway": { "port": 18789, "bind": "loopback", "cors": true, "maxConcurrency": 20 }, "agents": [ { "id": "coordinator", "name": "Coordinator", "model": "anthropic/claude-sonnet-4-20250514", "workspace": {"root": "./agents/coordinator"}, "maxConcurrency": 5 }, { "id": "email-manager", "name": "Email Manager", "model": "anthropic/claude-sonnet-4-20250514", "workspace": {"root": "./agents/email-manager"}, "maxConcurrency": 3 }, { "id": "calendar-manager", "name": "Calendar Manager", "model": "anthropic/claude-sonnet-4-20250514", "workspace": {"root": "./agents/calendar-manager"}, "maxConcurrency": 2 }, { "id": "doc-processor", "name": "Document Processor", "model": "anthropic/claude-sonnet-4-20250514", "workspace": {"root": "./agents/doc-processor"}, "maxConcurrency": 3 }, { "id": "data-analyst", "name": "Data Analyst", "model": "anthropic/claude-sonnet-4-20250514", "workspace": {"root": "./agents/data-analyst"}, "maxConcurrency": 2 } ], "tools": { "allowlists": { "coordinator": [ "sessions_send", "sessions_list", "memory_search", "memory_get", "read", "write", "message" ], "email-manager": [ "read", "write", "web_search", "message", "cron" ], "calendar-manager": [ "read", "write", "cron", "web_search", "message" ], "doc-processor": [ "read", "write", "exec", "web_search", "memory_search" ], "data-analyst": [ "read", "write", "exec", "web_search", "canvas" ] } } } Enter fullscreen mode Exit fullscreen mode CODE_BLOCK: { "gateway": { "port": 18789, "bind": "loopback", "cors": true, "maxConcurrency": 20 }, "agents": [ { "id": "coordinator", "name": "Coordinator", "model": "anthropic/claude-sonnet-4-20250514", "workspace": {"root": "./agents/coordinator"}, "maxConcurrency": 5 }, { "id": "email-manager", "name": "Email Manager", "model": "anthropic/claude-sonnet-4-20250514", "workspace": {"root": "./agents/email-manager"}, "maxConcurrency": 3 }, { "id": "calendar-manager", "name": "Calendar Manager", "model": "anthropic/claude-sonnet-4-20250514", "workspace": {"root": "./agents/calendar-manager"}, "maxConcurrency": 2 }, { "id": "doc-processor", "name": "Document Processor", "model": "anthropic/claude-sonnet-4-20250514", "workspace": {"root": "./agents/doc-processor"}, "maxConcurrency": 3 }, { "id": "data-analyst", "name": "Data Analyst", "model": "anthropic/claude-sonnet-4-20250514", "workspace": {"root": "./agents/data-analyst"}, "maxConcurrency": 2 } ], "tools": { "allowlists": { "coordinator": [ "sessions_send", "sessions_list", "memory_search", "memory_get", "read", "write", "message" ], "email-manager": [ "read", "write", "web_search", "message", "cron" ], "calendar-manager": [ "read", "write", "cron", "web_search", "message" ], "doc-processor": [ "read", "write", "exec", "web_search", "memory_search" ], "data-analyst": [ "read", "write", "exec", "web_search", "canvas" ] } } } CODE_BLOCK: { "gateway": { "port": 18789, "bind": "loopback", "cors": true, "maxConcurrency": 20 }, "agents": [ { "id": "coordinator", "name": "Coordinator", "model": "anthropic/claude-sonnet-4-20250514", "workspace": {"root": "./agents/coordinator"}, "maxConcurrency": 5 }, { "id": "email-manager", "name": "Email Manager", "model": "anthropic/claude-sonnet-4-20250514", "workspace": {"root": "./agents/email-manager"}, "maxConcurrency": 3 }, { "id": "calendar-manager", "name": "Calendar Manager", "model": "anthropic/claude-sonnet-4-20250514", "workspace": {"root": "./agents/calendar-manager"}, "maxConcurrency": 2 }, { "id": "doc-processor", "name": "Document Processor", "model": "anthropic/claude-sonnet-4-20250514", "workspace": {"root": "./agents/doc-processor"}, "maxConcurrency": 3 }, { "id": "data-analyst", "name": "Data Analyst", "model": "anthropic/claude-sonnet-4-20250514", "workspace": {"root": "./agents/data-analyst"}, "maxConcurrency": 2 } ], "tools": { "allowlists": { "coordinator": [ "sessions_send", "sessions_list", "memory_search", "memory_get", "read", "write", "message" ], "email-manager": [ "read", "write", "web_search", "message", "cron" ], "calendar-manager": [ "read", "write", "cron", "web_search", "message" ], "doc-processor": [ "read", "write", "exec", "web_search", "memory_search" ], "data-analyst": [ "read", "write", "exec", "web_search", "canvas" ] } } } COMMAND_BLOCK: # Create directories for each Agent mkdir -p agents/{coordinator,email-manager,calendar-manager,doc-processor,data-analyst} # Create base files for each Agent for agent in coordinator email-manager calendar-manager doc-processor data-analyst; do mkdir -p agents/$agent/{memory,logs,projects} touch agents/$agent/{MEMORY.md,SOUL.md,USER.md} done Enter fullscreen mode Exit fullscreen mode COMMAND_BLOCK: # Create directories for each Agent mkdir -p agents/{coordinator,email-manager,calendar-manager,doc-processor,data-analyst} # Create base files for each Agent for agent in coordinator email-manager calendar-manager doc-processor data-analyst; do mkdir -p agents/$agent/{memory,logs,projects} touch agents/$agent/{MEMORY.md,SOUL.md,USER.md} done COMMAND_BLOCK: # Create directories for each Agent mkdir -p agents/{coordinator,email-manager,calendar-manager,doc-processor,data-analyst} # Create base files for each Agent for agent in coordinator email-manager calendar-manager doc-processor data-analyst; do mkdir -p agents/$agent/{memory,logs,projects} touch agents/$agent/{MEMORY.md,SOUL.md,USER.md} done COMMAND_BLOCK: # Start the OpenClaw Gateway openclaw gateway start # Verify all Agents loaded successfully openclaw status # Test Agent communication curl -X POST http://localhost:18789/v1/chat/completions \ -H "Content-Type: application/json" \ -d '{ "model": "coordinator", "messages": [ {"role": "user", "content": "Please introduce the team members"} ] }' Enter fullscreen mode Exit fullscreen mode COMMAND_BLOCK: # Start the OpenClaw Gateway openclaw gateway start # Verify all Agents loaded successfully openclaw status # Test Agent communication curl -X POST http://localhost:18789/v1/chat/completions \ -H "Content-Type: application/json" \ -d '{ "model": "coordinator", "messages": [ {"role": "user", "content": "Please introduce the team members"} ] }' COMMAND_BLOCK: # Start the OpenClaw Gateway openclaw gateway start # Verify all Agents loaded successfully openclaw status # Test Agent communication curl -X POST http://localhost:18789/v1/chat/completions \ -H "Content-Type: application/json" \ -d '{ "model": "coordinator", "messages": [ {"role": "user", "content": "Please introduce the team members"} ] }' CODE_BLOCK: { "loadBalancer": { "strategy": "round-robin", // round-robin, least-connections, weighted "healthCheck": { "enabled": true, "interval": 30000, "timeout": 5000 } } } Enter fullscreen mode Exit fullscreen mode CODE_BLOCK: { "loadBalancer": { "strategy": "round-robin", // round-robin, least-connections, weighted "healthCheck": { "enabled": true, "interval": 30000, "timeout": 5000 } } } CODE_BLOCK: { "loadBalancer": { "strategy": "round-robin", // round-robin, least-connections, weighted "healthCheck": { "enabled": true, "interval": 30000, "timeout": 5000 } } } CODE_BLOCK: { "agents": [ { "id": "coordinator", "retry": { "enabled": true, "maxAttempts": 3, "backoff": "exponential" }, "fallback": { "agent": "backup-coordinator", "message": "The primary coordinator is temporarily unavailable. A backup coordinator is serving you." } } ] } Enter fullscreen mode Exit fullscreen mode CODE_BLOCK: { "agents": [ { "id": "coordinator", "retry": { "enabled": true, "maxAttempts": 3, "backoff": "exponential" }, "fallback": { "agent": "backup-coordinator", "message": "The primary coordinator is temporarily unavailable. A backup coordinator is serving you." } } ] } CODE_BLOCK: { "agents": [ { "id": "coordinator", "retry": { "enabled": true, "maxAttempts": 3, "backoff": "exponential" }, "fallback": { "agent": "backup-coordinator", "message": "The primary coordinator is temporarily unavailable. A backup coordinator is serving you." } } ] } CODE_BLOCK: { "agents": [ { "id": "data-analyst", "caching": { "enabled": true, "ttl": 3600, "maxSize": "100MB", "strategy": "LRU" } } ] } Enter fullscreen mode Exit fullscreen mode CODE_BLOCK: { "agents": [ { "id": "data-analyst", "caching": { "enabled": true, "ttl": 3600, "maxSize": "100MB", "strategy": "LRU" } } ] } CODE_BLOCK: { "agents": [ { "id": "data-analyst", "caching": { "enabled": true, "ttl": 3600, "maxSize": "100MB", "strategy": "LRU" } } ] } - 🧠 Cognitive Overload: One Agent handling all task types - πŸ”„ Context Pollution: Information from different tasks mixed together - ⚑ Performance Bottleneck: Limited single-point processing capacity - 🎯 Lack of Specialization: Cannot deeply optimize for specific domains - πŸ”’ Security Risk: All permissions concentrated in one Agent - 🎯 Specialization: Each Agent focuses on a specific domain - πŸš€ Parallel Processing: Handle multiple tasks simultaneously - πŸ”’ Permission Isolation: Assign minimal permissions as needed - πŸ“Š Independent Monitoring: Each Agent's performance can be optimized independently - πŸ›‘οΈ Fault Isolation: A single Agent failure doesn't affect the whole system - [x] Multi-Agent architecture design patterns and use cases - [x] Inter-Agent communication mechanisms - [x] Complete multi-Agent system configuration and deployment - [x] Task dispatch and collaboration workflow design - [x] System monitoring and performance optimization - [x] Fault handling and load balancing strategies - Design 3–5 specialized Agents (pre-sales, post-sales, tech support, etc.) - Implement automatic issue classification and handoff - Build a knowledge base and FAQ system - Create multiple content creation Agents (writing, design, video, etc.) - Automate the content production pipeline - Establish quality control and review mechanisms - Design department-specific Agents (HR, Finance, IT, etc.) - Implement cross-department collaboration workflows - Build permission management and approval processes