OpenClaw 工作流编排实战:多 Agent 协作与自动化
当你的自动化需求超越单个 Agent 的能力时,就需要工作流编排——让多个 Agent 按照预定义的流程协作完成复杂任务。OpenClaw 的工作流编排系统支持任务依赖、并行执行、条件分支和错误恢复。
🔄 工作流可视化
↓ 通过
↓ 不通过
发布上线
→
通知推送
→
结束
内容改写
→
质量检查
⚙️ 工作流定义
# workflow.yaml - AI 新闻日报工作流
name: ai-news-daily
version: "1.0"
trigger:
type: cron
expr: "0 8 * * *"
tz: Asia/Shanghai
steps:
- id: fetch
agent: news-fetcher
task: "从多个来源抓取今日 AI 新闻"
timeout: 300s
outputs: [articles]
- id: generate
agent: content-writer
task: "将抓取的文章生成日报 HTML"
depends_on: [fetch]
inputs:
articles: ${fetch.outputs.articles}
timeout: 600s
outputs: [html_file]
- id: review
agent: quality-checker
task: "检查内容质量,过滤低质量内容"
depends_on: [generate]
inputs:
html: ${generate.outputs.html_file}
outputs: [approved, needs_revision]
- id: publish
agent: publisher
task: "发布到网站并更新 sitemap"
depends_on: [review]
condition: ${review.outputs.approved} == true
outputs: [url]
- id: notify
agent: notifier
task: "发送 Discord 和飞书通知"
depends_on: [publish]
parallel: true
inputs:
url: ${publish.outputs.url}
- id: revise
agent: content-writer
task: "根据审核意见改写内容"
depends_on: [review]
condition: ${review.outputs.needs_revision} == true
retry: 2
📊 工作流模式
| 模式 | 适用场景 | 示例 |
| 顺序执行 | 步骤间有依赖关系 | 抓取 → 生成 → 发布 |
| 并行执行 | 独立任务同时执行 | 同时推送到 Discord 和飞书 |
| 条件分支 | 根据结果选择不同路径 | 审核通过/不通过 |
| 循环重试 | 失败后自动重试 | 内容改写最多重试 2 次 |
| 扇出/扇入 | 多任务汇聚后合并 | 多源数据采集后合并 |
🔧 实战:多 Agent 内容管线
# 创建工作流
openclaw workflow create content-pipeline \
--config=workflow.yaml
# 手动触发
openclaw workflow run content-pipeline
# 查看执行状态
openclaw workflow status content-pipeline
# 查看执行历史
openclaw workflow history content-pipeline --limit=10
# 查看某次执行的详细日志
openclaw workflow logs content-pipeline --run-id=20260613-001
💡 最佳实践
✅ 错误处理:为每个步骤设置合理的超时时间和重试策略。使用 fail-closed 模式,确保异常步骤不会导致整个工作流静默失败。
1. 幂等设计
确保每个步骤都是幂等的——重复执行不会产生副作用。这对于错误恢复至关重要。
2. 可观测性
为每个步骤添加详细的日志和指标,方便排查问题:
steps:
- id: fetch
...
logging:
level: debug
include_inputs: true
include_outputs: true
metrics:
- name: fetch_duration_seconds
- name: articles_fetched_count
3. 渐进式复杂化
从简单的线性工作流开始,逐步添加并行、条件和重试逻辑。不要一开始就设计过于复杂的流程。