跳到主要内容

委托与并行工作

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 完成机械式数据收集,然后将推理密集型分析任务委托出去:

# 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 以低成本处理 10+ 个连续的工具调用,然后由一个子代理在干净的上下文中完成单一高成本的推理任务。


工具集选择

根据子代理的需求选择合适的工具集:

任务类型工具集原因
网络研究["web"]仅使用 web_search + web_extract
代码工作["terminal", "file"]提供 shell 访问 + 文件操作
全栈任务["terminal", "file", "web"]除消息通信外,具备全部能力
只读分析["file"]仅能读取文件,无法执行 shell 命令

限制工具集可使子代理保持专注,并防止意外副作用(例如研究子代理意外运行 shell 命令)。


约束条件

  • 最多 3 个并行任务 —— 批处理最多支持 3 个并发子代理
  • 不允许嵌套 —— 子代理无法调用 delegate_taskclarifymemorysend_messageexecute_code
  • 独立终端 —— 每个子代理拥有独立的终端会话,工作目录和状态相互隔离
  • 无对话历史 —— 子代理仅能看到你传入的 goalcontext
  • 默认 50 次迭代 —— 对简单任务可将 max_iterations 设置得更低以节省成本

使用技巧

在目标中尽量具体。 “修复 bug” 太模糊。应明确为“修复 api/handlers.py 第 47 行的 TypeError,该错误发生在 process_request() 从 parse_body() 接收 None 时”,这样子代理才能获得足够的信息开展工作。

包含文件路径。 子代理不了解你的项目结构。请始终提供相关文件的绝对路径、项目根目录以及测试命令。

利用委托实现上下文隔离。 有时你希望获得全新视角。委托强制你清晰地表达问题,而子代理则不会受到你对话中积累的假设影响。

检查结果。 子代理的摘要仅是摘要。如果子代理说“已修复 bug 且测试通过”,请自行运行测试或查看 diff 以验证。


如需完整的委托参考——包含所有参数、ACP 集成及高级配置,请参阅 子代理委托