# Claude Flow Agent System Documentation ## Table of Contents 1. [Introduction](#introduction) 2. [Agent Architecture](#agent-architecture) 3. [Defining Custom Agents](#defining-custom-agents) 4. [Invoking Agents via Prompts](#invoking-agents-via-prompts) 5. [Agent Orchestration Best Practices](#agent-orchestration-best-practices) 6. [Example Workflows](#example-workflows) 7. [Integration with claude-flow init](#integration-with-claude-flow-init) 8. [Advanced Features](#advanced-features) 9. [Troubleshooting](#troubleshooting) ## Introduction The Claude Flow agent system provides a powerful framework for creating and managing specialized AI agents that work together to accomplish complex tasks. This system leverages Model Context Protocol (MCP) tools for seamless integration and coordination. ### Key Features - **Specialized Agent Types**: Pre-built agents for research, coding, analysis, testing, and coordination - **Custom Agent Creation**: Define your own agent types with specific capabilities - **Swarm Intelligence**: Multiple agents working together with different coordination topologies - **Memory Integration**: Distributed memory system for persistent state and knowledge sharing - **Real-time Monitoring**: Health checks, performance metrics, and activity tracking - **Auto-scaling**: Dynamic agent provisioning based on workload ## Agent Architecture ### Core Components ``` ┌─────────────────────────────────────────────────────────────┐ │ Agent Manager │ │ ┌─────────────┐ ┌─────────────┐ ┌─────────────┐ │ │ │ Agent │ │ Agent │ │ Agent │ │ │ │ Registry │ │ Templates │ │ Pools │ │ │ └─────────────┘ └─────────────┘ └─────────────┘ │ └─────────────────────────────────────────────────────────────┘ │ ┌─────────────────────────────────────────────────────────────┐ │ Base Agent Class │ │ ┌─────────────┐ ┌─────────────┐ ┌─────────────┐ │ │ │ Capabilities│ │ Config │ │ Environment │ │ │ │ │ │ │ │ │ │ │ └─────────────┘ └─────────────┘ └─────────────┘ │ └─────────────────────────────────────────────────────────────┘ │ ┌─────────────────────────────────────────────────────────────┐ │ Specialized Agents │ │ ┌──────────┐ ┌──────────┐ ┌──────────┐ ┌──────────┐ │ │ │Researcher│ │ Coder │ │ Analyst │ │Coordinator│ │ │ └──────────┘ └──────────┘ └──────────┘ └──────────┘ │ └─────────────────────────────────────────────────────────────┘ ``` ### Agent Lifecycle 1. **Creation**: Agent instantiated with specific configuration 2. **Initialization**: Resources allocated, connections established 3. **Ready**: Agent available for task assignment 4. **Working**: Processing assigned tasks 5. **Idle**: Waiting for new tasks 6. **Termination**: Graceful shutdown with cleanup ## Defining Custom Agents ### Basic Agent Definition ```typescript import { BaseAgent } from './base-agent.js'; import type { AgentCapabilities, AgentConfig, TaskDefinition } from '../types.js'; export class CustomAgent extends BaseAgent { protected getDefaultCapabilities(): AgentCapabilities { return { codeGeneration: false, codeReview: true, testing: true, documentation: true, research: false, analysis: true, webSearch: false, apiIntegration: true, fileSystem: true, terminalAccess: false, languages: ['typescript', 'python'], frameworks: ['react', 'django'], domains: ['web-development', 'data-processing'], tools: ['custom-analyzer', 'report-generator'], maxConcurrentTasks: 5, maxMemoryUsage: 512 * 1024 * 1024, // 512MB maxExecutionTime: 600000, // 10 minutes reliability: 0.9, speed: 0.8, quality: 0.95 }; } protected getDefaultConfig(): Partial { return { autonomyLevel: 0.8, learningEnabled: true, adaptationEnabled: true, maxTasksPerHour: 20, permissions: ['file-read', 'api-access'], expertise: { 'custom-task': 0.95, 'analysis': 0.9 } }; } async executeTask(task: TaskDefinition): Promise { switch (task.type) { case 'custom-analysis': return await this.performCustomAnalysis(task); default: return await super.executeTask(task); } } private async performCustomAnalysis(task: TaskDefinition): Promise { // Custom implementation return { result: 'Analysis complete', confidence: 0.92 }; } } ``` ### Agent Registration ```javascript // Register custom agent template const customTemplate = { name: 'Custom Analyzer', type: 'custom-analyzer', capabilities: { // ... capabilities }, config: { // ... configuration }, environment: { runtime: 'deno', workingDirectory: './agents/custom', availableTools: ['analyzer', 'reporter'] } }; agentManager.registerTemplate('custom-analyzer', customTemplate); ``` ## Invoking Agents via Prompts ### Using MCP Tools The Claude Flow system integrates with Model Context Protocol (MCP) tools for agent management: #### 1. Initialize Swarm ```javascript // Initialize coordination topology mcp__claude-flow__swarm_init { topology: "hierarchical", // Options: hierarchical, mesh, ring, star maxAgents: 8, strategy: "auto" // Options: auto, manual, adaptive } ``` #### 2. Spawn Agents ```javascript // Spawn individual agents mcp__claude-flow__agent_spawn { type: "researcher", name: "Data Researcher", capabilities: ["web-research", "data-collection"] } mcp__claude-flow__agent_spawn { type: "coder", name: "Backend Developer", capabilities: ["api-development", "database-design"] } mcp__claude-flow__agent_spawn { type: "coordinator", name: "Project Manager" } ``` ### Prompt-Based Agent Invocation #### Pattern 1: Task-Specific Agent Creation ``` User: "I need to build a user authentication system" Claude: I'll set up a specialized team for building your authentication system. [Single Message - All operations concurrent]: - mcp__claude-flow__swarm_init { topology: "hierarchical", maxAgents: 6, strategy: "parallel" } - mcp__claude-flow__agent_spawn { type: "architect", name: "System Designer" } - mcp__claude-flow__agent_spawn { type: "coder", name: "Auth Specialist" } - mcp__claude-flow__agent_spawn { type: "coder", name: "API Developer" } - mcp__claude-flow__agent_spawn { type: "analyst", name: "Security Analyst" } - mcp__claude-flow__agent_spawn { type: "tester", name: "Security Tester" } - mcp__claude-flow__agent_spawn { type: "coordinator", name: "Auth Lead" } ``` #### Pattern 2: Research-Focused Workflow ``` User: "Research the latest AI trends in healthcare" Claude: Setting up research team for healthcare AI analysis. [Single Message - Concurrent execution]: - mcp__claude-flow__swarm_init { topology: "mesh", maxAgents: 4 } - mcp__claude-flow__agent_spawn { type: "researcher", name: "Medical AI Researcher" } - mcp__claude-flow__agent_spawn { type: "researcher", name: "Tech Trend Analyst" } - mcp__claude-flow__agent_spawn { type: "analyst", name: "Data Scientist" } - mcp__claude-flow__agent_spawn { type: "coordinator", name: "Research Lead" } ``` ### Direct CLI Invocation ```bash # Basic agent spawn claude-flow agent spawn researcher --name "Market Researcher" # Advanced spawn with configuration claude-flow agent spawn coder \ --name "Full-Stack Developer" \ --capabilities "frontend,backend,database" \ --max-tasks 5 \ --memory-limit 1GB # Spawn with custom template claude-flow agent spawn custom-analyzer \ --template ./templates/analyzer.json \ --start ``` ## Agent Orchestration Best Practices ### 1. Topology Selection Choose the right swarm topology for your use case: - **Hierarchical**: Best for complex projects with clear delegation - **Mesh**: Ideal for collaborative research and peer review - **Ring**: Efficient for sequential processing pipelines - **Star**: Centralized coordination with specialized workers ### 2. Agent Composition ```javascript // Effective agent team composition const projectTeam = { leadership: ["coordinator"], research: ["researcher", "analyst"], development: ["architect", "coder", "coder"], quality: ["tester", "reviewer"], support: ["documenter", "optimizer"] }; ``` ### 3. Task Distribution Strategies ```javascript // Capability-based distribution mcp__claude-flow__task_orchestrate { task: "Build microservices platform", strategy: "capability-based", priority: "high", dependencies: ["architecture", "api-design", "testing"] } // Load-balanced distribution mcp__claude-flow__load_balance { swarmId: "dev-team", tasks: ["api-endpoints", "database-schema", "auth-system"] } ``` ### 4. Memory and State Management ```javascript // Share knowledge between agents mcp__claude-flow__memory_usage { action: "store", key: "project-architecture", value: architectureDesign, namespace: "shared-knowledge", ttl: 86400000 // 24 hours } // Retrieve shared knowledge mcp__claude-flow__memory_usage { action: "retrieve", key: "project-architecture", namespace: "shared-knowledge" } ``` ### 5. Performance Monitoring ```javascript // Monitor swarm performance mcp__claude-flow__swarm_status { swarmId: "dev-team" } // Get detailed metrics mcp__claude-flow__performance_report { format: "detailed", timeframe: "24h" } // Identify bottlenecks mcp__claude-flow__bottleneck_analyze { component: "task-processing", metrics: ["throughput", "latency", "error-rate"] } ``` ## Example Workflows ### Workflow 1: Full-Stack Application Development ```javascript // Step 1: Initialize development swarm mcp__claude-flow__swarm_init { topology: "hierarchical", maxAgents: 10, strategy: "adaptive" } // Step 2: Spawn specialized team (all in one message) const agents = [ { type: "coordinator", name: "Tech Lead" }, { type: "architect", name: "System Architect" }, { type: "researcher", name: "Tech Researcher" }, { type: "coder", name: "Backend Dev 1" }, { type: "coder", name: "Backend Dev 2" }, { type: "coder", name: "Frontend Dev 1" }, { type: "coder", name: "Frontend Dev 2" }, { type: "analyst", name: "Database Designer" }, { type: "tester", name: "QA Engineer" }, { type: "documenter", name: "Tech Writer" } ]; // Spawn all agents concurrently agents.forEach(agent => { mcp__claude-flow__agent_spawn(agent); }); // Step 3: Orchestrate development tasks mcp__claude-flow__task_orchestrate { task: "Build e-commerce platform", strategy: "parallel", priority: "high" } // Step 4: Monitor progress mcp__claude-flow__swarm_monitor { swarmId: "dev-team", interval: 5000 // 5 seconds } ``` ### Workflow 2: Research and Analysis Pipeline ```javascript // Initialize research network mcp__claude-flow__swarm_init { topology: "mesh", maxAgents: 5 } // Spawn research team const researchTeam = [ { type: "researcher", name: "Primary Researcher" }, { type: "researcher", name: "Data Collector" }, { type: "analyst", name: "Data Analyst" }, { type: "analyst", name: "Trend Analyzer" }, { type: "coordinator", name: "Research Coordinator" } ]; // Create research workflow mcp__claude-flow__workflow_create { name: "market-research-pipeline", steps: [ { name: "data-collection", agent: "researcher", duration: "2h" }, { name: "initial-analysis", agent: "analyst", duration: "1h" }, { name: "deep-dive", agent: "researcher", duration: "3h" }, { name: "final-report", agent: "coordinator", duration: "1h" } ], triggers: ["manual", "schedule:daily"] } ``` ### Workflow 3: Code Review and Testing Automation ```javascript // Initialize testing swarm mcp__claude-flow__swarm_init { topology: "star", maxAgents: 6 } // Spawn QA team const qaTeam = [ { type: "coordinator", name: "QA Lead" }, { type: "reviewer", name: "Code Reviewer 1" }, { type: "reviewer", name: "Code Reviewer 2" }, { type: "tester", name: "Unit Tester" }, { type: "tester", name: "Integration Tester" }, { type: "specialist", name: "Security Auditor" } ]; // Automate code review process mcp__claude-flow__automation_setup { rules: [ { trigger: "pull-request", action: "code-review", assignTo: "reviewer", priority: "high" }, { trigger: "code-review-complete", action: "run-tests", assignTo: "tester", parallel: true }, { trigger: "tests-passed", action: "security-audit", assignTo: "specialist" } ] } ``` ## Integration with claude-flow init ### Automatic Agent Setup When initializing a new Claude Flow project, agents can be automatically configured: ```bash # Initialize with agent templates claude-flow init --with-agents # Custom agent configuration claude-flow init --agents-config ./my-agents.json ``` ### Project Templates with Agents ```json // .claude-flow/project-template.json { "name": "Full-Stack Project", "agents": { "autoSpawn": true, "topology": "hierarchical", "team": [ { "type": "coordinator", "count": 1 }, { "type": "architect", "count": 1 }, { "type": "coder", "count": 3 }, { "type": "tester", "count": 2 }, { "type": "researcher", "count": 1 } ] }, "workflows": { "development": { "stages": ["planning", "implementation", "testing", "deployment"], "agentAssignment": "automatic" } } } ``` ### Hook Integration ```javascript // .claude-flow/hooks/pre-task.js module.exports = async (context) => { // Automatically spawn agents based on task type if (context.task.type === 'development' && !context.agents.length) { await context.spawnAgent('coder', { name: 'Auto-spawned Developer' }); } }; // .claude-flow/hooks/post-command.js module.exports = async (context) => { // Scale agents based on workload if (context.metrics.queueLength > 10) { await context.scaleAgents({ type: 'coder', delta: 2 }); } }; ``` ## Advanced Features ### 1. Dynamic Agent Provisioning ```javascript // Auto-scaling configuration mcp__claude-flow__swarm_scale { swarmId: "dev-team", targetSize: 12 } // Demand-based provisioning mcp__claude-flow__daa_agent_create { agent_type: "specialized-coder", capabilities: ["rust", "wasm", "optimization"], resources: { memory: "2GB", cpu: "4 cores" } } ``` ### 2. Agent Learning and Adaptation ```javascript // Enable learning for specific agents mcp__claude-flow__learning_adapt { experience: { task: "api-development", outcome: "success", metrics: { time: 3600000, quality: 0.95, issues: 2 } } } // Transfer knowledge between agents mcp__claude-flow__transfer_learn { sourceModel: "senior-developer", targetDomain: "junior-developer" } ``` ### 3. Custom Tool Integration ```javascript // Add specialized tools to agents const customTools = { "code-analyzer": { command: "analyze-code", input: ["file-path", "rules"], output: "analysis-report" }, "performance-profiler": { command: "profile", input: ["target", "duration"], output: "performance-metrics" } }; // Assign tools to agent mcp__claude-flow__agent_spawn { type: "specialist", name: "Performance Expert", tools: customTools } ``` ### 4. Distributed Execution ```javascript // Parallel task execution mcp__claude-flow__parallel_execute { tasks: [ { id: "api-dev", agent: "backend-dev" }, { id: "ui-dev", agent: "frontend-dev" }, { id: "db-design", agent: "data-architect" } ] } // Pipeline execution mcp__claude-flow__pipeline_create { config: { stages: [ { name: "research", agents: ["researcher"], parallel: true }, { name: "design", agents: ["architect"], depends: ["research"] }, { name: "implement", agents: ["coder"], parallel: true, depends: ["design"] }, { name: "test", agents: ["tester"], parallel: true, depends: ["implement"] } ] } } ``` ## Troubleshooting ### Common Issues and Solutions #### 1. Agent Not Responding ```bash # Check agent health claude-flow agent health # View agent logs claude-flow agent logs --tail 100 # Restart unresponsive agent claude-flow agent restart --force ``` #### 2. Memory Issues ```javascript // Check memory usage mcp__claude-flow__memory_analytics { timeframe: "1h" } // Compress memory data mcp__claude-flow__memory_compress { namespace: "agent-data" } // Clear old data mcp__claude-flow__cache_manage { action: "clear", key: "expired-data" } ``` #### 3. Performance Bottlenecks ```javascript // Analyze bottlenecks mcp__claude-flow__bottleneck_analyze { component: "agent-communication", metrics: ["latency", "throughput"] } // Optimize topology mcp__claude-flow__topology_optimize { swarmId: "dev-team" } ``` ### Debug Mode ```bash # Enable debug logging export CLAUDE_FLOW_DEBUG=true claude-flow agent spawn researcher --debug # Trace agent execution claude-flow agent trace --verbose ``` ### Health Monitoring Dashboard ```bash # Start monitoring dashboard claude-flow agent monitor --dashboard --port 8080 # CLI monitoring claude-flow agent monitor --all --metrics all --refresh 5s ``` ## Best Practices Summary 1. **Always batch operations** - Use single messages for multiple agent operations 2. **Choose appropriate topologies** - Match topology to your workflow needs 3. **Monitor agent health** - Regular health checks prevent issues 4. **Use shared memory wisely** - Namespace and TTL for efficient memory usage 5. **Scale gradually** - Start small and scale based on actual needs 6. **Implement proper error handling** - Use hooks for automated recovery 7. **Document agent interactions** - Clear documentation for team collaboration ## Conclusion The Claude Flow agent system provides a powerful foundation for building complex, multi-agent AI workflows. By following these guidelines and best practices, you can create efficient, scalable, and maintainable agent-based solutions. For more information and updates, visit the [Claude Flow GitHub repository](https://github.com/ruvnet/claude-code-flow).