記憶工具使 Claude 能夠通過記憶文件目錄在對話之間存儲和檢索信息。Claude 可以創建、讀取、更新和刪除在會話之間持久保存的文件,允許它隨著時間推移建立知識,而無需將所有內容保留在上下文窗口中。
記憶工具在客戶端運行——您可以通過自己的基礎設施控制數據的存儲位置和方式。
記憶工具目前處於測試版。要啟用它,請在您的 API 請求中使用測試版標頭 context-management-2025-06-27。
請通過我們的反饋表單分享您對此功能的反饋。
啟用後,Claude 在開始任務前會自動檢查其記憶目錄。Claude 可以在 /memories 目錄中創建、讀取、更新和刪除文件,以存儲在工作時學到的內容,然後在未來的對話中引用這些記憶,以更有效地處理類似任務或從中斷的地方繼續。
由於這是一個客戶端工具,Claude 進行工具調用來執行記憶操作,您的應用程序在本地執行這些操作。這使您能夠完全控制記憶的存儲位置和方式。出於安全考慮,您應該將所有記憶操作限制在 /memories 目錄中。
當您要求 Claude 幫助完成任務時,Claude 會自動首先檢查其記憶目錄。以下是典型交互的樣子:
1. 用戶請求:
"幫我回應這個客戶服務工單。"2. Claude 檢查記憶目錄:
"我會幫您回應客戶服務工單。讓我檢查我的記憶中是否有任何先前的上下文。"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 使用記憶來幫助:
"根據您的客戶服務指南,我可以幫您起草回應。請分享工單詳情..."記憶工具可用於:
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)來實現您自己的記憶後端(基於文件、數據庫、雲存儲、加密文件等)。
有關工作示例,請參閱:
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"
}]
}'您的客戶端實現需要處理這些記憶工具命令。雖然這些規範描述了 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)要同時使用兩個功能:
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
}
}
]
}
)您也可以排除記憶工具調用被清除,以確保 Claude 始終可以訪問最近的記憶操作:
context_management={
"edits": [
{
"type": "clear_tool_uses_20250919",
"exclude_tools": ["memory"]
}
]
}