この機能はZero Data Retention(ZDR)の対象です。組織がZDR契約を締結している場合、この機能を通じて送信されたデータは、APIレスポンスが返された後に保存されることはありません。
Claudeは、Anthropicスキーマのテキストエディタツールを使用してテキストファイルを表示および変更でき、コードやその他のテキストドキュメントのデバッグ、修正、改善を支援します。これにより、Claudeは変更を提案するだけでなく、ファイルと直接やり取りして実践的な支援を提供できます。
モデルのサポートについては、ツールリファレンスを参照してください。
テキストエディタツールを使用する例をいくつか挙げます:
Messages APIを使用して、テキストエディタツール(str_replace_based_edit_toolという名前)をClaudeに提供します。
大きなファイルを表示する際の切り詰めを制御するために、オプションでmax_charactersパラメータを指定できます。
max_charactersは、テキストエディタツールのtext_editor_20250728以降のバージョンとのみ互換性があります。
client = anthropic.Anthropic()
response = client.messages.create(
model="claude-opus-4-8",
max_tokens=1024,
tools=[
{
"type": "text_editor_20250728",
"name": "str_replace_based_edit_tool",
"max_characters": 10000,
}
],
messages=[
{
"role": "user",
"content": "There's a syntax error in my primes.py file. Can you help me fix it?",
}
],
)
print(response)テキストエディタツールは次のように使用できます:
Claudeにテキストエディタツールとユーザープロンプトを提供する
Claudeがツールを使用してファイルまたはディレクトリを確認する
viewコマンドを使用してファイルの内容を確認したり、ディレクトリの内容を一覧表示したりしますviewコマンドを含むtool_useコンテンツブロックが含まれますviewコマンドを実行して結果を返す
max_charactersパラメータが指定されている場合は、ファイルの内容をその長さに切り詰めますtool_resultコンテンツブロックを含む新しいuserメッセージで会話を継続することにより、結果をClaudeに返しますClaudeがツールを使用してファイルを変更する
str_replaceなどのコマンドを使用して変更を加えたり、insertを使用して特定の行番号にテキストを追加したりすることがあります。str_replaceコマンドを使用する場合、Claudeは古いテキストとそれを置き換える新しいテキストを含む、適切にフォーマットされたツール使用リクエストを構築します編集を実行して結果を返す
Claudeが分析と説明を提供する
テキストエディタツールは、ファイルの表示と変更のためのいくつかのコマンドをサポートしています:
viewコマンドを使用すると、Claudeはファイルの内容を確認したり、ディレクトリの内容を一覧表示したりできます。ファイル全体または特定の行範囲を読み取ることができます。
パラメータ:
command: "view"である必要がありますpath: 表示するファイルまたはディレクトリへのパスview_range(オプション): 表示する開始行番号と終了行番号を指定する2つの整数の配列。行番号は1から始まり、終了行に-1を指定するとファイルの末尾まで読み取ります。このパラメータはファイルを表示する場合にのみ適用され、ディレクトリには適用されません。str_replaceコマンドを使用すると、Claudeはファイル内の特定の文字列を新しい文字列に置き換えることができます。これは正確な編集を行うために使用されます。
パラメータ:
command: "str_replace"である必要がありますpath: 変更するファイルへのパスold_str: 置き換えるテキスト(空白やインデントを含めて正確に一致する必要があります)new_str: 古いテキストの代わりに挿入する新しいテキストcreateコマンドを使用すると、Claudeは指定された内容で新しいファイルを作成できます。
パラメータ:
command: "create"である必要がありますpath: 新しいファイルを作成するパスfile_text: 新しいファイルに書き込む内容insertコマンドを使用すると、Claudeはファイル内の特定の場所にテキストを挿入できます。
パラメータ:
command: "insert"である必要がありますpath: 変更するファイルへのパスinsert_line: テキストを挿入する位置の直前の行番号(ファイルの先頭の場合は0)insert_text: 挿入するテキストこの例では、ClaudeがテキストエディタツールをどのようにしてPythonファイルの構文エラーを修正するために使用するかを示します。
まず、アプリケーションがClaudeにテキストエディタツールと構文エラーを修正するためのプロンプトを提供します:
client = anthropic.Anthropic()
response = client.messages.create(
model="claude-opus-4-8",
max_tokens=1024,
tools=[{"type": "text_editor_20250728", "name": "str_replace_based_edit_tool"}],
messages=[
{
"role": "user",
"content": "There's a syntax error in my primes.py file. Can you help me fix it?",
}
],
)
print(response)Claudeはまずテキストエディタツールを使用してファイルを表示します:
{
"id": "msg_01XAbCDeFgHiJkLmNoPQrStU",
"model": "claude-opus-4-8",
"stop_reason": "tool_use",
"role": "assistant",
"content": [
{
"type": "text",
"text": "I'll help you fix the syntax error in your primes.py file. First, let me take a look at the file to identify the issue."
},
{
"type": "tool_use",
"id": "toolu_01AbCdEfGhIjKlMnOpQrStU",
"name": "str_replace_based_edit_tool",
"input": {
"command": "view",
"path": "primes.py"
}
}
]
}次に、アプリケーションはファイルを読み取り、その内容をClaudeに返す必要があります:
response = client.messages.create(
model="claude-opus-4-8",
max_tokens=1024,
tools=[{"type": "text_editor_20250728", "name": "str_replace_based_edit_tool"}],
messages=[
{
"role": "user",
"content": "There's a syntax error in my primes.py file. Can you help me fix it?",
},
{
"role": "assistant",
"content": [
{
"type": "text",
"text": "I'll help you fix the syntax error in your primes.py file. First, let me take a look at the file to identify the issue.",
},
{
"type": "tool_use",
"id": "toolu_01AbCdEfGhIjKlMnOpQrStU",
"name": "str_replace_based_edit_tool",
"input": {"command": "view", "path": "primes.py"},
},
],
},
{
"role": "user",
"content": [
{
"type": "tool_result",
"tool_use_id": "toolu_01AbCdEfGhIjKlMnOpQrStU",
"content": '1: def is_prime(n):\n2: """Check if a number is prime."""\n3: if n <= 1:\n4: return False\n5: if n <= 3:\n6: return True\n7: if n % 2 == 0 or n % 3 == 0:\n8: return False\n9: i = 5\n10: while i * i <= n:\n11: if n % i == 0 or n % (i + 2) == 0:\n12: return False\n13: i += 6\n14: return True\n15: \n16: def get_primes(limit):\n17: """Generate a list of prime numbers up to the given limit."""\n18: primes = []\n19: for num in range(2, limit + 1)\n20: if is_prime(num):\n21: primes.append(num)\n22: return primes\n23: \n24: def main():\n25: """Main function to demonstrate prime number generation."""\n26: limit = 100\n27: prime_list = get_primes(limit)\n28: print(f"Prime numbers up to {limit}:")\n29: print(prime_list)\n30: print(f"Found {len(prime_list)} prime numbers.")\n31: \n32: if __name__ == "__main__":\n33: main()',
}
],
},
],
)
print(response)行番号
上記の例では、viewツールの結果には、各行の先頭に行番号が付加されたファイルの内容が含まれています(例:「1: def is_prime(n):」)。行番号は必須ではありませんが、ファイルの特定のセクションを確認するためのview_rangeパラメータや、正確な位置にコンテンツを追加するためのinsert_lineパラメータを正しく使用するためには不可欠です。
Claudeは構文エラーを特定し、str_replaceコマンドを使用して修正します:
{
"id": "msg_01VwXyZAbCdEfGhIjKlMnO",
"model": "claude-opus-4-8",
"stop_reason": "tool_use",
"role": "assistant",
"content": [
{
"type": "text",
"text": "I found the syntax error in your primes.py file. In the `get_primes` function, there is a missing colon (:) at the end of the for loop line. Let me fix that for you."
},
{
"type": "tool_use",
"id": "toolu_01PqRsTuVwXyZAbCdEfGh",
"name": "str_replace_based_edit_tool",
"input": {
"command": "str_replace",
"path": "primes.py",
"old_str": " for num in range(2, limit + 1)",
"new_str": " for num in range(2, limit + 1):"
}
}
]
}次に、アプリケーションは編集を行い、結果を返す必要があります:
response = client.messages.create(
model="claude-opus-4-8",
max_tokens=1024,
tools=[{"type": "text_editor_20250728", "name": "str_replace_based_edit_tool"}],
messages=[
# 以前のメッセージ...
{
"role": "assistant",
"content": [
{
"type": "text",
"text": "I found the syntax error in your primes.py file. In the `get_primes` function, there is a missing colon (:) at the end of the for loop line. Let me fix that for you.",
},
{
"type": "tool_use",
"id": "toolu_01PqRsTuVwXyZAbCdEfGh",
"name": "str_replace_based_edit_tool",
"input": {
"command": "str_replace",
"path": "primes.py",
"old_str": " for num in range(2, limit + 1)",
"new_str": " for num in range(2, limit + 1):",
},
},
],
},
{
"role": "user",
"content": [
{
"type": "tool_result",
"tool_use_id": "toolu_01PqRsTuVwXyZAbCdEfGh",
"content": "Successfully replaced text at exactly one location.",
}
],
},
],
)
print(response)最後に、Claudeは修正の完全な説明を提供します:
{
"id": "msg_01IjKlMnOpQrStUvWxYzAb",
"model": "claude-opus-4-8",
"stop_reason": "end_turn",
"role": "assistant",
"content": [
{
"type": "text",
"text": "I've fixed the syntax error in your primes.py file. The issue was in the `get_primes` function at line 19. There was a missing colon (:) at the end of the for loop line.\n\nHere's what I changed:\n\nFrom:\n```python\nfor num in range(2, limit + 1)\n```\n\nTo:\n```python\nfor num in range(2, limit + 1):\n```\n\nIn Python, a colon is required at the end of statements that start a new code block, such as for loops, if statements, function definitions, etc. Without the colon, Python raises a syntax error because it doesn't know where the code block begins.\n\nYour code should now run correctly. The function will properly generate a list of prime numbers up to the specified limit."
}
]
}テキストエディタツールはスキーマレスツールとして実装されています。このツールを使用する場合、他のツールのように入力スキーマを提供する必要はありません。スキーマはClaudeのモデルに組み込まれており、変更できません。
Claude 4モデルのツールタイプはtype: "text_editor_20250728"です。
エディタ実装を初期化する
ファイルの読み取り、書き込み、変更などのファイル操作を処理するヘルパー関数を作成します。ミスから回復するためのバックアップ機能の実装を検討してください。
エディタツールの呼び出しを処理する
コマンドタイプに基づいてClaudeからのツール呼び出しを処理する関数を作成します:
def handle_editor_tool(tool_call):
input_params = tool_call.input
command = input_params.get("command", "")
file_path = input_params.get("path", "")
if command == "view":
# ファイルの内容を読み取って返す
pass
elif command == "str_replace":
# ファイル内のテキストを置換する
pass
elif command == "create":
# 新しいファイルを作成する
pass
elif command == "insert":
# 指定位置にテキストを挿入する
passセキュリティ対策を実装する
検証とセキュリティチェックを追加します:
Claudeのレスポンスを処理する
Claudeのレスポンスからツール呼び出しを抽出して処理します:
# Claudeのレスポンス内のツール使用を処理する
for content in response.content:
if content.type == "tool_use":
# コマンドに基づいてツールを実行する
result = handle_editor_tool(content)
# 結果をClaudeに返す
tool_result = {
"type": "tool_result",
"tool_use_id": content.id,
"content": result,
}テキストエディタツールを実装する際は、次の点に注意してください:
テキストエディタツールを使用する際、さまざまなエラーが発生する可能性があります。以下はそれらの処理方法に関するガイダンスです:
テキストエディタツールは、Claudeで使用される他のツールと同じ料金体系を採用しています。使用しているClaudeモデルに基づいた標準の入力および出力トークン料金に従います。
基本トークンに加えて、テキストエディタツールには以下の追加入力トークンが必要です。
| ツール | 追加入力トークン |
|---|---|
text_editor_20250429(Claude 4.x) | 700トークン |
ツールの料金に関する詳細については、ツール使用の料金を参照してください。
テキストエディタツールは、他のClaudeツールと併用できます。ツールを組み合わせる際は、次の点を確認してください:
| 日付 | バージョン | 変更内容 |
|---|---|---|
| 2025年7月28日 | text_editor_20250728 | いくつかの問題を修正し、オプションのmax_charactersパラメータを追加した更新版テキストエディタツールのリリース。それ以外はtext_editor_20250429と同一です。 |
| 2025年4月29日 | text_editor_20250429 | Claude 4向けテキストエディタツールのリリース。このバージョンではundo_editコマンドが削除されていますが、その他のすべての機能は維持されています。ツール名はstr_replaceベースのアーキテクチャを反映するように更新されました。 |
| 2025年3月13日 | text_editor_20250124 | スタンドアロンのテキストエディタツールドキュメントの導入。このバージョンはClaude Sonnet 3.7向けに最適化されていますが、以前のバージョンと同一の機能を持っています。 |
| 2024年10月22日 | text_editor_20241022 | Claude Sonnet 3.5(廃止済み)でのテキストエディタツールの初回リリース。view、create、str_replace、insert、undo_editコマンドによるファイルの表示、作成、編集機能を提供します。 |
テキストエディタツールをより便利で強力な方法で使用するためのアイデアをいくつか紹介します:
テキストエディタツールにより、Claudeはコードベースを直接操作でき、デバッグから自動ドキュメント作成までのワークフローをサポートします。
Was this page helpful?