Skip to main content

概述

监督者模式是一种多智能体架构,其中一个中央监督者智能体协调专业的工作者智能体。当任务需要不同类型的专业知识时,这种方法表现出色。与其构建一个跨领域管理工具选择的智能体,不如创建由理解整体工作流的监督者协调的专注型专家。 在本教程中,你将构建一个个人助手系统,通过一个现实的工作流来展示这些优势。系统将协调两个具有根本不同职责的专家:
  • 日历智能体,处理日程安排、可用性检查和事件管理。
  • 邮件智能体,管理通信、撰写消息和发送通知。
我们还将整合人机协作审查,允许用户根据需要批准、编辑和拒绝操作(如外发邮件)。

为什么使用监督者?

多智能体架构允许你将工具分配给各个工作者,每个工作者都有自己的提示或指令。考虑一个直接访问所有日历和邮件 API 的智能体:它必须从许多类似的工具中选择、理解每个 API 的确切格式,并同时处理多个领域。如果性能下降,将相关工具和关联提示分离到逻辑组中可能会有帮助(部分原因是为了管理迭代改进)。

概念

我们将涵盖以下概念:

设置

安装

This tutorial requires the langchain package:
For more details, see our Installation guide.

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:
👉 Read the OpenAI chat model integration docs

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.
Test the calendar agent to see how it handles natural language scheduling:
The agent parses “next Tuesday at 2pm” into ISO format (“2024-01-16T14:00:00”), calculates the end time, calls 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.
Test the email agent with a natural language request:
The agent infers the recipient from the informal request, crafts a professional subject line and body, calls 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”.
The tool descriptions help the supervisor decide when to use each tool, so make them clear and specific. We return only the sub-agent’s final response, as the supervisor doesn’t need to see intermediate reasoning or tool calls.

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:简单的单领域请求

The supervisor identifies this as a calendar task, calls schedule_event, and the calendar agent handles date parsing and event creation.
For full transparency into the information flow, including prompts and responses for each chat model call, check out the LangSmith trace for the above run.

示例 2:复杂的多领域请求

The supervisor recognizes this requires both calendar and email actions, calls 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.
Refer to the LangSmith trace to see the detailed information flow for the above run, including individual chat model prompts and responses.

完整工作示例

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_event and send_email tools 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.
Let’s repeat the query. Note that we gather interrupt events into a list to access downstream:
This time we’ve interrupted execution. Let’s inspect the interrupt events:
We can specify decisions for each interrupt by referring to its ID using a 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:
The run proceeds with our input.

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.

向子智能体传递额外的对话上下文

This allows sub-agents to see the full conversation context, which can be useful for resolving ambiguities like “schedule it for the same time tomorrow” (referencing a previous conversation).
You can see the full context received by the sub agent in the chat model call of the LangSmith trace.

控制监督者接收的内容

You can also customize what information flows back to the supervisor:
Important: Make sure sub-agent prompts emphasize that their final message should contain all relevant information. A common failure mode is sub-agents that perform tool calls but don’t include the results in their final response.
For a complete working example that demonstrates the full supervisor pattern with human-in-the-loop review and advanced information flow control, check out supervisor_complete.ts in the LangChain.js examples.

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.
When to use the supervisor patternUse the supervisor pattern when you have multiple distinct domains (calendar, email, CRM, database), each domain has multiple tools or complex logic, you want centralized workflow control, and sub-agents don’t need to converse directly with users.For simpler cases with just a few tools, use a single agent. When agents need to have conversations with users, use handoffs instead. For peer-to-peer collaboration between agents, consider other multi-agent patterns.

后续步骤

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.