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

deep agent
- 安裝:
pip install deepagents- 如果要跑下面的入門示例,還需要:
pip install tavily-python一個(gè)簡(jiǎn)潔的入門示例
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?"}]})這個(gè) ??agent?? 本質(zhì)上就是一個(gè) LangGraph 圖,所以你可以用 LangGraph 的常用能力(流式、HITL、人類介入、記憶、Studio 等)。
自定義一個(gè)“深”智能體
- tools(必填):一組函數(shù)或 LangChain 的?
?@tool??。主智能體和子智能體都能用。 - instructions(必填):這會(huì)成為提示詞的一部分(系統(tǒng)提示詞已內(nèi)置,會(huì)和它一起起作用)。
- subagents(選填):自定義子智能體,做專門的子任務(wù)。
子智能體有兩種寫(xiě)法:
- 簡(jiǎn)單版?
?SubAgent??
- 必填字段:?
?name??(名字)、??description??(說(shuō)明)、??prompt??(提示詞) - 可選字段:?
?tools??(可用工具,默認(rèn)繼承全部)、??model_settings??(該子智能體獨(dú)立的模型設(shè)置)
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]
)- 進(jìn)階版?
?CustomSubAgent??
- 直接把一個(gè)預(yù)先構(gòu)建好的 LangGraph 圖當(dāng)作子智能體用:
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]
)模型怎么配
- 默認(rèn)模型:?
?"claude-sonnet-4-20250514"?? - 你可以傳任意 LangChain 模型對(duì)象作為默認(rèn)模型;也可以為某個(gè)子智能體單獨(dú)指定模型與參數(shù)。
示例:用 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,
)示例:給“評(píng)審子智能體”單獨(dú)上一個(gè)更快、更穩(wěn)的模型
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],
)內(nèi)置工具
默認(rèn)自帶 5 個(gè)工具(可通過(guò) ??builtin_tools?? 精簡(jiǎn)):
- write_todos:寫(xiě)待辦(幫助“先計(jì)劃,再執(zhí)行”)
- write_file:寫(xiě)文件(虛擬文件系統(tǒng))
- read_file:讀文件
- ls:列文件
- edit_file:編輯文件
精簡(jiǎn)示例(只保留待辦工具):
builtin_tools = ["write_todos"]
agent = create_deep_agent(..., builtin_tools=builtin_tools, ...)關(guān)鍵部件
- 系統(tǒng)提示詞(System Prompt)已內(nèi)置,參考了 Claude Code 的風(fēng)格,又更通用。它把“怎么規(guī)劃、怎么用文件、怎么調(diào)用子智能體”等規(guī)則說(shuō)清楚。好的提示詞,是深度的關(guān)鍵。
- 規(guī)劃工具(Planning Tool)類似 Claude Code 的 TodoWrite。它不直接“做事”,而是先把計(jì)劃寫(xiě)下來(lái),放在上下文里,幫助后續(xù)執(zhí)行。
- 虛擬文件系統(tǒng)(File System Tools)提供?
?ls/read_file/write_file/edit_file??,用 LangGraph 的 State 模擬,不會(huì)動(dòng)到真實(shí)磁盤,方便在一臺(tái)機(jī)上開(kāi)多個(gè)智能體也不相互影響。目前支持一層目錄;可以通過(guò) State 中的??files?? 注入和讀取。 - 子智能體(Sub Agents)內(nèi)置一個(gè)通用子智能體(和主智能體同指令、同工具),也支持你自定義多個(gè)專門子智能體。好處是“隔離上下文”、“專人做專事”。
- 人機(jī)協(xié)同(Human-in-the-Loop)你可以給某些工具加“人工審批”攔截(?
?interrupt_config??)。支持:
a.allow_accept:直接執(zhí)行
b.allow_edit:改工具或改參數(shù)再執(zhí)行
c.allow_respond:不執(zhí)行,追加一條“工具消息”作為反饋需要配一個(gè)檢查點(diǎn)(如 ??InMemorySaver??)。當(dāng)前一次只能攔截一個(gè)并行工具調(diào)用。
- MCP 工具通過(guò) LangChain MCP Adapter 可以讓 deepagents 使用 MCP 工具(注意使用 async 版本)。
- 配置化智能體(Configurable Agent)用?
?create_configurable_agent??,把??instructions/subagents?? 等做成可配的構(gòu)建器,方便在??langgraph.json?? 里部署和更新。也有 async 版本。
適合做什么
- 深度研究:查資料、比對(duì)觀點(diǎn)、整合成文。
- 代碼助手:規(guī)劃變更、讀改文件、分派子任務(wù)、總結(jié)提交。
- 數(shù)據(jù)/文檔處理:拆分任務(wù)、多步加工、階段性落盤。
- 流程自動(dòng)化:有計(jì)劃、有記憶、有分工、更可靠。
總結(jié)
建議先從小規(guī)模開(kāi)始,逐步引入工具、子智能體和文件系統(tǒng),并編寫(xiě)清晰的提示詞指導(dǎo)智能體“先想后做、不確定時(shí)提問(wèn)、記錄關(guān)鍵步驟”。關(guān)鍵操作加入人工審核,并針對(duì)不同任務(wù)選用不同模型以平衡效果與成本。通過(guò)這些改進(jìn),原有的工具循環(huán)型智能體將變得更耐心、穩(wěn)定,能更好地完成復(fù)雜任務(wù)。
本文轉(zhuǎn)載自????????AI 博物院???????? 作者:longyunfeigu

















