# Multi-Agent Coordination Tutorial Learn how to orchestrate complex workflows with multiple specialized agents working in harmony. ## Overview Multi-agent systems in Claude Flow allow you to: - Parallelize work across specialized agents - Coordinate dependencies between tasks - Share information between agents - Optimize for performance and quality ## Tutorial: Building a Full-Stack Application We'll build a complete e-commerce platform using multiple specialized agents. ### Step 1: Understanding Agent Roles First, let's explore available agent types: ```bash cd examples ../claude-flow agent list ``` Common agent types: - **architect**: System design and planning - **developer**: Code implementation - **tester**: Test creation and validation - **reviewer**: Code quality and security - **documenter**: Documentation generation - **devops**: Deployment and infrastructure ### Step 2: Create the Workflow Definition Create `ecommerce-workflow.json`: ```json { "name": "E-commerce Platform", "description": "Build a complete e-commerce solution", "agents": [ { "id": "system-architect", "type": "architect", "capabilities": ["system-design", "api-design", "database-design"] }, { "id": "backend-dev-1", "type": "developer", "capabilities": ["nodejs", "api", "database"] }, { "id": "backend-dev-2", "type": "developer", "capabilities": ["nodejs", "api", "payments"] }, { "id": "frontend-dev", "type": "developer", "capabilities": ["react", "ui", "responsive"] }, { "id": "qa-engineer", "type": "tester", "capabilities": ["unit-testing", "integration-testing", "e2e"] } ], "tasks": [ { "id": "design-system", "name": "Design System Architecture", "agentId": "system-architect", "type": "design" }, { "id": "create-user-service", "name": "Build User Management Service", "agentId": "backend-dev-1", "dependencies": ["design-system"], "parallel": true }, { "id": "create-product-service", "name": "Build Product Catalog Service", "agentId": "backend-dev-2", "dependencies": ["design-system"], "parallel": true }, { "id": "create-frontend", "name": "Build React Frontend", "agentId": "frontend-dev", "dependencies": ["create-user-service", "create-product-service"] }, { "id": "test-system", "name": "Create and Run Tests", "agentId": "qa-engineer", "dependencies": ["create-frontend"] } ] } ``` ### Step 3: Execute the Workflow Create the e-commerce platform using the swarm system: ```bash ../claude-flow swarm create "Build complete e-commerce platform with user management, product catalog, and frontend" --agents 5 --strategy development --output ./output/ecommerce --monitor ``` ### Step 4: Understanding Coordination Modes Claude Flow supports different coordination patterns: #### Hub-Spoke Pattern Central coordinator manages all agents: ```bash ../claude-flow orchestrate ./workflow.json \ --coordination hub-spoke \ --monitor ``` Best for: - Centralized control - Simple dependencies - Clear hierarchy #### Mesh Pattern Agents communicate directly: ```bash ../claude-flow orchestrate ./workflow.json \ --coordination mesh \ --monitor ``` Best for: - Complex interdependencies - High collaboration needs - Distributed decision making #### Pipeline Pattern Sequential processing: ```bash ../claude-flow orchestrate ./workflow.json \ --coordination pipeline \ --monitor ``` Best for: - Linear workflows - Data transformation - Stage-based processing ### Step 5: Advanced Task Dependencies Create complex dependency graphs: ```json { "tasks": [ { "id": "task-a", "name": "Initial Task", "agentId": "agent-1" }, { "id": "task-b", "name": "Depends on A", "agentId": "agent-2", "dependencies": ["task-a"] }, { "id": "task-c", "name": "Depends on A", "agentId": "agent-3", "dependencies": ["task-a"], "parallel": true }, { "id": "task-d", "name": "Depends on B and C", "agentId": "agent-4", "dependencies": ["task-b", "task-c"] } ] } ``` ### Step 6: Information Sharing Enable agents to share data: ```json { "agents": [ { "id": "researcher", "type": "researcher", "sharedMemory": { "namespace": "project-research", "access": "write" } }, { "id": "developer", "type": "developer", "sharedMemory": { "namespace": "project-research", "access": "read" } } ] } ``` ### Step 7: Error Handling and Recovery Configure robust error handling: ```json { "execution": { "mode": "parallel", "errorStrategy": "continue", "retries": { "max": 3, "backoff": "exponential", "delay": 1000 }, "checkpoints": ["design-system", "create-frontend"], "rollback": true } } ``` ### Step 8: Performance Optimization Optimize workflow execution: ```json { "execution": { "mode": "smart", "parallelism": { "max": 5, "strategy": "resource-based" }, "caching": { "enabled": true, "ttl": 3600 }, "optimization": { "taskBatching": true, "lazyLoading": true } } } ``` ## Real-World Example: Microservices Development Complete workflow for microservices: ```bash ../claude-flow orchestrate \ ./02-workflows/complex/microservices-workflow.json \ --monitor \ --output ./microservices-project ``` This creates: - Multiple service implementations - Shared API contracts - Integration tests - Docker configurations - CI/CD pipelines ## Monitoring and Debugging ### Real-time Monitoring ```bash ../claude-flow orchestrate ./workflow.json --monitor ``` ### Debug Mode ```bash ../claude-flow orchestrate ./workflow.json --debug ``` ### Performance Metrics ```bash ../claude-flow orchestrate ./workflow.json --metrics ``` ## Best Practices ### 1. Agent Specialization - Keep agents focused on specific domains - Don't overload single agents - Use appropriate agent types ### 2. Task Granularity - Break large tasks into smaller ones - Enable better parallelization - Easier to debug and retry ### 3. Dependency Management - Minimize dependencies where possible - Use parallel execution - Consider task priorities ### 4. Error Resilience - Plan for failures - Use checkpoints - Implement retry logic ### 5. Performance - Monitor resource usage - Optimize parallel execution - Cache intermediate results ## Advanced Patterns ### Dynamic Agent Spawning ```json { "agents": { "dynamic": true, "scaling": { "min": 2, "max": 10, "trigger": "queue-size" } } } ``` ### Conditional Execution ```json { "tasks": [ { "id": "check-quality", "condition": "previous.output.quality > 0.8" } ] } ``` ### Task Templates ```json { "templates": { "service-template": { "type": "development", "pattern": "microservice", "includes": ["api", "tests", "docs"] } } } ``` ## Troubleshooting ### Agents Not Starting - Check agent definitions - Verify capabilities match tasks - Review resource limits ### Tasks Stuck - Check dependencies - Look for circular dependencies - Verify agent availability ### Poor Performance - Reduce parallelism - Check resource constraints - Optimize task size ## Summary You've learned: - ✅ Multi-agent workflow creation - ✅ Coordination patterns - ✅ Dependency management - ✅ Performance optimization - ✅ Error handling strategies ## Next Steps 1. Create custom agent types 2. Build complex workflows 3. Integrate with external systems 4. Implement custom coordination patterns Continue to [Advanced Orchestration Patterns](./advanced-orchestration.md)