跳到主要內容

委託與並行工作

Hermes 可以啟動獨立的子 Agent,以並行方式處理任務。每個子 Agent 都擁有自己的對話、終端會話和工具集。只有最終的總結會返回——中間的工具調用永遠不會進入你的上下文窗口。

有關完整功能參考,請參閱 子 Agent 委託


何時進行委託

適合委託的良好候選任務:

  • 需要大量推理的子任務(調試、代碼審查、研究綜合)
  • 會用中間數據填滿你上下文窗口的任務
  • 可並行獨立執行的工作流(同時進行研究 A 和研究 B)
  • 需要全新上下文的任務,希望 Agent 以無偏見的方式處理

應使用其他方式:

  • 單個工具調用 → 直接使用工具
  • 步驟間存在邏輯的機械式多步任務 → 使用 execute_code
  • 需要用戶交互的任務 → 子 Agent 無法使用 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"]
}
])

三個任務併發執行。每個子 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,每個 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 完成機械式數據收集,然後將推理密集型分析任務委託出去:

# 第 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+ 個連續的工具調用,然後由一個子 Agent 在乾淨的上下文中完成單一高成本的推理任務。


工具集選擇

根據子 Agent 的需求選擇合適的工具集:

任務類型工具集原因
網絡研究["web"]僅使用 web_search + web_extract
代碼工作["terminal", "file"]提供 shell 訪問 + 文件操作
全棧任務["terminal", "file", "web"]除消息通信外,具備全部能力
只讀分析["file"]僅能讀取文件,無法執行 shell 命令

限制工具集可使子 Agent 保持專注,並防止意外副作用(例如研究子 Agent 意外運行 shell 命令)。


約束條件

  • 最多 3 個並行任務 —— 批處理最多支持 3 個併發子 Agent
  • 不允許嵌套 —— 子 Agent 無法調用 delegate_taskclarifymemorysend_messageexecute_code
  • 獨立終端 —— 每個子 Agent 擁有獨立的終端會話,工作目錄和狀態相互隔離
  • 無對話歷史 —— 子 Agent 僅能看到你傳入的 goalcontext
  • 默認 50 次迭代 —— 對簡單任務可將 max_iterations 設置得更低以節省成本

使用技巧

在目標中儘量具體。 “修復 bug” 太模糊。應明確為“修復 api/handlers.py 第 47 行的 TypeError,該錯誤發生在 process_request() 從 parse_body() 接收 None 時”,這樣子 Agent 才能獲得足夠的信息開展工作。

包含文件路徑。 子 Agent 不瞭解你的項目結構。請始終提供相關文件的絕對路徑、項目根目錄以及測試命令。

利用委託實現上下文隔離。 有時你希望獲得全新視角。委託強制你清晰地表達問題,而子 Agent 則不會受到你對話中積累的假設影響。

檢查結果。 子 Agent 的摘要僅是摘要。如果子 Agent 說“已修復 bug 且測試通過”,請自行運行測試或查看 diff 以驗證。


如需完整的委託參考——包含所有參數、ACP 集成及高級配置,請參閱 子 Agent 委託