字节笔记本
2026年5月16日
Hermes Agent 委托与并行:多 Agent 协作模式详解
Hermes Agent 支持生成隔离的子 Agent 来并行处理任务,每个子 Agent 拥有独立的对话、终端会话和工具集,只有最终摘要会返回给父 Agent,中间工具调用不会污染上下文窗口。本文将详细介绍 Hermes 的委托与并行工作模式,涵盖适用场景、常用模式和关键配置。
何时使用委托
适合委托的场景:
- 推理密集型子任务(调试、代码审查、研究综合)
- 会用中间数据淹没上下文的任务
- 并行独立工作流(同时研究 A 和 B)
- 需要无偏见视角的新上下文任务
不适合委托,应使用其他方式:
- 单次工具调用 -> 直接使用工具
- 步骤间有逻辑的机械多步操作 ->
execute_code - 需要用户交互的任务 -> 子 Agent 无法使用
clarify - 快速文件编辑 -> 直接操作
- 需要超过当前回合持续运行的长期工作 -> 使用
cronjob或terminal(background=True, notify_on_complete=True)。delegate_task是同步的:如果父回合被中断,活跃的子 Agent 会被取消,其工作将被丢弃。
模式:并行研究
同时研究三个主题并获取结构化摘要:
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"]
}
])三个任务并发运行,每个子 Agent 独立搜索网络并返回摘要,父 Agent 再将它们综合成一份连贯的简报。
模式:代码审查
将安全审查委托给一个拥有全新上下文的子 Agent,使其不带任何预设地审查代码:
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 字段——它必须包含子 Agent 所需的一切信息:
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"]
)上下文问题 子 Agent 对你的对话一无所知。它们从完全空白的状态开始。如果你委托"修复我们讨论过的那个 bug",子 Agent 完全不知道你说的是哪个 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.每个子 Agent 独立研究一个方案。由于彼此隔离,不存在交叉污染——每个评估都基于自身优势独立进行。父 Agent 获取三份摘要后做出比较。
模式:多文件重构
将大型重构任务拆分到并行子 Agent,每个负责代码库的不同部分:
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"]
}
])提示 每个子 Agent 拥有独立的终端会话。它们可以在同一个项目目录下工作而不会互相干扰——前提是编辑不同的文件。如果两个子 Agent 可能修改同一文件,请在并行工作完成后自行处理该文件。
模式:先采集后分析
使用 execute_code 进行机械式数据采集,然后将推理密集的分析工作委托给子 Agent:
# Step 1: Mechanical gathering (execute_code is better here — no reasoning needed)
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"]})
# Extract full content from top 5 most relevant
urls = [r["url"] for r in results[:5]]
content = web_extract(urls)
# Save for the analysis step
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")
""")
# Step 2: Reasoning-heavy analysis (delegation is better here)
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 以低成本处理十多次顺序工具调用,然后子 Agent 在干净的上下文中完成单一的昂贵推理任务。
工具集选择
根据子 Agent 的需求选择工具集:
| 任务类型 | 工具集 | 原因 |
|---|---|---|
| 网络研究 | ["web"] | 仅 web_search + web_extract |
| 代码工作 | ["terminal", "file"] | Shell 访问 + 文件操作 |
| 全栈 | ["terminal", "file", "web"] | 除消息外的所有工具 |
| 只读分析 | ["file"] | 只能读取文件,无 Shell |
限制工具集可以保持子 Agent 专注,防止意外副作用(例如研究子 Agent 运行 Shell 命令)。
约束与限制
- 默认 3 个并行任务:批次默认 3 个并发子 Agent(可通过 config.yaml 中的
delegation.max_concurrent_children配置,无硬性上限,最低为 1) - 嵌套委托需手动启用:叶子子 Agent(默认)无法调用
delegate_task、clarify、memory、send_message或execute_code。编排者子 Agent(role="orchestrator")保留delegate_task以支持进一步委托,但仅当delegation.max_spawn_depth提升到默认值 1 以上时生效(支持 1-3);其余四个仍被禁止。通过delegation.orchestrator_enabled: false可全局禁用。
并发与深度调优
| 配置项 | 默认值 | 范围 | 效果 |
|---|---|---|---|
max_concurrent_children | 3 | >=1 | 每次 delegate_task 调用的并行批次大小 |
max_spawn_depth | 1 | 1-3 | 允许多少层委托继续生成子 Agent |
示例:运行 30 个并行 Worker 并启用嵌套子 Agent:
delegation:
max_concurrent_children: 30
max_spawn_depth: 2- 独立终端——每个子 Agent 拥有自己的终端会话,具有独立的工作目录和状态
- 无对话历史——子 Agent 只能看到父 Agent 调用
delegate_task时传递的goal和context - 默认 50 次迭代——简单任务可设置更低的
max_iterations以节省成本 - 非持久化——
delegate_task是同步的,在父回合内运行。如果父 Agent 被中断(新用户消息、/stop、/new),所有活跃的子 Agent 会被取消(status="interrupted"),其工作将被丢弃。对于必须超过当前回合持续运行的工作,使用cronjob或terminal(background=True, notify_on_complete=True)。
实用技巧
目标要具体。 "修复这个 bug" 太模糊了。"修复 api/handlers.py 第 47 行的 TypeError,process_request() 从 parse_body() 接收了 None" 才能给子 Agent 足够的工作信息。
包含文件路径。 子 Agent 不知道你的项目结构。务必包含相关文件的绝对路径、项目根目录和测试命令。
善用委托实现上下文隔离。 有时你需要一个全新的视角。委托迫使你清晰地表述问题,子 Agent 不会带有对话中累积的假设。
检查结果。 子 Agent 的摘要终究只是摘要。如果子 Agent 说"修复了 bug 并且测试通过",请自行运行测试或查看 diff 来验证。
完整委托参考——包括所有参数、ACP 集成和高级配置——请参阅 Subagent Delegation。