以编程方式使用文档

LangChain 托管 托管深度智能体,因此您可以创建智能体并流式传输响应而无需设置基础设施。本快速入门展示了如何使用 CLI 或 SDK 创建智能体,然后使用 Python SDK、TypeScript SDK 或 React 运行它。 useStream.

有关完整的部署工作流程和所有后端选项,请参阅 部署智能体。有关包配置和 API 详情,请参阅 托管深度智能体 SDK.

前提条件

在开始之前,请确保您已具备:

  • - 托管深度智能体 私人测试版访问权限.
  • - A LangSmith API 密钥 适用于具有私人测试版访问权限的工作空间。
  • - 一个客户端。Python SDK(managed-deepagents)或 TypeScript SDK(@langchain/managed-deepagents) 涵盖了整个快速入门。 deepagents-cli>=0.2.2 可以创建代理但无法运行它,因此运行代理需要 SDK。

创建并运行代理

Install a client

为您的运行时选择客户端:

uv tool install "deepagents-cli>=0.2.2"

# Or with pip:
pip install -U "deepagents-cli>=0.2.2"
uv add managed-deepagents

# Or with pip:
pip install managed-deepagents
npm install @langchain/managed-deepagents

# For React streaming:
npm install @langchain/react

要升级现有的 CLI 安装,请运行 uv tool upgrade deepagents-cli (or pip install -U "deepagents-cli" (如果您使用 pip 安装)。

Set your API key

为拥有私人测试版访问权限的工作区设置 LangSmith API 密钥:

Create the agent

创建托管深度代理。保存返回的 agent_id 用于运行步骤。

deepagents init research-assistant
cd research-assistant

# Edit AGENTS.md to define the agent behavior, then deploy.
deepagents deploy
from managed_deepagents import Client

with Client() as client:
    agent = client.agents.create(
        name="research-assistant",
        description="Research assistant that can search the web and summarize sources.",
        model="openai:gpt-5.5",
        backend={"type": "state"},
        instructions=(
            "You are a careful research assistant. Search for sources, "
            "keep notes, and return concise answers with citations."
        ),
    )

agent_id = agent["id"]
print(f"Agent ID: {agent_id}")
const client = new Client({
  apiKey: process.env.LANGSMITH_API_KEY,
});

const agent = await client.agents.create({
  name: "research-assistant",
  description: "Research assistant that can search the web and summarize sources.",
  model: "openai:gpt-5.5",
  backend: { type: "state" },
  instructions:
    "You are a careful research assistant. Search for sources, keep notes, and return concise answers with citations.",
});

const agentId = agent.id;
console.log(`Agent ID: ${agentId}`);

CLI 会创建 agent.json, AGENTS.md, .gitignore、一个空的 tools.json、一个示例技能和一个示例子代理,然后再部署。SDK 示例直接创建托管代理。

这些示例使用 state 后端,以便代理可以在无需沙箱特定配置的情况下运行。切换到 sandbox 后端,当代理需要 LangSmith 沙箱 用于代码执行、文件系统操作或长时间运行的任务。如需了解选项,请参阅 选择后端.

如果代理调用 MCP 工具, 连接工具 后再创建或部署代理。

Run the agent

从代理流式传输响应:

from managed_deepagents import Client

agent_id = "<agent_id>"

with Client() as client:
    thread = client.threads.create(
        agent_id=agent_id,
        options={
            "test_run": False,
            "skip_memory_write_protection": False,
        },
    )

    for event in client.threads.stream(
        thread["id"],
        agent_id=agent_id,
        messages=[
            {
                "role": "user",
                "content": "Research recent approaches to agent memory and summarize the main trade-offs.",
            }
        ],
        stream_mode=["values", "updates", "messages-tuple"],
        stream_subgraphs=True,
    ):
        print(event.event, event.data)
const agentId = "<agent_id>";
const client = new Client({
  apiKey: process.env.LANGSMITH_API_KEY,
});

const thread = await client.threads.create({
  agent_id: agentId,
  options: {
    test_run: false,
    skip_memory_write_protection: false,
  },
});

const langGraphClient = client.getLangGraphClient({ agentId });
const stream = langGraphClient.runs.stream(thread.id, agentId, {
  input: {
    messages: [
      {
        role: "user",
        content:
          "Research recent approaches to agent memory and summarize the main trade-offs.",
      },
    ],
  },
  streamMode: ["values", "updates", "messages-tuple"],
  streamSubgraphs: true,
});

for await (const event of stream) {
  console.log(event.event, event.data);
}
const agentId = "<agent_id>";

const managedDeepAgents = new Client({
  // In browser apps, prefer passing a custom fetch that calls your backend.
  apiKey: process.env.LANGSMITH_API_KEY,
});

const client = managedDeepAgents.getLangGraphClient({ agentId });

  const stream = useStream({
    client,
    assistantId: agentId
  });

  return (
    <section>
      <button
        type="button"
        disabled={stream.isLoading}
        onClick={() => {
          void stream.submit({
            messages: [
              {
                role: "user",
                content:
                  "Research recent approaches to agent memory and summarize the main trade-offs.",
              },
            ],
          });
        }}
      >
        Run agent
      </button>

      {stream.messages.map((message, index) => (
        <p key={message.id ?? index}>{String(message.content)}</p>
      ))}
    
); }

Python 流会生成来自运行的服务器发送事件。TypeScript SDK 和 React useStream 示例使用 LangGraph 流式预测来处理消息、值和输出状态。

后续步骤

Run an agent

使用所有流模式和事件类型创建线程和流运行。

Connect tools

在部署需要外部功能的代理前,添加 MCP 支持的工具。

Deploy an agent

了解完整的 CLI、SDK 和 REST API 部署工作流程。

SDKs

使用 Python、TypeScript 和 React SDK 进行托管深度代理。

CLI reference

查看所有命令、标志、项目文件和验证规则。

API reference

查看生成的端点参考页面和常见 REST 命令。