🔄 OpenClaw 工作流编排实战

📅 2026年5月19日 | 🏷️ 工作流专题 | ⏱️ 阅读时间:22分钟

Pipeline、Swarm、Hierarchy 三种模式全解析,附实战案例代码。

🎬 为什么需要工作流编排?

凌晨4点47分,我让一个 Agent 同时做数据抓取、分析、可视化、报告生成——结果它卡在第2步就不动了。

就像周星驰电影《唐伯虎点秋香》里的"含笑半步癫"——一个环节掉链子,整个流程就崩了。工作流编排就是给你的 Agent 配上"钢铁侠战衣",让每个环节都有序衔接。

🔍 工作流 vs 单 Agent

维度 单 Agent 工作流编排
任务复杂度 简单任务 复杂多步骤
容错能力 低(一步错全崩) 高(可重试/跳过)
执行效率 串行(慢) 并行(快)

🏗️ 模式1:Pipeline(流水线)

最简单的编排模式:Agent A → Agent B → Agent C,像工厂流水线。

配置示例:数据抓取→分析→报告

// openclaw-pipeline.json
{
    "name": "data-analysis-pipeline",
    "description": "自动抓取数据、分析、生成报告",
    "agents": {
        "scraper": {
            "skills": ["web-scraper", "rss-fetcher"],
            "config": {
                "maxPages": 100,
                "timeout": 300000
            }
        },
        "analyzer": {
            "skills": ["pandas", "matplotlib"],
            "config": {
                "model": "gpt-4",
                "temperature": 0.3
            }
        },
        "reporter": {
            "skills": ["markdown-gen", "chart-js"],
            "config": {
                "outputFormat": "html"
            }
        }
    },
    "pipeline": [
        {
            "step": 1,
            "agent": "scraper",
            "input": "{{trigger.url}}",
            "output": "$rawData",
            "onError": "retry|3"
        },
        {
            "step": 2,
            "agent": "analyzer",
            "input": "$rawData",
            "output": "$analysis",
            "dependsOn": [1]
        },
        {
            "step": 3,
            "agent": "reporter",
            "input": "$analysis",
            "output": "$report",
            "dependsOn": [2]
        }
    ],
    "output": "$report"
}
💡 最佳实践: Pipeline 适合步骤固定的任务,每个步骤应该职责单一,易于测试和调试。

🏗️ 模式2:Swarm(集群)

多个 Agent 并行执行相同或不同的任务,最后汇总结果。

配置示例:多平台舆情监控

// openclaw-swarm.json
{
    "name": "sentiment-monitor-swarm",
    "description": "监控多个平台的舆情",
    "workers": [
        {
            "id": "twitter-bot",
            "agent": {
                "skills": ["twitter-api"],
                "config": { "keywords": ["OpenClaw", "AI Agent"] }
            }
        },
        {
            "id": "reddit-bot",
            "agent": {
                "skills": ["reddit-api", "web-scraper"],
                "config": { "subreddits": ["r/OpenClaw", "r/AI"] }
            }
        },
        {
            "id": "hn-bot",
            "agent": {
                "skills": ["hackernews-api"],
                "config": { "threshold": 100 }  // 只抓 100+ points
            }
        }
    ],
    "aggregator": {
        "agent": {
            "skills": ["data-dedup", "sentiment-analysis"],
            "config": { "outputFormat": "json" }
        },
        "inputs": ["twitter-bot", "reddit-bot", "hn-bot"]
    },
    "schedule": {
        "cron": "0 6,18 * * *",  // 每天 6:00 和 18:00
        "timeout": 3600000
    }
}

🎯 实战效果(miaoquai.com 数据)

  • 📊 运行 2392 期 RSS 聚合,零失败
  • ⚡ 3个 Agent 并行,速度提升 300%
  • 🔄 自动去重,节省 40% 存储空间
  • 📈 每天 2 次执行,已持续 50+ 天

🏗️ 模式3:Hierarchy(层级)

一个主 Agent 协调多个子 Agent,主 Agent 负责任务分配和质量把控。

配置示例:AI 行业报告生成

// openclaw-hierarchy.json
{
    "name": "ai-report-hierarchy",
    "description": "生成 AI 行业深度报告",
    "coordinator": {
        "agent": {
            "skills": ["task-distribution", "quality-check"],
            "model": "gpt-4-turbo",
            "config": {
                "maxRetries": 2,
                "qualityThreshold": 0.8
            }
        }
    },
    "subordinates": [
        {
            "name": "researcher",
            "agent": {
                "skills": ["web-fetch", "academic-search"],
                "config": { "maxSources": 50 }
            },
            "tasks": ["搜索2026年AI行业数据", "收集竞品动态"]
        },
        {
            "name": "analyst",
            "agent": {
                "skills": ["data-viz", "statistics"],
                "config": { "chartTypes": ["line", "bar", "pie"] }
            },
            "tasks": ["分析增长趋势", "生成可视化图表"]
        },
        {
            "name": "writer",
            "agent": {
                "skills": ["content-gen", "grammar-check"],
                "config": { "tone": "professional", "length": "3000+ words" }
            },
            "tasks": ["撰写报告正文", "生成执行摘要"]
        }
    ],
    "qualityGate": {
        "checks": [
            { "type": "plagiarism", "threshold": 0.1 },
            { "type": "readability", "minScore": 60 },
            { "type": "factCheck", "minConfidence": 0.9 }
        ],
        "onFail": "return_to_agent"
    }
}

⚡ 错误处理与重试

// 全局错误策略
{
    "errorHandling": {
        "defaultAction": "retry",
        "maxRetries": 3,
        "backoff": {
            "type": "exponential",
            "initialDelay": 1000,
            "maxDelay": 30000
        },
        "fallbackAgent": "backup-agent",  // 备用Agent
        "onMaxRetries": "skip_or_fail"
    },
    "specificErrors": {
        "rate_limit": {
            "action": "wait_and_retry",
            "waitTime": 60000
        },
        "timeout": {
            "action": "retry",
            "maxRetries": 5
        },
        "invalid_input": {
            "action": "skip",
            "logLevel": "warn"
        }
    }
}
💡 最佳实践: 生产环境务必配置错误重试和备用方案,避免因临时故障导致整个工作流失败。

📊 监控与可视化

# 查看工作流状态
openclaw workflow status --name=data-analysis-pipeline

# 输出示例:
# Workflow: data-analysis-pipeline
# Status: Running
# Progress: 2/3 steps completed
# Current: reporter (step 3)
# Started: 2026-05-19 01:23:45
# Estimated completion: 01:25:30

# 查看执行日志
openclaw workflow logs --name=data-analysis-pipeline --tail=50

# 可视化工作流(生成 SVG)
openclaw workflow visualize --name=data-analysis-pipeline --output=workflow.svg

🌟 总结

3分37秒,我决定了要让工作流成为 Agent 的"神经系统"——每个环节都精准配合,像一支交响乐团。

三种模式适用场景:

实战经验:miaoquai.com 的 RSS 聚合使用 Swarm 模式,已稳定运行 2392 期。