如果你觉得 4000 行代码还是太多,TinyClaw 会让你震惊:400 行 Shell 脚本,实现多代理协作。
TinyAGI 组织做的这个项目,用文件队列 + tmux + 现有 CLI 工具(Claude Code / Codex),搭建了一个多代理团队系统。每个 agent 有独立的 workspace,通过文件传递消息,实时监控,24/7 运行。
GitHub 上 2.3k 星标,社区里有人说"400 行重建 OpenClaw""多代理神器"。
为什么需要多代理
单个 AI Agent 的问题:能力有限。
一个 agent 要同时做研究、写代码、测试、部署,很容易出错。就像一个人既当产品经理、又当开发、又当测试,效率低、质量差。
多代理协作的思路:分工合作。
- 研究员 agent:负责调研、收集资料
- 编码员 agent:负责写代码
- 测试员 agent:负责测试、找 bug
- 部署员 agent:负责部署、监控
每个 agent 专注自己的领域,通过消息传递协作。
TinyClaw 的设计哲学
TinyClaw 的核心思想:不重新发明轮子。
已经有很多优秀的 AI CLI 工具(Claude Code、Codex、Aider),为什么要重写?直接用它们,只需要一个协调层。
这个协调层只做三件事:
1. 消息路由:把任务分配给对应的 agent
2. 状态管理:跟踪每个 agent 的状态
3. 结果聚合:收集各 agent 的输出
用 Shell 脚本实现,400 行搞定。
架构设计
TinyClaw 的架构极简:
~/.tinyclaw/
├── queue/ # 消息队列(文件)
│ ├── researcher.in # 研究员的输入队列
│ ├── coder.in # 编码员的输入队列
│ └── tester.in # 测试员的输入队列
├── workspaces/ # 每个 agent 的工作空间
│ ├── researcher/
│ ├── coder/
│ └── tester/
├── logs/ # 日志
└── config.json # 配置
消息队列
TinyClaw 用文件队列传递消息:
# tinyclaw/queue.sh (简化版)
send_message() {
local agent=$1
local message=$2
local queue_file="$HOME/.tinyclaw/queue/${agent}.in"
# 追加消息到队列文件
echo "$message" >> "$queue_file"
}
receive_message() {
local agent=$1
local queue_file="$HOME/.tinyclaw/queue/${agent}.in"
# 读取第一条消息
if [ -f "$queue_file" ] && [ -s "$queue_file" ]; then
# 读取第一行
local message=$(head -n 1 "$queue_file")
# 删除第一行(已处理)
sed -i '1d' "$queue_file"
echo "$message"
fi
}
为什么用文件队列?
- 简单:不需要 Redis、RabbitMQ
- 可靠:文件系统保证持久化
- 可视化:直接 cat 就能看到队列内容
Agent 处理器
每个 agent 是一个独立的进程,循环处理队列消息:
# tinyclaw/processor.sh (简化版)
process_agent() {
local agent=$1
local workspace="$HOME/.tinyclaw/workspaces/$agent"
local queue_file="$HOME/.tinyclaw/queue/${agent}.in"
while true; do
# 检查队列
if [ -f "$queue_file" ] && [ -s "$queue_file" ]; then
# 读取消息
message=$(head -n 1 "$queue_file")
sed -i '1d' "$queue_file"
# 切换到 workspace
cd "$workspace"
# 调用 Claude Code 处理
response=$(echo "$message" | claude-code --non-interactive)
# 记录日志
echo "[$(date)] $agent: $message -> $response" >> "$HOME/.tinyclaw/logs/$agent.log"
# 如果需要,发送给下一个 agent
if [[ "$response" == *"@next:"* ]]; then
next_agent=$(echo "$response" | grep -oP '@next:\K\w+')
next_message=$(echo "$response" | sed 's/@next:\w+//')
send_message "$next_agent" "$next_message"
fi
fi
sleep 1
done
}
Tmux 管理
TinyClaw 用 tmux 管理多个 agent 进程:
# tinyclaw/start.sh (简化版)
start_team() {
local agents=("researcher" "coder" "tester")
# 创建 tmux session
tmux new-session -d -s tinyclaw
# 为每个 agent 创建窗口
for agent in "${agents[@]}"; do
tmux new-window -t tinyclaw -n "$agent"
tmux send-keys -t "tinyclaw:$agent" "process_agent $agent" C-m
done
# 创建监控窗口
tmux new-window -t tinyclaw -n "monitor"
tmux send-keys -t "tinyclaw:monitor" "watch -n 1 'tail -n 20 ~/.tinyclaw/logs/*.log'" C-m
# 附加到 session
tmux attach -t tinyclaw
}
用 tmux 的好处:
- 后台运行:关闭终端后继续运行
- 实时监控:随时切换到任意 agent 查看状态
- 易管理:一个命令启动/停止所有 agent
实战:搭建一个三人团队
假设你要做一个项目:写一个 Web 爬虫。
用 TinyClaw 搭建三人团队:
1. 定义 agent 角色:
// ~/.tinyclaw/config.json
{
"agents": [
{
"name": "researcher",
"role": "Research web scraping best practices and libraries",
"workspace": "~/.tinyclaw/workspaces/researcher",
"llm": "claude-sonnet-4"
},
{
"name": "coder",
"role": "Write Python code for web scraping",
"workspace": "~/.tinyclaw/workspaces/coder",
"llm": "claude-sonnet-4"
},
{
"name": "tester",
"role": "Test the code and report bugs",
"workspace": "~/.tinyclaw/workspaces/tester",
"llm": "claude-sonnet-4"
}
],
"workflow": [
{"from": "researcher", "to": "coder"},
{"from": "coder", "to": "tester"},
{"from": "tester", "to": "coder"} // 如果有 bug,返回给编码员
]
}
2. 启动团队:
# 初始化
tinyclaw init
# 启动所有 agent
tinyclaw start
# 发送初始任务
tinyclaw send researcher "Research how to scrape https://news.ycombinator.com"
3. 工作流程:
User: "Research how to scrape https://news.ycombinator.com"
↓
Researcher Agent:
- 调研 HN 的 HTML 结构
- 推荐用 BeautifulSoup
- 输出:@next:coder Use BeautifulSoup to scrape HN front page, extract titles and URLs
↓
Coder Agent:
- 写 Python 代码
- 输出:@next:tester Test scraper.py on https://news.ycombinator.com
↓
Tester Agent:
- 运行代码
- 发现 bug:没有处理分页
- 输出:@next:coder Bug: scraper only gets first page, need pagination
↓
Coder Agent:
- 修复 bug
- 输出:@next:tester Test updated scraper.py
↓
Tester Agent:
- 测试通过
- 输出:All tests passed!
4. 监控:
# 查看所有 agent 的日志
tmux attach -t tinyclaw
# 按 Ctrl+B, W 切换窗口
# 或者直接看日志文件
tail -f ~/.tinyclaw/logs/*.log
核心代码解析
TinyClaw 的核心只有 400 行,主要是这几个文件:
1. queue.sh - 消息队列
#!/bin/bash
QUEUE_DIR="$HOME/.tinyclaw/queue"
send() {
local agent=$1
local msg=$2
echo "$(date +%s)|$msg" >> "$QUEUE_DIR/${agent}.in"
}
receive() {
local agent=$1
local file="$QUEUE_DIR/${agent}.in"
if [ -s "$file" ]; then
local line=$(head -n 1 "$file")
sed -i '1d' "$file"
echo "$line" | cut -d'|' -f2-
fi
}
2. processor.sh - Agent 处理器
#!/bin/bash
process() {
local agent=$1
local workspace="$HOME/.tinyclaw/workspaces/$agent"
local context_file="$workspace/CONTEXT.md"
while true; do
msg=$(receive "$agent")
if [ -n "$msg" ]; then
# 更新上下文
echo "## $(date)" >> "$context_file"
echo "$msg" >> "$context_file"
# 调用 Claude Code
cd "$workspace"
response=$(echo "$msg" | claude-code --context "$context_file")
# 记录响应
echo "$response" >> "$context_file"
# 路由到下一个 agent
if [[ "$response" =~ @next:([a-z]+) ]]; then
next="${BASH_REMATCH[1]}"
send "$next" "$response"
fi
fi
sleep 1
done
}
3. monitor.sh - 实时监控
#!/bin/bash
monitor() {
while true; do
clear
echo "=== TinyClaw Team Status ==="
echo ""
for agent in researcher coder tester; do
queue_size=$(wc -l < "$HOME/.tinyclaw/queue/${agent}.in" 2>/dev/null || echo 0)
last_log=$(tail -n 1 "$HOME/.tinyclaw/logs/${agent}.log" 2>/dev/null || echo "No activity")
echo "[$agent]"
echo " Queue: $queue_size messages"
echo " Last: $last_log"
echo ""
done
sleep 2
done
}
4. main.sh - 主入口
#!/bin/bash
case "$1" in
init)
mkdir -p ~/.tinyclaw/{queue,workspaces,logs}
;;
start)
tmux new-session -d -s tinyclaw
for agent in researcher coder tester; do
tmux new-window -t tinyclaw -n "$agent"
tmux send-keys -t "tinyclaw:$agent" "bash processor.sh $agent" C-m
done
tmux new-window -t tinyclaw -n "monitor"
tmux send-keys -t "tinyclaw:monitor" "bash monitor.sh" C-m
tmux attach -t tinyclaw
;;
send)
send "$2" "$3"
;;
stop)
tmux kill-session -t tinyclaw
;;
esac
就这么简单,400 行搞定。
与 AutoGPT/MetaGPT 的对比
维度
TinyClaw
AutoGPT
MetaGPT
代码量
400 行
数万行
数万行
语言
Shell
Python
Python
依赖
现有 CLI 工具
自己实现
自己实现
部署
一个脚本
pip install + 配置
pip install + 配置
扩展性
高(加新 agent 只需改配置)
中
中
学习曲线
低(Shell 脚本)
高
高
TinyClaw 的优势:极简。
不需要学习复杂的框架,只需要懂 Shell 脚本和文件操作。
适合谁
TinyClaw 适合:
- 想快速搭建多代理系统的人:400 行,一天就能看懂
- 已经在用 Claude Code/Codex 的人:直接复用现有工具
- 喜欢极简方案的人:不想学复杂框架
- 需要可视化监控的人:tmux 实时查看
不适合:
- 需要复杂协作逻辑的人:TinyClaw 只支持简单的链式/扇出
- 需要 Web UI 的人:只有命令行
- 不熟悉 Shell 的人:需要懂基本的 Shell 脚本
扩展:加入第四个 agent
假设你想加一个"部署员"agent,负责部署代码:
1. 更新配置:
// ~/.tinyclaw/config.json
{
"agents": [
// ... 原有的三个 agent
{
"name": "deployer",
"role": "Deploy code to production",
"workspace": "~/.tinyclaw/workspaces/deployer",
"llm": "claude-sonnet-4"
}
],
"workflow": [
// ... 原有的 workflow
{"from": "tester", "to": "deployer"} // 测试通过后部署
]
}
2. 创建 workspace:
mkdir -p ~/.tinyclaw/workspaces/deployer
3. 重启团队:
tinyclaw stop
tinyclaw start
就这么简单,不需要改代码。
实战案例:自动化博客发布
用 TinyClaw 搭建一个自动化博客发布系统:
团队配置:
{
"agents": [
{
"name": "writer",
"role": "Write blog posts based on topics"
},
{
"name": "editor",
"role": "Edit and improve blog posts"
},
{
"name": "publisher",
"role": "Publish to WordPress"
}
]
}
工作流程:
# 发送主题
tinyclaw send writer "Write a blog post about Rust vs Go for web development"
# Writer agent:
# - 调研 Rust 和 Go 的 web 框架
# - 写初稿
# - 输出:@next:editor [draft content]
# Editor agent:
# - 检查语法、逻辑
# - 优化结构
# - 输出:@next:publisher [edited content]
# Publisher agent:
# - 生成封面图
# - 上传到 WordPress
# - 输出:Published at https://blog.com/rust-vs-go
全自动,不需要人工干预。
总结
TinyClaw 证明了:多代理协作,400 行 Shell 脚本就够了。
它不重新发明轮子,直接用现有的 AI CLI 工具(Claude Code、Codex),只做一个轻量级的协调层。用文件队列传递消息,用 tmux 管理进程,用 Shell 脚本实现逻辑。
对于想快速搭建多代理系统、喜欢极简方案、已经在用 Claude Code 的人来说,这是最好的选择。
相关链接:
- TinyClaw GitHub
- ← 上一篇:ZeroClaw vs IronClaw - Rust 重写的两种哲学
- ← 返回系列总览
- 下一篇:MimiClaw - 在 $5 ESP32 芯片上跑 AI Agent →