第 3 章 · 01 Director 编排 Agent 本节摘要:本节精读 AutoHedge 的「大脑」——Director Agent(Trading-Director)。它是整个多 Agent 系统的编排者:接收用户任务,自主决定何时把子任务通过 handoffs 交给哪个专家。本节逐行拆解 里 directoragent 的实例化(agentname、systemprompt、modelname、maxloops、handoffs),精读它的 systemprompt(DIRECTORPROMPT)如何定义「交易总监」的角色与四大目标,并分析为什么选 gpt-4.1、为什么 maxloops=1。
本节摘要:本节精读 AutoHedge 的「大脑」——Director Agent(Trading-Director)。它是整个多 Agent 系统的编排者:接收用户任务,自主决定何时把子任务通过 handoffs 交给哪个专家。本节逐行拆解
workers.py里 director_agent 的实例化(agent_name、system_prompt、model_name、max_loops、handoffs),精读它的 system_prompt(DIRECTOR_PROMPT)如何定义「交易总监」的角色与四大目标,并分析为什么选 gpt-4.1、为什么 max_loops=1。读完本节,你理解了 AutoHedge「决策权」的所在,以及 Orchestrator-Worker 模式在代码里长什么样。
内容来源:原项目源码
autohedge/workers.py、autohedge/prompts.py,精读并套用体系化模板。
阅读完本节,你应当能够:
workers.py 里 Director 的定义(节选关键部分):
from datetime import datetime from swarms import Agent from autohedge.prompts import DIRECTOR_PROMPT, EXECUTION_PROMPT, QUANT_PROMPT, RISK_PROMPT, SENTIMENT_PROMPT from autohedge.tools.exa_search_tool import exa_search _NOW = datetime.now() _DATE_TIME_LINE = _NOW.strftime("%A, %B %d, %Y at %H:%M") if _NOW.tzinfo: _DATE_TIME_LINE += f" {_NOW.tzinfo.tzname() or ''}" _SYSTEM_SUFFIX = f"\n\nCurrent date and time (use this as now): {_DATE_TIME_LINE.strip()}" # ... 四个专家 Agent 先定义(见下一节) ... ALL_AGENTS = [sentiment_agent, risk_agent, execution_agent, quant_agent] director_agent = Agent( agent_name="Trading-Director", system_prompt=DIRECTOR_PROMPT + _SYSTEM_SUFFIX, model_name="gpt-4.1", max_loops=1, handoffs=ALL_AGENTS, )
逐项解读:
| 参数 | 值 | 含义与意图 |
|---|---|---|
agent_name |
"Trading-Director" |
日志与交接时识别用;「交易总监」点明角色 |
system_prompt |
DIRECTOR_PROMPT + _SYSTEM_SUFFIX |
角色 + 目标 + 当前时间注入(第 4 章详讲) |
model_name |
"gpt-4.1" |
用较强的模型,因为要统筹全局、做交接判断 |
max_loops |
1 |
单轮推理,不自我迭代 |
handoffs |
ALL_AGENTS |
关键:可交接给全部四个专家 |
注意定义顺序:四个专家必须先于 Director 定义,这样 ALL_AGENTS 列表才存在,Director 才能引用它。这是 Python 模块级代码的自然约束。
Director 的「性格」全在 system_prompt 里。原文(prompts.py):
You are a Trading Director AI, responsible for orchestrating the trading process. Your primary objectives are: 1. Conduct in-depth market analysis to identify opportunities and challenges. 2. Develop comprehensive trading theses, encompassing both technical and fundamental aspects. 3. Collaborate with specialized agents to ensure a cohesive strategy. 4. Make informed, data-driven decisions on trade executions. For each stock under consideration, please provide the following: - A concise market thesis, outlining the overall market position and expected trends. - Key technical and fundamental factors influencing the stock's performance. - A detailed risk assessment, highlighting potential pitfalls and mitigation strategies. - Trade parameters, including entry and exit points, position sizing, and risk management guidelines.
翻译要点解析:
💡 核心心法:注意目标 3「Collaborate with specialized agents」——这句配合
handoffs=ALL_AGENTS,就是告诉 LLM「你有四个专家同事,该找人帮忙就 handoff」。system_prompt 与 handoffs 参数是配套的:prompt 给出协作意图,handoffs 给出协作能力。
Director 用 gpt-4.1,而情绪 Agent 用 gpt-4o-mini。这是有意的模型分层:
| Agent | 模型 | 理由 |
|---|---|---|
| Director | gpt-4.1 | 需统筹全局、判断何时交接、综合多专家输出——要强推理 |
| 情绪 Agent | gpt-4o-mini | 主要是调 exa_search 读新闻、打分——轻量任务,省钱 |
| 量化/风控/执行 | gpt-4.1 | 需要结构化数值推理与严谨输出 |
这是 LLM 应用的常见优化:贵的强模型用在需要推理的关键节点,便宜的快模型用在轻量任务。在多 Agent 系统里,这种分层能显著降本。
所有 Agent 都设 max_loops=1。这意味着:
好处:
代价:
max_loops>1,让 Agent 自我纠错。💡 取舍:教学项目选单轮是合理的简化。如果你要拿这套架构做生产,这是第一个该重新评估的点——关键决策 Agent(如风控)值得多轮。
Director 是编排者,不是执行者。它的职责:
这是经典的 Orchestrator-Worker 模式:一个协调者 + 多个专业工人。它的好处是职责清晰、专家可独立优化;风险是高度依赖编排者的判断力——Director 判断错交接对象,全盘出错。
⚠️ 现实澄清:Director 的交接决策完全依赖 LLM 对 system_prompt 的理解。AutoHedge 没有显式的交接规则代码(比如「if 有新闻则交情绪 Agent」),一切交给 LLM 自由判断。这意味着行为有一定随机性,难以严格审计——这在交易场景是隐患。第 10 章会详谈。
Director(gpt-4.1, max_loops=1, handoffs=ALL_AGENTS) ← 本节 │ handoffs ▼ [下一节] 四大专家 Agent
理解了 Director,下一节就看它 handoff 的对象——四个专家——各自长什么样。
Agent(agent_name="Trading-Director", system_prompt=DIRECTOR_PROMPT+时间后缀, model_name="gpt-4.1", max_loops=1, handoffs=ALL_AGENTS);专家须先定义,Director 才能引用 ALL_AGENTS。下一节,我们看 Director handoff 的四个对象——情绪、量化、风控、执行专家 Agent——各自的配置与产出约定。