import { createDeepAgent } from "deepagents";process.env.OPENAI_API_KEY = "your-api-key";const agent = createDeepAgent({ model: "gpt-5.4" });// this calls initChatModel for the specified model with default parameters// to use specific model parameters, use initChatModel directly
import { createDeepAgent } from "deepagents";process.env.ANTHROPIC_API_KEY = "your-api-key";const agent = createDeepAgent({ model: "anthropic:claude-sonnet-4-6" });// this calls initChatModel for the specified model with default parameters// to use specific model parameters, use initChatModel directly
import { createDeepAgent } from "deepagents";process.env.AZURE_OPENAI_API_KEY = "your-api-key";process.env.AZURE_OPENAI_ENDPOINT = "your-endpoint";process.env.OPENAI_API_VERSION = "your-api-version";const agent = createDeepAgent({ model: "azure_openai:gpt-5.4" });// this calls initChatModel for the specified model with default parameters// to use specific model parameters, use initChatModel directly
import { createDeepAgent } from "deepagents";process.env.GOOGLE_API_KEY = "your-api-key";const agent = createDeepAgent({ model: "google-genai:gemini-3.1-pro-preview" });// this calls initChatModel for the specified model with default parameters// to use specific model parameters, use initChatModel directly
import { createDeepAgent } from "deepagents";// Follow the steps here to configure your credentials:// https://docs.aws.amazon.com/bedrock/latest/userguide/getting-started.htmlconst agent = createDeepAgent({ model: "bedrock:anthropic.claude-sonnet-4-6" });// this calls initChatModel for the specified model with default parameters// to use specific model parameters, use initChatModel directly
传递任何支持的模型字符串或初始化的模型实例
import { initChatModel } from "langchain";import { createDeepAgent } from "deepagents";const model = await initChatModel("provider:model-name");const agent = createDeepAgent({ model });
Deep Agents 附带内置系统提示词。Deep Agent 的价值源于 SDK 在模型之上提供的编排层——规划、虚拟文件系统工具和子智能体——模型需要知道这些工具的存在以及何时使用它们。内置提示词教会智能体如何使用这些脚手架,这样你就不必为每个项目重新推导它;请通过配置文件 (profile) 或你自己的 system_prompt= 来微调它,而不是逐字复制。当中间件添加特殊工具(如文件系统工具)时,它会将它们附加到系统提示词中。每个 Deep Agent 还应包含针对其特定用例的自定义系统提示词:
import { createDeepAgent } from "deepagents";const researchInstructions = `You are an expert researcher. ` + `Your job is to conduct thorough research, and then ` + `write a polished report.`;const agent = createDeepAgent({ systemPrompt: researchInstructions,});
agent = create_deep_agent( model="anthropic:claude-sonnet-4-6", system_prompt="You are a customer-support agent for ACME Corp.",)# Final = USER + BASE + SUFFIX# = "You are a customer-support agent for ACME Corp."# + "\n\n"# + BASE_AGENT_PROMPT# + "\n\n"# + <Claude-specific guidance>
子智能体没有 USER 部分 —— 规范中编写的 system_prompt 是最接近的类比,并保留在 BASE 插槽中。仅包含 system_prompt_suffix 的配置文件(内置 Anthropic / OpenAI 配置文件的常见情况)只会附加到子智能体作者编写的任何内容之后;设置 base_system_prompt 的配置文件将彻底 替换 编写的提示词,因此请谨慎使用该字段。
通用子智能体提示词
自动添加的通用子智能体 (GP) 遵循相同的叠加规则,但多了一层:GP 基础提示词解析顺序为 general_purpose_subagent.system_prompt(如果已设置) -> HarnessProfile.base_system_prompt(如果已设置) -> SDK GP 默认值。无论哪种方式,配置文件后缀都会叠加在上面。这两个覆盖字段都可以承载基础提示词替换,但它们不可互换。general_purpose_subagent.system_prompt 是特定于 GP 的配置;base_system_prompt 是一个全局覆盖,主要针对主智能体。当两者都设置时,特定于 GP 的意图在 GP 子智能体中胜出,因此调整这两个字段的用户永远不会看到他们的 GP 覆盖被静默丢弃:
register_harness_profile( "anthropic", HarnessProfile( base_system_prompt="You are ACME's support orchestrator.", # main agent general_purpose_subagent=GeneralPurposeSubagentProfile( system_prompt="You are a research subagent. Cite sources.", # GP subagent ), system_prompt_suffix="Always think step by step.", ),)
堆栈
最终系统提示词
主智能体
“你是个 ACME 的支持编排者。” + SUFFIX
GP 子智能体
“你是个研究子智能体。请引用来源。” + SUFFIX
如果未设置 general_purpose_subagent.system_prompt,GP 子智能体将回退到 base_system_prompt(如果已设置),最后回退到 SDK GP 默认值。
Deep Agent 的工具可以利用虚拟文件系统来存储、访问和编辑文件。默认情况下,Deep Agents 使用 StateBackend。如果您正在使用技能或记忆,在创建智能体之前,必须将预期的技能或记忆文件添加到后端。
StateBackend
FilesystemBackend
LocalShellBackend
StoreBackend
CompositeBackend
存储在 langgraph 状态中的临时文件系统后端。此文件系统仅 针对单个线程 持久化。
import { createDeepAgent, StateBackend } from "deepagents";// By default we provide a StateBackendconst agent = createDeepAgent();// Under the hood, it looks likeconst agent2 = createDeepAgent({ backend: new StateBackend(),});
import { createDeepAgent, LocalShellBackend } from "deepagents";const backend = new LocalShellBackend({ workingDirectory: "." });const agent = createDeepAgent({ backend });
一个提供长期存储的系统,可 跨线程持久化。
import { createDeepAgent, StoreBackend } from "deepagents";import { InMemoryStore } from "@langchain/langgraph";const store = new InMemoryStore(); // Good for local dev; omit for LangSmith Deploymentconst agent = createDeepAgent({ backend: new StoreBackend({ namespace: (ctx) => [ctx.runtime.context.userId], }), store});
import { createDeepAgent, CompositeBackend, StateBackend, StoreBackend } from "deepagents";import { InMemoryStore } from "@langchain/langgraph";const store = new InMemoryStore();const agent = createDeepAgent({ backend: new CompositeBackend( new StateBackend(), { "/memories/": new StoreBackend(), } ), store,});
沙箱是专门的后端,它们在具有自己文件系统和用于 shell 命令的 execute 工具的隔离环境中运行智能体代码。当您希望您的 Deep Agent 编写文件、安装依赖项并运行命令而不更改本地机器上的任何内容时,请使用沙箱后端。您可以在创建 Deep Agent 时通过将沙箱后端传递给 backend 来配置沙箱:
import { createDeepAgent } from "deepagents";import { ChatAnthropic } from "@langchain/anthropic";import { DenoSandbox } from "@langchain/deno";// Create and initialize the sandboxconst sandbox = await DenoSandbox.create({ memoryMb: 1024, lifetime: "10m",});try { const agent = createDeepAgent({ model: new ChatAnthropic({ model: "claude-opus-4-6" }), systemPrompt: "You are a JavaScript coding assistant with sandbox access.", backend: sandbox, }); const result = await agent.invoke({ messages: [ { role: "user", content: "Create a simple HTTP server using Deno.serve and test it with curl", }, ], });} finally { await sandbox.close();}
您可以使用技能为您的 Deep Agent 提供新的能力和专业知识。虽然工具往往涵盖底层功能,如原生文件系统操作或规划,但技能可以包含关于如何完成任务的详细指令、参考信息和其他资产(如模板)。这些文件仅在智能体确定该技能对当前提示词有用时才会被加载。这种渐进式披露减少了智能体在启动时必须考虑的令牌数量和上下文。有关示例技能,请参阅 Deep Agents 示例技能。要向您的 Deep Agent 添加技能,请在调用 create_deep_agent 时将它们作为参数传递:
StateBackend
StoreBackend
FilesystemBackend
import { createDeepAgent, type FileData } from "deepagents";import { MemorySaver } from "@langchain/langgraph";const checkpointer = new MemorySaver();function createFileData(content: string): FileData { const now = new Date().toISOString(); return { content: content.split("\n"), created_at: now, modified_at: now, };}const skillsFiles: Record<string, FileData> = {};const skillUrl = "https://raw.githubusercontent.com/langchain-ai/deepagentsjs/refs/heads/main/examples/skills/langgraph-docs/SKILL.md";const response = await fetch(skillUrl);const skillContent = await response.text();skillsFiles["/skills/langgraph-docs/SKILL.md"] = createFileData(skillContent);const agent = await createDeepAgent({ checkpointer, // IMPORTANT: deepagents skill source paths are virtual (POSIX) paths relative to the backend root. skills: ["/skills/"],});const config = { configurable: { thread_id: `thread-${Date.now()}`, },};const result = await agent.invoke( { messages: [ { role: "user", content: "what is langraph? Use the langgraph-docs skill if available.", }, ], files: skillsFiles, }, config,);
import { createDeepAgent, StoreBackend, type FileData } from "deepagents";import { InMemoryStore, MemorySaver,} from "@langchain/langgraph";const checkpointer = new MemorySaver();const store = new InMemoryStore();function createFileData(content: string): FileData { const now = new Date().toISOString(); return { content: content.split("\n"), created_at: now, modified_at: now, };}const skillUrl = "https://raw.githubusercontent.com/langchain-ai/deepagentsjs/refs/heads/main/examples/skills/langgraph-docs/SKILL.md";const response = await fetch(skillUrl);const skillContent = await response.text();const fileData = createFileData(skillContent);await store.put(["filesystem"], "/skills/langgraph-docs/SKILL.md", fileData);const agent = await createDeepAgent({ backend: new StoreBackend(), store: store, checkpointer, // IMPORTANT: deepagents skill source paths are virtual (POSIX) paths relative to the backend root. skills: ["/skills/"],});const config = { recursionLimit: 50, configurable: { thread_id: `thread-${Date.now()}`, },};const result = await agent.invoke( { messages: [ { role: "user", content: "what is langraph? Use the langgraph-docs skill if available.", }, ], }, config,);
import { createDeepAgent, FilesystemBackend } from "deepagents";import { MemorySaver } from "@langchain/langgraph";const checkpointer = new MemorySaver();const backend = new FilesystemBackend({ rootDir: process.cwd() });const agent = await createDeepAgent({ backend, skills: ["./examples/skills/"], interruptOn: { read_file: true, write_file: true, delete_file: true, }, checkpointer, // Required!});const config = { configurable: { thread_id: `thread-${Date.now()}`, },};const result = await agent.invoke( { messages: [ { role: "user", content: "what is langraph? Use the langgraph-docs skill if available.", }, ], }, config,);
from deepagents import HarnessProfile, register_harness_profile# Append a system-prompt suffix whenever gpt-5.4 is selected.register_harness_profile( "openai:gpt-5.4", HarnessProfile(system_prompt_suffix="Respond in under 100 words."),)