← 返回首页 | 工具教程目录 | 术语百科 | 踩坑实录

🤖 OpenClaw SubAgent 与多 Agent 协作

OpenClaw SubAgent Multi-Agent 协作 编排

凌晨1点42分,我突然想到——一个 AI 干活像光杆司令,一群 AI 干活就是复仇者联盟。

什么是 SubAgent?

SubAgent 是 OpenClaw 中可被父 Agent 动态创建的「子 Agent」。每个 SubAgent 可以有自己的 Goals、Skills 和 Context,就像你雇了一群远程员工,各自干自己的活。

1️⃣ SubAgent 基础用法

# 创建和启动 SubAgent
from openclaw import spawn_agent

# 创建一个研究型 SubAgent
researcher = spawn_agent(
    name="research-agent",
    system_prompt="你是一个信息研究专家,擅长收集和分析信息。",
    skills=["web_search", "data_extraction"],
    context={"topic": "OpenClaw 生态趋势"},
    timeout_ms=30000,
    max_tokens=8000
)

# 获取结果
result = researcher.await_result()
print(f"Research complete: {result.summary}")

# 创建多个并行 SubAgent
agents = []
for topic in ["安全", "性能", "社区趋势"]:
    agent = spawn_agent(
        name=f"research-{topic}",
        system_prompt=f"研究{topic}方向的最新动态",
        skills=["web_search"]
    )
    agents.append(agent)

# 等待所有完成
for agent in agents:
    result = agent.await_result()
    process_result(agent.name, result)

2️⃣ 多 Agent 协作模式

先搭建「执行者-分析师-审查者」三角协作架构:

# multi-agent-team.yaml
team:
  name: "content-production-team"
  coordinator: "content-manager"
  
  agents:
    - name: "researcher"
      role: "信息收集"
      skills: ["web_search", "data_fetch", "fact_checking"]
      parallel: true
      
    - name: "analyst"
      role: "数据分析"
      skills: ["data_analysis", "insight_generation"]
      depends_on: ["researcher"]
      
    - name: "writer"
      role: "内容创作"
      skills: ["content_writing", "seo_optimization"]
      depends_on: ["analyst"]
      
    - name: "reviewer"
      role: "质量审核"
      skills: ["fact_checking", "grammar_check", "readability"]
      depends_on: ["writer"]
  
  # 任务分发策略
  strategy:
    task_type: "batch"
    batch_size: 5
    parallelism: 2
    retry_on_failure: true
    max_retries: 3

3️⃣ SubAgent 通信协议

# subagent-communication.yaml
communication:
  protocol: "message_bus"
  transport: "in_process"
  
  message_types:
    - type: "task_request"
      fields: ["task_id", "payload", "deadline"]
    - type: "progress_update"
      fields: ["task_id", "progress", "eta"]
    - type: "result"
      fields: ["task_id", "data", "confidence"]
    - type: "error"
      fields: ["task_id", "error", "stacktrace"]
  
  # 通信质量保证
  reliability:
    delivery: "at_least_once"
    timeout_ms: 5000
    retry_policy: "exponential_backoff"

4️⃣ 实战:内容工厂的 SubAgent 架构

miaoquai.com 的内容工厂就用这套架构每天自动产出 10+ 页面:

# content-factory-v2.yaml
factory:
  name: "miaoquai-content-factory"
  schedule: "0 1 * * *"  # 每天凌晨1点
  
  pipeline:
    - stage: "选题策划"
      coordinator: true
      agent: "topic-planner"
      tools: ["trend_analysis", "keyword_research"]
      
    - stage: "并行调研"
      agents:
        - name: "tech-researcher"
          context: "技术细节和代码示例"
        - name: "competitor-researcher"
          context: "竞品对比和信息补充"
        - name: "seo-researcher"
          context: "关键词优化建议"
      
    - stage: "内容生成"
      agent: "content-writer"
      input_merge: true  # 合并所有 SubAgent 结果
      
    - stage: "质量审核"
      agent: "content-reviewer"
      criteria:
        - "五维≥4.0"
        - "AI味<2.0"
        - "内链完整性"
  
  # 错误处理
  error_handling:
    subagent_failure: "restart"
    coordinator_failure: "notify_admin"

⚠️ 踩坑提醒:SubAgent 之间别搞出死锁了!A 等 B,B 等 C,C 又等 A——我之前就遇到过,三个 Agent 互相等待,像极了我们团队的日常沟通 🙃 解决办法:加超时 + 看门狗(watchdog)。

5️⃣ SubAgent 生命周期管理

# lifecycle-management.yaml
subagent_lifecycle:
  phases:
    - name: "spawning"
      timeout: "10s"
      failure: "retry"
    - name: "initializing"
      timeout: "5s"
      failure: "abort"
    - name: "executing"
      timeout: "300s"
      failure: "restart"
    - name: "cleanup"
      timeout: "30s"
      always: true
    
  # 资源回收
  cleanup:
    auto_clean: true
    max_idle: "5m"
    hard_limit: 20  # 同时运行的 SubAgent 上限
    
  # 监控
  monitoring:
    snapshot_on_error: true
    log_level: "info"

📊 SubAgent vs 传统 Agent

特性传统单 AgentSubAgent 多 Agent
Context 消耗单个窗口,大分散,精准,小
任务速度串行并行
容错一个挂全家子 Agent 独立重试
复杂任务处理天然适合

📚 相关资源

💡 妙趣总结

凌晨1点42分,你的 Agent 军团准备就绪——下一个复仇者就是你 🦸

相关阅读SubAgent 编排实战SubAgent 工作流模式SubAgent 协作指南


© 2026 妙趣AI (miaoquai.com) 🤖