メモリツールにより、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-opus-4-6)claude-opus-4-5-20251101)claude-opus-4-1-20250805)claude-opus-4-20250514)claude-sonnet-4-5-20250929)claude-sonnet-4-20250514)claude-haiku-4-5-20251001)メモリツールを使用するには:
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-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"
}]
}'クライアントサイドの実装では、これらのメモリツールコマンドを処理する必要があります。これらの仕様は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がメモリに書き込む内容をガイドすることもできます。例:「メモリシステムには <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-opus-4-6",
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"]
}
]
}メモリツールはコンパクションとも組み合わせることができます。コンパクションは、古い会話コンテキストのサーバーサイド要約を提供します。コンテキスト編集がクライアントサイドで特定のツール結果をクリアするのに対し、コンパクションは会話がコンテキストウィンドウの制限に近づいたときに、サーバーサイドで会話全体を自動的に要約します。
長時間実行のエージェントワークフローでは、両方の使用を検討してください。コンパクションはクライアントサイドの管理なしにアクティブなコンテキストを管理可能に保ち、メモリはコンパクション境界を越えて重要な情報を永続化するため、要約で重要な情報が失われることがありません。
Was this page helpful?