The memory tool enables Claude to store and retrieve information across conversations through a memory file directory. Claude can create, read, update, and delete files that persist between sessions, allowing it to build knowledge over time without keeping everything in the context window.
This is the key primitive for just-in-time context retrieval: rather than loading all relevant information upfront, agents store what they learn in memory and pull it back on demand. This keeps the active context focused on what's currently relevant, critical for long-running workflows where loading everything at once would overwhelm the context window. See Effective context engineering for the broader pattern.
The memory tool operates client-side: you control where and how the data is stored through your own infrastructure.
Reach out through the feedback form to share your feedback on this feature.
This feature is eligible for Zero Data Retention (ZDR). When your organization has a ZDR arrangement, data sent through this feature is not stored after the API response is returned.
When enabled, Claude automatically checks its memory directory before starting tasks. Claude can create, read, update, and delete files in the /memories directory to store what it learns while working, then reference those memories in future conversations to handle similar tasks more effectively or pick up where it left off.
Since this is a client-side tool, Claude makes tool calls to perform memory operations, and your application executes those operations locally. This gives you complete control over where and how the memory is stored. For security, you should restrict all memory operations to the /memories directory.
When you ask Claude to help with a task, Claude automatically checks its memory directory first. Here's what a typical interaction looks like:
1. User request:
"Help me respond to this customer service ticket."2. Claude checks the memory directory:
"I'll help you respond to the customer service ticket. Let me check my memory for any previous context."Claude calls the memory tool:
{
"type": "tool_use",
"id": "toolu_01C4D5E6F7G8H9I0J1K2L3M4",
"name": "memory",
"input": {
"command": "view",
"path": "/memories"
}
}3. Your application returns the directory contents:
{
"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 reads relevant files:
{
"type": "tool_use",
"id": "toolu_01D5E6F7G8H9I0J1K2L3M4N5",
"name": "memory",
"input": {
"command": "view",
"path": "/memories/customer_service_guidelines.xml"
}
}5. Your application returns the file contents:
{
"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 uses the memory to help:
"Based on your customer service guidelines, I can help you craft a response. Please share the ticket details..."The memory tool is available on:
claude-opus-4-6)claude-opus-4-5-20251101)claude-opus-4-1-20250805)claude-opus-4-20250514)claude-sonnet-4-6)claude-sonnet-4-5-20250929)claude-sonnet-4-20250514)claude-haiku-4-5-20251001)To use the memory tool:
To handle memory tool operations in your application, you need to implement handlers for each memory command. The SDKs provide memory tool helpers that handle the tool interface. You can subclass BetaAbstractMemoryTool (Python) or use betaMemoryTool (TypeScript) to implement your own memory backend (file-based, database, cloud storage, encrypted files, etc.).
For working examples, see:
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" \
--data '{
"model": "claude-opus-4-6",
"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"
}]
}'Your client-side implementation needs to handle these memory tool commands. While these specifications describe the recommended behaviors that Claude is most familiar with, you can modify your implementation and return strings as needed for your use case.
Shows directory contents or file contents with optional line ranges:
{
"command": "view",
"path": "/memories",
"view_range": [1, 10] // Optional: view specific lines
}For directories: Return a listing that shows files and directories with their sizes:
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).) and node_modulesFor files: Return file contents with a header and line numbers:
Here's the content of {path} with line numbers:
{line_numbers}{tab}{content}Line number formatting:
"File {path} exceeds maximum line limit of 999,999 lines."Example output:
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."Create a new file:
{
"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"Replace text in a file:
{
"command": "str_replace",
"path": "/memories/preferences.txt",
"old_str": "Favorite color: blue",
"new_str": "Favorite color: green"
}"The memory file has been edited." followed by a snippet of the edited file with line numbers"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 appears multiple times, return: "No replacement was performed. Multiple occurrences of old_str `\{old_str}` in lines: {line_numbers}. Please ensure it is unique"If the path is a directory, return a "file does not exist" error.
Insert text at a specific line:
{
"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}]"If the path is a directory, return a "file does not exist" error.
Delete a file or directory:
{
"command": "delete",
"path": "/memories/old_file.txt"
}"Successfully deleted {path}""Error: The path {path} does not exist"Deletes the directory and all its contents recursively.
Rename or move a file/directory:
{
"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"Renames the directory.
This instruction is automatically included in the system prompt when the memory tool is enabled:
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.If you observe Claude creating cluttered memory files, you can include this instruction:
Note: when editing your memory folder, always try to keep its content up-to-date, coherent and organized. You can rename or delete files that are no longer relevant. Do not create new files unless necessary.
You can also guide what Claude writes to memory. For example: "Only write down information relevant to <topic> in your memory system."
Here are important security concerns when implementing your memory store:
Claude will usually refuse to write down sensitive information in memory files. However, you may want to implement stricter validation that strips out potentially sensitive information.
Consider tracking memory file sizes and preventing files from growing too large. Consider adding a maximum number of characters the memory read command can return, and let Claude paginate through contents.
Consider clearing out memory files periodically that haven't been accessed in an extended time.
Malicious path inputs could attempt to access files outside the /memories directory. Your implementation MUST validate all paths to prevent directory traversal attacks.
Consider these safeguards:
/memories../, ..\\, or other traversal patterns%2e%2e%2f)pathlib.Path.resolve() and relative_to())The memory tool uses similar error handling patterns to the text editor tool. See the individual tool command sections above for detailed error messages and behaviors. Common errors include file not found, permission errors, invalid paths, and duplicate text matches.
The memory tool can be combined with context editing, which automatically clears old tool results when conversation context grows beyond a configured threshold. This combination enables long-running agentic workflows that would otherwise exceed context limits.
When context editing is enabled and your conversation approaches the clearing threshold, Claude automatically receives a warning notification. This prompts Claude to preserve any important information from tool results into memory files before those results are cleared from the context window.
After tool results are cleared, Claude can retrieve the stored information from memory files whenever needed, effectively treating memory as an extension of its working context. This allows Claude to:
Consider a code refactoring project with many file operations:
/memories/refactoring_progress.xml)To use both features together:
response = client.messages.create(
model="claude-opus-4-6",
max_tokens=4096,
messages=[...],
tools=[
{"type": "memory_20250818", "name": "memory"},
# Your other tools
],
context_management={
"edits": [
{
"type": "clear_tool_uses_20250919",
"trigger": {"type": "input_tokens", "value": 100000},
"keep": {"type": "tool_uses", "value": 3},
}
]
},
)You can also exclude memory tool calls from being cleared to ensure Claude always has access to recent memory operations:
context_management = {
"edits": [{"type": "clear_tool_uses_20250919", "exclude_tools": ["memory"]}]
}The memory tool can also be paired with compaction, which provides server-side summarization of older conversation context. While context editing clears specific tool results on the client side, compaction automatically summarizes the entire conversation on the server side when it approaches the context window limit.
For long-running agentic workflows, consider using both: compaction keeps the active context manageable without client-side bookkeeping, and memory persists important information across compaction boundaries so that nothing critical is lost in the summary.
For long-running software projects that span multiple agent sessions, memory files need to be bootstrapped deliberately, not just written ad hoc as work progresses. The pattern below turns memory into a structured recovery mechanism, so each new session can pick up exactly where the last one left off.
Initializer session: The first session sets up the memory artifacts before any substantive work begins. This includes a progress log (tracking what has been done and what comes next), a feature checklist (defining the scope of work), and a reference to any startup or initialization script the project needs.
Subsequent sessions: Each new session opens by reading those memory artifacts. This recovers the full state of the project in seconds, without needing to re-explore the codebase or retrace earlier decisions.
End-of-session update: Before a session ends, it updates the progress log with what was completed and what remains. This ensures the next session has an accurate starting point.
Work on one feature at a time. Only mark a feature complete after end-to-end verification confirms it works, not just after the code is written. This keeps the progress log trustworthy and prevents scope creep from compounding across sessions.
For a detailed case study of this pattern in practice, including the initializer script, progress file structure, and git-based recovery, see Effective harnesses for long-running agents.
Was this page helpful?