LangChain 發布深度智能體框架Deepagents! 讓智能體不再“淺嘗輒止” 原創
很多人做智能體時,會用“大模型 + 循環調用工具”的最簡單架構。它好用,但常常很“淺”,一遇到復雜、長鏈路任務就容易跑偏、忘事、或停在半路。
像 Deep Research、Manus、Claude Code 這類“深度”智能體,是怎么補上的?核心其實就四件事:
- 規劃工具:先想清楚要做什么,再一步步做。
- 子智能體:把復雜任務拆給更專精的小助手。
- 文件系統:能讀寫文件,保留中間成果和上下文。
- 詳細提示詞:把工作方法講清楚,少走彎路。
deepagents 是什么
??deepagents?? 是一個 Python 包,把上面這四件事做成了通用能力,幫你更容易地搭出“深”智能體。它受 Claude Code 啟發很深,目標是更通用、更好用。

deep agent
- 安裝:
pip install deepagents- 如果要跑下面的入門示例,還需要:
pip install tavily-python一個簡潔的入門示例
import os
from typing import Literal
from tavily import TavilyClient
from deepagents import create_deep_agent
tavily_client = TavilyClient(api_key=os.environ["TAVILY_API_KEY"])
def internet_search(
query: str,
max_results: int = 5,
topic: Literal["general", "news", "finance"] = "general",
include_raw_content: bool = False,
):
"""Run a web search"""
return tavily_client.search(
query,
max_results=max_results,
include_raw_cnotallow=include_raw_content,
topic=topic,
)
research_instructions = """You are an expert researcher. Your job is to conduct thorough research, and then write a polished report.
You have access to a few tools.
## `internet_search`
Use this to run an internet search for a given query. You can specify the number of results, the topic, and whether raw content should be included.
"""
agent = create_deep_agent(
[internet_search],
research_instructions,
)
result = agent.invoke({"messages": [{"role": "user", "content": "what is langgraph?"}]})這個 ??agent?? 本質上就是一個 LangGraph 圖,所以你可以用 LangGraph 的常用能力(流式、HITL、人類介入、記憶、Studio 等)。
自定義一個“深”智能體
- tools(必填):一組函數或 LangChain 的?
?@tool??。主智能體和子智能體都能用。 - instructions(必填):這會成為提示詞的一部分(系統提示詞已內置,會和它一起起作用)。
- subagents(選填):自定義子智能體,做專門的子任務。
子智能體有兩種寫法:
- 簡單版?
?SubAgent??
- 必填字段:?
?name??(名字)、??description??(說明)、??prompt??(提示詞) - 可選字段:?
?tools??(可用工具,默認繼承全部)、??model_settings??(該子智能體獨立的模型設置)
research_subagent = {
"name": "research-agent",
"description": "Used to research more in depth questions",
"prompt": sub_research_prompt,
}
agent = create_deep_agent(
tools,
prompt,
subagents=[research_subagent]
)- 進階版?
?CustomSubAgent??
- 直接把一個預先構建好的 LangGraph 圖當作子智能體用:
from langgraph.prebuilt import create_react_agent
custom_graph = create_react_agent(
model=your_model,
tools=specialized_tools,
prompt="You are a specialized agent for data analysis..."
)
custom_subagent = {
"name": "data-analyzer",
"description": "Specialized agent for complex data analysis tasks",
"graph": custom_graph
}
agent = create_deep_agent(
tools,
prompt,
subagents=[custom_subagent]
)模型怎么配
- 默認模型:?
?"claude-sonnet-4-20250514"?? - 你可以傳任意 LangChain 模型對象作為默認模型;也可以為某個子智能體單獨指定模型與參數。
示例:用 Ollama 的自定義模型
from deepagents import create_deep_agent
from langchain.chat_models import init_chat_model
model = init_chat_model(model="ollama:gpt-oss:20b")
agent = create_deep_agent(
tools=tools,
instructinotallow=instructions,
model=model,
)示例:給“評審子智能體”單獨上一個更快、更穩的模型
critique_sub_agent = {
"name": "critique-agent",
"description": "Critique the final report",
"prompt": "You are a tough editor.",
"model_settings": {
"model": "anthropic:claude-3-5-haiku-20241022",
"temperature": 0,
"max_tokens": 8192
}
}
agent = create_deep_agent(
tools=[internet_search],
instructinotallow="You are an expert researcher...",
model="claude-sonnet-4-20250514",
subagents=[critique_sub_agent],
)內置工具
默認自帶 5 個工具(可通過 ??builtin_tools?? 精簡):
- write_todos:寫待辦(幫助“先計劃,再執行”)
- write_file:寫文件(虛擬文件系統)
- read_file:讀文件
- ls:列文件
- edit_file:編輯文件
精簡示例(只保留待辦工具):
builtin_tools = ["write_todos"]
agent = create_deep_agent(..., builtin_tools=builtin_tools, ...)關鍵部件
- 系統提示詞(System Prompt)已內置,參考了 Claude Code 的風格,又更通用。它把“怎么規劃、怎么用文件、怎么調用子智能體”等規則說清楚。好的提示詞,是深度的關鍵。
- 規劃工具(Planning Tool)類似 Claude Code 的 TodoWrite。它不直接“做事”,而是先把計劃寫下來,放在上下文里,幫助后續執行。
- 虛擬文件系統(File System Tools)提供?
?ls/read_file/write_file/edit_file??,用 LangGraph 的 State 模擬,不會動到真實磁盤,方便在一臺機上開多個智能體也不相互影響。目前支持一層目錄;可以通過 State 中的??files?? 注入和讀取。 - 子智能體(Sub Agents)內置一個通用子智能體(和主智能體同指令、同工具),也支持你自定義多個專門子智能體。好處是“隔離上下文”、“專人做專事”。
- 人機協同(Human-in-the-Loop)你可以給某些工具加“人工審批”攔截(?
?interrupt_config??)。支持:
a.allow_accept:直接執行
b.allow_edit:改工具或改參數再執行
c.allow_respond:不執行,追加一條“工具消息”作為反饋需要配一個檢查點(如 ??InMemorySaver??)。當前一次只能攔截一個并行工具調用。
- MCP 工具通過 LangChain MCP Adapter 可以讓 deepagents 使用 MCP 工具(注意使用 async 版本)。
- 配置化智能體(Configurable Agent)用?
?create_configurable_agent??,把??instructions/subagents?? 等做成可配的構建器,方便在??langgraph.json?? 里部署和更新。也有 async 版本。
適合做什么
- 深度研究:查資料、比對觀點、整合成文。
- 代碼助手:規劃變更、讀改文件、分派子任務、總結提交。
- 數據/文檔處理:拆分任務、多步加工、階段性落盤。
- 流程自動化:有計劃、有記憶、有分工、更可靠。
總結
建議先從小規模開始,逐步引入工具、子智能體和文件系統,并編寫清晰的提示詞指導智能體“先想后做、不確定時提問、記錄關鍵步驟”。關鍵操作加入人工審核,并針對不同任務選用不同模型以平衡效果與成本。通過這些改進,原有的工具循環型智能體將變得更耐心、穩定,能更好地完成復雜任務。
本文轉載自????????AI 博物院???????? 作者:longyunfeigu

















