Loading...
    • Developer Guide
    • API Reference
    • MCP
    • Resources
    • Release Notes
    Search...
    ⌘K

    First steps

    Intro to ClaudeQuickstart

    Models & pricing

    Models overviewChoosing a modelWhat's new in Claude 4.5Migrating to Claude 4.5Model deprecationsPricing

    Build with Claude

    Features overviewUsing the Messages APIContext windowsPrompting best practices

    Capabilities

    Prompt cachingContext editingExtended thinkingStreaming MessagesBatch processingCitationsMultilingual supportToken countingEmbeddingsVisionPDF supportFiles APISearch resultsGoogle Sheets add-on

    Tools

    OverviewHow to implement tool useToken-efficient tool useFine-grained tool streamingBash toolCode execution toolComputer use toolText editor toolWeb fetch toolWeb search toolMemory tool

    Agent Skills

    OverviewQuickstartBest practicesUsing Skills with the API

    Agent SDK

    OverviewTypeScript SDKPython SDK

    Guides

    Streaming InputHandling PermissionsSession ManagementHosting the Agent SDKModifying system promptsMCP in the SDKCustom ToolsSubagents in the SDKSlash Commands in the SDKAgent Skills in the SDKTracking Costs and UsageTodo ListsPlugins in the SDK

    MCP in the API

    MCP connectorRemote MCP servers

    Claude on 3rd-party platforms

    Amazon BedrockVertex AI

    Prompt engineering

    OverviewPrompt generatorUse prompt templatesPrompt improverBe clear and directUse examples (multishot prompting)Let Claude think (CoT)Use XML tagsGive Claude a role (system prompts)Prefill Claude's responseChain complex promptsLong context tipsExtended thinking tips

    Test & evaluate

    Define success criteriaDevelop test casesUsing the Evaluation ToolReducing latency

    Strengthen guardrails

    Reduce hallucinationsIncrease output consistencyMitigate jailbreaksStreaming refusalsReduce prompt leakKeep Claude in character

    Administration and monitoring

    Admin API overviewUsage and Cost APIClaude Code Analytics API
    Console
    Tools

    Memory tool

    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.

    The memory tool operates client-side—you control where and how the data is stored through your own infrastructure.

    The memory tool is currently in beta. To enable it, use the beta header context-management-2025-06-27 in your API requests.

    Please reach out through our feedback form to share your feedback on this feature.

    Use cases

    • Maintain project context across multiple agent executions
    • Learn from past interactions, decisions, and feedback
    • Build knowledge bases over time
    • Enable cross-conversation learning where Claude improves at recurring workflows

    How it works

    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.

    Example: How memory tool calls work

    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": "Directory: /memories\n- customer_service_guidelines.xml\n- 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": "<guidelines>\n<addressing_customers>\n- Always address customers by their first name\n- 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..."

    Supported models

    The memory tool is available on:

    • Claude Sonnet 4.5 (claude-sonnet-4-5-20250929)
    • Claude Sonnet 4 (claude-sonnet-4-20250514)
    • Claude Haiku 4.5 (claude-haiku-4-5-20251001)
    • Claude Opus 4.1 (claude-opus-4-1-20250805)
    • Claude Opus 4 (claude-opus-4-20250514)

    Getting started

    To use the memory tool:

    1. Include the beta header context-management-2025-06-27 in your API requests
    2. Add the memory tool to your request
    3. Implement client-side handlers for memory operations

    To handle memory tool operations in your application, you need to implement handlers for each memory command. Our 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:

    • Python: examples/memory/basic.py
    • TypeScript: examples/tools-helpers-memory.ts

    Basic usage

    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"
            }]
        }'

    Tool commands

    Your client-side implementation needs to handle these memory tool commands:

    view

    Shows directory contents or file contents with optional line ranges:

    {
      "command": "view",
      "path": "/memories",
      "view_range": [1, 10]  // Optional: view specific lines
    }

    create

    Create or overwrite a file:

    {
      "command": "create",
      "path": "/memories/notes.txt",
      "file_text": "Meeting notes:\n- Discussed project timeline\n- Next steps defined\n"
    }

    str_replace

    Replace text in a file:

    {
      "command": "str_replace",
      "path": "/memories/preferences.txt",
      "old_str": "Favorite color: blue",
      "new_str": "Favorite color: green"
    }

    insert

    Insert text at a specific line:

    {
      "command": "insert",
      "path": "/memories/todo.txt",
      "insert_line": 2,
      "insert_text": "- Review memory tool documentation\n"
    }

    delete

    Delete a file or directory:

    {
      "command": "delete",
      "path": "/memories/old_file.txt"
    }

    rename

    Rename or move a file/directory:

    {
      "command": "rename",
      "old_path": "/memories/draft.txt",
      "new_path": "/memories/final.txt"
    }

    Prompting guidance

    We automatically include this instruction to the system prompt when the memory tool is included:

    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, e.g., "Only write down information relevant to <topic> in your memory system."

    Security considerations

    Here are important security concerns when implementing your memory store:

    Sensitive information

    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.

    File storage size

    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.

    Memory expiration

    Consider clearing out memory files periodically that haven't been accessed in an extended time.

    Path traversal protection

    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:

    • Validate that all paths start with /memories
    • Resolve paths to their canonical form and verify they remain within the memory directory
    • Reject paths containing sequences like ../, ..\\, or other traversal patterns
    • Watch for URL-encoded traversal sequences (%2e%2e%2f)
    • Use your language's built-in path security utilities (e.g., Python's pathlib.Path.resolve() and relative_to())

    Error handling

    The memory tool uses the same error handling patterns as the text editor tool. Common errors include file not found, permission errors, and invalid paths.

    Using with Context Editing

    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.

    How they work together

    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:

    • Continue complex, multi-step workflows without losing critical information
    • Reference past work and decisions even after tool results are removed
    • Maintain coherent context across conversations that would exceed typical context limits
    • Build up a knowledge base over time while keeping the active context window manageable

    Example workflow

    Consider a code refactoring project with many file operations:

    1. Claude makes numerous edits to files, generating many tool results
    2. As the context grows and approaches your threshold, Claude receives a warning
    3. Claude summarizes the changes made so far to a memory file (e.g., /memories/refactoring_progress.xml)
    4. Context editing clears the older tool results automatically
    5. Claude continues working, referencing the memory file when it needs to recall what changes were already completed
    6. The workflow can continue indefinitely, with Claude managing both active context and persistent memory

    Configuration

    To use both features together:

    response = client.beta.messages.create(
        model="claude-sonnet-4-5",
        max_tokens=4096,
        messages=[...],
        tools=[
            {
                "type": "memory_20250818",
                "name": "memory"
            },
            # Your other tools
        ],
        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
                    }
                }
            ]
        }
    )

    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"]
            }
        ]
    }
    • Use cases
    • How it works
    • Example: How memory tool calls work
    • Supported models
    • Getting started
    • Basic usage
    • Tool commands
    • view
    • create
    • str_replace
    • insert
    • delete
    • rename
    • Prompting guidance
    • Security considerations
    • Sensitive information
    • File storage size
    • Memory expiration
    • Path traversal protection
    • Error handling
    • Using with Context Editing
    • How they work together
    • Example workflow
    • Configuration
    © 2025 ANTHROPIC PBC

    Products

    • Claude
    • Claude Code
    • Max plan
    • Team plan
    • Enterprise plan
    • Download app
    • Pricing
    • Log in

    Features

    • Claude and Slack
    • Claude in Excel

    Models

    • Opus
    • Sonnet
    • Haiku

    Solutions

    • AI agents
    • Code modernization
    • Coding
    • Customer support
    • Education
    • Financial services
    • Government
    • Life sciences

    Claude Developer Platform

    • Overview
    • Developer docs
    • Pricing
    • Amazon Bedrock
    • Google Cloud’s Vertex AI
    • Console login

    Learn

    • Blog
    • Catalog
    • Courses
    • Connectors
    • Customer stories
    • Engineering at Anthropic
    • Events
    • Powered by Claude
    • Service partners
    • Startups program

    Company

    • Anthropic
    • Careers
    • Economic Futures
    • Research
    • News
    • Responsible Scaling Policy
    • Security and compliance
    • Transparency

    Help and security

    • Availability
    • Status
    • Support center

    Terms and policies

    • Privacy policy
    • Responsible disclosure policy
    • Terms of service: Commercial
    • Terms of service: Consumer
    • Usage policy

    Products

    • Claude
    • Claude Code
    • Max plan
    • Team plan
    • Enterprise plan
    • Download app
    • Pricing
    • Log in

    Features

    • Claude and Slack
    • Claude in Excel

    Models

    • Opus
    • Sonnet
    • Haiku

    Solutions

    • AI agents
    • Code modernization
    • Coding
    • Customer support
    • Education
    • Financial services
    • Government
    • Life sciences

    Claude Developer Platform

    • Overview
    • Developer docs
    • Pricing
    • Amazon Bedrock
    • Google Cloud’s Vertex AI
    • Console login

    Learn

    • Blog
    • Catalog
    • Courses
    • Connectors
    • Customer stories
    • Engineering at Anthropic
    • Events
    • Powered by Claude
    • Service partners
    • Startups program

    Company

    • Anthropic
    • Careers
    • Economic Futures
    • Research
    • News
    • Responsible Scaling Policy
    • Security and compliance
    • Transparency

    Help and security

    • Availability
    • Status
    • Support center

    Terms and policies

    • Privacy policy
    • Responsible disclosure policy
    • Terms of service: Commercial
    • Terms of service: Consumer
    • Usage policy
    © 2025 ANTHROPIC PBC