GuidesAgents
用 LangChain Supervisor 模式构建个人助理 Multi-Agent
按官方 subagents personal assistant 教程:日历与邮件子代理各自专注,再包装成工具,由 supervisor 协调跨域请求。
基于 LangChain Docs 整理 · 官方资料 ↗
一个 Agent 同时管日历 API 和邮件 API 时,工具太多、格式太碎,路由容易乱。Supervisor 模式把专家拆成子代理,由中心 supervisor 只看高层工具并协调结果。
这篇跟官方「Build a personal assistant with subagents」:日历助手 + 邮件助手,再合成个人助理。工具在教程里是 stub,重点是分层结构。
为什么要用 Supervisor
多 Agent 把工具与提示按域切开。日历只懂排期,邮件只懂写信;supervisor 决定何时 schedule_event、何时 manage_email,而不是在几十个底层 API 里挑。
适合:多个独立域、子代理不必直接对用户说话、你希望工作流中心化控制。
先定义域内 Tools
官方安装
pip install langchain官方 stub tools
from langchain.tools import tool
@tool
def create_calendar_event(
title: str,
start_time: str,
end_time: str,
attendees: list[str],
location: str = "",
) -> str:
"""Create a calendar event. Requires exact ISO datetime format."""
return f"Event created: {title} from {start_time} to {end_time} with {len(attendees)} attendees"
@tool
def send_email(
to: list[str],
subject: str,
body: str,
cc: list[str] = [],
) -> str:
"""Send an email via email API. Requires properly formatted addresses."""
return f"Email sent to {', '.join(to)} - Subject: {subject}"
@tool
def get_available_time_slots(
attendees: list[str],
date: str,
duration_minutes: int,
) -> list[str]:
"""Check calendar availability for given attendees on a specific date."""
return ["09:00", "14:00", "16:00"]做成专注的子代理
日历与邮件子代理
from datetime import date
from langchain.agents import create_agent
CALENDAR_AGENT_PROMPT = (
f"Today's date is {date.today().isoformat()}. "
"You are a calendar scheduling assistant. "
"Parse natural language scheduling requests into proper ISO datetime formats. "
"Use get_available_time_slots to check availability when needed. "
"Use create_calendar_event to schedule events. "
"Always confirm what was scheduled in your final response."
)
calendar_agent = create_agent(
model,
tools=[create_calendar_event, get_available_time_slots],
system_prompt=CALENDAR_AGENT_PROMPT,
)
EMAIL_AGENT_PROMPT = (
"You are an email assistant. "
"Compose professional emails based on natural language requests. "
"Use send_email to send the message. "
"Always confirm what was sent in your final response."
)
email_agent = create_agent(
model,
tools=[send_email],
system_prompt=EMAIL_AGENT_PROMPT,
)包装成工具,再交给 Supervisor
关键一步:子代理对 supervisor 只暴露高层工具。supervisor 看到的是 schedule_event / manage_email,不是 create_calendar_event。
子代理包装 + supervisor
@tool
def schedule_event(request: str) -> str:
"""Schedule calendar events using natural language."""
result = calendar_agent.invoke({
"messages": [{"role": "user", "content": request}]
})
return result["messages"][-1].text
@tool
def manage_email(request: str) -> str:
"""Send emails using natural language."""
result = email_agent.invoke({
"messages": [{"role": "user", "content": request}]
})
return result["messages"][-1].text
SUPERVISOR_PROMPT = (
"You are a helpful personal assistant. "
"You can schedule calendar events and send emails. "
"Break down user requests into appropriate tool calls and coordinate the results. "
"When a request involves multiple actions, use multiple tools in sequence or in parallel as appropriate."
)
supervisor_agent = create_agent(
model,
tools=[schedule_event, manage_email],
system_prompt=SUPERVISOR_PROMPT,
)跨域请求示例
query = "Schedule a team standup for tomorrow at 9am"
# 或同时排会 + 发提醒的组合请求
result = supervisor_agent.invoke({
"messages": [{"role": "user", "content": query}]
})
print(result["messages"][-1].text)返回给 supervisor 的 ideally 只有子代理最终回复,避免把中间 tool 轨迹灌进上层上下文。官方后续还可加 human-in-the-loop 审核外发邮件。
容易踩的坑
supervisor 仍直接调用底层 API 工具
确认只把 schedule_event / manage_email 传给 supervisor,底层 tools 只挂在子代理上。
子代理上下文泄漏到上层
包装工具只返回 messages[-1].text,不要把完整中间轨迹回传。
还在找 langgraph-supervisor 包
当前成熟教程已迁到 LangChain multi-agent subagents 模式;迁移说明见官方 Migrate from langgraph-supervisor。
官方资料
LangChain Docs
Build a personal assistant with subagents ↗