LangGraph for Agents
When to use
- - Use this skill when the user asks to build agents or multi-agent systems using LangGraph/LangChain.
How to refer
Integrated Reference Examples
Read the examples in "./references/" to understand common patterns.
Start with "./references/README.md" for an overview, then read the target file, it will show more details.
!Important:
To build an agent, API_KEY credentials is necessary.
This is user privacy, please do not hard-code it, just hold a placeholder, e.g. API_KEY=your-api-key, and let the user manage the actual keys.
External Resources
[Search]
If the "search" tool is available, you can refine the query keywords and execute the search.
[Browse]
If the "browse" tool is available, you can visit the following three websites:
- - LangGraph Official GitHub Repository (https://github.com/langchain-ai/langgraph)
- LangGraph Official Documentation (https://docs.langchain.com/oss/python/langgraph/overview)
- LangChain Official Documentation (https://docs.langchain.com/oss/python/langchain/overview)
[Fetch]
If the "fetch" tool is available, you can retrieve content from the following URL:
- - Context-7 LangGraph (https://context7.com/websites/langchainosspython_langgraph/llms.txt?tokens=10000)
You may adjust the number of tokens by modifying the
tokens parameter in the URL. The default value is 10,000.
Project Structure
For demos or tests, use a single .py file. For production-grade applications, use:
CODEBLOCK0
Process for Agent System Design
Step 1: Determine System Level
- - Single-Agent System: Focus on the internal structure of one agent.
- Multi-Agent System: Focus on collaboration and communication between multiple agents.
Step 2: Choose Framework
- - LangGraph: Best for stateful, complex workflows.
- LangChain: Best for standard agent patterns based on tool calling.
Step 3: Design Specific Implementation
For Single-Agent Systems:
- - With LangGraph: Build a workflow with several nodes, or implement a ReAct Agent with manual tool_node.
- With LangChain: Build a ReAct Agent by
create_agent API.
For Multi-Agent Systems:
- Option 1: Treat each node as an independent agent, connecting them via the Graph API.
- Option 2: Encapsulate a multi-node workflow as a single agent, calling other agents as tools.
- Create a main ReAct Agent and encapsulate other agents as tools for collaboration.
Build Philosophy
- - Prefer Native: Check if a tool or integration already exists in LangChain before custom building.
- Single File First: Keep core logic in one file initially to simplify debugging.
- Clean Code: Provide only essential comments and use clear, descriptive variable names.
- Real Data: Use actual API URLs and schemas whenever possible.
LangGraph for Agents
使用时机
- - 当用户要求使用LangGraph/LangChain构建智能体或多智能体系统时,使用此技能。
参考方式
集成参考示例
阅读./references/中的示例以了解常见模式。
首先阅读./references/README.md获取概述,然后阅读目标文件,其中将展示更多细节。
!重要提示:
要构建智能体,API_KEY凭证是必需的。
这属于用户隐私,请勿硬编码,只需保留占位符,例如API_KEY=your-api-key,让用户管理实际的密钥。
外部资源
[搜索]
如果搜索工具可用,您可以优化查询关键词并执行搜索。
[浏览]
如果浏览工具可用,您可以访问以下三个网站:
- - LangGraph官方GitHub仓库 (https://github.com/langchain-ai/langgraph)
- LangGraph官方文档 (https://docs.langchain.com/oss/python/langgraph/overview)
- LangChain官方文档 (https://docs.langchain.com/oss/python/langchain/overview)
[获取]
如果获取工具可用,您可以从以下URL检索内容:
- - Context-7 LangGraph (https://context7.com/websites/langchainosspython_langgraph/llms.txt?tokens=10000)
您可以通过修改URL中的tokens参数来调整令牌数量。默认值为10,000。
项目结构
对于演示或测试,使用单个.py文件。对于生产级应用,请使用:
├── app/
│ ├── api/ # API端点
│ ├── backend/ # LangGraph/LangChain逻辑
│ └── frontend/ # 用户界面
├── .env.example
├── requirements.txt
└── README.md
智能体系统设计流程
步骤1:确定系统层级
- - 单智能体系统:关注单个智能体的内部结构。
- 多智能体系统:关注多个智能体之间的协作与通信。
步骤2:选择框架
- - LangGraph:最适合有状态、复杂的工作流。
- LangChain:最适合基于工具调用的标准智能体模式。
步骤3:设计具体实现
对于单智能体系统:
- - 使用LangGraph:构建包含多个节点的工作流,或通过手动toolnode实现ReAct智能体。
- 使用LangChain:通过createagent API构建ReAct智能体。
对于多智能体系统:
- 选项1:将每个节点视为独立智能体,通过Graph API连接它们。
- 选项2:将多节点工作流封装为单个智能体,将其他智能体作为工具调用。
- 创建一个主ReAct智能体,并将其他智能体封装为工具进行协作。
构建理念
- - 优先使用原生:在自定义构建之前,检查LangChain中是否已存在相关工具或集成。
- 单文件优先:初始时将核心逻辑保留在一个文件中,以简化调试。
- 代码整洁:仅提供必要的注释,并使用清晰、描述性的变量名。
- 真实数据:尽可能使用实际的API URL和模式。