メモリツールにより、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)を使用して、独自のメモリバックエンド(ファイルベース、データベース、クラウドストレージ、暗号化ファイルなど)を実装できます。
動作例については、以下を参照してください:
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が散らかったメモリファイルを作成していることに気付いた場合、この指示を含めることができます:
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.
また、Claudeがメモリに何を書き込むかをガイドすることもできます。例えば、「Only write down information relevant to <topic> in your memory system.」
メモリストアを実装する際の重要なセキュリティ上の懸念事項は以下の通りです:
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"
},
# 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
}
}
]
}
)メモリツール呼び出しがクリアされるのを除外して、Claudeが常に最近のメモリ操作にアクセスできるようにすることもできます:
context_management={
"edits": [
{
"type": "clear_tool_uses_20250919",
"exclude_tools": ["memory"]
}
]
}