> ## Documentation Index
> Fetch the complete documentation index at: https://oma-codex-session-events-atomic-log.mintlify.site/llms.txt
> Use this file to discover all available pages before exploring further.

# 使用智能体记忆

> 使用记忆存储为您的智能体提供可跨会话持久保存的记忆。

默认情况下，每个托管智能体会话都以全新的上下文开始。当会话结束时，智能体积累的任何状态都会消失。记忆存储让智能体能够跨会话携带信息：用户偏好、项目约定、先前的错误以及领域上下文。

<Note>
  托管智能体 API 请求需要 `managed-agents-2026-04-01` Beta 请求头，但记忆存储端点除外，它们使用 `agent-memory-2026-07-22`。SDK 会自动设置正确的 Beta 请求头。请参阅[Beta 请求头](/docs/zh/api/versioning-beta)。
</Note>

<Note>
  不要在记忆存储请求中同时使用 `agent-memory-2026-07-22` 和 `managed-agents-2026-04-01`：同时发送两者会返回 `400` 错误。如果您的代码显式设置了 Beta 请求头，请在记忆存储调用中将 `managed-agents-2026-04-01` 替换为 `agent-memory-2026-07-22`，而不是添加第二个值。会话端点（包括将记忆存储附加到会话）仍然使用 `managed-agents-2026-04-01`。

  2026 年 7 月 22 日，`managed-agents-2026-04-01` 标头将在 `GET /v1/memory_stores/{memory_store_id}/memories` 上采用相同的列表行为；现在发送 `agent-memory-2026-07-22` 即可选择启用该行为。在没有该标头的情况下发出的请求所产生的分页游标与该标头不兼容，因此请从第一页重新开始。
</Note>

## 概述

“记忆存储”是一个以工作区为范围、供智能体使用的文本文档集合。当您将存储附加到会话时，它会作为目录挂载到会话的沙箱中。智能体使用与文件系统其余部分相同的文件工具来读取和写入它，并且描述每个挂载的说明会自动添加到系统提示中，告诉智能体去哪里查找。这些交互需要[智能体工具集](/docs/zh/tools)；请确保在[智能体创建](/docs/zh/agent-setup)期间启用它。

存储中的每条记忆（`memory`）都通过路径寻址，并且可以直接通过 API 或 OMA 控制台读取和编辑，从而支持调优、导入和导出。

每次更改记忆都会创建一个不可变的记忆版本，为智能体写入的所有内容提供审计跟踪和时间点恢复能力。

## 创建记忆存储

为存储指定 `name` 和 `description`。description 会传递给智能体，告诉它存储包含什么内容。

<CodeGroup defaultLanguage="CLI">
  ```bash cURL theme={null}
  store=$(curl -s http://localhost:38080/v1/memory_stores \
    -H "x-api-key: $OMA_API_KEY" \
    -H "anthropic-version: 2023-06-01" \
    -H "anthropic-beta: agent-memory-2026-07-22" \
    -H "content-type: application/json" \
    -d '{"name": "User Preferences", "description": "Per-user preferences and project context."}')
  store_id=$(jq -r '.id' <<< "$store")
  echo "$store_id"  # memstore_01Hx...
  ```

  ```bash CLI theme={null}
  store_id=$(ant beta:memory-stores create \
    --name "User Preferences" \
    --description "Per-user preferences and project context." \
    --transform id --raw-output)
  ```

  ```python Python theme={null}
  store = client.beta.memory_stores.create(
      name="User Preferences",
      description="Per-user preferences and project context.",
  )
  print(store.id)  # memstore_01Hx...
  ```

  ```typescript TypeScript theme={null}
  const store = await client.beta.memoryStores.create({
    name: "User Preferences",
    description: "Per-user preferences and project context."
  });
  console.log(store.id); // memstore_01Hx...
  ```

  ```csharp C# theme={null}
  var store = await client.Beta.MemoryStores.Create(new()
  {
      Name = "User Preferences",
      Description = "Per-user preferences and project context.",
  });
  Console.WriteLine(store.ID);  // memstore_01Hx...
  ```

  ```go Go theme={null}
  store, err := client.Beta.MemoryStores.New(ctx, anthropic.BetaMemoryStoreNewParams{
      Name:        "User Preferences",
      Description: anthropic.String("Per-user preferences and project context."),
  })
  if err != nil {
      panic(err)
  }
  fmt.Println(store.ID) // memstore_01Hx...
  ```

  ```java Java theme={null}
  var store = client.beta().memoryStores().create(
      MemoryStoreCreateParams.builder()
          .name("User Preferences")
          .description("Per-user preferences and project context.")
          .build()
  );
  IO.println(store.id());  // memstore_01Hx...
  ```

  ```php PHP theme={null}
  use Anthropic\Client;

  $client = new Client();

  $store = $client->beta->memoryStores->create(
      name: 'User Preferences',
      description: 'Per-user preferences and project context.',
  );
  echo "{$store->id}\n"; // memstore_01Hx...
  ```

  ```ruby Ruby theme={null}
  require "anthropic"

  client = Anthropic::Client.new

  store = client.beta.memory_stores.create(
    name: "User Preferences",
    description: "Per-user preferences and project context."
  )
  puts store.id # memstore_01Hx...
  ```
</CodeGroup>

记忆存储的 `id`（`memstore_...`）就是您在将存储附加到会话时需要传递的值。

### 用内容进行初始填充（可选）

在任何智能体运行之前，用参考材料预加载存储：

<CodeGroup defaultLanguage="CLI">
  ```bash cURL theme={null}
  curl -s "http://localhost:38080/v1/memory_stores/$store_id/memories" \
    -H "x-api-key: $OMA_API_KEY" \
    -H "anthropic-version: 2023-06-01" \
    -H "anthropic-beta: agent-memory-2026-07-22" \
    -H "content-type: application/json" \
    -d '{"path": "/formatting_standards.md", "content": "All reports use GAAP formatting. Dates are ISO-8601..."}' > /dev/null
  ```

  ```bash CLI theme={null}
  ant beta:memory-stores:memories create \
    --memory-store-id "$store_id" \
    --path "/formatting_standards.md" \
    --content "All reports use GAAP formatting. Dates are ISO-8601..." \
    > /dev/null
  ```

  ```python Python theme={null}
  client.beta.memory_stores.memories.create(
      store.id,
      path="/formatting_standards.md",
      content="All reports use GAAP formatting. Dates are ISO-8601...",
  )
  ```

  ```typescript TypeScript theme={null}
  await client.beta.memoryStores.memories.create(store.id, {
    path: "/formatting_standards.md",
    content: "All reports use GAAP formatting. Dates are ISO-8601..."
  });
  ```

  ```csharp C# theme={null}
  await client.Beta.MemoryStores.Memories.Create(store.ID, new()
  {
      Path = "/formatting_standards.md",
      Content = "All reports use GAAP formatting. Dates are ISO-8601...",
  });
  ```

  ```go Go theme={null}
  _, err = client.Beta.MemoryStores.Memories.New(ctx, store.ID, anthropic.BetaMemoryStoreMemoryNewParams{
      Path:    "/formatting_standards.md",
      Content: anthropic.String("All reports use GAAP formatting. Dates are ISO-8601..."),
  })
  if err != nil {
      panic(err)
  }
  ```

  ```java Java theme={null}
  client.beta().memoryStores().memories().create(
      store.id(),
      MemoryCreateParams.builder()
          .path("/formatting_standards.md")
          .content("All reports use GAAP formatting. Dates are ISO-8601...")
          .build()
  );
  ```

  ```php PHP theme={null}
  $client->beta->memoryStores->memories->create(
      $store->id,
      path: '/formatting_standards.md',
      content: 'All reports use GAAP formatting. Dates are ISO-8601...',
  );
  ```

  ```ruby Ruby theme={null}
  client.beta.memory_stores.memories.create(
    store.id,
    path: "/formatting_standards.md",
    content: "All reports use GAAP formatting. Dates are ISO-8601..."
  )
  ```
</CodeGroup>

<Tip>
  存储中的单个记忆上限为 100 kB（约 25k 令牌）。一个存储最多可容纳 2,000 条记忆。请将记忆组织为许多小而专注的文件，而不是少数几个大文件。
</Tip>

## 将记忆存储附加到会话

记忆存储在创建会话时通过会话的 `resources[]` 数组附加。与文件和仓库资源不同，记忆存储只能在会话创建时附加；不支持在运行中的会话中添加或移除记忆存储。

可以选择包含 `instructions`，为智能体应如何使用此存储提供特定于会话的指导。它会与存储的 `name` 和 `description` 一起展示给智能体，上限为 4,096 个字符。

您也可以配置 `access`。它默认为 `read_write`（在以下示例中显式展示），但也支持 `read_only`。

<CodeGroup defaultLanguage="CLI">
  ```bash cURL theme={null}
  curl -s http://localhost:38080/v1/sessions \
    -H "x-api-key: $OMA_API_KEY" \
    -H "anthropic-version: 2023-06-01" \
    -H "anthropic-beta: managed-agents-2026-04-01" \
    -H "content-type: application/json" \
    --data @- <<EOF
  {
    "agent": "$agent_id",
    "environment_id": "$environment_id",
    "resources": [
      {
        "type": "memory_store",
        "memory_store_id": "$store_id",
        "access": "read_write",
        "instructions": "User preferences and project context. Check before starting any task."
      }
    ]
  }
  EOF
  ```

  ```bash CLI theme={null}
  ant beta:sessions create <<YAML
  agent: $agent_id
  environment_id: $environment_id
  resources:
    - type: memory_store
      memory_store_id: $store_id
      access: read_write
      instructions: User preferences and project context. Check before starting any task.
  YAML
  ```

  ```python Python theme={null}
  session = client.beta.sessions.create(
      agent=agent.id,
      environment_id=environment.id,
      resources=[
          {
              "type": "memory_store",
              "memory_store_id": store.id,
              "access": "read_write",
              "instructions": "User preferences and project context. Check before starting any task.",
          }
      ],
  )
  ```

  ```typescript TypeScript theme={null}
  const session = await client.beta.sessions.create({
    agent: agent.id,
    environment_id: environment.id,
    resources: [
      {
        type: "memory_store",
        memory_store_id: store.id,
        access: "read_write",
        instructions: "User preferences and project context. Check before starting any task."
      }
    ]
  });
  ```

  ```csharp C# theme={null}
  var session = await client.Beta.Sessions.Create(new()
  {
      Agent = agent.ID,
      EnvironmentID = environment.ID,
      Resources =
      [
          new BetaManagedAgentsMemoryStoreResourceParam
          {
              Type = "memory_store",
              MemoryStoreID = store.ID,
              Access = "read_write",
              Instructions = "User preferences and project context. Check before starting any task.",
          },
      ],
  });
  ```

  ```go Go theme={null}
  session, err := client.Beta.Sessions.New(ctx, anthropic.BetaSessionNewParams{
      Agent: anthropic.BetaSessionNewParamsAgentUnion{
          OfString: anthropic.String(agent.ID),
      },
      EnvironmentID: environment.ID,
      Resources: []anthropic.BetaSessionNewParamsResourceUnion{{
          OfMemoryStore: &anthropic.BetaManagedAgentsMemoryStoreResourceParam{
              Type:          anthropic.BetaManagedAgentsMemoryStoreResourceParamTypeMemoryStore,
              MemoryStoreID: store.ID,
              Access:        anthropic.BetaManagedAgentsMemoryStoreResourceParamAccessReadWrite,
              Instructions:  anthropic.String("User preferences and project context. Check before starting any task."),
          },
      }},
  })
  if err != nil {
      panic(err)
  }
  ```

  ```java Java theme={null}
  var session = client.beta().sessions().create(
      SessionCreateParams.builder()
          .agent(agent.id())
          .environmentId(environment.id())
          .addResource(
              BetaManagedAgentsMemoryStoreResourceParam.builder()
                  .type(BetaManagedAgentsMemoryStoreResourceParam.Type.MEMORY_STORE)
                  .memoryStoreId(store.id())
                  .access(BetaManagedAgentsMemoryStoreResourceParam.Access.READ_WRITE)
                  .instructions("User preferences and project context. Check before starting any task.")
                  .build()
          )
          .build()
  );
  ```

  ```php PHP theme={null}
  $session = $client->beta->sessions->create(
      agent: $agent->id,
      environmentID: $environment->id,
      resources: [
          [
              'type' => 'memory_store',
              'memory_store_id' => $store->id,
              'access' => 'read_write',
              'instructions' => 'User preferences and project context. Check before starting any task.',
          ],
      ],
  );
  ```

  ```ruby Ruby theme={null}
  session = client.beta.sessions.create(
    agent: agent.id,
    environment_id: environment.id,
    resources: [
      {
        type: "memory_store",
        memory_store_id: store.id,
        access: "read_write",
        instructions: "User preferences and project context. Check before starting any task."
      }
    ]
  )
  ```
</CodeGroup>

<Warning>
  记忆存储默认以 `read_write` 访问权限附加。如果智能体处理不受信任的输入（用户提供的提示、抓取的网页内容或第三方工具输出），成功的提示注入可能会将恶意内容写入存储。之后的会话会将该内容作为受信任的记忆读取。对于参考材料、共享查询以及智能体不需要修改的任何存储，请使用 `read_only`。
</Warning>

每个会话最多支持 **8 个记忆存储**。当记忆的不同部分有不同的所有者或访问规则时，可以附加多个存储。常见原因：

* **共享参考材料：** 一个只读存储附加到多个会话（标准、约定、领域知识），与每个会话自己的读写存储分开。
* **映射到您产品的结构：** 每个最终用户、每个团队或每个项目一个存储，同时共享单个智能体配置。
* **不同的生命周期：** 一个比任何单个会话存活更久的存储，或者一个您希望按自己的计划归档的存储。

### 智能体如何访问记忆

每个附加的存储都作为 `/mnt/memory/` 下的目录挂载到会话的沙箱中。目录名是存储的显示名称经过清理后得到的文件系统安全 slug（小写；非字母数字的连续字符变为单个连字符），因此名为 "Demo Memory" 的存储会挂载在 `/mnt/memory/demo-memory/`。确切路径会在会话的记忆存储资源的 `mount_path` 字段中返回；请从那里读取，而不是自行构造。智能体使用标准的[智能体工具集](/docs/zh/tools)读取和写入存储。挂载路径下的写入会持久化回存储，并在共享该存储的会话之间保持同步；对 `/mnt/memory/` 下任何其他路径的写入会落在容器本地的临时空间中，并在会话结束时丢失。每个挂载的简短描述（显示名称、挂载路径、访问模式、存储的 `description` 以及任何 `instructions`）会自动添加到系统提示中。

`access` 在文件系统级别强制执行：`read_only` 挂载会拒绝写入，而对 `read_write` 挂载的写入会产生归属于该会话的[记忆版本](/docs/zh/memory#audit-memory-changes)。

智能体的读取和写入会作为普通的 `agent.tool_use` 和 `agent.tool_result` 事件出现在[事件流](/docs/zh/events-and-streaming)中，对应于触及该挂载的任何工具。

## 查看和编辑记忆

记忆存储可以直接通过 API 管理。可用于构建审查工作流、纠正错误的记忆，或在任何会话运行之前初始填充存储。

### 列出记忆

列出存储中的记忆。结果以稳定的、由服务器定义的顺序返回。

* `path_prefix` 将列表范围限定到一个目录。它必须以 `/` 结尾，并匹配完整的路径段，因此 `path_prefix=/notes/` 会返回 `/notes/todo.md`，但不会返回 `/notes-archive/todo.md`。
* `depth` 控制列表在 `path_prefix` 之下的深度：省略它（或传递 `0`）以列出整个子树，或传递 `1` 以仅列出直接子项。其他值会返回 `400` 错误。

<CodeGroup defaultLanguage="CLI">
  ```bash cURL theme={null}
  curl -s "http://localhost:38080/v1/memory_stores/$store_id/memories?path_prefix=/" \
    -H "x-api-key: $OMA_API_KEY" \
    -H "anthropic-version: 2023-06-01" \
    -H "anthropic-beta: agent-memory-2026-07-22" | jq -r '.data[] | "\(.type)  \(.path)"'
  ```

  ```bash CLI theme={null}
  ant beta:memory-stores:memories list \
    --memory-store-id "$store_id" \
    --path-prefix "/"
  ```

  ```python Python theme={null}
  page = client.beta.memory_stores.memories.list(
      store.id,
      path_prefix="/",
  )
  for item in page.data:
      print(item.type, item.path)
  ```

  ```typescript TypeScript theme={null}
  const page = await client.beta.memoryStores.memories.list(store.id, {
    path_prefix: "/"
  });
  for (const item of page.data) {
    console.log(item.type, item.path);
  }
  ```

  ```csharp C# theme={null}
  var page = await client.Beta.MemoryStores.Memories.List(store.ID, new()
  {
      PathPrefix = "/",
  });
  await foreach (var item in page.Paginate())
  {
      var line = item.Match(m => $"memory  {m.Path}", p => $"memory_prefix  {p.Path}");
      Console.WriteLine(line);
  }
  ```

  ```go Go theme={null}
  page, err := client.Beta.MemoryStores.Memories.List(ctx, store.ID, anthropic.BetaMemoryStoreMemoryListParams{
      PathPrefix: anthropic.String("/"),
  })
  if err != nil {
      panic(err)
  }
  for _, item := range page.Data {
      fmt.Println(item.Type, item.Path)
  }
  ```

  ```java Java theme={null}
  var page = client.beta().memoryStores().memories().list(
      store.id(),
      MemoryListParams.builder()
          .pathPrefix("/")
          .build()
  );
  for (var item : page.data()) {
      item.memory().ifPresent(m -> IO.println("memory  " + m.path()));
      item.memoryPrefix().ifPresent(p -> IO.println("memory_prefix  " + p.path()));
  }
  ```

  ```php PHP theme={null}
  $page = $client->beta->memoryStores->memories->list(
      $store->id,
      pathPrefix: '/',
  );
  foreach ($page->data as $item) {
      echo "{$item->type}  {$item->path}\n";
  }
  ```

  ```ruby Ruby theme={null}
  page = client.beta.memory_stores.memories.list(
    store.id,
    path_prefix: "/"
  )
  page.data.each do |entry|
    puts "#{entry.type}  #{entry.path}"
  end
  ```
</CodeGroup>

有关完整的参数和响应模式，请参阅[列出记忆参考](/docs/zh/api/memories/list-memories)。

### 读取记忆

获取单个记忆会返回完整内容。

<CodeGroup defaultLanguage="CLI">
  ```bash cURL theme={null}
  curl -s "http://localhost:38080/v1/memory_stores/$store_id/memories/$mem_id" \
    -H "x-api-key: $OMA_API_KEY" \
    -H "anthropic-version: 2023-06-01" \
    -H "anthropic-beta: agent-memory-2026-07-22" | jq -r '.content'
  ```

  ```bash CLI theme={null}
  ant beta:memory-stores:memories retrieve \
    --memory-store-id "$store_id" \
    --memory-id "$mem_id"
  ```

  ```python Python theme={null}
  retrieved = client.beta.memory_stores.memories.retrieve(
      mem.id,
      memory_store_id=store.id,
  )
  print(retrieved.content)
  ```

  ```typescript TypeScript theme={null}
  const retrieved = await client.beta.memoryStores.memories.retrieve(mem.id, {
    memory_store_id: store.id
  });
  console.log(retrieved.content);
  ```

  ```csharp C# theme={null}
  var retrieved = await client.Beta.MemoryStores.Memories.Retrieve(mem.ID, new()
  {
      MemoryStoreID = store.ID,
  });
  Console.WriteLine(retrieved.Content);
  ```

  ```go Go theme={null}
  retrieved, err := client.Beta.MemoryStores.Memories.Get(ctx, mem.ID, anthropic.BetaMemoryStoreMemoryGetParams{
      MemoryStoreID: store.ID,
  })
  if err != nil {
      panic(err)
  }
  fmt.Println(retrieved.Content)
  ```

  ```java Java theme={null}
  var retrieved = client.beta().memoryStores().memories().retrieve(
      mem.id(),
      MemoryRetrieveParams.builder().memoryStoreId(store.id()).build()
  );
  IO.println(retrieved.content().orElseThrow());
  ```

  ```php PHP theme={null}
  $retrieved = $client->beta->memoryStores->memories->retrieve($mem->id, memoryStoreID: $store->id);
  echo "{$retrieved->content}\n";
  ```

  ```ruby Ruby theme={null}
  retrieved = client.beta.memory_stores.memories.retrieve(
    mem.id,
    memory_store_id: store.id
  )
  puts retrieved.content
  ```
</CodeGroup>

有关完整的参数和响应模式，请参阅[检索记忆参考](/docs/zh/api/memories/get-a-memory)。

### 创建记忆

`memories.create` 在给定的 `path` 处创建一条记忆。创建操作不会覆盖；要更改现有记忆，请使用 [`memories.update`](/docs/zh/memory#update-a-memory)。

<CodeGroup defaultLanguage="CLI">
  ```bash cURL theme={null}
  mem=$(curl -s "http://localhost:38080/v1/memory_stores/$store_id/memories" \
    -H "x-api-key: $OMA_API_KEY" \
    -H "anthropic-version: 2023-06-01" \
    -H "anthropic-beta: agent-memory-2026-07-22" \
    -H "content-type: application/json" \
    -d '{"path": "/preferences/formatting.md", "content": "Always use tabs, not spaces."}')
  mem_id=$(jq -r '.id' <<< "$mem")
  mem_sha=$(jq -r '.content_sha256' <<< "$mem")
  ```

  ```bash CLI theme={null}
  mem=$(ant beta:memory-stores:memories create \
    --memory-store-id "$store_id" \
    --path "/preferences/formatting.md" \
    --content "Always use tabs, not spaces." \
    --format json)
  mem_id=$(jq -r '.id' <<< "$mem")
  mem_sha=$(jq -r '.content_sha256' <<< "$mem")
  ```

  ```python Python theme={null}
  mem = client.beta.memory_stores.memories.create(
      store.id,
      path="/preferences/formatting.md",
      content="Always use tabs, not spaces.",
  )
  ```

  ```typescript TypeScript theme={null}
  const mem = await client.beta.memoryStores.memories.create(store.id, {
    path: "/preferences/formatting.md",
    content: "Always use tabs, not spaces."
  });
  ```

  ```csharp C# theme={null}
  var mem = await client.Beta.MemoryStores.Memories.Create(store.ID, new()
  {
      Path = "/preferences/formatting.md",
      Content = "Always use tabs, not spaces.",
  });
  ```

  ```go Go theme={null}
  mem, err := client.Beta.MemoryStores.Memories.New(ctx, store.ID, anthropic.BetaMemoryStoreMemoryNewParams{
      Path:    "/preferences/formatting.md",
      Content: anthropic.String("Always use tabs, not spaces."),
  })
  if err != nil {
      panic(err)
  }
  ```

  ```java Java theme={null}
  var mem = client.beta().memoryStores().memories().create(
      store.id(),
      MemoryCreateParams.builder()
          .path("/preferences/formatting.md")
          .content("Always use tabs, not spaces.")
          .build()
  );
  ```

  ```php PHP theme={null}
  $mem = $client->beta->memoryStores->memories->create(
      $store->id,
      path: '/preferences/formatting.md',
      content: 'Always use tabs, not spaces.',
  );
  ```

  ```ruby Ruby theme={null}
  mem = client.beta.memory_stores.memories.create(
    store.id,
    path: "/preferences/formatting.md",
    content: "Always use tabs, not spaces."
  )
  ```
</CodeGroup>

有关完整的参数和响应模式，请参阅[创建记忆参考](/docs/zh/api/memories/create-a-memory)。

### 更新记忆

`memories.update` 通过 ID 修改现有记忆。您可以更改 `content`、`path`（重命名）或两者。以下示例将一条记忆重命名为归档路径：

<CodeGroup defaultLanguage="CLI">
  ```bash cURL theme={null}
  curl -s -X POST "http://localhost:38080/v1/memory_stores/$store_id/memories/$mem_id" \
    -H "x-api-key: $OMA_API_KEY" \
    -H "anthropic-version: 2023-06-01" \
    -H "anthropic-beta: agent-memory-2026-07-22" \
    -H "content-type: application/json" \
    -d '{"path": "/archive/2026_q1_formatting.md"}' > /dev/null
  ```

  ```bash CLI theme={null}
  ant beta:memory-stores:memories update \
    --memory-store-id "$store_id" \
    --memory-id "$mem_id" \
    --path "/archive/2026_q1_formatting.md" \
    > /dev/null
  ```

  ```python Python theme={null}
  client.beta.memory_stores.memories.update(
      mem.id,
      memory_store_id=store.id,
      path="/archive/2026_q1_formatting.md",
  )
  ```

  ```typescript TypeScript theme={null}
  await client.beta.memoryStores.memories.update(mem.id, {
    memory_store_id: store.id,
    path: "/archive/2026_q1_formatting.md"
  });
  ```

  ```csharp C# theme={null}
  await client.Beta.MemoryStores.Memories.Update(mem.ID, new()
  {
      MemoryStoreID = store.ID,
      Path = "/archive/2026_q1_formatting.md",
  });
  ```

  ```go Go theme={null}
  _, err = client.Beta.MemoryStores.Memories.Update(ctx, mem.ID, anthropic.BetaMemoryStoreMemoryUpdateParams{
      MemoryStoreID: store.ID,
      Path:          anthropic.String("/archive/2026_q1_formatting.md"),
  })
  if err != nil {
      panic(err)
  }
  ```

  ```java Java theme={null}
  client.beta().memoryStores().memories().update(
      mem.id(),
      MemoryUpdateParams.builder()
          .memoryStoreId(store.id())
          .path("/archive/2026_q1_formatting.md")
          .build()
  );
  ```

  ```php PHP theme={null}
  $client->beta->memoryStores->memories->update(
      $mem->id,
      memoryStoreID: $store->id,
      path: '/archive/2026_q1_formatting.md',
  );
  ```

  ```ruby Ruby theme={null}
  client.beta.memory_stores.memories.update(
    mem.id,
    memory_store_id: store.id,
    path: "/archive/2026_q1_formatting.md"
  )
  ```
</CodeGroup>

有关完整的参数和响应模式，请参阅[更新记忆参考](/docs/zh/api/memories/update-a-memory)。

#### 安全的内容编辑（乐观并发）

为避免覆盖并发写入，请传递 `content_sha256` 前置条件。只有当存储的内容哈希仍与您读取到的哈希匹配时，更新才会生效；如果不匹配，请重新读取该记忆并针对最新状态重试。

<CodeGroup defaultLanguage="CLI">
  ```bash cURL theme={null}
  curl -s -X POST "http://localhost:38080/v1/memory_stores/$store_id/memories/$mem_id" \
    -H "x-api-key: $OMA_API_KEY" \
    -H "anthropic-version: 2023-06-01" \
    -H "anthropic-beta: agent-memory-2026-07-22" \
    -H "content-type: application/json" \
    --data @- > /dev/null <<EOF
  {
    "content": "CORRECTED: Always use 2-space indentation.",
    "precondition": {"type": "content_sha256", "content_sha256": "$mem_sha"}
  }
  EOF
  ```

  ```bash CLI theme={null}
  ant beta:memory-stores:memories update \
    --memory-store-id "$store_id" \
    --memory-id "$mem_id" \
    --content "CORRECTED: Always use 2-space indentation." \
    --precondition "{type: content_sha256, content_sha256: $mem_sha}" \
    > /dev/null
  ```

  ```python Python theme={null}
  client.beta.memory_stores.memories.update(
      memory_id=mem.id,
      memory_store_id=store.id,
      content="CORRECTED: Always use 2-space indentation.",
      precondition={"type": "content_sha256", "content_sha256": mem.content_sha256},
  )
  ```

  ```typescript TypeScript theme={null}
  await client.beta.memoryStores.memories.update(mem.id, {
    memory_store_id: store.id,
    content: "CORRECTED: Always use 2-space indentation.",
    precondition: { type: "content_sha256", content_sha256: mem.content_sha256 }
  });
  ```

  ```csharp C# theme={null}
  await client.Beta.MemoryStores.Memories.Update(mem.ID, new()
  {
      MemoryStoreID = store.ID,
      Content = "CORRECTED: Always use 2-space indentation.",
      Precondition = new BetaManagedAgentsPrecondition
      {
          Type = "content_sha256",
          ContentSha256 = mem.ContentSha256,
      },
  });
  ```

  ```go Go theme={null}
  _, err = client.Beta.MemoryStores.Memories.Update(ctx, mem.ID, anthropic.BetaMemoryStoreMemoryUpdateParams{
      MemoryStoreID: store.ID,
      Content:       anthropic.String("CORRECTED: Always use 2-space indentation."),
      Precondition: anthropic.BetaManagedAgentsPreconditionParam{
          Type:          anthropic.BetaManagedAgentsPreconditionTypeContentSha256,
          ContentSha256: anthropic.String(mem.ContentSha256),
      },
  })
  if err != nil {
      panic(err)
  }
  ```

  ```java Java theme={null}
  client.beta().memoryStores().memories().update(
      mem.id(),
      MemoryUpdateParams.builder()
          .memoryStoreId(store.id())
          .content("CORRECTED: Always use 2-space indentation.")
          .precondition(
              BetaManagedAgentsPrecondition.builder()
                  .type(BetaManagedAgentsPrecondition.Type.CONTENT_SHA256)
                  .contentSha256(mem.contentSha256())
                  .build()
          )
          .build()
  );
  ```

  ```php PHP theme={null}
  $client->beta->memoryStores->memories->update(
      $mem->id,
      memoryStoreID: $store->id,
      content: 'CORRECTED: Always use 2-space indentation.',
      precondition: ['type' => 'content_sha256', 'content_sha256' => $mem->contentSha256],
  );
  ```

  ```ruby Ruby theme={null}
  client.beta.memory_stores.memories.update(
    mem.id,
    memory_store_id: store.id,
    content: "CORRECTED: Always use 2-space indentation.",
    precondition: {type: "content_sha256", content_sha256: mem.content_sha256}
  )
  ```
</CodeGroup>

### 删除记忆

<CodeGroup defaultLanguage="CLI">
  ```bash cURL theme={null}
  curl -s -X DELETE "http://localhost:38080/v1/memory_stores/$store_id/memories/$mem_id" \
    -H "x-api-key: $OMA_API_KEY" \
    -H "anthropic-version: 2023-06-01" \
    -H "anthropic-beta: agent-memory-2026-07-22" > /dev/null
  ```

  ```bash CLI theme={null}
  ant beta:memory-stores:memories delete \
    --memory-store-id "$store_id" \
    --memory-id "$mem_id" \
    > /dev/null
  ```

  ```python Python theme={null}
  client.beta.memory_stores.memories.delete(
      mem.id,
      memory_store_id=store.id,
  )
  ```

  ```typescript TypeScript theme={null}
  await client.beta.memoryStores.memories.delete(mem.id, {
    memory_store_id: store.id
  });
  ```

  ```csharp C# theme={null}
  await client.Beta.MemoryStores.Memories.Delete(mem.ID, new()
  {
      MemoryStoreID = store.ID,
  });
  ```

  ```go Go theme={null}
  _, err = client.Beta.MemoryStores.Memories.Delete(ctx, mem.ID, anthropic.BetaMemoryStoreMemoryDeleteParams{
      MemoryStoreID: store.ID,
  })
  if err != nil {
      panic(err)
  }
  ```

  ```java Java theme={null}
  client.beta().memoryStores().memories().delete(
      mem.id(),
      MemoryDeleteParams.builder().memoryStoreId(store.id()).build()
  );
  ```

  ```php PHP theme={null}
  $client->beta->memoryStores->memories->delete($mem->id, memoryStoreID: $store->id);
  ```

  ```ruby Ruby theme={null}
  client.beta.memory_stores.memories.delete(
    mem.id,
    memory_store_id: store.id
  )
  ```
</CodeGroup>

有关完整的参数和响应模式，请参阅[删除记忆参考](/docs/zh/api/memories/delete-a-memory)。

## 审计记忆更改

对记忆的每次变更都会创建一个不可变的**记忆版本**（`memver_...`）。使用版本端点可审计谁在何时更改了什么、检查或恢复先前的快照，以及通过脱敏操作从历史记录中清除敏感内容。

版本属于存储（而非单个记忆），即使记忆本身被删除后版本仍然存在，因此审计跟踪保持完整。版本保留 30 天；不过，最近的版本无论多久都会始终保留，因此不经常更改的记忆可能会保留超过 30 天的历史记录。实时的 `memories.retrieve` 调用始终返回最新版本；版本端点为您提供保留的历史记录。

没有专门的恢复端点；要回滚，请检索您想要的版本，并使用 `memories.update` 将其 `content` 写回（如果父记忆已被删除，则使用 `memories.create`，因为版本的存活时间比其父记忆更长）。

过去的记忆版本可能会在 30 天后被删除。要更长时间地保留记忆历史，请通过 API 导出版本。

### 列出版本

列出存储的版本历史，最新的在前。以下示例筛选出单个记忆的历史：

<CodeGroup defaultLanguage="CLI">
  ```bash cURL theme={null}
  versions=$(curl -s "http://localhost:38080/v1/memory_stores/$store_id/memory_versions?memory_id=$mem_id" \
    -H "x-api-key: $OMA_API_KEY" \
    -H "anthropic-version: 2023-06-01" \
    -H "anthropic-beta: agent-memory-2026-07-22")
  jq -r '.data[] | "\(.id): \(.operation)"' <<< "$versions"
  version_id=$(jq -r '.data[1].id' <<< "$versions")
  ```

  ```bash CLI theme={null}
  versions=$(ant beta:memory-stores:memory-versions list \
    --memory-store-id "$store_id" \
    --memory-id "$mem_id" \
    --format json)
  # `list --format json` 为每个条目输出一个 JSON 对象。
  jq -r '"\(.id): \(.operation)"' <<< "$versions"
  version_id=$(jq -rs '.[1].id' <<< "$versions")
  ```

  ```python Python theme={null}
  versions = client.beta.memory_stores.memory_versions.list(
      store.id,
      memory_id=mem.id,
  )
  for version in versions:
      print(f"{version.id}: {version.operation}")

  version_id = versions.data[1].id
  ```

  ```typescript TypeScript theme={null}
  const versions = await client.beta.memoryStores.memoryVersions.list(store.id, {
    memory_id: mem.id
  });
  for await (const v of versions) {
    console.log(`${v.id}: ${v.operation}`);
  }

  const versionId = versions.data[1].id;
  ```

  ```csharp C# theme={null}
  var versions = await client.Beta.MemoryStores.MemoryVersions.List(store.ID, new()
  {
      MemoryID = mem.ID,
  });
  var versionIds = new List<string>();
  await foreach (var v in versions.Paginate())
  {
      Console.WriteLine($"{v.ID}: {v.Operation.Raw()}");
      versionIds.Add(v.ID);
  }

  var versionId = versionIds[1];
  ```

  ```go Go theme={null}
  versions := client.Beta.MemoryStores.MemoryVersions.ListAutoPaging(ctx, store.ID, anthropic.BetaMemoryStoreMemoryVersionListParams{
      MemoryID: anthropic.String(mem.ID),
  })
  for versions.Next() {
      v := versions.Current()
      fmt.Printf("%s: %s\n", v.ID, v.Operation)
  }
  if err := versions.Err(); err != nil {
      panic(err)
  }

  vpage, err := client.Beta.MemoryStores.MemoryVersions.List(ctx, store.ID, anthropic.BetaMemoryStoreMemoryVersionListParams{
      MemoryID: anthropic.String(mem.ID),
  })
  if err != nil {
      panic(err)
  }
  versionID := vpage.Data[1].ID
  ```

  ```java Java theme={null}
  var versions = client.beta().memoryStores().memoryVersions().list(
      store.id(),
      MemoryVersionListParams.builder().memoryId(mem.id()).build()
  );
  for (var v : versions.autoPager()) {
      IO.println(v.id() + ": " + v.operation());
  }

  var versionId = versions.data().get(1).id();
  ```

  ```php PHP theme={null}
  $versions = $client->beta->memoryStores->memoryVersions->list(
      $store->id,
      memoryID: $mem->id,
  );
  foreach ($versions->pagingEachItem() as $v) {
      echo "{$v->id}: {$v->operation}\n";
  }

  $versionId = $versions->data[1]->id;
  ```

  ```ruby Ruby theme={null}
  versions = client.beta.memory_stores.memory_versions.list(
    store.id,
    memory_id: mem.id
  )
  versions.auto_paging_each do |version|
    puts "#{version.id}: #{version.operation}"
  end

  version_id = versions.data[1].id
  ```
</CodeGroup>

有关完整的参数和响应模式，请参阅[列出记忆版本参考](/docs/zh/api/memories/list-memory-versions)。

### 检索版本

获取单个版本会返回与列表响应相同的字段，外加完整的 `content` 正文。

<CodeGroup defaultLanguage="CLI">
  ```bash cURL theme={null}
  curl -s "http://localhost:38080/v1/memory_stores/$store_id/memory_versions/$version_id" \
    -H "x-api-key: $OMA_API_KEY" \
    -H "anthropic-version: 2023-06-01" \
    -H "anthropic-beta: agent-memory-2026-07-22"
  ```

  ```bash CLI theme={null}
  ant beta:memory-stores:memory-versions retrieve \
    --memory-store-id "$store_id" \
    --memory-version-id "$version_id"
  ```

  ```python Python theme={null}
  version = client.beta.memory_stores.memory_versions.retrieve(
      version_id,
      memory_store_id=store.id,
  )
  print(version.content)
  ```

  ```typescript TypeScript theme={null}
  const version = await client.beta.memoryStores.memoryVersions.retrieve(versionId, {
    memory_store_id: store.id
  });
  console.log(version.content);
  ```

  ```csharp C# theme={null}
  var version = await client.Beta.MemoryStores.MemoryVersions.Retrieve(versionId, new()
  {
      MemoryStoreID = store.ID,
  });
  Console.WriteLine(version.Content);
  ```

  ```go Go theme={null}
  version, err := client.Beta.MemoryStores.MemoryVersions.Get(ctx, versionID, anthropic.BetaMemoryStoreMemoryVersionGetParams{
      MemoryStoreID: store.ID,
  })
  if err != nil {
      panic(err)
  }
  fmt.Println(version.Content)
  ```

  ```java Java theme={null}
  var version = client.beta().memoryStores().memoryVersions().retrieve(
      versionId,
      MemoryVersionRetrieveParams.builder().memoryStoreId(store.id()).build()
  );
  IO.println(version.content().orElseThrow());
  ```

  ```php PHP theme={null}
  $version = $client->beta->memoryStores->memoryVersions->retrieve(
      $versionId,
      memoryStoreID: $store->id,
  );
  echo "{$version->content}\n";
  ```

  ```ruby Ruby theme={null}
  version = client.beta.memory_stores.memory_versions.retrieve(
    version_id,
    memory_store_id: store.id
  )
  puts version.content
  ```
</CodeGroup>

有关完整的参数和响应模式，请参阅[检索记忆版本参考](/docs/zh/api/memories/get-a-memory-version)。

### 脱敏记忆版本

脱敏（`redact`）会从历史版本中清除内容，同时保留审计跟踪（谁在何时做了什么）。可将其用于合规工作流，例如移除泄露的密钥、个人身份信息或处理用户删除请求。

作为实时记忆当前头部的版本无法被编辑删除。请先写入一个新版本（或删除该记忆），然后再编辑删除旧版本。

<CodeGroup defaultLanguage="CLI">
  ```bash cURL theme={null}
  curl -s -X POST "http://localhost:38080/v1/memory_stores/$store_id/memory_versions/$version_id/redact" \
    -H "x-api-key: $OMA_API_KEY" \
    -H "anthropic-version: 2023-06-01" \
    -H "anthropic-beta: agent-memory-2026-07-22" \
    -H "content-type: application/json" \
    -d '{}'
  ```

  ```bash CLI theme={null}
  ant beta:memory-stores:memory-versions redact \
    --memory-store-id "$store_id" \
    --memory-version-id "$version_id"
  ```

  ```python Python theme={null}
  client.beta.memory_stores.memory_versions.redact(
      version_id,
      memory_store_id=store.id,
  )
  ```

  ```typescript TypeScript theme={null}
  await client.beta.memoryStores.memoryVersions.redact(versionId, {
    memory_store_id: store.id
  });
  ```

  ```csharp C# theme={null}
  await client.Beta.MemoryStores.MemoryVersions.Redact(versionId, new()
  {
      MemoryStoreID = store.ID,
  });
  ```

  ```go Go theme={null}
  _, err = client.Beta.MemoryStores.MemoryVersions.Redact(ctx, versionID, anthropic.BetaMemoryStoreMemoryVersionRedactParams{
      MemoryStoreID: store.ID,
  })
  if err != nil {
      panic(err)
  }
  ```

  ```java Java theme={null}
  client.beta().memoryStores().memoryVersions().redact(
      versionId,
      MemoryVersionRedactParams.builder().memoryStoreId(store.id()).build()
  );
  ```

  ```php PHP theme={null}
  $client->beta->memoryStores->memoryVersions->redact(
      $versionId,
      memoryStoreID: $store->id,
  );
  ```

  ```ruby Ruby theme={null}
  client.beta.memory_stores.memory_versions.redact(
    version_id,
    memory_store_id: store.id
  )
  ```
</CodeGroup>

有关完整的参数和响应模式，请参阅[脱敏记忆版本参考](/docs/zh/api/memories/redact-a-memory-version)。

## 管理记忆存储

除了 [`create`](/docs/zh/api/memory-stores/create-a-memory-store) 之外，记忆存储还支持 [`retrieve`](/docs/zh/api/memory-stores/get-a-memory-store)、[`update`](/docs/zh/api/memory-stores/update-a-memory-store)、[`list`](/docs/zh/api/memory-stores/list-memory-stores)、[`archive`](/docs/zh/api/memory-stores/archive-a-memory-store) 和 [`delete`](/docs/zh/api/memory-stores/delete-a-memory-store)。

### 列出存储

列出工作区中的存储。默认排除已归档的存储；传递 `include_archived: true` 以包含它们。

<CodeGroup defaultLanguage="CLI">
  ```bash cURL theme={null}
  curl -s "http://localhost:38080/v1/memory_stores?include_archived=true" \
    -H "x-api-key: $OMA_API_KEY" \
    -H "anthropic-version: 2023-06-01" \
    -H "anthropic-beta: agent-memory-2026-07-22" | jq '.data[] | {id, name, archived_at}'
  ```

  ```bash CLI theme={null}
  ant beta:memory-stores list --include-archived
  ```

  ```python Python theme={null}
  for memory_store in client.beta.memory_stores.list(include_archived=True):
      print(memory_store.id, memory_store.name, memory_store.archived_at)
  ```

  ```typescript TypeScript theme={null}
  for await (const s of client.beta.memoryStores.list({ include_archived: true })) {
    console.log(s.id, s.name, s.archived_at);
  }
  ```

  ```csharp C# theme={null}
  var stores = await client.Beta.MemoryStores.List(new() { IncludeArchived = true });
  await foreach (var s in stores.Paginate())
  {
      Console.WriteLine($"{s.ID} {s.Name} {s.ArchivedAt}");
  }
  ```

  ```go Go theme={null}
  stores := client.Beta.MemoryStores.ListAutoPaging(ctx, anthropic.BetaMemoryStoreListParams{
      IncludeArchived: anthropic.Bool(true),
  })
  for stores.Next() {
      s := stores.Current()
      fmt.Println(s.ID, s.Name, s.ArchivedAt)
  }
  if err := stores.Err(); err != nil {
      panic(err)
  }
  ```

  ```java Java theme={null}
  for (var s : client.beta().memoryStores().list(
      MemoryStoreListParams.builder().includeArchived(true).build()
  ).autoPager()) {
      IO.println(s.id() + " " + s.name() + " " + s.archivedAt());
  }
  ```

  ```php PHP theme={null}
  foreach ($client->beta->memoryStores->list(includeArchived: true)->pagingEachItem() as $s) {
      // archivedAt 仅在已归档的存储上设置。
      $archivedAt = isset($s->archivedAt) ? $s->archivedAt->format(DATE_ATOM) : '';
      echo "{$s->id} {$s->name} {$archivedAt}\n";
  }
  ```

  ```ruby Ruby theme={null}
  client.beta.memory_stores.list(include_archived: true).auto_paging_each do |memory_store|
    puts "#{memory_store.id} #{memory_store.name} #{memory_store.archived_at}"
  end
  ```
</CodeGroup>

有关完整的参数和响应模式，请参阅[列出记忆存储参考](/docs/zh/api/memory-stores/list-memory-stores)。

### 归档存储

归档会使存储变为只读，并阻止其被附加到新会话。归档是单向的；没有取消归档的操作。

<CodeGroup defaultLanguage="CLI">
  ```bash cURL theme={null}
  curl -s -X POST "http://localhost:38080/v1/memory_stores/$store_id/archive" \
    -H "x-api-key: $OMA_API_KEY" \
    -H "anthropic-version: 2023-06-01" \
    -H "anthropic-beta: agent-memory-2026-07-22" > /dev/null
  ```

  ```bash CLI theme={null}
  ant beta:memory-stores archive --memory-store-id "$store_id"
  ```

  ```python Python theme={null}
  client.beta.memory_stores.archive(store.id)
  ```

  ```typescript TypeScript theme={null}
  await client.beta.memoryStores.archive(store.id);
  ```

  ```csharp C# theme={null}
  await client.Beta.MemoryStores.Archive(store.ID);
  ```

  ```go Go theme={null}
  _, err = client.Beta.MemoryStores.Archive(ctx, store.ID, anthropic.BetaMemoryStoreArchiveParams{})
  if err != nil {
      panic(err)
  }
  ```

  ```java Java theme={null}
  client.beta().memoryStores().archive(store.id());
  ```

  ```php PHP theme={null}
  $client->beta->memoryStores->archive($store->id);
  ```

  ```ruby Ruby theme={null}
  client.beta.memory_stores.archive(store.id)
  ```
</CodeGroup>

有关完整的参数和响应模式，请参阅[归档记忆存储参考](/docs/zh/api/memory-stores/archive-a-memory-store)。

要永久移除存储及其所有记忆和版本，请使用 [`memory_stores.delete`](/docs/zh/api/memory-stores/delete-a-memory-store)。

## 记忆管理最佳实践

当存储达到其 2,000 条记忆的上限时，对新记忆的写入会失败：包括直接的 `memories.create` 调用以及智能体对未映射路径的文件写入。现有记忆仍然可读可编辑。以下实践可帮助您保持在上限之下，并在达到上限时优雅地恢复。

* **使用专注的存储。** 与其使用一个大型通用存储，不如使用更小的专用存储：每个用户一个、共享领域知识一个、项目特定上下文一个。每个存储都有自己的 2,000 条记忆上限，因此保持存储范围明确可以降低任何单个存储被填满的可能性。

* **在存储填满之前进行压缩或修剪。** 使用 `memories.delete` 删除过时或冗余的记忆。您也可以运行[梦境会话](/docs/zh/dreams)，它会将碎片化的内容整合到一个单独的新输出存储中，而不是修改原始存储。将您的会话切换到该输出存储，然后归档或删除原始存储。

* **在合适的时候附加新存储。** 如果存储已超出其有用范围，请为新内容附加一个全新的存储，并以 `read_only` 访问权限附加原始存储。智能体可以从两者读取，但只写入新存储。

* **在适当的情况下限制写入权限。** 仅读取共享参考材料的会话不需要 `read_write`。将写入权限限定在实际添加新记忆的会话，可以更容易地追踪增长的来源。
