multi-agent-architecture

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese

Multi-Agent Architecture - 多 Agent 架构

Multi-Agent Architecture

概述

Overview

OpenClaw 支持多 Agent 架构,每个 Agent 可以有不同的:
  • 专业领域和 System Prompt
  • 模型配置和成本策略
  • Channel 绑定和权限
  • 工具集和 MCP 配置
OpenClaw supports multi-agent architecture, where each Agent can have different:
  • Professional domains and System Prompt
  • Model configurations and cost strategies
  • Channel bindings and permissions
  • Toolkits and MCP configurations

架构设计

Architecture Design

推荐的 Agent 分工

Recommended Agent Division of Labor

┌─────────────────────────────────────────────────────────────┐
│                      Main Agent (小a)                        │
│  - 主会话处理                                                 │
│  - 任务分发和协调                                             │
│  - 复杂决策和规划                                             │
│  - 模型: opus-4.5 (高质量)                                    │
└─────────────────────────────────────────────────────────────┘
          ┌───────────────────┼───────────────────┐
          ▼                   ▼                   ▼
┌─────────────────┐ ┌─────────────────┐ ┌─────────────────┐
│   News Agent    │ │   Code Agent    │ │  Research Agent │
│  - 新闻抓取     │ │  - 代码生成     │ │  - 深度研究     │
│  - 内容摘要     │ │  - Bug 修复     │ │  - 文档分析     │
│  - 定时推送     │ │  - 代码审查     │ │  - 知识整合     │
│  模型: sonnet   │ │  模型: codex    │ │  模型: opus     │
└─────────────────┘ └─────────────────┘ └─────────────────┘
          │                   │                   │
          ▼                   ▼                   ▼
┌─────────────────┐ ┌─────────────────┐ ┌─────────────────┐
│  Quick Agent    │ │  Batch Agent    │ │  Monitor Agent  │
│  - 快速问答     │ │  - 批量处理     │ │  - 系统监控     │
│  - 简单任务     │ │  - 数据处理     │ │  - 健康检查     │
│  - 低延迟响应   │ │  - 文件操作     │ │  - 告警通知     │
│  模型: flash    │ │  模型: mini     │ │  模型: mini     │
└─────────────────┘ └─────────────────┘ └─────────────────┘
┌─────────────────────────────────────────────────────────────┐
│                      Main Agent (Xiaoa)                        │
│  - Main session processing                                                 │
│  - Task distribution and coordination                                             │
│  - Complex decision-making and planning                                             │
│  - Model: opus-4.5 (High quality)                                    │
└─────────────────────────────────────────────────────────────┘
          ┌───────────────────┼───────────────────┐
          ▼                   ▼                   ▼
┌─────────────────┐ ┌─────────────────┐ ┌─────────────────┐
│   News Agent    │ │   Code Agent    │ │  Research Agent │
│  - News scraping     │ │  - Code generation     │ │  - In-depth research     │
│  - Content summarization     │ │  - Bug fixing     │ │  - Document analysis     │
│  - Scheduled push notifications     │ │  - Code review     │ │  - Knowledge integration     │
│  Model: sonnet   │ │  Model: codex    │ │  Model: opus     │
└─────────────────┘ └─────────────────┘ └─────────────────┘
          │                   │                   │
          ▼                   ▼                   ▼
┌─────────────────┐ ┌─────────────────┐ ┌─────────────────┐
│  Quick Agent    │ │  Batch Agent    │ │  Monitor Agent  │
│  - Quick Q&A     │ │  - Batch processing     │ │  - System monitoring     │
│  - Simple tasks     │ │  - Data processing     │ │  - Health check     │
│  - Low-latency response   │ │  - File operations     │ │  - Alert notifications     │
│  Model: flash    │ │  Model: mini     │ │  Model: mini     │
└─────────────────┘ └─────────────────┘ └─────────────────┘

Agent 配置

Agent Configuration

1. 创建 Agent 目录结构

1. Create Agent Directory Structure

bash
~/.openclaw/agents/
├── main/           # 主 Agent (已存在)
│   └── agent/
│       ├── AGENTS.md
│       ├── SOUL.md
│       └── ...
├── news/           # 新闻 Agent
│   └── agent/
│       ├── AGENTS.md
│       └── config.json
├── code/           # 代码 Agent
│   └── agent/
│       ├── AGENTS.md
│       └── config.json
├── research/       # 研究 Agent
│   └── agent/
│       ├── AGENTS.md
│       └── config.json
├── quick/          # 快速响应 Agent
│   └── agent/
│       └── config.json
└── batch/          # 批量处理 Agent
    └── agent/
        └── config.json
bash
~/.openclaw/agents/
├── main/           # Main Agent (Exists)
│   └── agent/
│       ├── AGENTS.md
│       ├── SOUL.md
│       └── ...
├── news/           # News Agent
│   └── agent/
│       ├── AGENTS.md
│       └── config.json
├── code/           # Code Agent
│   └── agent/
│       ├── AGENTS.md
│       └── config.json
├── research/       # Research Agent
│   └── agent/
│       ├── AGENTS.md
│       └── config.json
├── quick/          # Quick Response Agent
│   └── agent/
│       └── config.json
└── batch/          # Batch Processing Agent
    └── agent/
        └── config.json

2. Agent 配置示例

2. Agent Configuration Examples

News Agent (
~/.openclaw/agents/news/agent/config.json
)

News Agent (
~/.openclaw/agents/news/agent/config.json
)

json
{
  "model": {
    "primary": "anthropic/claude-sonnet-4-5"
  },
  "systemPrompt": "你是新闻抓取和摘要专家。专注于:\n1. 从权威来源抓取真实新闻\n2. 生成简洁准确的摘要\n3. 确保每条新闻有原文链接\n4. 按时推送到指定渠道",
  "tools": {
    "allow": ["web_fetch", "exec", "message"]
  }
}
json
{
  "model": {
    "primary": "anthropic/claude-sonnet-4-5"
  },
  "systemPrompt": "You are an expert in news scraping and summarization. Focus on:\n1. Scrape real news from authoritative sources\n2. Generate concise and accurate summaries\n3. Ensure each news item has the original link\n4. Push to specified channels on time",
  "tools": {
    "allow": ["web_fetch", "exec", "message"]
  }
}

Code Agent (
~/.openclaw/agents/code/agent/config.json
)

Code Agent (
~/.openclaw/agents/code/agent/config.json
)

json
{
  "model": {
    "primary": "openrouter-vip/gpt-5.2-codex"
  },
  "systemPrompt": "你是代码专家。专注于:\n1. 高质量代码生成\n2. Bug 分析和修复\n3. 代码审查和优化\n4. 技术文档编写",
  "tools": {
    "allow": ["read", "write", "edit", "exec"]
  }
}
json
{
  "model": {
    "primary": "openrouter-vip/gpt-5.2-codex"
  },
  "systemPrompt": "You are a code expert. Focus on:\n1. High-quality code generation\n2. Bug analysis and fixing\n3. Code review and optimization\n4. Technical documentation writing",
  "tools": {
    "allow": ["read", "write", "edit", "exec"]
  }
}

Quick Agent (
~/.openclaw/agents/quick/agent/config.json
)

Quick Agent (
~/.openclaw/agents/quick/agent/config.json
)

json
{
  "model": {
    "primary": "google/gemini-flash-latest"
  },
  "systemPrompt": "你是快速响应助手。特点:\n1. 简洁直接的回答\n2. 低延迟响应\n3. 处理简单查询\n4. 不需要深度分析的任务"
}
json
{
  "model": {
    "primary": "google/gemini-flash-latest"
  },
  "systemPrompt": "You are a quick response assistant. Features:\n1. Concise and direct answers\n2. Low-latency response\n3. Handle simple queries\n4. Tasks that do not require in-depth analysis"
}

3. 在 openclaw.json 中注册 Agent

3. Register Agents in openclaw.json

json
{
  "agents": {
    "entries": {
      "news": {
        "enabled": true,
        "allowSpawnFrom": ["main"]
      },
      "code": {
        "enabled": true,
        "allowSpawnFrom": ["main"]
      },
      "research": {
        "enabled": true,
        "allowSpawnFrom": ["main"]
      },
      "quick": {
        "enabled": true,
        "allowSpawnFrom": ["main"]
      },
      "batch": {
        "enabled": true,
        "allowSpawnFrom": ["main"]
      }
    },
    "defaults": {
      "maxConcurrent": 4,
      "subagents": {
        "maxConcurrent": 8
      }
    }
  }
}
json
{
  "agents": {
    "entries": {
      "news": {
        "enabled": true,
        "allowSpawnFrom": ["main"]
      },
      "code": {
        "enabled": true,
        "allowSpawnFrom": ["main"]
      },
      "research": {
        "enabled": true,
        "allowSpawnFrom": ["main"]
      },
      "quick": {
        "enabled": true,
        "allowSpawnFrom": ["main"]
      },
      "batch": {
        "enabled": true,
        "allowSpawnFrom": ["main"]
      }
    },
    "defaults": {
      "maxConcurrent": 4,
      "subagents": {
        "maxConcurrent": 8
      }
    }
  }
}

智能 Spawn 系统

Intelligent Spawn System

任务分类规则

Task Classification Rules

Main Agent 根据任务类型自动选择合适的 Agent:
任务类型关键词目标 Agent模型
新闻抓取news, 新闻, 早报, 推送newssonnet
代码任务code, 代码, bug, 开发codecodex
深度研究research, 分析, 调研researchopus
快速问答简单, 快速, 查询quickflash
批量处理batch, 批量, 文件batchmini
复杂任务保留在 mainmainopus
Main Agent automatically selects the appropriate Agent based on task type:
Task TypeKeywordsTarget AgentModel
News Scrapingnews, news, morning briefing, pushnewssonnet
Code Taskscode, code, bug, developmentcodecodex
In-depth Researchresearch, analysis, researchresearchopus
Quick Q&Asimple, quick, queryquickflash
Batch Processingbatch, batch, filebatchmini
Complex TasksRetained in mainmainopus

智能 Spawn 实现

Intelligent Spawn Implementation

python
undefined
python
undefined

在 AGENTS.md 中添加智能 Spawn 逻辑

Add intelligent spawn logic in AGENTS.md

🧠 智能任务分发

🧠 Intelligent Task Distribution

当收到任务时,评估以下因素:
  1. 任务复杂度
    • 简单查询 → quick agent
    • 中等任务 → 专业 agent
    • 复杂任务 → main 处理或 research agent
  2. 任务类型
    • 新闻相关 → news agent
    • 代码相关 → code agent
    • 研究分析 → research agent
    • 批量操作 → batch agent
  3. 时间敏感度
    • 需要快速响应 → quick agent
    • 可以等待 → 专业 agent
  4. 资源消耗
    • 高 token 消耗 → 使用便宜模型的 agent
    • 需要高质量 → 使用 opus 的 agent
When receiving a task, evaluate the following factors:
  1. Task Complexity
    • Simple queries → quick agent
    • Medium tasks → specialized agents
    • Complex tasks → handled by main or research agent
  2. Task Type
    • News-related → news agent
    • Code-related → code agent
    • Research and analysis → research agent
    • Batch operations → batch agent
  3. Time Sensitivity
    • Requires quick response → quick agent
    • Can wait → specialized agents
  4. Resource Consumption
    • High token consumption → use agents with cheaper models
    • Requires high quality → use agents with opus

Spawn 命令示例

Spawn Command Examples

python
undefined
python
undefined

新闻任务

News Task

sessions_spawn( task="抓取今日科技新闻并推送到 DailyNews 群组", agentId="news", label="news-morning" )
sessions_spawn( task="Scrape today's tech news and push to the DailyNews group", agentId="news", label="news-morning" )

代码任务

Code Task

sessions_spawn( task="修复 auth.py 中的登录 bug", agentId="code", label="fix-auth-bug" )
sessions_spawn( task="Fix the login bug in auth.py", agentId="code", label="fix-auth-bug" )

研究任务

Research Task

sessions_spawn( task="深度分析 GPT-5 的技术架构", agentId="research", label="gpt5-analysis" )
sessions_spawn( task="In-depth analysis of GPT-5's technical architecture", agentId="research", label="gpt5-analysis" )

快速查询

Quick Query

sessions_spawn( task="查询今天的天气", agentId="quick", label="weather-check" )
undefined
sessions_spawn( task="Check today's weather", agentId="quick", label="weather-check" )
undefined

并发处理

Concurrent Processing

配置并发限制

Configure Concurrency Limits

json
{
  "agents": {
    "defaults": {
      "maxConcurrent": 4,      // 主 agent 最大并发
      "subagents": {
        "maxConcurrent": 8    // 子 agent 最大并发
      }
    }
  }
}
json
{
  "agents": {
    "defaults": {
      "maxConcurrent": 4,      // Maximum concurrency for main agent
      "subagents": {
        "maxConcurrent": 8    // Maximum concurrency for sub agents
      }
    }
  }
}

并发场景

Concurrency Scenarios

用户消息 → Main Agent
              ├─→ spawn(news) ──→ 抓取新闻
              ├─→ spawn(code) ──→ 修复 bug
              └─→ spawn(research) ──→ 深度分析
              
              ↓ (并行执行)
              
         所有任务完成后汇报
User message → Main Agent
              ├─→ spawn(news) ──→ Scrape news
              ├─→ spawn(code) ──→ Fix bug
              └─→ spawn(research) ──→ In-depth analysis
              
              ↓ (Executed in parallel)
              
         Report after all tasks are completed

Channel 绑定

Channel Binding

不同 Channel 使用不同 Agent

Use Different Agents for Different Channels

json
{
  "channels": {
    "telegram": {
      "defaultAgent": "main"
    },
    "whatsapp": {
      "defaultAgent": "main"
    }
  },
  "agents": {
    "entries": {
      "news": {
        "channels": ["telegram-newsbot"]
      }
    }
  }
}
json
{
  "channels": {
    "telegram": {
      "defaultAgent": "main"
    },
    "whatsapp": {
      "defaultAgent": "main"
    }
  },
  "agents": {
    "entries": {
      "news": {
        "channels": ["telegram-newsbot"]
      }
    }
  }
}

监控和管理

Monitoring and Management

查看活跃 Session

View Active Sessions

bash
undefined
bash
undefined

列出所有 session

List all sessions

openclaw sessions list
openclaw sessions list

查看特定 agent 的 session

View sessions for a specific agent

openclaw sessions list --agent news
undefined
openclaw sessions list --agent news
undefined

查看 Spawn 状态

View Spawn Status

python
undefined
python
undefined

在代码中

In code

sessions_list(kinds=["spawn"], limit=10)
undefined
sessions_list(kinds=["spawn"], limit=10)
undefined

最佳实践

Best Practices

1. 任务分发原则

1. Task Distribution Principles

  • 简单任务不 spawn - 直接处理更快
  • 耗时任务必 spawn - 不阻塞主会话
  • 相关任务批量 spawn - 提高效率
  • Do not spawn for simple tasks - Direct processing is faster
  • Always spawn for time-consuming tasks - Do not block the main session
  • Spawn related tasks in batches - Improve efficiency

2. 模型选择原则

2. Model Selection Principles

  • 质量优先 → opus
  • 速度优先 → flash
  • 代码任务 → codex
  • 成本优先 → mini
  • Quality first → opus
  • Speed first → flash
  • Code tasks → codex
  • Cost first → mini

3. 错误处理

3. Error Handling

python
undefined
python
undefined

spawn 时设置超时

Set timeout when spawning

sessions_spawn( task="...", agentId="code", runTimeoutSeconds=300, # 5分钟超时 cleanup="keep" # 保留 session 用于调试 )
undefined
sessions_spawn( task="...", agentId="code", runTimeoutSeconds=300, # 5-minute timeout cleanup="keep" # Keep session for debugging )
undefined

相关资源

Related Resources


由小a设计 - 实现真正的多 Agent 协作

Designed by Xiaoa - Achieve true multi-agent collaboration