委托与并行工作
Hermes 可以生成隔离的子代理来并行处理任务。每个子代理获得自己的对话、终端会话和工具集。只有最终摘要返回——中间的工具调用永远不会进入你的上下文窗口。
有关完整功能参考,请参阅 子代理委托。
何时委托
适合委托的好候选:
- 推理重的子任务(调试、代码审查、研究综合)
- 会用中间数据淹没你的上下文的任务
- 并行独立的工作流(同时研究 A 和 B)
- 需要新上下文的任务,你希望代理不带偏见地处理
使用其他方式:
- 单个工具调用 → 直接使用工具
- 带步骤间逻辑的机械多步工作 →
execute_code - 需要用户交互的任务 → 子代理不能使用
clarify - 快速文件编辑 → 直接做
模式:并行研究
同时研究三个主题并获得结构化摘要:
Research these three topics in parallel:
1. Current state of WebAssembly outside the browser
2. RISC-V server chip adoption in 2025
3. Practical quantum computing applications
Focus on recent developments and key players.
幕后,Hermes 使用:
delegate_task(tasks=[
{
"goal": "Research WebAssembly outside the browser in 2025",
"context": "Focus on: runtimes (Wasmtime, Wasmer), cloud/edge use cases, WASI progress",
"toolsets": ["web"]
},
{
"goal": "Research RISC-V server chip adoption",
"context": "Focus on: server chips shipping, cloud providers adopting, software ecosystem",
"toolsets": ["web"]
},
{
"goal": "Research practical quantum computing applications",
"context": "Focus on: error correction breakthroughs, real-world use cases, key companies",
"toolsets": ["web"]
}
])
所有三个同时运行。每个子代理独立搜索网络并返回摘要。然后父代理将它们综合成一份连贯的简报。
模式:代码审查
委托一个安全审查给全新上下文子代理,该代理不带预设立场地处理代码:
Review the authentication module at src/auth/ for security issues.
Check for SQL injection, JWT validation problems, password handling,
and session management. Fix anything you find and run the tests.
关键是 context 字段——它必须包括子代理需要的一切:
delegate_task(
goal="Review src/auth/ for security issues and fix any found",
context="""Project at /home/user/webapp. Python 3.11, Flask, PyJWT, bcrypt.
Auth files: src/auth/login.py, src/auth/jwt.py, src/auth/middleware.py
Test command: pytest tests/auth/ -v
Focus on: SQL injection, JWT validation, password hashing, session management.
Fix issues found and verify tests pass.""",
toolsets=["terminal", "file"]
)
子代理绝对不知道你的对话。他们完全新鲜地开始。如果你委托"修复我们正在讨论的 bug",子代理不知道你指的是哪个 bug。始终显式传递文件路径、错误消息、项目结构和约束。
模式:比较替代方案
并行评估解决同一问题的三种方法,然后选择最好的:
I need to add full-text search to our Django app. Evaluate three approaches
in parallel:
1. PostgreSQL tsvector (built-in)
2. Elasticsearch via django-elasticsearch-dsl
3. Meilisearch via meilisearch-python
For each: setup complexity, query capabilities, resource requirements,
and maintenance overhead. Compare them and recommend one.
每个子代理独立研究一个选项。因为它们是隔离的,没有交叉污染——每个评估都基于自身优点。父代理获得所有三个摘要并进行对比。
模式:多文件重构
将大型重构任务拆分到并行子代理,每个处理代码库的不同部分:
delegate_task(tasks=[
{
"goal": "Refactor all API endpoint handlers to use the new response format",
"context": """Project at /home/user/api-server.
Files: src/handlers/users.py, src/handlers/auth.py, src/handlers/billing.py
Old format: return {"data": result, "status": "ok"}
New format: return APIResponse(data=result, status=200).to_dict()
Import: from src.responses import APIResponse
Run tests after: pytest tests/handlers/ -v""",
"toolsets": ["terminal", "file"]
},
{
"goal": "Update all client SDK methods to handle the new response format",
"context": """Project at /home/user/api-server.
Files: sdk/python/client.py, sdk/python/models.py
Old parsing: result = response.json()["data"]
New parsing: result = response.json()["data"] (same key, but add status code checking)
Also update sdk/python/tests/test_client.py""",
"toolsets": ["terminal", "file"]
},
{
"goal": "Update API documentation to reflect the new response format",
"context": """Project at /home/user/api-server.
Docs at: docs/api/. Format: Markdown with code examples.
Update all response examples from old format to new format.
Add a 'Response Format' section to docs/api/overview.md explaining the schema.""",
"toolsets": ["terminal", "file"]
}
])
每个子代理获得自己的终端会话。只要它们编辑不同的文件,它们可以在同一项目目录上工作而不会相互干扰。如果两个子代理可能接触同一文件,在你完成并行工作后自己处理该文件。
模式:先收集再分析
使用 execute_code 进行机械数据收集,然后委托推理重的分析:
# 步骤 1:机械收集(execute_code 在这里更好——不需要推理)
execute_code("""
from hermes_tools import web_search, web_extract
results = []
for query in ["AI funding Q1 2026", "AI startup acquisitions 2026", "AI IPOs 2026"]:
r = web_search(query, limit=5)
for item in r["data"]["web"]:
results.append({"title": item["title"], "url": item["url"], "desc": item["description"]})
# 从前 5 个最相关的提取完整内容
urls = [r["url"] for r in results[:5]]
content = web_extract(urls)
# 保存用于分析步骤
import json
with open("/tmp/ai-funding-data.json", "w") as f:
json.dump({"search_results": results, "extracted": content["results"]}, f)
print(f"Collected {len(results)} results, extracted {len(content['results'])} pages")
""")
# 步骤 2:推理重的分析(委托在这里更好)
delegate_task(
goal="Analyze AI funding data and write a market report",
context="""Raw data at /tmp/ai-funding-data.json contains search results and
extracted web pages about AI funding, acquisitions, and IPOs in Q1 2026.
Write a structured market report: key deals, trends, notable players,
and outlook. Focus on deals over $100M.""",
toolsets=["terminal", "file"]
)
这通常是最有效的模式:execute_code 处理 10+ 顺序工具调用(便宜),然后子代理用干净的上下文做一个昂贵的推理任务。
工具集选择
根据子代理需要什么选择工具集:
| 任务类型 | 工具集 | 为什么 |
|---|---|---|
| 网络研究 | ["web"] | 仅 web_search + web_extract |
| 代码工作 | ["terminal", "file"] | Shell 访问 + 文件操作 |
| 全栈 | ["terminal", "file", "web"] | 除消息外的所有 |
| 只读分析 | ["file"] | 只能读取文件,无 shell |
限制工具集可以保持子代理专注并防止意外副作用(如研究子代理运行 shell 命令)。
约束
- 默认 3 个并行任务 — 批次默认为 3 个并发子代理(可通过
delegation.max_concurrent_children在 config.yaml 中配置) - 无嵌套 — 子代理不能调用
delegate_task、clarify、memory、send_message或execute_code - 独立终端 — 每个子代理获得自己的终端会话,具有独立的工作目录和状态
- 无对话历史 — 子代理只看到你放在
goal和context中的内容 - 默认 50 次迭代 — 为简单任务设置较低的
max_iterations以节省成本
技巧
在目标中具体化。 "修复 bug" 太模糊。"修复 api/handlers.py 第 47 行的 TypeError,其中 process_request() 从 parse_body() 接收到 None" 给子代理足够的工作内容。
包含文件路径。 子代理不知道你的项目结构。始终包含相关文件的绝对路径、项目根目录和测试命令。
为上下文隔离使用委托。 有时你想要一个新视角。委托迫使你清楚地表达问题,子代理以你的对话中积累的假设来处理它。
检查结果。 子代理摘要只是摘要。如果子代理说"修复了 bug,测试通过",通过运行测试或阅读 diff 自己验证。
有关完整的委托参考——所有参数、ACP 集成和高级配置——请参阅 子代理委托。