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.
概述
路由器模式是一种多智能体架构,其中路由步骤对输入进行分类并将其定向到专业智能体,结果被综合成一个组合响应。当你的组织的知识分布在不同的垂直领域(各自需要具有专用工具和提示的独立智能体的独立知识域)时,此模式表现出色。 在本教程中,你将构建一个多源知识库路由器,通过一个现实的企业场景来展示这些优势。系统将协调三个专家:- GitHub 智能体,搜索代码、issues 和 pull requests。
- Notion 智能体,搜索内部文档和 wiki。
- Slack 智能体,搜索相关线程和讨论。
为什么使用路由器?
路由器模式提供了几个优势:- 并行执行:同时查询多个数据源,与顺序方法相比减少延迟。
- 专业智能体:每个垂直领域都有针对其领域优化的专用工具和提示。
- 选择性路由:并非每个查询都需要每个数据源——路由器智能地选择相关垂直领域。
- 针对性子问题:每个智能体收到针对其领域定制的问题,提高结果质量。
- 清晰的综合:来自多个数据源的结果被组合成单一、连贯的响应。
概念
We will cover the following concepts:- Multi-agent systems
- StateGraph for workflow orchestration
- Send API for parallel execution
设置
安装
This tutorial requires thelangchain and langgraph packages:
LangSmith
Set up LangSmith to inspect what is happening inside your agent. Then set the following environment variables:选择 LLM
Select a chat model from LangChain’s suite of integrations:- OpenAI
- Anthropic
- Azure
- Google Gemini
- Bedrock Converse
1. 定义状态
First, define the state schemas. We use three types:AgentInput: Simple state passed to each subagent (just a query)AgentOutput: Result returned by each subagent (source name + result)RouterState: Main workflow state tracking the query, classifications, results, and final answer
results field uses a reducer (operator.add in Python, a concat function in JS) to collect outputs from parallel agent executions into a single list.
2. 为每个垂直领域定义工具
Create tools for each knowledge domain. In a production system, these would call actual APIs. For this tutorial, we use stub implementations that return mock data. We define 7 tools across 3 verticals: GitHub (search code, issues, PRs), Notion (search docs, get page), and Slack (search messages, get thread).3. 创建专业智能体
Create an agent for each vertical. Each agent has domain-specific tools and a prompt optimized for its knowledge source. All three follow the same pattern—only the tools and system prompt differ.4. 构建路由器工作流
Now build the router workflow using a StateGraph. The workflow has four main steps:- Classify: Analyze the query and determine which agents to invoke with what sub-questions
- Route: Fan out to selected agents in parallel using
Send - Query agents: Each agent receives a simple
AgentInputand returns anAgentOutput - Synthesize: Combine collected results into a coherent response
5. 编译工作流
Now assemble the workflow by connecting nodes with edges. The key is usingadd_conditional_edges with the routing function to enable parallel execution:
add_conditional_edges call connects the classify node to the agent nodes through the route_to_agents function. When route_to_agents returns multiple Send objects, those nodes execute in parallel.
6. 使用路由器
Test your router with queries that span multiple knowledge domains:7. 理解架构
The router workflow follows a clear pattern:分类阶段
Theclassify_query function uses structured output to analyze the user’s query and determine which agents to invoke. This is where the routing intelligence lives:
- Uses a Pydantic model (Python) or Zod schema (JS) to ensure valid output
- Returns a list of
Classificationobjects, each with asourceand targetedquery - Only includes relevant sources—irrelevant ones are simply omitted
使用 send 进行并行执行
Theroute_to_agents function maps classifications to Send objects. Each Send specifies the target node and the state to pass:
AgentInput with just a query field—not the full router state. This keeps the interface clean and explicit.
使用 reducers 收集结果
Agent results flow back to the main state via a reducer. Each agent returns:operator.add in Python) concatenates these lists, collecting all parallel results into state["results"].
综合阶段
After all agents complete, thesynthesize_results function iterates over the collected results:
- Waits for all parallel branches to complete (LangGraph handles this automatically)
- References the original query to ensure the answer addresses what the user asked
- Combines information from all sources without redundancy
Partial results: In this tutorial, all selected agents must complete before synthesis.
8. 完整工作示例
Here’s everything together in a runnable script:9. 进阶:有状态路由器
The router we’ve built so far is stateless (each request is handled independently with no memory between calls). For multi-turn conversations, you need a stateful approach.工具包装方法
The simplest way to add conversation memory is to wrap the stateless router as a tool that a conversational agent can call:完全持久化方法
If you need the router itself to maintain state—for example, to use previous search results in routing decisions—use persistence to store message history at the router level.10. 关键要点
The router pattern excels when you have:- Distinct verticals: Separate knowledge domains that each require specialized tools and prompts
- Parallel query needs: Questions that benefit from querying multiple sources simultaneously
- Synthesis requirements: Results from multiple sources need to be combined into a coherent response
后续步骤
- Learn about handoffs for agent-to-agent conversations
- Explore the subagents pattern for centralized orchestration
- Read the multi-agent overview to compare different patterns
- Use LangSmith to debug and monitor your router
将这些文档连接到 Claude、VSCode 等,通过 MCP 获取实时答案。

