Swarm
Swarm — 将您的LLM成本降低200倍
将您昂贵的大模型转变为负担得起的日常工具。将那些枯燥的任务——并行处理、批量作业、研究分析——卸载给Gemini Flash工作节点,只需极低的成本。
概览
| 通过30项任务 | 耗时 | 成本 |
|---|---|---|
| Opus(顺序处理) | 约30秒 | 约0.50美元 |
| Swarm(并行处理) | 约1秒 | 约0.003美元 |
适用场景
Swarm非常适合:
- 3个以上独立任务(研究、摘要、比较)
- 比较或研究多个主题
- 多个URL需要抓取/分析
- 批量处理(文档、实体、事实)
- 复杂分析需要多角度分析 → 使用链式处理
快速参考
# Check daemon (do this every session)
swarm status
# Start if not running
swarm start
# Parallel prompts
swarm parallel "What is X?" "What is Y?" "What is Z?"
# Research multiple subjects
swarm research "OpenAI" "Anthropic" "Mistral" --topic "AI safety"
# Discover capabilities
swarm capabilities
执行模式
并行模式 (v1.0)
N个提示 → N个工作者同时处理。最适合独立任务。
swarm parallel "prompt1" "prompt2" "prompt3"
研究模式 (v1.1)
多阶段:搜索 → 获取 → 分析。使用谷歌搜索作为基础。
swarm research "Buildertrend" "Jobber" --topic "pricing 2026"
链式模式 (v1.3) — 精炼流水线
数据流经多个阶段,每个阶段具有不同的视角/过滤器。阶段按顺序运行;阶段内的任务并行运行。
阶段模式:
并行— N个输入 → N个工作者(相同视角)单一— 合并输入 → 1个工作者扇出— 1个输入 → N个具有不同视角的工作者归约— N个输入 → 1个综合输出
自动链— 描述您的需求,获得最优流水线:
curl -X POST http://localhost:9999/chain/auto \
-d '{"task":"Find business opportunities","data":"...market data...","depth":"standard"}'
手动链:
swarm chain pipeline.json
# or
echo '{"stages":[...]}' | swarm chain --stdin
深度预设: 快速(2个阶段),标准(4个),深度(6个),详尽(8个)
内置视角:提取器、过滤器、丰富器、分析师、综合器、挑战者、优化器、策略师、研究员、评论家
预览而不执行:
curl -X POST http://localhost:9999/chain/preview \
-d '{"task":"...","depth":"standard"}'
基准测试(v1.3)
在同一任务上比较单一、并行和链式执行,并使用LLM作为评判者进行评分。
curl -X POST http://localhost:9999/benchmark \
-d '{"task":"Analyze X","data":"...","depth":"standard"}'
基于6个FLASK维度的评分:准确性(2倍权重)、深度(1.5倍)、完整性、连贯性、可操作性(1.5倍)、细致度。
能力发现(v1.3)
让编排器发现可用的执行模式:
swarm capabilities
# or
curl http://localhost:9999/capabilities
提示缓存(v1.3.2)
用于LLM响应的LRU缓存。缓存命中时速度提升212倍(并行),链式执行时提升514倍。
- 以指令、输入和视角的哈希值为键
- 最大500条记录,生存时间1小时
- 跳过网络搜索任务(需要最新数据)
- 守护进程重启后仍持久化保存至磁盘
- 单任务旁路设置:将
task.cache = false
# View cache stats
curl http://localhost:9999/cache
# Clear cache
curl -X DELETE http://localhost:9999/cache
缓存统计信息显示于swarm status中
阶段重试机制(v1.3.2)
若链式阶段中的任务失败,仅重试失败任务(而非整个阶段)。默认重试次数:1次。可通过phase.retries按阶段配置,或通过options.stageRetries全局配置
成本追踪功能(v1.3.1)
所有端点在其complete事件中返回成本数据:
session——当前守护进程会话总量daily——每日总量— 重启后持续存在,全天累积
swarm status # Shows session + daily cost
swarm savings # Monthly savings report
网络搜索 (v1.1)
工作人员通过谷歌搜索基础功能(仅限Gemini,无需额外费用)搜索实时网络。
# Research uses web search by default
swarm research "Subject" --topic "angle"
# Parallel with web search
curl -X POST http://localhost:9999/parallel \
-d '{"prompts":["Current price of X?"],"options":{"webSearch":true}}'
JavaScript API
const { parallel, research } = require('~/clawd/skills/node-scaling/lib');
const { SwarmClient } = require('~/clawd/skills/node-scaling/lib/client');
// Simple parallel
const result = await parallel(['prompt1', 'prompt2', 'prompt3']);
// Client with streaming
const client = new SwarmClient();
for await (const event of client.parallel(prompts)) { ... }
for await (const event of client.research(subjects, topic)) { ... }
// Chain
const result = await client.chainSync({ task, data, depth });
守护进程管理
swarm start # Start daemon (background)
swarm stop # Stop daemon
swarm status # Status, cost, cache stats
swarm restart # Restart daemon
swarm savings # Monthly savings report
swarm logs [N] # Last N lines of daemon log
性能 (v1.3.2)
| 模式 | 任务 | 时间 | 备注 |
|---|---|---|---|
| 并行(简单) | 5 | 约700毫秒 | 有效142毫秒/任务 |
| 并行(压力) | 10 | 约1.2秒 | 有效123毫秒/任务 |
| 链式(标准) | 5 | 约14秒 | 3阶段多视角 |
| 链式(快速) | 2 | 约3秒 | 两阶段提取+合成 |
| 缓存命中 | 任意 | 约3-5毫秒 | 200-500倍加速 |
| 研究(网络) | 2 | 约15秒 | 谷歌基础数据延迟 |
配置
位置:~/.config/clawdbot/node-scaling.yaml
node_scaling:
enabled: true
limits:
max_nodes: 16
max_concurrent_api: 16
provider:
name: gemini
model: gemini-2.0-flash
web_search:
enabled: true
parallel_default: false
cost:
max_daily_spend: 10.00
故障排除
| 问题 | 修复 |
|---|---|
| 守护进程未运行 | swarm start |
| 无API密钥 | 设置GEMINI_API_KEY或运行npm run setup |
| 速率受限 | 降低配置中的max_concurrent_api |
| 网络搜索无法使用 | 确保提供者为 gemini 并启用 web_search.enabled |
| 缓存了过时结果 | curl -X DELETE http://localhost:9999/cache |
| 链式处理速度过慢 | 使用深度: "快速"或检查上下文大小 |
结构化输出 (v1.3.7)
强制使用JSON输出并进行模式验证——在结构化任务上实现零解析失败。
# With built-in schema
curl -X POST http://localhost:9999/structured \
-d '{"prompt":"Extract entities from: Tim Cook announced iPhone 17","schema":"entities"}'
# With custom schema
curl -X POST http://localhost:9999/structured \
-d '{"prompt":"Classify this text","data":"...","schema":{"type":"object","properties":{"category":{"type":"string"}}}}'
# JSON mode (no schema, just force JSON)
curl -X POST http://localhost:9999/structured \
-d '{"prompt":"Return a JSON object with name, age, city for a fictional person"}'
# List available schemas
curl http://localhost:9999/structured/schemas
内置模式: 实体、摘要、比较、行动、分类、问答
使用 Gemini 原生的response_mime_type: application/json+responseSchema用于保证JSON输出。包含对响应的模式验证。
多数投票(v1.3.7)
相同提示 → N次并行执行 → 选择最佳答案。在事实性/分析性任务上准确性更高。
# Judge strategy (LLM picks best — most reliable)
curl -X POST http://localhost:9999/vote \
-d '{"prompt":"What are the key factors in SaaS pricing?","n":3,"strategy":"judge"}'
# Similarity strategy (consensus — zero extra cost)
curl -X POST http://localhost:9999/vote \
-d '{"prompt":"What year was Python released?","n":3,"strategy":"similarity"}'
# Longest strategy (heuristic — zero extra cost)
curl -X POST http://localhost:9999/vote \
-d '{"prompt":"Explain recursion","n":3,"strategy":"longest"}'
策略:
judge— LLM根据准确性/完整性/清晰度/可操作性对所有候选答案进行评分,选出优胜者(N+1次调用)similarity— 基于Jaccard词集相似度,选择共识答案(N次调用,零额外成本)longest— 选择最长的回复作为详尽性的启发式判断(N次调用,零额外成本)
使用场景:事实性问题、关键决策,或任何准确性重于速度的任务。
| 策略 | 调用次数 | 额外成本 | 质量 |
|---|---|---|---|
| similarity | N | $0 | 良好(共识) |
| 最长 | N | 0美元 | 尚可(启发式) |
| 判断 | N+1 | 约0.0001美元 | 最佳(LLM评分) |
自我反思(v1.3.5)
在链/骨架输出后可选的批判性检查环节。评估五个维度,若低于阈值则自动优化。
# Add reflect:true to any chain or skeleton request
curl -X POST http://localhost:9999/chain/auto \
-d '{"task":"Analyze the AI chip market","data":"...","reflect":true}'
curl -X POST http://localhost:9999/skeleton \
-d '{"task":"Write a market analysis","reflect":true}'
已证实:将弱输出从平均5.0分提升至7.6分。骨架+反思得分为9.4/10。
思维骨架法(v1.3.6)
生成大纲 → 并行扩展每个部分 → 合并成连贯文档。最适合长篇幅内容。
curl -X POST http://localhost:9999/skeleton \
-d '{"task":"Write a comprehensive guide to SaaS pricing","maxSections":6,"reflect":true}'
性能:21秒内生成14,478字符(675字符/秒)—— 比链式方法多出5.1倍内容,吞吐量高出2.9倍。
| 指标 | 链式 | 思维骨架法 | 优胜者 |
|---|---|---|---|
| 输出规模 | 2,856字符 | 14,478 字符 | SoT (5.1倍) |
| 吞吐量 | 234 字符/秒 | 675 字符/秒 | SoT (2.9倍) |
| 持续时间 | 12秒 | 21秒 | 链式 (更快) |
| 质量 (含反思) | ~7-8/10 | 9.4/10 | SoT |
何时使用何种方法:
- SoT→ 长文本内容、报告、指南、文档 (任何具有自然章节的内容)
- 链式→ 分析、研究、对抗性评审 (任何需要多视角的内容)
- 并行→ 独立任务、批量处理
- 结构化→ 实体抽取、分类、任何需要可靠JSON输出的任务
- 投票事实准确性、关键决策、建立共识
API 端点
| 方法 | 路径 | 描述 |
|---|---|---|
| GET | /health | 健康检查 |
| GET | /status | 详细状态 + 成本 + 缓存 |
| GET | /capabilities | 发现执行模式 |
| POST | /parallel | 并行执行 N 个提示 |
| POST | /research | 多阶段网络研究 |
| POST | /skeleton | 思维骨架(大纲 → 扩展 → 合并) |
| POST | /chain | 手动链式管道 |
| POST | /chain/auto | 自动构建并执行链 |
| POST | /chain/preview | 预览链而不执行 |
| POST | /chain/template | 执行预构建模板 |
| POST | /structured | 强制JSON输出并进行模式验证 |
| GET | /structured/schemas | 列出内置模式 |
| POST | /vote | 多数投票(N选最佳) |
| POST | /benchmark | 质量对比测试 |
| GET | /templates | 列出链式模板 |
| GET | /cache | 缓存统计 |
| DELETE | /cache | 清除缓存 |
成本对比
| 模型 | 每百万令牌成本 | 相对成本 |
|---|---|---|
| Claude Opus 4 | 约15美元输入 / 75美元输出 | 1倍 |
| GPT-4o | 约2.5美元输入 / 10美元输出 | 约便宜7倍 |
| Gemini Flash | 约0.075美元输入 / 0.30美元输出 | 便宜200倍 |
缓存命中基本上是免费的(约3-5毫秒,无需API调用)。


微信扫一扫,打赏作者吧~