- 单个智能体拥有太多工具,难以做出关于使用哪个工具的明智决策。
- 上下文或记忆量过大,单个智能体难以有效追踪。
- 任务需要专业化(例如,规划者、研究人员、数学专家)。
多智能体模式
Tutorial: Build a supervisor agent
学习如何使用监督者模式构建个人助理,其中中央监督智能体协调专门的工人智能体。
本教程演示了以下内容:
- 为不同的领域(日历和电子邮件)创建专门的子智能体
- 将子智能体包装为集中编排的工具
- 为敏感操作添加人工审核
选择模式
| 问题 | 工具调用 | 交接 |
|---|---|---|
| 需要集中控制工作流程吗? | ✅ 是 | ❌ 否 |
| 想要智能体直接与用户交互吗? | ❌ 否 | ✅ 是 |
| 专家之间复杂、类似人类的对话? | ❌ 有限 | ✅ 强 |
自定义智能体上下文
多智能体设计的核心是上下文工程——决定每个智能体能看到哪些信息。LangChain让您能够精细控制:- 每个智能体接收到的对话或状态的哪些部分。
- 为子智能体定制的专业提示。
- 中间推理的包含/排除。
- 根据每个智能体自定义输入/输出格式。
工具调用
在工具调用中,一个智能体(即“控制器”)将其他智能体视为在需要时调用的工具。控制器负责协调,而工具智能体执行特定任务并返回结果。 流程:- 控制器接收输入并决定调用哪个工具(子智能体)。
- 工具智能体根据控制器的指令执行其任务。
- 工具智能体将结果返回给控制器。
- 控制器决定下一步操作或结束。
Implementation
Below is a minimal example where the main agent is given access to a single subagent via a tool definition: CODE_BLOCK_1 In this pattern:- The main agent invokes
调用子智能体1when it decides the task matches the subagent’s description. - The subagent runs independently and returns its result.
- The main agent receives the result and continues orchestration.
Where to customize
There are several points where you can control how context is passed between the main agent and its subagents:- Subagent name (
"subagent1_name"): This is how the main agent refers to the subagent. Since it influences prompting, choose it carefully. - Subagent description (
"subagent1_description"): This is what the main agent “knows” about the subagent. It directly shapes how the main agent decides when to call it. - Input to the subagent: You can customize this input to better shape how the subagent interprets tasks. In the example above, we pass the agent-generated
查询directly. - Output from the subagent: This is the response passed back to the main agent. You can adjust what is returned to control how the main agent interprets results. In the example above, we return the final message text, but you could return additional state or metadata.
Control the input to the subagent
There are two main levers to control the input that the main agent passes to a subagent:- Modify the prompt – Adjust the main agent’s prompt or the tool metadata (i.e., sub-agent’s name and description) to better guide when and how it calls the subagent.
- Context injection – Add input that isn’t practical to capture in a static prompt (e.g., full message history, prior results, task metadata) by adjusting the tool call to pull from the agent’s state.
Control the output from the subagent
Two common strategies for shaping what the main agent receives back from a subagent:- Modify the prompt – Refine the subagent’s prompt to specify exactly what should be returned.
- Useful when outputs are incomplete, too verbose, or missing key details.
- A common failure mode is that the subagent performs tool calls or reasoning but does not include the results in its final message. Remind it that the controller (and user) only see the final output, so all relevant info must be included there.
- Custom output formatting – adjust or enrich the subagent’s response in code before handing it back to the main agent.
- Example: pass specific state keys back to the main agent in addition to the final text.
- This requires wrapping the result in a `命令(或等效结构),以便您可以将自定义状态与子智能体的响应合并。
交接
在交接过程中,智能体可以直接将控制权相互传递。活动的智能体会发生变化,用户将与当前拥有控制权的智能体进行交互。 流程:- 当前智能体决定需要从另一个智能体那里获得帮助。
- 它将控制权(以及状态)传递给下一个智能体。
- 新的智能体直接与用户交互,直到它决定再次转交或完成。