> ## Documentation Index
> Fetch the complete documentation index at: https://cndoc-langchain.site/llms.txt
> Use this file to discover all available pages before exploring further.

# 长期记忆

> 为 LangChain 代理添加长期记忆，以跨对话和会话存储和召回数据

长期记忆让您的代理能够跨不同的对话和会话存储和召回信息。
与[短期记忆](/oss/javascript/langchain/short-term-memory)（其作用范围限于单个线程）不同，长期记忆可以跨线程持久化，并可随时召回。

长期记忆构建于 [LangGraph 存储](/oss/javascript/langgraph/persistence#memory-store)之上，它将数据保存为按命名空间和键组织的 JSON 文档。

## 用法

要为代理添加长期记忆，请创建一个存储并将其传递给 [`create_agent`](https://reference.langchain.com/javascript/langchain/index/createAgent)：

<Tabs>
  <Tab title="InMemoryStore">
    <CodeGroup>
      ```ts Google theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
      import { createAgent } from "langchain";
      import { InMemoryStore } from "@langchain/langgraph";

      // InMemoryStore 将数据保存到内存字典中。在生产环境中请使用基于数据库的存储。
      const store = new InMemoryStore();

      const agent = createAgent({
        model: "google-genai:gemini-3.1-pro-preview",
        tools: [],
        store,
      });
      ```

      ```ts OpenAI theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
      import { createAgent } from "langchain";
      import { InMemoryStore } from "@langchain/langgraph";

      // InMemoryStore 将数据保存到内存字典中。在生产环境中请使用基于数据库的存储。
      const store = new InMemoryStore();

      const agent = createAgent({
        model: "openai:gpt-5.4",
        tools: [],
        store,
      });
      ```

      ```ts Anthropic theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
      import { createAgent } from "langchain";
      import { InMemoryStore } from "@langchain/langgraph";

      // InMemoryStore 将数据保存到内存字典中。在生产环境中请使用基于数据库的存储。
      const store = new InMemoryStore();

      const agent = createAgent({
        model: "anthropic:claude-sonnet-4-6",
        tools: [],
        store,
      });
      ```

      ```ts OpenRouter theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
      import { createAgent } from "langchain";
      import { InMemoryStore } from "@langchain/langgraph";

      // InMemoryStore 将数据保存到内存字典中。在生产环境中请使用基于数据库的存储。
      const store = new InMemoryStore();

      const agent = createAgent({
        model: "openrouter:anthropic/claude-sonnet-4-6",
        tools: [],
        store,
      });
      ```

      ```ts Fireworks theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
      import { createAgent } from "langchain";
      import { InMemoryStore } from "@langchain/langgraph";

      // InMemoryStore 将数据保存到内存字典中。在生产环境中请使用基于数据库的存储。
      const store = new InMemoryStore();

      const agent = createAgent({
        model: "fireworks:accounts/fireworks/models/qwen3p5-397b-a17b",
        tools: [],
        store,
      });
      ```

      ```ts Baseten theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
      import { createAgent } from "langchain";
      import { InMemoryStore } from "@langchain/langgraph";

      // InMemoryStore 将数据保存到内存字典中。在生产环境中请使用基于数据库的存储。
      const store = new InMemoryStore();

      const agent = createAgent({
        model: "baseten:zai-org/GLM-5",
        tools: [],
        store,
      });
      ```

      ```ts Ollama theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
      import { createAgent } from "langchain";
      import { InMemoryStore } from "@langchain/langgraph";

      // InMemoryStore 将数据保存到内存字典中。在生产环境中请使用基于数据库的存储。
      const store = new InMemoryStore();

      const agent = createAgent({
        model: "ollama:devstral-2",
        tools: [],
        store,
      });
      ```
    </CodeGroup>
  </Tab>

  <Tab title="PostgreSQL">
    ```shell theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
    npm install @langchain/langgraph-checkpoint-postgres
    ```

    <CodeGroup>
      ```ts Google theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
      import { createAgent } from "langchain";
      import { PostgresStore } from "@langchain/langgraph-checkpoint-postgres/store";

      const DB_URI =
        process.env.POSTGRES_URI ??
        "postgresql://postgres:postgres@localhost:5442/postgres?sslmode=disable";
      const store = PostgresStore.fromConnString(DB_URI);
      await store.setup();

      const agent = createAgent({
        model: "google-genai:gemini-3.1-pro-preview",
        tools: [],
        store,
      });
      ```

      ```ts OpenAI theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
      import { createAgent } from "langchain";
      import { PostgresStore } from "@langchain/langgraph-checkpoint-postgres/store";

      const DB_URI =
        process.env.POSTGRES_URI ??
        "postgresql://postgres:postgres@localhost:5442/postgres?sslmode=disable";
      const store = PostgresStore.fromConnString(DB_URI);
      await store.setup();

      const agent = createAgent({
        model: "openai:gpt-5.4",
        tools: [],
        store,
      });
      ```

      ```ts Anthropic theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
      import { createAgent } from "langchain";
      import { PostgresStore } from "@langchain/langgraph-checkpoint-postgres/store";

      const DB_URI =
        process.env.POSTGRES_URI ??
        "postgresql://postgres:postgres@localhost:5442/postgres?sslmode=disable";
      const store = PostgresStore.fromConnString(DB_URI);
      await store.setup();

      const agent = createAgent({
        model: "anthropic:claude-sonnet-4-6",
        tools: [],
        store,
      });
      ```

      ```ts OpenRouter theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
      import { createAgent } from "langchain";
      import { PostgresStore } from "@langchain/langgraph-checkpoint-postgres/store";

      const DB_URI =
        process.env.POSTGRES_URI ??
        "postgresql://postgres:postgres@localhost:5442/postgres?sslmode=disable";
      const store = PostgresStore.fromConnString(DB_URI);
      await store.setup();

      const agent = createAgent({
        model: "openrouter:anthropic/claude-sonnet-4-6",
        tools: [],
        store,
      });
      ```

      ```ts Fireworks theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
      import { createAgent } from "langchain";
      import { PostgresStore } from "@langchain/langgraph-checkpoint-postgres/store";

      const DB_URI =
        process.env.POSTGRES_URI ??
        "postgresql://postgres:postgres@localhost:5442/postgres?sslmode=disable";
      const store = PostgresStore.fromConnString(DB_URI);
      await store.setup();

      const agent = createAgent({
        model: "fireworks:accounts/fireworks/models/qwen3p5-397b-a17b",
        tools: [],
        store,
      });
      ```

      ```ts Baseten theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
      import { createAgent } from "langchain";
      import { PostgresStore } from "@langchain/langgraph-checkpoint-postgres/store";

      const DB_URI =
        process.env.POSTGRES_URI ??
        "postgresql://postgres:postgres@localhost:5442/postgres?sslmode=disable";
      const store = PostgresStore.fromConnString(DB_URI);
      await store.setup();

      const agent = createAgent({
        model: "baseten:zai-org/GLM-5",
        tools: [],
        store,
      });
      ```

      ```ts Ollama theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
      import { createAgent } from "langchain";
      import { PostgresStore } from "@langchain/langgraph-checkpoint-postgres/store";

      const DB_URI =
        process.env.POSTGRES_URI ??
        "postgresql://postgres:postgres@localhost:5442/postgres?sslmode=disable";
      const store = PostgresStore.fromConnString(DB_URI);
      await store.setup();

      const agent = createAgent({
        model: "ollama:devstral-2",
        tools: [],
        store,
      });
      ```
    </CodeGroup>
  </Tab>
</Tabs>

然后，工具可以使用 `runtime.store` 参数从存储中读取和写入数据。有关示例，请参阅[在工具中读取长期记忆](#read-long-term-memory-in-tools)和[从工具写入长期记忆](#write-long-term-memory-from-tools)。

<Tip>
  要深入了解记忆类型（语义、情景、程序性）以及编写记忆的策略，请参阅[记忆概念指南](/oss/javascript/concepts/memory#long-term-memory)。
</Tip>

## 记忆存储

LangGraph 将长期记忆作为 JSON 文档存储在[存储](/oss/javascript/langgraph/persistence#memory-store)中。

每条记忆都组织在一个自定义的 `namespace`（类似于文件夹）和一个独特的 `key`（类似于文件名）下。命名空间通常包含用户或组织 ID 或其他标签，以便于组织信息。

这种结构支持记忆的层次化组织。然后可以通过内容过滤器支持跨命名空间搜索。

<Tabs>
  <Tab title="InMemoryStore">
    ```ts theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
    import { InMemoryStore } from "@langchain/langgraph";

    const embed = (texts: string[]): number[][] => {
      // 替换为实际的嵌入函数或LangChain嵌入对象
      return texts.map(() => [1.0, 2.0]);
    };

    // InMemoryStore 将数据保存到内存字典中。在生产环境中请使用基于数据库的存储。
    const store = new InMemoryStore({ index: { embed, dims: 2 } });
    const userId = "my-user";
    const applicationContext = "chitchat";
    const namespace = [userId, applicationContext];

    await store.put(namespace, "a-memory", {
      rules: [
        "User likes short, direct language",
        "User only speaks English & TypeScript",
      ],
      "my-key": "my-value",
    });

    // 通过ID获取"记忆"
    const item = await store.get(namespace, "a-memory");

    // 在此命名空间内搜索"记忆"，根据内容等价性进行过滤，并按向量相似度排序
    const items = await store.search(namespace, {
      filter: { "my-key": "my-value" },
      query: "language preferences",
    });
    ```
  </Tab>

  <Tab title="PostgreSQL">
    ```ts theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
    import { PostgresStore } from "@langchain/langgraph-checkpoint-postgres/store";

    const embed = (texts: string[]): number[][] => {
      return texts.map(() => [1.0, 2.0]);
    };

    const DB_URI =
      process.env.POSTGRES_URI ??
      "postgresql://postgres:postgres@localhost:5442/postgres?sslmode=disable";
    const store = PostgresStore.fromConnString(DB_URI, {
      index: { embed, dims: 2 },
    });
    await store.setup();

    const userId = "my-user";
    const applicationContext = "chitchat";
    const namespace = [userId, applicationContext];

    await store.put(namespace, "a-memory", {
      rules: [
        "用户喜欢简短、直接的语言",
        "用户只说英语和 TypeScript",
      ],
      "my-key": "my-value",
    });

    const item = await store.get(namespace, "a-memory");
    const items = await store.search(namespace, {
      filter: { "my-key": "my-value" },
      query: "语言偏好",
    });
    ```
  </Tab>
</Tabs>

有关记忆存储的更多信息，请参阅[持久化](/oss/javascript/langgraph/persistence#memory-store)指南。

## 在工具中读取长期记忆

<Tabs>
  <Tab title="InMemoryStore">
    <CodeGroup>
      ```ts Google theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
      import * as z from "zod";
      import { createAgent, tool, type ToolRuntime } from "langchain";
      import { InMemoryStore } from "@langchain/langgraph";

      // InMemoryStore 将数据保存到内存字典中。在生产环境中请使用基于数据库的存储。
      const store = new InMemoryStore();
      const contextSchema = z.object({
        userId: z.string(),
      });

      // 使用 put 方法将示例数据写入存储
      await store.put(
        ["users"], // 用于将相关数据分组的命名空间（用户数据的 users 命名空间）
        "user_123", // 命名空间内的键（用户 ID 作为键）
        {
          name: "John Smith",
          language: "English",
        }, // 为给定用户存储的数据
      );

      const getUserInfo = tool(
        // 查找用户信息。
        async (_, runtime: ToolRuntime<unknown, z.infer<typeof contextSchema>>) => {
          // 访问存储 - 与提供给 `createAgent` 的相同
          const userId = runtime.context.userId;
          if (!userId) {
            throw new Error("userId is required");
          }
          // 从存储中检索数据 - 返回包含值和元数据的 StoreValue 对象
          const userInfo = await runtime.store.get(["users"], userId);
          return userInfo?.value ? JSON.stringify(userInfo.value) : "Unknown user";
        },
        {
          name: "getUserInfo",
          description: "根据 userId 从存储中查找用户信息。",
          schema: z.object({}),
        },
      );

      const agent = createAgent({
        model: "google-genai:gemini-3.1-pro-preview",
        tools: [getUserInfo],
        contextSchema,
        // 将存储传递给代理 - 使代理在运行工具时能够访问存储
        store,
      });

      // 运行代理
      const result = await agent.invoke(
        { messages: [{ role: "user", content: "look up user information" }] },
        { context: { userId: "user_123" } },
      );

      console.log(result.messages.at(-1)?.content);

      /**
       * 输出：
       * 用户信息：
       * - **姓名：** John Smith
       * - **语言：** English
       */
      ```

      ```ts OpenAI theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
      import * as z from "zod";
      import { createAgent, tool, type ToolRuntime } from "langchain";
      import { InMemoryStore } from "@langchain/langgraph";

      // InMemoryStore 将数据保存到内存字典中。在生产环境中请使用基于数据库的存储。
      const store = new InMemoryStore();
      const contextSchema = z.object({
        userId: z.string(),
      });

      // 使用 put 方法将示例数据写入存储
      await store.put(
        ["users"], // 用于将相关数据分组的命名空间（用户数据的 users 命名空间）
        "user_123", // 命名空间内的键（用户 ID 作为键）
        {
          name: "John Smith",
          language: "English",
        }, // 为给定用户存储的数据
      );

      const getUserInfo = tool(
        // 查找用户信息。
        async (_, runtime: ToolRuntime<unknown, z.infer<typeof contextSchema>>) => {
          // 访问存储 - 与提供给 `createAgent` 的相同
          const userId = runtime.context.userId;
          if (!userId) {
            throw new Error("userId is required");
          }
          // 从存储中检索数据 - 返回包含值和元数据的 StoreValue 对象
          const userInfo = await runtime.store.get(["users"], userId);
          return userInfo?.value ? JSON.stringify(userInfo.value) : "Unknown user";
        },
        {
          name: "getUserInfo",
          description: "根据 userId 从存储中查找用户信息。",
          schema: z.object({}),
        },
      );

      const agent = createAgent({
        model: "openai:gpt-5.4",
        tools: [getUserInfo],
        contextSchema,
        // 将存储传递给代理 - 使代理在运行工具时能够访问存储
        store,
      });

      // 运行代理
      const result = await agent.invoke(
        { messages: [{ role: "user", content: "look up user information" }] },
        { context: { userId: "user_123" } },
      );

      console.log(result.messages.at(-1)?.content);

      /**
       * 输出：
       * 用户信息：
       * - **姓名：** John Smith
       * - **语言：** English
       */
      ```

      ```ts Anthropic theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
      import * as z from "zod";
      import { createAgent, tool, type ToolRuntime } from "langchain";
      import { InMemoryStore } from "@langchain/langgraph";

      // InMemoryStore 将数据保存到内存字典中。在生产环境中请使用基于数据库的存储。
      const store = new InMemoryStore();
      const contextSchema = z.object({
        userId: z.string(),
      });

      // 使用 put 方法将示例数据写入存储
      await store.put(
        ["users"], // 用于将相关数据分组的命名空间（用户数据的 users 命名空间）
        "user_123", // 命名空间内的键（用户 ID 作为键）
        {
          name: "John Smith",
          language: "English",
        }, // 为给定用户存储的数据
      );

      const getUserInfo = tool(
        // 查找用户信息。
        async (_, runtime: ToolRuntime<unknown, z.infer<typeof contextSchema>>) => {
          // 访问存储 - 与提供给 `createAgent` 的相同
          const userId = runtime.context.userId;
          if (!userId) {
            throw new Error("userId is required");
          }
          // 从存储中检索数据 - 返回包含值和元数据的 StoreValue 对象
          const userInfo = await runtime.store.get(["users"], userId);
          return userInfo?.value ? JSON.stringify(userInfo.value) : "Unknown user";
        },
        {
          name: "getUserInfo",
          description: "根据 userId 从存储中查找用户信息。",
          schema: z.object({}),
        },
      );

      const agent = createAgent({
        model: "anthropic:claude-sonnet-4-6",
        tools: [getUserInfo],
        contextSchema,
        // 将存储传递给代理 - 使代理在运行工具时能够访问存储
        store,
      });

      // 运行代理
      const result = await agent.invoke(
        { messages: [{ role: "user", content: "look up user information" }] },
        { context: { userId: "user_123" } },
      );

      console.log(result.messages.at(-1)?.content);

      /**
       * 输出：
       * 用户信息：
       * - **姓名：** John Smith
       * - **语言：** English
       */
      ```

      ```ts OpenRouter theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
      import * as z from "zod";
      import { createAgent, tool, type ToolRuntime } from "langchain";
      import { InMemoryStore } from "@langchain/langgraph";

      // InMemoryStore 将数据保存到内存字典中。在生产环境中请使用基于数据库的存储。
      const store = new InMemoryStore();
      const contextSchema = z.object({
        userId: z.string(),
      });

      // 使用 put 方法将示例数据写入存储
      await store.put(
        ["users"], // 用于将相关数据分组的命名空间（用户数据的 users 命名空间）
        "user_123", // 命名空间内的键（用户 ID 作为键）
        {
          name: "John Smith",
          language: "English",
        }, // 为给定用户存储的数据
      );

      const getUserInfo = tool(
        // 查找用户信息。
        async (_, runtime: ToolRuntime<unknown, z.infer<typeof contextSchema>>) => {
          // 访问存储 - 与提供给 `createAgent` 的相同
          const userId = runtime.context.userId;
          if (!userId) {
            throw new Error("userId is required");
          }
          // 从存储中检索数据 - 返回包含值和元数据的 StoreValue 对象
          const userInfo = await runtime.store.get(["users"], userId);
          return userInfo?.value ? JSON.stringify(userInfo.value) : "Unknown user";
        },
        {
          name: "getUserInfo",
          description: "根据 userId 从存储中查找用户信息。",
          schema: z.object({}),
        },
      );

      const agent = createAgent({
        model: "openrouter:anthropic/claude-sonnet-4-6",
        tools: [getUserInfo],
        contextSchema,
        // 将存储传递给代理 - 使代理在运行工具时能够访问存储
        store,
      });

      // 运行代理
      const result = await agent.invoke(
        { messages: [{ role: "user", content: "look up user information" }] },
        { context: { userId: "user_123" } },
      );

      console.log(result.messages.at(-1)?.content);

      /**
       * 输出：
       * 用户信息：
       * - **姓名：** John Smith
       * - **语言：** English
       */
      ```

      ```ts Fireworks theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
      import * as z from "zod";
      import { createAgent, tool, type ToolRuntime } from "langchain";
      import { InMemoryStore } from "@langchain/langgraph";

      // InMemoryStore 将数据保存到内存字典中。在生产环境中请使用基于数据库的存储。
      const store = new InMemoryStore();
      const contextSchema = z.object({
        userId: z.string(),
      });

      // 使用 put 方法将示例数据写入存储
      await store.put(
        ["users"], // 用于将相关数据分组的命名空间（用户数据的 users 命名空间）
        "user_123", // 命名空间内的键（用户 ID 作为键）
        {
          name: "John Smith",
          language: "English",
        }, // 为给定用户存储的数据
      );

      const getUserInfo = tool(
        // 查找用户信息。
        async (_, runtime: ToolRuntime<unknown, z.infer<typeof contextSchema>>) => {
          // 访问存储 - 与提供给 `createAgent` 的相同
          const userId = runtime.context.userId;
          if (!userId) {
            throw new Error("userId is required");
          }
          // 从存储中检索数据 - 返回包含值和元数据的 StoreValue 对象
          const userInfo = await runtime.store.get(["users"], userId);
          return userInfo?.value ? JSON.stringify(userInfo.value) : "Unknown user";
        },
        {
          name: "getUserInfo",
          description: "根据 userId 从存储中查找用户信息。",
          schema: z.object({}),
        },
      );

      const agent = createAgent({
        model: "fireworks:accounts/fireworks/models/qwen3p5-397b-a17b",
        tools: [getUserInfo],
        contextSchema,
        // 将存储传递给代理 - 使代理在运行工具时能够访问存储
        store,
      });

      // 运行代理
      const result = await agent.invoke(
        { messages: [{ role: "user", content: "look up user information" }] },
        { context: { userId: "user_123" } },
      );

      console.log(result.messages.at(-1)?.content);

      /**
       * 输出：
       * 用户信息：
       * - **姓名：** John Smith
       * - **语言：** English
       */
      ```

      ```ts Baseten theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
      import * as z from "zod";
      import { createAgent, tool, type ToolRuntime } from "langchain";
      import { InMemoryStore } from "@langchain/langgraph";

      // InMemoryStore 将数据保存到内存字典中。在生产环境中请使用基于数据库的存储。
      const store = new InMemoryStore();
      const contextSchema = z.object({
        userId: z.string(),
      });

      // 使用 put 方法将示例数据写入存储
      await store.put(
        ["users"], // 用于将相关数据分组的命名空间（用户数据的 users 命名空间）
        "user_123", // 命名空间内的键（用户 ID 作为键）
        {
          name: "John Smith",
          language: "English",
        }, // 为给定用户存储的数据
      );

      const getUserInfo = tool(
        // 查找用户信息。
        async (_, runtime: ToolRuntime<unknown, z.infer<typeof contextSchema>>) => {
          // 访问存储 - 与提供给 `createAgent` 的相同
          const userId = runtime.context.userId;
          if (!userId) {
            throw new Error("userId is required");
          }
          // 从存储中检索数据 - 返回包含值和元数据的 StoreValue 对象
          const userInfo = await runtime.store.get(["users"], userId);
          return userInfo?.value ? JSON.stringify(userInfo.value) : "Unknown user";
        },
        {
          name: "getUserInfo",
          description: "根据 userId 从存储中查找用户信息。",
          schema: z.object({}),
        },
      );

      const agent = createAgent({
        model: "baseten:zai-org/GLM-5",
        tools: [getUserInfo],
        contextSchema,
        // 将存储传递给代理 - 使代理在运行工具时能够访问存储
        store,
      });

      // 运行代理
      const result = await agent.invoke(
        { messages: [{ role: "user", content: "look up user information" }] },
        { context: { userId: "user_123" } },
      );

      console.log(result.messages.at(-1)?.content);

      /**
       * 输出：
       * 用户信息：
       * - **姓名：** John Smith
       * - **语言：** English
       */
      ```

      ```ts Ollama theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
      import * as z from "zod";
      import { createAgent, tool, type ToolRuntime } from "langchain";
      import { InMemoryStore } from "@langchain/langgraph";

      // InMemoryStore 将数据保存到内存字典中。在生产环境中请使用基于数据库的存储。
      const store = new InMemoryStore();
      const contextSchema = z.object({
        userId: z.string(),
      });

      // 使用 put 方法将示例数据写入存储
      await store.put(
        ["users"], // 用于将相关数据分组的命名空间（用户数据的 users 命名空间）
        "user_123", // 命名空间内的键（用户 ID 作为键）
        {
          name: "John Smith",
          language: "English",
        }, // 为给定用户存储的数据
      );

      const getUserInfo = tool(
        // 查找用户信息。
        async (_, runtime: ToolRuntime<unknown, z.infer<typeof contextSchema>>) => {
          // 访问存储 - 与提供给 `createAgent` 的相同
          const userId = runtime.context.userId;
          if (!userId) {
            throw new Error("userId is required");
          }
          // 从存储中检索数据 - 返回包含值和元数据的 StoreValue 对象
          const userInfo = await runtime.store.get(["users"], userId);
          return userInfo?.value ? JSON.stringify(userInfo.value) : "Unknown user";
        },
        {
          name: "getUserInfo",
          description: "根据 userId 从存储中查找用户信息。",
          schema: z.object({}),
        },
      );

      const agent = createAgent({
        model: "ollama:devstral-2",
        tools: [getUserInfo],
        contextSchema,
        // 将存储传递给代理 - 使代理在运行工具时能够访问存储
        store,
      });

      // 运行代理
      const result = await agent.invoke(
        { messages: [{ role: "user", content: "look up user information" }] },
        { context: { userId: "user_123" } },
      );

      console.log(result.messages.at(-1)?.content);

      /**
       * 输出：
       * 用户信息：
       * - **姓名：** John Smith
       * - **语言：** English
       */
      ```
    </CodeGroup>
  </Tab>

  <Tab title="PostgreSQL">
    <CodeGroup>
      ```ts Google theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
      import * as z from "zod";
      import { createAgent, tool, type ToolRuntime } from "langchain";
      import { PostgresStore } from "@langchain/langgraph-checkpoint-postgres/store";

      const DB_URI =
        process.env.POSTGRES_URI ??
        "postgresql://postgres:postgres@localhost:5442/postgres?sslmode=disable";
      const store = PostgresStore.fromConnString(DB_URI);
      await store.setup();

      const contextSchema = z.object({ userId: z.string() });

      await store.put(["users"], "user_123", {
        name: "John Smith",
        language: "English",
      });

      const getUserInfo = tool(
        async (_, runtime: ToolRuntime<unknown, z.infer<typeof contextSchema>>) => {
          const userId = runtime.context.userId;
          if (!userId) throw new Error("userId is required");
          const userInfo = await runtime.store.get(["users"], userId);
          return userInfo?.value ? JSON.stringify(userInfo.value) : "Unknown user";
        },
        {
          name: "getUserInfo",
          description: "Look up user info by userId from the store.",
          schema: z.object({}),
        },
      );

      const agent = createAgent({
        model: "google-genai:gemini-3.1-pro-preview",
        tools: [getUserInfo],
        contextSchema,
        store,
      });

      await agent.invoke(
        { messages: [{ role: "user", content: "look up user information" }] },
        { context: { userId: "user_123" } },
      );
      ```

      ```ts OpenAI theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
      import * as z from "zod";
      import { createAgent, tool, type ToolRuntime } from "langchain";
      import { PostgresStore } from "@langchain/langgraph-checkpoint-postgres/store";

      const DB_URI =
        process.env.POSTGRES_URI ??
        "postgresql://postgres:postgres@localhost:5442/postgres?sslmode=disable";
      const store = PostgresStore.fromConnString(DB_URI);
      await store.setup();

      const contextSchema = z.object({ userId: z.string() });

      await store.put(["users"], "user_123", {
        name: "John Smith",
        language: "English",
      });

      const getUserInfo = tool(
        async (_, runtime: ToolRuntime<unknown, z.infer<typeof contextSchema>>) => {
          const userId = runtime.context.userId;
          if (!userId) throw new Error("userId is required");
          const userInfo = await runtime.store.get(["users"], userId);
          return userInfo?.value ? JSON.stringify(userInfo.value) : "Unknown user";
        },
        {
          name: "getUserInfo",
          description: "Look up user info by userId from the store.",
          schema: z.object({}),
        },
      );

      const agent = createAgent({
        model: "openai:gpt-5.4",
        tools: [getUserInfo],
        contextSchema,
        store,
      });

      await agent.invoke(
        { messages: [{ role: "user", content: "look up user information" }] },
        { context: { userId: "user_123" } },
      );
      ```

      ```ts Anthropic theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
      import * as z from "zod";
      import { createAgent, tool, type ToolRuntime } from "langchain";
      import { PostgresStore } from "@langchain/langgraph-checkpoint-postgres/store";

      const DB_URI =
        process.env.POSTGRES_URI ??
        "postgresql://postgres:postgres@localhost:5442/postgres?sslmode=disable";
      const store = PostgresStore.fromConnString(DB_URI);
      await store.setup();

      const contextSchema = z.object({ userId: z.string() });

      await store.put(["users"], "user_123", {
        name: "John Smith",
        language: "English",
      });

      const getUserInfo = tool(
        async (_, runtime: ToolRuntime<unknown, z.infer<typeof contextSchema>>) => {
          const userId = runtime.context.userId;
          if (!userId) throw new Error("userId is required");
          const userInfo = await runtime.store.get(["users"], userId);
          return userInfo?.value ? JSON.stringify(userInfo.value) : "Unknown user";
        },
        {
          name: "getUserInfo",
          description: "Look up user info by userId from the store.",
          schema: z.object({}),
        },
      );

      const agent = createAgent({
        model: "anthropic:claude-sonnet-4-6",
        tools: [getUserInfo],
        contextSchema,
        store,
      });

      await agent.invoke(
        { messages: [{ role: "user", content: "look up user information" }] },
        { context: { userId: "user_123" } },
      );
      ```

      ```ts OpenRouter theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
      import * as z from "zod";
      import { createAgent, tool, type ToolRuntime } from "langchain";
      import { PostgresStore } from "@langchain/langgraph-checkpoint-postgres/store";

      const DB_URI =
        process.env.POSTGRES_URI ??
        "postgresql://postgres:postgres@localhost:5442/postgres?sslmode=disable";
      const store = PostgresStore.fromConnString(DB_URI);
      await store.setup();

      const contextSchema = z.object({ userId: z.string() });

      await store.put(["users"], "user_123", {
        name: "John Smith",
        language: "English",
      });

      const getUserInfo = tool(
        async (_, runtime: ToolRuntime<unknown, z.infer<typeof contextSchema>>) => {
          const userId = runtime.context.userId;
          if (!userId) throw new Error("userId is required");
          const userInfo = await runtime.store.get(["users"], userId);
          return userInfo?.value ? JSON.stringify(userInfo.value) : "Unknown user";
        },
        {
          name: "getUserInfo",
          description: "Look up user info by userId from the store.",
          schema: z.object({}),
        },
      );

      const agent = createAgent({
        model: "openrouter:anthropic/claude-sonnet-4-6",
        tools: [getUserInfo],
        contextSchema,
        store,
      });

      await agent.invoke(
        { messages: [{ role: "user", content: "look up user information" }] },
        { context: { userId: "user_123" } },
      );
      ```

      ```ts Fireworks theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
      import * as z from "zod";
      import { createAgent, tool, type ToolRuntime } from "langchain";
      import { PostgresStore } from "@langchain/langgraph-checkpoint-postgres/store";

      const DB_URI =
        process.env.POSTGRES_URI ??
        "postgresql://postgres:postgres@localhost:5442/postgres?sslmode=disable";
      const store = PostgresStore.fromConnString(DB_URI);
      await store.setup();

      const contextSchema = z.object({ userId: z.string() });

      await store.put(["users"], "user_123", {
        name: "John Smith",
        language: "English",
      });

      const getUserInfo = tool(
        async (_, runtime: ToolRuntime<unknown, z.infer<typeof contextSchema>>) => {
          const userId = runtime.context.userId;
          if (!userId) throw new Error("userId is required");
          const userInfo = await runtime.store.get(["users"], userId);
          return userInfo?.value ? JSON.stringify(userInfo.value) : "Unknown user";
        },
        {
          name: "getUserInfo",
          description: "Look up user info by userId from the store.",
          schema: z.object({}),
        },
      );

      const agent = createAgent({
        model: "fireworks:accounts/fireworks/models/qwen3p5-397b-a17b",
        tools: [getUserInfo],
        contextSchema,
        store,
      });

      await agent.invoke(
        { messages: [{ role: "user", content: "look up user information" }] },
        { context: { userId: "user_123" } },
      );
      ```

      ```ts Baseten theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
      import * as z from "zod";
      import { createAgent, tool, type ToolRuntime } from "langchain";
      import { PostgresStore } from "@langchain/langgraph-checkpoint-postgres/store";

      const DB_URI =
        process.env.POSTGRES_URI ??
        "postgresql://postgres:postgres@localhost:5442/postgres?sslmode=disable";
      const store = PostgresStore.fromConnString(DB_URI);
      await store.setup();

      const contextSchema = z.object({ userId: z.string() });

      await store.put(["users"], "user_123", {
        name: "John Smith",
        language: "English",
      });

      const getUserInfo = tool(
        async (_, runtime: ToolRuntime<unknown, z.infer<typeof contextSchema>>) => {
          const userId = runtime.context.userId;
          if (!userId) throw new Error("userId is required");
          const userInfo = await runtime.store.get(["users"], userId);
          return userInfo?.value ? JSON.stringify(userInfo.value) : "Unknown user";
        },
        {
          name: "getUserInfo",
          description: "Look up user info by userId from the store.",
          schema: z.object({}),
        },
      );

      const agent = createAgent({
        model: "baseten:zai-org/GLM-5",
        tools: [getUserInfo],
        contextSchema,
        store,
      });

      await agent.invoke(
        { messages: [{ role: "user", content: "look up user information" }] },
        { context: { userId: "user_123" } },
      );
      ```

      ```ts Ollama theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
      import * as z from "zod";
      import { createAgent, tool, type ToolRuntime } from "langchain";
      import { PostgresStore } from "@langchain/langgraph-checkpoint-postgres/store";

      const DB_URI =
        process.env.POSTGRES_URI ??
        "postgresql://postgres:postgres@localhost:5442/postgres?sslmode=disable";
      const store = PostgresStore.fromConnString(DB_URI);
      await store.setup();

      const contextSchema = z.object({ userId: z.string() });

      await store.put(["users"], "user_123", {
        name: "John Smith",
        language: "English",
      });

      const getUserInfo = tool(
        async (_, runtime: ToolRuntime<unknown, z.infer<typeof contextSchema>>) => {
          const userId = runtime.context.userId;
          if (!userId) throw new Error("userId is required");
          const userInfo = await runtime.store.get(["users"], userId);
          return userInfo?.value ? JSON.stringify(userInfo.value) : "Unknown user";
        },
        {
          name: "getUserInfo",
          description: "Look up user info by userId from the store.",
          schema: z.object({}),
        },
      );

      const agent = createAgent({
        model: "ollama:devstral-2",
        tools: [getUserInfo],
        contextSchema,
        store,
      });

      await agent.invoke(
        { messages: [{ role: "user", content: "look up user information" }] },
        { context: { userId: "user_123" } },
      );
      ```
    </CodeGroup>
  </Tab>
</Tabs>

<a id="write-long-term" />

## 从工具写入长期记忆

<Tabs>
  <Tab title="InMemoryStore">
    <CodeGroup>
      ```ts Google theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
      import * as z from "zod";
      import { tool, createAgent, type ToolRuntime } from "langchain";
      import { InMemoryStore } from "@langchain/langgraph";

      // InMemoryStore 将数据保存到内存字典中。在生产环境中请使用基于数据库的存储。
      const store = new InMemoryStore();

      const contextSchema = z.object({
        userId: z.string(),
      });

      // Schema 定义了 LLM 使用的用户信息结构
      const UserInfo = z.object({
        name: z.string(),
      });

      // 允许代理更新用户信息的工具（对聊天应用很有用）
      const saveUserInfo = tool(
        async (
          userInfo: z.infer<typeof UserInfo>,
          runtime: ToolRuntime<unknown, z.infer<typeof contextSchema>>,
        ) => {
          const userId = runtime.context.userId;
          if (!userId) {
            throw new Error("userId is required");
          }
          // 将数据存储在存储中（命名空间，键，数据）
          await runtime.store.put(["users"], userId, userInfo);
          return "Successfully saved user info.";
        },
        {
          name: "save_user_info",
          description: "Save user info",
          schema: UserInfo,
        },
      );

      const agent = createAgent({
        model: "google-genai:gemini-3.1-pro-preview",
        tools: [saveUserInfo],
        contextSchema,
        store,
      });

      // 运行代理
      await agent.invoke(
        { messages: [{ role: "user", content: "My name is John Smith" }] },
        // 在上下文中传递 userId 以标识正在更新谁的信息
        { context: { userId: "user_123" } },
      );

      // 你可以直接访问存储来获取值
      const result = await store.get(["users"], "user_123");
      console.log(result?.value); // 输出: { name: "John Smith" }
      ```

      ```ts OpenAI theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
      import * as z from "zod";
      import { tool, createAgent, type ToolRuntime } from "langchain";
      import { InMemoryStore } from "@langchain/langgraph";

      // InMemoryStore 将数据保存到内存字典中。在生产环境中请使用基于数据库的存储。
      const store = new InMemoryStore();

      const contextSchema = z.object({
        userId: z.string(),
      });

      // Schema 定义了 LLM 使用的用户信息结构
      const UserInfo = z.object({
        name: z.string(),
      });

      // 允许代理更新用户信息的工具（对聊天应用很有用）
      const saveUserInfo = tool(
        async (
          userInfo: z.infer<typeof UserInfo>,
          runtime: ToolRuntime<unknown, z.infer<typeof contextSchema>>,
        ) => {
          const userId = runtime.context.userId;
          if (!userId) {
            throw new Error("userId is required");
          }
          // 将数据存储在存储中（命名空间，键，数据）
          await runtime.store.put(["users"], userId, userInfo);
          return "Successfully saved user info.";
        },
        {
          name: "save_user_info",
          description: "Save user info",
          schema: UserInfo,
        },
      );

      const agent = createAgent({
        model: "openai:gpt-5.4",
        tools: [saveUserInfo],
        contextSchema,
        store,
      });

      // 运行代理
      await agent.invoke(
        { messages: [{ role: "user", content: "My name is John Smith" }] },
        // 在上下文中传递 userId 以标识正在更新谁的信息
        { context: { userId: "user_123" } },
      );

      // 你可以直接访问存储来获取值
      const result = await store.get(["users"], "user_123");
      console.log(result?.value); // 输出: { name: "John Smith" }
      ```

      ```ts Anthropic theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
      import * as z from "zod";
      import { tool, createAgent, type ToolRuntime } from "langchain";
      import { InMemoryStore } from "@langchain/langgraph";

      // InMemoryStore 将数据保存到内存字典中。在生产环境中请使用基于数据库的存储。
      const store = new InMemoryStore();

      const contextSchema = z.object({
        userId: z.string(),
      });

      // Schema 定义了 LLM 使用的用户信息结构
      const UserInfo = z.object({
        name: z.string(),
      });

      // 允许代理更新用户信息的工具（对聊天应用很有用）
      const saveUserInfo = tool(
        async (
          userInfo: z.infer<typeof UserInfo>,
          runtime: ToolRuntime<unknown, z.infer<typeof contextSchema>>,
        ) => {
          const userId = runtime.context.userId;
          if (!userId) {
            throw new Error("userId is required");
          }
          // 将数据存储在存储中（命名空间，键，数据）
          await runtime.store.put(["users"], userId, userInfo);
          return "Successfully saved user info.";
        },
        {
          name: "save_user_info",
          description: "Save user info",
          schema: UserInfo,
        },
      );

      const agent = createAgent({
        model: "anthropic:claude-sonnet-4-6",
        tools: [saveUserInfo],
        contextSchema,
        store,
      });

      // 运行代理
      await agent.invoke(
        { messages: [{ role: "user", content: "My name is John Smith" }] },
        // 在上下文中传递 userId 以标识正在更新谁的信息
        { context: { userId: "user_123" } },
      );

      // 你可以直接访问存储来获取值
      const result = await store.get(["users"], "user_123");
      console.log(result?.value); // 输出: { name: "John Smith" }
      ```

      ```ts OpenRouter theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
      import * as z from "zod";
      import { tool, createAgent, type ToolRuntime } from "langchain";
      import { InMemoryStore } from "@langchain/langgraph";

      // InMemoryStore 将数据保存到内存字典中。在生产环境中请使用基于数据库的存储。
      const store = new InMemoryStore();

      const contextSchema = z.object({
        userId: z.string(),
      });

      // Schema 定义了 LLM 使用的用户信息结构
      const UserInfo = z.object({
        name: z.string(),
      });

      // 允许代理更新用户信息的工具（对聊天应用很有用）
      const saveUserInfo = tool(
        async (
          userInfo: z.infer<typeof UserInfo>,
          runtime: ToolRuntime<unknown, z.infer<typeof contextSchema>>,
        ) => {
          const userId = runtime.context.userId;
          if (!userId) {
            throw new Error("userId is required");
          }
          // 将数据存储在存储中（命名空间，键，数据）
          await runtime.store.put(["users"], userId, userInfo);
          return "Successfully saved user info.";
        },
        {
          name: "save_user_info",
          description: "Save user info",
          schema: UserInfo,
        },
      );

      const agent = createAgent({
        model: "openrouter:anthropic/claude-sonnet-4-6",
        tools: [saveUserInfo],
        contextSchema,
        store,
      });

      // 运行代理
      await agent.invoke(
        { messages: [{ role: "user", content: "My name is John Smith" }] },
        // 在上下文中传递 userId 以标识正在更新谁的信息
        { context: { userId: "user_123" } },
      );

      // 你可以直接访问存储来获取值
      const result = await store.get(["users"], "user_123");
      console.log(result?.value); // 输出: { name: "John Smith" }
      ```

      ```ts Fireworks theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
      import * as z from "zod";
      import { tool, createAgent, type ToolRuntime } from "langchain";
      import { InMemoryStore } from "@langchain/langgraph";

      // InMemoryStore 将数据保存到内存字典中。在生产环境中请使用基于数据库的存储。
      const store = new InMemoryStore();

      const contextSchema = z.object({
        userId: z.string(),
      });

      // Schema 定义了 LLM 使用的用户信息结构
      const UserInfo = z.object({
        name: z.string(),
      });

      // 允许代理更新用户信息的工具（对聊天应用很有用）
      const saveUserInfo = tool(
        async (
          userInfo: z.infer<typeof UserInfo>,
          runtime: ToolRuntime<unknown, z.infer<typeof contextSchema>>,
        ) => {
          const userId = runtime.context.userId;
          if (!userId) {
            throw new Error("userId is required");
          }
          // 将数据存储在存储中（命名空间，键，数据）
          await runtime.store.put(["users"], userId, userInfo);
          return "Successfully saved user info.";
        },
        {
          name: "save_user_info",
          description: "Save user info",
          schema: UserInfo,
        },
      );

      const agent = createAgent({
        model: "fireworks:accounts/fireworks/models/qwen3p5-397b-a17b",
        tools: [saveUserInfo],
        contextSchema,
        store,
      });

      // 运行代理
      await agent.invoke(
        { messages: [{ role: "user", content: "My name is John Smith" }] },
        // 在上下文中传递 userId 以标识正在更新谁的信息
        { context: { userId: "user_123" } },
      );

      // 你可以直接访问存储来获取值
      const result = await store.get(["users"], "user_123");
      console.log(result?.value); // 输出: { name: "John Smith" }
      ```

      ```ts Baseten theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
      import * as z from "zod";
      import { tool, createAgent, type ToolRuntime } from "langchain";
      import { InMemoryStore } from "@langchain/langgraph";

      // InMemoryStore 将数据保存到内存字典中。在生产环境中请使用基于数据库的存储。
      const store = new InMemoryStore();

      const contextSchema = z.object({
        userId: z.string(),
      });

      // Schema 定义了 LLM 使用的用户信息结构
      const UserInfo = z.object({
        name: z.string(),
      });

      // 允许代理更新用户信息的工具（对聊天应用很有用）
      const saveUserInfo = tool(
        async (
          userInfo: z.infer<typeof UserInfo>,
          runtime: ToolRuntime<unknown, z.infer<typeof contextSchema>>,
        ) => {
          const userId = runtime.context.userId;
          if (!userId) {
            throw new Error("userId is required");
          }
          // 将数据存储在存储中（命名空间，键，数据）
          await runtime.store.put(["users"], userId, userInfo);
          return "Successfully saved user info.";
        },
        {
          name: "save_user_info",
          description: "Save user info",
          schema: UserInfo,
        },
      );

      const agent = createAgent({
        model: "baseten:zai-org/GLM-5",
        tools: [saveUserInfo],
        contextSchema,
        store,
      });

      // 运行代理
      await agent.invoke(
        { messages: [{ role: "user", content: "My name is John Smith" }] },
        // 在上下文中传递 userId 以标识正在更新谁的信息
        { context: { userId: "user_123" } },
      );

      // 你可以直接访问存储来获取值
      const result = await store.get(["users"], "user_123");
      console.log(result?.value); // 输出: { name: "John Smith" }
      ```

      ```ts Ollama theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
      import * as z from "zod";
      import { tool, createAgent, type ToolRuntime } from "langchain";
      import { InMemoryStore } from "@langchain/langgraph";

      // InMemoryStore 将数据保存到内存字典中。在生产环境中请使用基于数据库的存储。
      const store = new InMemoryStore();

      const contextSchema = z.object({
        userId: z.string(),
      });

      // Schema 定义了 LLM 使用的用户信息结构
      const UserInfo = z.object({
        name: z.string(),
      });

      // 允许代理更新用户信息的工具（对聊天应用很有用）
      const saveUserInfo = tool(
        async (
          userInfo: z.infer<typeof UserInfo>,
          runtime: ToolRuntime<unknown, z.infer<typeof contextSchema>>,
        ) => {
          const userId = runtime.context.userId;
          if (!userId) {
            throw new Error("userId is required");
          }
          // 将数据存储在存储中（命名空间，键，数据）
          await runtime.store.put(["users"], userId, userInfo);
          return "Successfully saved user info.";
        },
        {
          name: "save_user_info",
          description: "Save user info",
          schema: UserInfo,
        },
      );

      const agent = createAgent({
        model: "ollama:devstral-2",
        tools: [saveUserInfo],
        contextSchema,
        store,
      });

      // 运行代理
      await agent.invoke(
        { messages: [{ role: "user", content: "My name is John Smith" }] },
        // 在上下文中传递 userId 以标识正在更新谁的信息
        { context: { userId: "user_123" } },
      );

      // 你可以直接访问存储来获取值
      const result = await store.get(["users"], "user_123");
      console.log(result?.value); // 输出: { name: "John Smith" }
      ```
    </CodeGroup>
  </Tab>

  <Tab title="PostgreSQL">
    <CodeGroup>
      ```ts Google theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
      import * as z from "zod";
      import { tool, createAgent, type ToolRuntime } from "langchain";
      import { PostgresStore } from "@langchain/langgraph-checkpoint-postgres/store";

      const DB_URI =
        process.env.POSTGRES_URI ??
        "postgresql://postgres:postgres@localhost:5442/postgres?sslmode=disable";
      const store = PostgresStore.fromConnString(DB_URI);
      await store.setup();

      const contextSchema = z.object({ userId: z.string() });

      const UserInfo = z.object({ name: z.string() });

      const saveUserInfo = tool(
        async (
          userInfo: z.infer<typeof UserInfo>,
          runtime: ToolRuntime<unknown, z.infer<typeof contextSchema>>,
        ) => {
          const userId = runtime.context.userId;
          if (!userId) throw new Error("userId is required");
          await runtime.store.put(["users"], userId, userInfo);
          return "Successfully saved user info.";
        },
        { name: "save_user_info", description: "Save user info", schema: UserInfo },
      );

      const agent = createAgent({
        model: "google-genai:gemini-3.1-pro-preview",
        tools: [saveUserInfo],
        contextSchema,
        store,
      });

      await agent.invoke(
        { messages: [{ role: "user", content: "My name is John Smith" }] },
        { context: { userId: "user_123" } },
      );

      const result = await store.get(["users"], "user_123");
      console.log(result?.value);
      ```

      ```ts OpenAI theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
      import * as z from "zod";
      import { tool, createAgent, type ToolRuntime } from "langchain";
      import { PostgresStore } from "@langchain/langgraph-checkpoint-postgres/store";

      const DB_URI =
        process.env.POSTGRES_URI ??
        "postgresql://postgres:postgres@localhost:5442/postgres?sslmode=disable";
      const store = PostgresStore.fromConnString(DB_URI);
      await store.setup();

      const contextSchema = z.object({ userId: z.string() });

      const UserInfo = z.object({ name: z.string() });

      const saveUserInfo = tool(
        async (
          userInfo: z.infer<typeof UserInfo>,
          runtime: ToolRuntime<unknown, z.infer<typeof contextSchema>>,
        ) => {
          const userId = runtime.context.userId;
          if (!userId) throw new Error("userId is required");
          await runtime.store.put(["users"], userId, userInfo);
          return "Successfully saved user info.";
        },
        { name: "save_user_info", description: "Save user info", schema: UserInfo },
      );

      const agent = createAgent({
        model: "openai:gpt-5.4",
        tools: [saveUserInfo],
        contextSchema,
        store,
      });

      await agent.invoke(
        { messages: [{ role: "user", content: "My name is John Smith" }] },
        { context: { userId: "user_123" } },
      );

      const result = await store.get(["users"], "user_123");
      console.log(result?.value);
      ```

      ```ts Anthropic theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
      import * as z from "zod";
      import { tool, createAgent, type ToolRuntime } from "langchain";
      import { PostgresStore } from "@langchain/langgraph-checkpoint-postgres/store";

      const DB_URI =
        process.env.POSTGRES_URI ??
        "postgresql://postgres:postgres@localhost:5442/postgres?sslmode=disable";
      const store = PostgresStore.fromConnString(DB_URI);
      await store.setup();

      const contextSchema = z.object({ userId: z.string() });

      const UserInfo = z.object({ name: z.string() });

      const saveUserInfo = tool(
        async (
          userInfo: z.infer<typeof UserInfo>,
          runtime: ToolRuntime<unknown, z.infer<typeof contextSchema>>,
        ) => {
          const userId = runtime.context.userId;
          if (!userId) throw new Error("userId is required");
          await runtime.store.put(["users"], userId, userInfo);
          return "Successfully saved user info.";
        },
        { name: "save_user_info", description: "Save user info", schema: UserInfo },
      );

      const agent = createAgent({
        model: "anthropic:claude-sonnet-4-6",
        tools: [saveUserInfo],
        contextSchema,
        store,
      });

      await agent.invoke(
        { messages: [{ role: "user", content: "My name is John Smith" }] },
        { context: { userId: "user_123" } },
      );

      const result = await store.get(["users"], "user_123");
      console.log(result?.value);
      ```

      ```ts OpenRouter theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
      import * as z from "zod";
      import { tool, createAgent, type ToolRuntime } from "langchain";
      import { PostgresStore } from "@langchain/langgraph-checkpoint-postgres/store";

      const DB_URI =
        process.env.POSTGRES_URI ??
        "postgresql://postgres:postgres@localhost:5442/postgres?sslmode=disable";
      const store = PostgresStore.fromConnString(DB_URI);
      await store.setup();

      const contextSchema = z.object({ userId: z.string() });

      const UserInfo = z.object({ name: z.string() });

      const saveUserInfo = tool(
        async (
          userInfo: z.infer<typeof UserInfo>,
          runtime: ToolRuntime<unknown, z.infer<typeof contextSchema>>,
        ) => {
          const userId = runtime.context.userId;
          if (!userId) throw new Error("userId is required");
          await runtime.store.put(["users"], userId, userInfo);
          return "Successfully saved user info.";
        },
        { name: "save_user_info", description: "Save user info", schema: UserInfo },
      );

      const agent = createAgent({
        model: "openrouter:anthropic/claude-sonnet-4-6",
        tools: [saveUserInfo],
        contextSchema,
        store,
      });

      await agent.invoke(
        { messages: [{ role: "user", content: "My name is John Smith" }] },
        { context: { userId: "user_123" } },
      );

      const result = await store.get(["users"], "user_123");
      console.log(result?.value);
      ```

      ```ts Fireworks theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
      import * as z from "zod";
      import { tool, createAgent, type ToolRuntime } from "langchain";
      import { PostgresStore } from "@langchain/langgraph-checkpoint-postgres/store";

      const DB_URI =
        process.env.POSTGRES_URI ??
        "postgresql://postgres:postgres@localhost:5442/postgres?sslmode=disable";
      const store = PostgresStore.fromConnString(DB_URI);
      await store.setup();

      const contextSchema = z.object({ userId: z.string() });

      const UserInfo = z.object({ name: z.string() });

      const saveUserInfo = tool(
        async (
          userInfo: z.infer<typeof UserInfo>,
          runtime: ToolRuntime<unknown, z.infer<typeof contextSchema>>,
        ) => {
          const userId = runtime.context.userId;
          if (!userId) throw new Error("userId is required");
          await runtime.store.put(["users"], userId, userInfo);
          return "Successfully saved user info.";
        },
        { name: "save_user_info", description: "Save user info", schema: UserInfo },
      );

      const agent = createAgent({
        model: "fireworks:accounts/fireworks/models/qwen3p5-397b-a17b",
        tools: [saveUserInfo],
        contextSchema,
        store,
      });

      await agent.invoke(
        { messages: [{ role: "user", content: "My name is John Smith" }] },
        { context: { userId: "user_123" } },
      );

      const result = await store.get(["users"], "user_123");
      console.log(result?.value);
      ```

      ```ts Baseten theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
      import * as z from "zod";
      import { tool, createAgent, type ToolRuntime } from "langchain";
      import { PostgresStore } from "@langchain/langgraph-checkpoint-postgres/store";

      const DB_URI =
        process.env.POSTGRES_URI ??
        "postgresql://postgres:postgres@localhost:5442/postgres?sslmode=disable";
      const store = PostgresStore.fromConnString(DB_URI);
      await store.setup();

      const contextSchema = z.object({ userId: z.string() });

      const UserInfo = z.object({ name: z.string() });

      const saveUserInfo = tool(
        async (
          userInfo: z.infer<typeof UserInfo>,
          runtime: ToolRuntime<unknown, z.infer<typeof contextSchema>>,
        ) => {
          const userId = runtime.context.userId;
          if (!userId) throw new Error("userId is required");
          await runtime.store.put(["users"], userId, userInfo);
          return "Successfully saved user info.";
        },
        { name: "save_user_info", description: "Save user info", schema: UserInfo },
      );

      const agent = createAgent({
        model: "baseten:zai-org/GLM-5",
        tools: [saveUserInfo],
        contextSchema,
        store,
      });

      await agent.invoke(
        { messages: [{ role: "user", content: "My name is John Smith" }] },
        { context: { userId: "user_123" } },
      );

      const result = await store.get(["users"], "user_123");
      console.log(result?.value);
      ```

      ```ts Ollama theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
      import * as z from "zod";
      import { tool, createAgent, type ToolRuntime } from "langchain";
      import { PostgresStore } from "@langchain/langgraph-checkpoint-postgres/store";

      const DB_URI =
        process.env.POSTGRES_URI ??
        "postgresql://postgres:postgres@localhost:5442/postgres?sslmode=disable";
      const store = PostgresStore.fromConnString(DB_URI);
      await store.setup();

      const contextSchema = z.object({ userId: z.string() });

      const UserInfo = z.object({ name: z.string() });

      const saveUserInfo = tool(
        async (
          userInfo: z.infer<typeof UserInfo>,
          runtime: ToolRuntime<unknown, z.infer<typeof contextSchema>>,
        ) => {
          const userId = runtime.context.userId;
          if (!userId) throw new Error("userId is required");
          await runtime.store.put(["users"], userId, userInfo);
          return "Successfully saved user info.";
        },
        { name: "save_user_info", description: "Save user info", schema: UserInfo },
      );

      const agent = createAgent({
        model: "ollama:devstral-2",
        tools: [saveUserInfo],
        contextSchema,
        store,
      });

      await agent.invoke(
        { messages: [{ role: "user", content: "My name is John Smith" }] },
        { context: { userId: "user_123" } },
      );

      const result = await store.get(["users"], "user_123");
      console.log(result?.value);
      ```
    </CodeGroup>
  </Tab>
</Tabs>

***

<div className="source-links">
  <Callout icon="terminal-2">
    [将这些文档连接](/use-these-docs)到 Claude、VSCode 等，通过 MCP 获取实时答案。
  </Callout>

  <Callout icon="edit">
    [在 GitHub 上编辑此页面](https://github.com/langchain-ai/docs/edit/main/src/oss/langchain/long-term-memory.mdx) 或 [提交问题](https://github.com/langchain-ai/docs/issues/new/choose)。
  </Callout>
</div>
