LangChain 1.0 使用指南。提供 Agent、Tool、Memory、Middleware 等核心概念的快速参考。当用户需要创建 AI Agent、集成 LangChain、或解决 LangChain 相关问题时激活。
View on GitHubNanmiCoder/claude-code-skills
langchain-use
January 20, 2026
Select agents to install to:
npx add-skill https://github.com/NanmiCoder/claude-code-skills/blob/main/plugins/langchain-use/skills/langchain-use-skill/SKILL.md -a claude-code --skill langchain-useInstallation paths:
.claude/skills/langchain-use/# LangChain Use Skill
LangChain 是构建 LLM 驱动的智能体和应用程序的开源框架。
## 安装
使用 uv 安装 LangChain(推荐,需要 Python 3.10+):
```bash
# 安装核心包
uv add langchain
# 安装模型提供商集成
uv add langchain-anthropic # Anthropic/Claude
uv add langchain-openai # OpenAI
```
## 快速参考
### 核心工作流程
```
用户查询 -> create_agent() -> ReAct 循环 -> Tool 调用 -> 返回结果
```
### 创建 Agent
详见 [Agent 基础](references/agents/agent-basics.md)
```python
from langchain.agents import create_agent
agent = create_agent(
model="claude-sonnet-4-5-20250929",
tools=[get_weather],
system_prompt="You are a helpful assistant",
)
# 运行 agent
result = agent.invoke(
{"messages": [{"role": "user", "content": "what is the weather in sf"}]}
)
```
### 定义 Tool
详见 [Tool 基础](references/tools/tool-basics.md)
```python
from langchain.tools import tool
@tool
def get_weather(city: str) -> str:
"""Get weather for a given city."""
return f"It's always sunny in {city}!"
```
### 访问 Runtime Context
使用 `ToolRuntime` 访问 state、context、store:
```python
from langchain.tools import tool, ToolRuntime
from dataclasses import dataclass
@dataclass
class Context:
user_id: str
@tool
def get_user_location(runtime: ToolRuntime[Context]) -> str:
"""Retrieve user location based on user ID."""
user_id = runtime.context.user_id
return "Florida" if user_id == "1" else "SF"
```
### 管理 Memory
详见 [短期记忆](references/memory/short-term-memory.md)
详见 [长期记忆](references/memory/long-term-memory.md)
```python
from langgraph.checkpoint.memory import InMemorySaver
agent = create_agent(
model,
tools,
checkpointer=InMemorySaver(), # 短期记忆
)
# 使用 thread_id 维护会话
config = {"configurable": {"thread_id": "1"}}
agent.invoke({"messages": [...]}, config)
```
### 添加 Middleware
详见 [中间件概述](references/middleware/middleware-overview.md)
```python
from langchain.agents.middleware import before_model, after_model
@before_model
def trim_messages(state, runtime):
# 消息修剪逻辑
return None
```
## 进阶主题
| 主题 | 文档 | 说明 |
|------|--Issues Found: