Was this page helpful?
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.
Claudeは、Anthropicスキーマのテキストエディタツールを使用してテキストファイルを表示・修正でき、コードやその他のテキストドキュメントのデバッグ、修正、改善を支援します。これにより、Claudeはファイルと直接対話でき、変更を提案するだけでなく、実践的な支援を提供できます。
モデルサポートについては、ツールリファレンスを参照してください。
テキストエディタツールを使用する場合の例は以下の通りです:
Messages APIを使用してテキストエディタツール(str_replace_based_edit_toolという名前)をClaudeに提供します。
大きなファイルを表示する際の切り詰めを制御するために、オプションでmax_charactersパラメータを指定できます。
max_charactersはtext_editor_20250728以降のバージョンのテキストエディタツールとのみ互換性があります。
テキストエディタツールは以下のように使用できます:
テキストエディタツールは、ファイルの表示と修正のための複数のコマンドをサポートしています:
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 にテキストエディタツールと構文エラーを修正するためのプロンプトを提供します:
Claude はまずテキストエディタツールを使用してファイルを表示します:
{
"id": "msg_01XAbCDeFgHiJkLmNoPQrStU",
"model": "claude-opus-4-7",
"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 に返す必要があります:
行番号
上記の例では、view ツール結果には各行の前に行番号が付いたファイル内容が含まれています(例:「1: def is_prime(n):」)。行番号は必須ではありませんが、view_range パラメータを使用してファイルの特定のセクションを検査し、insert_line パラメータを使用して正確な場所にコンテンツを追加するために必須です。
Claude は構文エラーを特定し、str_replace コマンドを使用してそれを修正します:
{
"id": "msg_01VwXyZAbCdEfGhIjKlMnO",
"model": "claude-opus-4-7",
"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):"
}
}
]
}その後、アプリケーションは編集を行い、結果を返す必要があります:
最後に、Claude は修正の完全な説明を提供します:
{
"id": "msg_01IjKlMnOpQrStUvWxYzAb",
"model": "claude-opus-4-7",
"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" です。
テキストエディタツールを実装する際は、以下の点に注意してください:
テキストエディタツールを使用する場合、さまざまなエラーが発生する可能性があります。以下は、それらを処理する方法に関するガイダンスです:
The text editor tool uses the same pricing structure as other tools used with Claude. It follows the standard input and output token pricing based on the Claude model you're using.
In addition to the base tokens, the following additional input tokens are needed for the text editor tool:
| Tool | Additional input tokens |
|---|---|
text_editor_20250429 (Claude 4.x) | 700 tokens |
text_editor_20250124 (Claude Sonnet 3.7 (deprecated)) | 700 tokens |
ツール価格の詳細については、ツール使用価格を参照してください。
テキストエディタツールは他の 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、、、および コマンドを通じてファイルを表示、作成、編集する機能を提供します。 |
テキストエディタツールをより便利で強力な方法で使用するためのアイデアをいくつか紹介します:
テキストエディタツールにより、Claude はコードベースで直接作業でき、デバッグから自動ドキュメント化までのワークフローをサポートします。
client = anthropic.Anthropic()
response = client.messages.create(
model="claude-opus-4-7",
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が分析と説明を提供する
client = anthropic.Anthropic()
response = client.messages.create(
model="claude-opus-4-7",
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)response = client.messages.create(
model="claude-opus-4-7",
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)response = client.messages.create(
model="claude-opus-4-7",
max_tokens=1024,
tools=[{"type": "text_editor_20250728", "name": "str_replace_based_edit_tool"}],
messages=[
# Previous 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 からのツール呼び出しを処理する関数を作成します:
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":
# Read and return file contents
pass
elif command == "str_replace":
# Replace text in file
pass
elif command == "create":
# Create new file
pass
elif command == "insert":
# Insert text at location
passセキュリティ対策を実装する
検証とセキュリティチェックを追加します:
Claude の応答を処理する
Claude の応答からツール呼び出しを抽出して処理します:
# Process tool use in Claude's response
for content in response.content:
if content.type == "tool_use":
# Execute the tool based on command
result = handle_editor_tool(content)
# Return result to Claude
tool_result = {
"type": "tool_result",
"tool_use_id": content.id,
"content": result,
}str_replaceinsertundo_edit