Documentation Index
Fetch the complete documentation index at: https://nvd-54.mintlify.app/llms.txt
Use this file to discover all available pages before exploring further.
概述
监督者模式是一种多智能体架构,其中一个中央监督者智能体协调专业的工作者智能体。当任务需要不同类型的专业知识时,这种方法表现出色。与其构建一个跨领域管理工具选择的智能体,不如创建由理解整体工作流的监督者协调的专注型专家。 在本教程中,你将构建一个个人助手系统,通过一个现实的工作流来展示这些优势。系统将协调两个具有根本不同职责的专家:- 日历智能体,处理日程安排、可用性检查和事件管理。
- 邮件智能体,管理通信、撰写消息和发送通知。
为什么使用监督者?
多智能体架构允许你将工具分配给各个工作者,每个工作者都有自己的提示或指令。考虑一个直接访问所有日历和邮件 API 的智能体:它必须从许多类似的工具中选择、理解每个 API 的确切格式,并同时处理多个领域。如果性能下降,将相关工具和关联提示分离到逻辑组中可能会有帮助(部分原因是为了管理迭代改进)。概念
我们将涵盖以下概念:设置
安装
This tutorial requires thelangchain package:
LangSmith
Set up LangSmith to inspect what is happening inside your agent. Then set the following environment variables:组件
We will need to select a chat model from LangChain’s suite of integrations:- OpenAI
- Anthropic
- Azure
- Google Gemini
- Bedrock Converse
1. 定义工具
Start by defining the tools that require structured inputs. In real applications, these would call actual APIs (Google Calendar, SendGrid, etc.). For this tutorial, you’ll use stubs to demonstrate the pattern.2. 创建专业子智能体
Next, we’ll create specialized sub-agents that handle each domain.创建日历智能体
The calendar agent understands natural language scheduling requests and translates them into precise API calls. It handles date parsing, availability checking, and event creation.create_calendar_event, and returns a natural language confirmation.
创建邮件智能体
The email agent handles message composition and sending. It focuses on extracting recipient information, crafting appropriate subject lines and body text, and managing email communication.send_email, and returns a confirmation. Each sub-agent has a narrow focus with domain-specific tools and prompts, allowing it to excel at its specific task.
3. 将子智能体包装为工具
Now wrap each sub-agent as a tool that the supervisor can invoke. This is the key architectural step that creates the layered system. The supervisor will see high-level tools like “schedule_event”, not low-level tools like “create_calendar_event”.4. 创建监督者智能体
Now create the supervisor that orchestrates the sub-agents. The supervisor only sees high-level tools and makes routing decisions at the domain level, not the individual API level.5. 使用监督者
Now test your complete system with complex requests that require coordination across multiple domains:示例 1:简单的单领域请求
schedule_event, and the calendar agent handles date parsing and event creation.
示例 2:复杂的多领域请求
schedule_event for the meeting, then calls manage_email for the reminder. Each sub-agent completes its task, and the supervisor synthesizes both results into a coherent response.
完整工作示例
Here’s everything together in a runnable script:理解架构
Your system has three layers. The bottom layer contains rigid API tools that require exact formats. The middle layer contains sub-agents that accept natural language, translate it to structured API calls, and return natural language confirmations. The top layer contains the supervisor that routes to high-level capabilities and synthesizes results. This separation of concerns provides several benefits: each layer has a focused responsibility, you can add new domains without affecting existing ones, and you can test and iterate on each layer independently.6. 添加人机协作审查
It can be prudent to incorporate human-in-the-loop review of sensitive actions. LangChain includes built-in middleware to review tool calls, in this case the tools invoked by sub-agents. Let’s add human-in-the-loop review to both sub-agents:- We configure the
create_calendar_eventandsend_emailtools to interrupt, permitting all response types (approve,edit,reject) - We add a checkpointer only to the top-level agent. This is required to pause and resume execution.
Command. Refer to the human-in-the-loop guide for additional details. For demonstration purposes, here we will accept the calendar event, but edit the subject of the outbound email:
7. 进阶:控制信息流
By default, sub-agents receive only the request string from the supervisor. You might want to pass additional context, such as conversation history or user preferences.向子智能体传递额外的对话上下文
控制监督者接收的内容
You can also customize what information flows back to the supervisor:8. 关键要点
The supervisor pattern creates layers of abstraction where each layer has a clear responsibility. When designing a supervisor system, start with clear domain boundaries and give each sub-agent focused tools and prompts. Write clear tool descriptions for the supervisor, test each layer independently before integration, and control information flow based on your specific needs.后续步骤
Learn about handoffs for agent-to-agent conversations, explore context engineering to fine-tune information flow, read the multi-agent overview to compare different patterns, and use LangSmith to debug and monitor your multi-agent system.将这些文档连接到 Claude、VSCode 等,通过 MCP 获取实时答案。

