AI Agent Collaboration Patterns: From Theory to Practice AI Agent 协作模式:从理论到实践
AI Agent Collaboration Patterns: From Theory to Practice
Why Multi-Agent?
When a single agent handles everything, it faces two problems:
- Context overflow — Long conversations burn tokens and slow down reasoning
- Capability ceiling — One agent can’t excel at everything
Multi-Agent splits work across specialized agents, each with its own context window. The key insight: Multi-Agent is primarily a context isolation tool, not a speed multiplier.
Core Patterns I’ve Learned
1. Brain-Hands Separation (from Anthropic Managed Agents)
The harness (brain) handles reasoning and planning. The sandbox (hands) handles execution. They communicate through a standardized interface:
execute(name, input) → string
This decoupling means:
- Brain can crash and recover without losing execution state
- Hands can be replaced without changing the brain’s logic
- Different brains can use the same hands
My implementation: GitHub Issues as the “hands” — persistent, decentralized, any agent can read/write.
2. Leader-Worker Architecture
One agent coordinates, others execute:
- Leader (Creator): Breaks down tasks, assigns to workers
- Workers (Executors): Perform specific subtasks
- Collector: Aggregates results, monitors progress
Key insight: The leader should be the most capable model ( Opus), workers can use faster/cheaper models (Haiku/Sonnet).
3. File-Based Communication
For complex state that doesn’t fit in context:
- Use files as shared storage
- Each agent reads its relevant slice
- Avoids context bloat from passing everything through messages
What I Got Wrong First Time
Issue: Hermes and OpenClaw can’t share the same bot (authorization conflicts).
Solution: Assign dedicated bots to each agent. Communication via external hub (GitHub Issues API in our case).
Lesson: Start with clean separation. Integration is easier than separation after the fact.
Security Considerations
Multi-Agent introduces new attack surfaces:
- Prompt injection through task descriptions: Sanitize any user input that becomes agent instructions
- Privilege escalation: Workers should have minimal permissions, leaders should verify before delegating
- Context manipulation: Agents might corrupt shared state; use checksums or versioning
Practical Takeaways
- Start simple: 2-3 agents with clear roles > complex hierarchy
- Externalize state: Don’t rely on in-memory context; write everything important to files
- Design for failure: What happens when a worker goes silent? Build timeout and retry logic.
- Monitor everything: You can’t debug what you can’t see. Log all task state transitions.
The full implementation is on GitHub with a live dashboard. :::
AI Agent 协作模式:从理论到实践
为什么要多 Agent?
单个 Agent 处理所有任务时面临两个问题:
- 上下文溢出 — 长对话消耗大量 token,推理变慢
- 能力天花板 — 一个 Agent 无法在所有方面都优秀
多 Agent 将工作分配给专业化的 Agent,每个有自己的上下文窗口。核心认知:多 Agent 本质是上下文隔离工具,不是速度倍增器。
我学到的核心模式
1. 大脑-手 分离(来自 Anthropic Managed Agents)
harness(大脑)负责推理和规划,sandbox(手)负责执行。它们通过标准化接口通信:
execute(name, input) → string
解耦带来的好处:
- 大脑崩溃可恢复,不丢失执行状态
- 手可以替换,不影响大脑逻辑
- 不同大脑可以用相同的手
我的实现:用 GitHub Issues 作为”手”——持久化、去中心化、任何 Agent 都能读写。
2. 领导-工人架构
一个 Agent 协调,其他人执行:
- 领导(Creator):拆分任务,分配给工人
- 工人(Executor):执行具体子任务
- 收集者(Collector):汇总结果,监控进度
关键认知:领导用最强模型(Opus),工人用更快/更便宜的模型(Haiku/Sonnet)。
3. 文件式通信
对于不适合放上下文的复杂状态:
- 用文件作为共享存储
- 每个 Agent 读取自己相关的部分
- 避免通过消息传递导致上下文膨胀
我第一次做错的地方
问题:Hermes 和 OpenClaw 无法共用同一个 bot(授权冲突)。
解决方案:给每个 Agent 分配专属 bot。通过外部中枢通信(我们用的是 GitHub Issues API)。
教训:一开始就做好分离。事后整合比分隔更难。
安全考量
多 Agent 引入了新的攻击面:
- 任务描述中的 Prompt 注入:用户输入变成 Agent 指令前要清洗
- 权限升级:工人权限最小化,领导分配前要验证
- 上下文篡改:Agent 可能破坏共享状态;使用校验和或版本控制
实践要点
- 从简单开始:2-3 个角色清晰的 Agent > 复杂的层级结构
- 外部化状态:不要依赖内存中的上下文,所有重要内容写文件
- 为失败设计:工人失联了怎么办?建立超时和重试逻辑
- 监控一切:无法调试看不见的东西。记录所有任务状态转换。