记忆工具使Claude能够通过记忆文件目录在对话间存储和检索信息。Claude可以创建、读取、更新和删除在会话间持久化的文件,允许它随着时间的推移构建知识,而无需将所有内容保留在上下文窗口中。
记忆工具在客户端运行——你通过自己的基础设施控制数据的存储位置和方式。
记忆工具目前处于测试阶段。要启用它,请在API请求中使用测试版标头context-management-2025-06-27。
请通过我们的反馈表单分享你对此功能的反馈。
启用后,Claude在开始任务前会自动检查其记忆目录。Claude可以在/memories目录中创建、读取、更新和删除文件,以存储其工作时学到的内容,然后在未来的对话中引用这些记忆,以更有效地处理类似任务或从中断处继续。
由于这是一个客户端工具,Claude进行工具调用来执行记忆操作,你的应用程序在本地执行这些操作。这给了你完全控制记忆存储位置和方式的权力。出于安全考虑,你应该将所有记忆操作限制在/memories目录内。
当你要求Claude帮助完成任务时,Claude会自动首先检查其记忆目录。以下是典型交互的样子:
1. 用户请求:
"Help me respond to this customer service ticket."2. Claude检查记忆目录:
"I'll help you respond to the customer service ticket. Let me check my memory for any previous context."Claude调用记忆工具:
{
"type": "tool_use",
"id": "toolu_01C4D5E6F7G8H9I0J1K2L3M4",
"name": "memory",
"input": {
"command": "view",
"path": "/memories"
}
}3. 你的应用程序返回目录内容:
{
"type": "tool_result",
"tool_use_id": "toolu_01C4D5E6F7G8H9I0J1K2L3M4",
"content": "Here're the files and directories up to 2 levels deep in /memories, excluding hidden items and node_modules:\n4.0K\t/memories\n1.5K\t/memories/customer_service_guidelines.xml\n2.0K\t/memories/refund_policies.xml"
}4. Claude读取相关文件:
{
"type": "tool_use",
"id": "toolu_01D5E6F7G8H9I0J1K2L3M4N5",
"name": "memory",
"input": {
"command": "view",
"path": "/memories/customer_service_guidelines.xml"
}
}5. 你的应用程序返回文件内容:
{
"type": "tool_result",
"tool_use_id": "toolu_01D5E6F7G8H9I0J1K2L3M4N5",
"content": "Here's the content of /memories/customer_service_guidelines.xml with line numbers:\n 1\t<guidelines>\n 2\t<addressing_customers>\n 3\t- Always address customers by their first name\n 4\t- Use empathetic language\n..."
}6. Claude使用记忆来帮助:
"Based on your customer service guidelines, I can help you craft a response. Please share the ticket details..."记忆工具可用于:
claude-sonnet-4-5-20250929)claude-sonnet-4-20250514)claude-haiku-4-5-20251001)claude-opus-4-5-20251101)claude-opus-4-1-20250805)claude-opus-4-20250514)要使用记忆工具:
context-management-2025-06-27要在你的应用程序中处理记忆工具操作,你需要为每个记忆命令实现处理程序。我们的SDK提供记忆工具助手,处理工具接口——你可以子类化BetaAbstractMemoryTool(Python)或使用betaMemoryTool(TypeScript)来实现你自己的记忆后端(基于文件、数据库、云存储、加密文件等)。
有关工作示例,请参阅:
你的客户端实现需要处理这些记忆工具命令。虽然这些规范描述了Claude最熟悉的推荐行为,但你可以修改你的实现并根据需要返回字符串。
显示目录内容或文件内容,带有可选的行范围:
{
"command": "view",
"path": "/memories",
"view_range": [1, 10] // 可选:查看特定行
}对于目录: 返回显示文件和目录及其大小的列表:
Here're the files and directories up to 2 levels deep in {path}, excluding hidden items and node_modules:
{size} {path}
{size} {path}/{filename1}
{size} {path}/{filename2}5.5K、1.2M).开头的文件)和node_modules对于文件: 返回带有标头和行号的文件内容:
Here's the content of {path} with line numbers:
{line_numbers}{tab}{content}行号格式:
"File {path} exceeds maximum line limit of 999,999 lines."示例输出:
Here's the content of /memories/notes.txt with line numbers:
1 Hello World
2 This is line two
10 Line ten
100 Line one hundred"The path {path} does not exist. Please provide a valid path."创建新文件:
{
"command": "create",
"path": "/memories/notes.txt",
"file_text": "Meeting notes:\n- Discussed project timeline\n- Next steps defined\n"
}"File created successfully at: {path}""Error: File {path} already exists"替换文件中的文本:
{
"command": "str_replace",
"path": "/memories/preferences.txt",
"old_str": "Favorite color: blue",
"new_str": "Favorite color: green"
}"The memory file has been edited."后跟编辑文件的代码片段和行号"Error: The path {path} does not exist. Please provide a valid path.""No replacement was performed, old_str `\{old_str}` did not appear verbatim in {path}."old_str出现多次时,返回:"No replacement was performed. Multiple occurrences of old_str `\{old_str}` in lines: {line_numbers}. Please ensure it is unique"如果路径是目录,返回"文件不存在"错误。
在特定行插入文本:
{
"command": "insert",
"path": "/memories/todo.txt",
"insert_line": 2,
"insert_text": "- Review memory tool documentation\n"
}"The file {path} has been edited.""Error: The path {path} does not exist""Error: Invalid `insert_line` parameter: {insert_line}. It should be within the range of lines of the file: [0, {n_lines}]"如果路径是目录,返回"文件不存在"错误。
删除文件或目录:
{
"command": "delete",
"path": "/memories/old_file.txt"
}"Successfully deleted {path}""Error: The path {path} does not exist"递归删除目录及其所有内容。
重命名或移动文件/目录:
{
"command": "rename",
"old_path": "/memories/draft.txt",
"new_path": "/memories/final.txt"
}"Successfully renamed {old_path} to {new_path}""Error: The path {old_path} does not exist""Error: The destination {new_path} already exists"重命名目录。
当包含记忆工具时,我们会自动将此指令包含到系统提示中:
IMPORTANT: ALWAYS VIEW YOUR MEMORY DIRECTORY BEFORE DOING ANYTHING ELSE.
MEMORY PROTOCOL:
1. Use the `view` command of your `memory` tool to check for earlier progress.
2. ... (work on the task) ...
- As you make progress, record status / progress / thoughts etc in your memory.
ASSUME INTERRUPTION: Your context window might be reset at any moment, so you risk losing any progress that is not recorded in your memory directory.如果你观察到Claude创建混乱的记忆文件,你可以包含此指令:
注意:编辑你的记忆文件夹时,始终尝试保持其内容最新、连贯和有组织。你可以重命名或删除不再相关的文件。除非必要,否则不要创建新文件。
你也可以指导Claude写入记忆的内容,例如,"仅在你的记忆系统中写下与<topic>相关的信息。"
以下是实现记忆存储时的重要安全问题:
Claude通常会拒绝在记忆文件中写下敏感信息。但是,你可能想实现更严格的验证,以剥离潜在的敏感信息。
考虑跟踪记忆文件大小并防止文件增长过大。考虑添加记忆读取命令可以返回的最大字符数,并让Claude分页浏览内容。
考虑定期清除在较长时间内未被访问的记忆文件。
恶意路径输入可能会尝试访问/memories目录外的文件。你的实现必须验证所有路径以防止目录遍历攻击。
考虑这些保护措施:
/memories开头../、..\或其他遍历模式的路径%2e%2e%2f)pathlib.Path.resolve()和relative_to())记忆工具使用类似于文本编辑器工具的错误处理模式。有关详细的错误消息和行为,请参阅上面的各个工具命令部分。常见错误包括文件未找到、权限错误、无效路径和重复文本匹配。
记忆工具可以与上下文编辑结合使用,当对话上下文增长超过配置的阈值时,它会自动清除旧的工具结果。这种组合支持长时间运行的代理工作流,否则会超过上下文限制。
启用上下文编辑且你的对话接近清除阈值时,Claude会自动收到警告通知。这会提示Claude在这些结果从上下文窗口中清除之前,将工具结果中的任何重要信息保存到记忆文件中。
工具结果被清除后,Claude可以在需要时从记忆文件中检索存储的信息,有效地将记忆视为其工作上下文的扩展。这允许Claude:
考虑一个有许多文件操作的代码重构项目:
/memories/refactoring_progress.xml)要同时使用两个功能:
你也可以排除记忆工具调用被清除,以确保Claude始终可以访问最近的记忆操作:
context_management={
"edits": [
{
"type": "clear_tool_uses_20250919",
"exclude_tools": ["memory"]
}
]
}curl https://api.anthropic.com/v1/messages \
--header "x-api-key: $ANTHROPIC_API_KEY" \
--header "anthropic-version: 2023-06-01" \
--header "content-type: application/json" \
--header "anthropic-beta: context-management-2025-06-27" \
--data '{
"model": "claude-sonnet-4-5",
"max_tokens": 2048,
"messages": [
{
"role": "user",
"content": "I'\''m working on a Python web scraper that keeps crashing with a timeout error. Here'\''s the problematic function:\n\n```python\ndef fetch_page(url, retries=3):\n for i in range(retries):\n try:\n response = requests.get(url, timeout=5)\n return response.text\n except requests.exceptions.Timeout:\n if i == retries - 1:\n raise\n time.sleep(1)\n```\n\nPlease help me debug this."
}
],
"tools": [{
"type": "memory_20250818",
"name": "memory"
}]
}'response = client.beta.messages.create(
model="claude-sonnet-4-5",
max_tokens=4096,
messages=[...],
tools=[
{
"type": "memory_20250818",
"name": "memory"
},
# 你的其他工具
],
betas=["context-management-2025-06-27"],
context_management={
"edits": [
{
"type": "clear_tool_uses_20250919",
"trigger": {
"type": "input_tokens",
"value": 100000
},
"keep": {
"type": "tool_uses",
"value": 3
}
}
]
}
)