ai-collab — Autonomous Multi-Agent Collaboration
Two OpenClaw agents working in parallel on shared tasks, coordinated via a structured chat log and daemon inbox protocol.
Architecture Overview
CODEBLOCK0
Agent A (Primary): Interactive Claude Code session. Handles browser, complex tasks, user-facing responses.
Agent B (Daemon): claude --print subprocess. Handles background tasks, monitoring, quick lookups. Triggered by messages dropped in inbox.
Configuration
All settings via environment variables — no hardcoded values:
CODEBLOCK1
Supported models for AGENT_B_MODEL:
- -
claude-haiku-4-5-20251001 — fastest, cheapest (recommended for daemon) - INLINECODE3 — more capable, higher cost
- Any OpenAI model if using the GPT daemon variant (see
examples/claude-gpt.md)
Quick Setup
CODEBLOCK2
Communication Protocol
Every message between agents follows this format. No open loops.
| Tag | Direction | Meaning |
|---|
| INLINECODE5 | A→B or B→A | Assign a task |
| INLINECODE6 |
receiver | Acknowledged, starting work |
|
[DONE:name] | executor | Task complete + one-line result |
|
[BLOCKED:name] | executor | Can't complete + reason |
|
[HANDOFF:name] | either | "Do X, reply [DONE:name] when finished" |
|
[STATUS:update] | either | Async update on long-running task |
|
[QUESTION:topic] | either | Needs info before proceeding |
Rules:
- 1. Answer questions before asking new ones
- Close tasks before starting new ones
- Every message moves work forward or closes a loop
- Never: "let me know", "ready when you are", "standing by"
Example exchange:
A → B: [TASK:price-check] Get BTC price from CoinGecko
B → A: [ACK:price-check] Checking now.
B → A: [DONE:price-check] BTC $94,230 as of 03:15 UTC
Daemon Script (Agent B)
INLINECODE12 — drop in your collab directory:
CODEBLOCK4
Sending Messages (A → B)
CODEBLOCK5
Atomic write pattern (prevents partial reads):
CODEBLOCK6
Always use mktemp + mv — never write directly to inbox. inotifywait fires on moved_to, not close_write.
Chat Log Polling (B → A)
INLINECODE17 — run via cron every 60s:
CODEBLOCK7
Add to crontab:
* * * * * /bin/bash ~/.openclaw/workspace/collab/poll_chatlog.sh
Shared Filesystem Conventions
CODEBLOCK9
Logging to chat.log:
CODEBLOCK10
Rules:
- - Never log secrets from INLINECODE18
- Always timestamp every line
- Prefix with sender:
A -> B: or B -> A: or SYSTEM: or INLINECODE22
Telegram Bridge (Optional)
Route user Telegram messages into Agent B's inbox. Full setup guide: INLINECODE23
Quick summary:
- 1. Create a bot via @BotFather → get INLINECODE24
- Add bot to your group → get
GROUP_ID (negative number, e.g. -1001234567890) - Disable Group Privacy Mode in BotFather so bot can read all messages
- Get your Telegram
USER_ID from @userinfobot - Set in
~/.openclaw/.env: TELEGRAM_BOT_TOKEN, TELEGRAM_GROUP_ID, INLINECODE31 - Run the bridge in tmux: INLINECODE32
CODEBLOCK11
See references/telegram-bridge.md for the complete production-ready implementation with message parsing, offset tracking, and error handling.
Agent Configurations
See examples/ for full configs:
Claude ↔ Claude (Jim + Clawdy)
- - Agent A:
claude code (interactive, full tool access) - Agent B:
claude --print claude-haiku-4-5-20251001 (fast, scripting-optimized) - Best for: heavy task parallelism, one agent researches while other implements
Claude ↔ GPT
- - Agent A: Claude Code (full tool use)
- Agent B:
openai api chat.completions.create via script - Best for: cross-model verification, Claude builds → GPT reviews
GPT ↔ GPT
- - Agent A: GPT-4o via
openai CLI - Agent B: GPT-4o-mini for fast background checks
- Best for: speed + cost optimization when all context is OpenAI
Task Handoff Pattern
When Agent A needs Agent B to own a task completely:
CODEBLOCK12
Agent A does not check on progress — waits for DONE or BLOCKED.
Approval Pattern
When Agent B's daemon needs user approval for a terminal command:
CODEBLOCK13
INLINECODE39 sends keystrokes to the tmux session running Agent B:
SESSION=$(tmux ls | grep agentb-session | head -1 | cut -d: -f1)
tmux send-keys -t "$SESSION" "$1" Enter
Rate Limiting
Agent B should implement a rate governor to prevent runaway API calls:
CODEBLOCK15
Financial Gate Protocol (TIERED — updated 2026-02-22)
Three tiers based on amount:
| Tier | Amount | Rule |
|---|
| 1 | Under $20 | Either agent acts independently — no approval needed |
| 2 |
$20–$50 | Both agents PROPOSE + APPROVE before acting |
| 3 | Over $50 | Requires
[AUTHORIZED:financial:amount:timestamp:Jeremy] tag |
CODEBLOCK16
Daemon tiered gate logic:
CODEBLOCK17
If Tier 3 blocked: log [BLOCKED:financial-gate:tier3], send Telegram alert, do NOT execute.
Daemon Watchdog Pattern
Check daemon health every 15 minutes. If silent >15 min, restart:
CODEBLOCK18
Task Takeover Threshold
If Agent A (Jim) blocks on the same task twice, Agent B (Clawdy) takes it over:
CODEBLOCK19
Rule: Blocked 2x on same task → take it over, don't reassign.
Daemon Error Diagnosis
When daemon produces blank/error responses:
- 1. Check
/tmp/clawdy_last_err — contains last stderr from INLINECODE43 - Check ANSI-stripped chat.log for
CLAWDY_ERR: lines - Verify RESPONSE trimming line not corrupted (common issue: accidentally set to
RESPONSE="") - Restart daemon: INLINECODE46
Auto-approve: Use Bash(*) in ~/.claude/settings.local.json. NEVER use tmux send-keys cron — it sends keypresses blindly to the wrong sessions.
ai-collab — 自主多智能体协作
两个OpenClaw智能体在共享任务上并行工作,通过结构化聊天日志和守护进程收件箱协议进行协调。
架构概览
┌─────────────────────────────────────────────────────────┐
│ 用户 (Jeremy) │
│ Telegram / 直接消息 │
└──────────────────────┬──────────────────────────────────┘
│
┌─────────────┴──────────────┐
▼ ▼
┌─────────────────┐ ┌──────────────────┐
│ 智能体 A │ │ 智能体 B │
│ (Jim / 主) │◄───────►│ (Clawdy / 守护) │
│ claude code │ │ claude --print │
│ 端口: main │ │ 收件箱: 文件系统 │
└────────┬────────┘ └────────┬─────────┘
│ │
└──────────┬────────────────┘
▼
┌──────────────────┐
│ chat.log │ ← 共享记录
│ collab/inbox/ │ ← A→B 消息
└──────────────────┘
智能体 A(主): 交互式Claude Code会话。处理浏览器、复杂任务、面向用户的响应。
智能体 B(守护): claude --print子进程。处理后台任务、监控、快速查询。由放入收件箱的消息触发。
配置
所有设置通过环境变量进行——无硬编码值:
bash
~/.openclaw/workspace/collab/.ai-collab.env
export AGENT
ANAME=Jim
export AGENT
BNAME=Clawdy
export AGENT
BMODEL=claude-haiku-4-5-20251001 # 任何兼容claude --print的模型
export AGENT
BSESSION=clawdy-session # tmux会话名称
export COLLAB_INBOX=$HOME/.openclaw/workspace/collab/inbox
export COLLAB_LOG=$HOME/.openclaw/workspace/collab/chat.log
AGENTBMODEL支持的模型:
- - claude-haiku-4-5-20251001 — 最快、最便宜(推荐用于守护进程)
- claude-sonnet-4-6 — 能力更强,成本更高
- 如果使用GPT守护变体,任何OpenAI模型(参见examples/claude-gpt.md)
快速设置
bash
1. 加载配置
source ~/.openclaw/workspace/collab/.ai-collab.env
2. 创建协作工作区
mkdir -p $COLLAB_INBOX
3. 启动智能体B守护进程(在tmux会话中)
tmux new-session -d -s $AGENT
BSESSION \
source ~/.openclaw/workspace/collab/.ai-collab.env && \
bash ~/.openclaw/workspace/skills/ai-collab/scripts/daemon.sh
4. 启动消息轮询(智能体B → 智能体A路由,通过cron每60秒运行一次)
bash ~/.openclaw/workspace/skills/ai-collab/scripts/poll_chatlog.sh &
5. 测试连接
bash ~/.openclaw/workspace/skills/ai-collab/scripts/send.sh 来自智能体A的问候
通信协议
智能体之间的每条消息都遵循此格式。无开放循环。
| 标签 | 方向 | 含义 |
|---|
| [TASK:名称] | A→B 或 B→A | 分配任务 |
| [ACK:名称] |
接收方 | 已确认,开始工作 |
| [DONE:名称] | 执行方 | 任务完成 + 一行结果 |
| [BLOCKED:名称] | 执行方 | 无法完成 + 原因 |
| [HANDOFF:名称] | 任意一方 | 执行X,完成后回复[DONE:名称] |
| [STATUS:更新] | 任意一方 | 长时间运行任务的异步更新 |
| [QUESTION:主题] | 任意一方 | 需要信息才能继续 |
规则:
- 1. 先回答问题再提出新问题
- 先关闭任务再开始新任务
- 每条消息推动工作前进或关闭循环
- 禁止使用:让我知道、准备好了告诉我、待命
示例交换:
A → B: [TASK:价格检查] 从CoinGecko获取BTC价格
B → A: [ACK:价格检查] 正在检查。
B → A: [DONE:价格检查] BTC $94,230 截至UTC时间03:15
守护进程脚本(智能体B)
scripts/daemon.sh — 放入你的协作目录:
bash
#!/bin/bash
PIDFILE=/tmp/agentb_daemon.pid
if [ -f $PIDFILE ] && kill -0 $(cat $PIDFILE) 2>/dev/null; then
echo 守护进程已在运行(PID $(cat $PIDFILE))。退出。 >&2
exit 1
fi
echo $$ > $PIDFILE
trap rm -f $PIDFILE EXIT
必需:取消设置以使嵌套的claude --print正常工作
unset CLAUDECODE
INBOX=$HOME/.openclaw/workspace/collab/inbox
LOG=$HOME/.openclaw/workspace/collab/chat.log
mkdir -p $INBOX
logline() {
printf %s %s\n $(date +%Y-%m-%d %H:%M:%S) $1 >> $LOG
}
logline SYSTEM: 智能体B守护进程已启动
inotifywait -m -e moved_to $INBOX 2>/dev/null | while read dir event file; do
FULLPATH=$INBOX/$file
[ ! -f $FULLPATH ] && continue
MSG=$(cat $FULLPATH)
rm $FULLPATH
logline A -> B: $MSG
# 运行智能体B(claude --print)
RESPONSE=$(claude --print --model claude-haiku-4-5-20251001 \
你是智能体B。智能体A说:$MSG
在100字以内回复。使用[DONE:任务名称]或[BLOCKED:任务名称]关闭循环。
上下文:共享协作系统。聊天日志:$LOG 2>/dev/null)
logline B -> A: $RESPONSE
# 将响应路由回智能体A
openclaw agent --agent main -m [AgentB]: $RESPONSE --json > /dev/null 2>&1
done
发送消息(A → B)
bash
从智能体A发送到智能体B守护进程收件箱
bash ~/.openclaw/workspace/skills/ai-collab/scripts/send.sh 你的消息
原子写入模式(防止部分读取):
bash
INBOX=$HOME/.openclaw/workspace/collab/inbox
TMPFILE=$(mktemp $INBOX/.msg.XXXXXX)
echo $* > $TMPFILE
mv $TMPFILE $INBOX/msg_$(date +%s%N).txt
始终使用mktemp + mv——切勿直接写入收件箱。inotifywait在movedto时触发,而非closewrite。
聊天日志轮询(B → A)
scripts/poll_chatlog.sh — 通过cron每60秒运行一次:
bash
#!/bin/bash
LOG=$HOME/.openclaw/workspace/collab/chat.log
PTRFILE=/tmp/chatlogptr
[ ! -f $LOG ] && exit 0
TOTAL=$(wc -l < $LOG)
LAST=$(cat $PTR_FILE 2>/dev/null || echo 0)
[ $TOTAL -le $LAST ] && echo $TOTAL > $PTR_FILE && exit 0
NEW=$(tail -n +$((LAST + 1)) $LOG | grep B -> A: | sed s/.*B -> A: //)
echo $TOTAL > $PTR_FILE
[ -z $NEW ] && exit 0
while IFS= read -r line; do
[ -z $line ] && continue
openclaw agent --agent main -m [AgentB]: $line --json > /dev/null 2>&1
done <<< $