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 dapat menggunakan alat editor teks dengan skema Anthropic untuk melihat dan memodifikasi file teks, membantu Anda men-debug, memperbaiki, dan meningkatkan kode atau dokumen teks lainnya. Ini memungkinkan Claude untuk berinteraksi langsung dengan file Anda, memberikan bantuan langsung daripada hanya menyarankan perubahan.
Untuk dukungan model, lihat Referensi alat.
Beberapa contoh kapan menggunakan alat editor teks adalah:
Sediakan alat editor teks (bernama str_replace_based_edit_tool) ke Claude menggunakan Messages API.
Anda dapat secara opsional menentukan parameter max_characters untuk mengontrol pemotongan saat melihat file besar.
max_characters hanya kompatibel dengan text_editor_20250728 dan versi alat editor teks yang lebih baru.
Alat editor teks dapat digunakan dengan cara berikut:
Alat editor teks mendukung beberapa perintah untuk melihat dan memodifikasi file:
Perintah view memungkinkan Claude untuk memeriksa konten file atau mendaftar konten direktori. Ini dapat membaca seluruh file atau rentang baris tertentu.
Parameter:
command: Harus "view"path: Jalur ke file atau direktori yang akan dilihatview_range (opsional): Array dari dua bilangan bulat yang menentukan nomor baris awal dan akhir yang akan dilihat. Nomor baris dimulai dari 1, dan -1 untuk baris akhir berarti baca hingga akhir file. Parameter ini hanya berlaku saat melihat file, bukan direktori.Perintah str_replace memungkinkan Claude untuk mengganti string tertentu dalam file dengan string baru. Ini digunakan untuk membuat pengeditan yang presisi.
Parameter:
command: Harus "str_replace"path: Jalur ke file yang akan dimodifikasiold_str: Teks yang akan diganti (harus cocok persis, termasuk spasi dan indentasi)new_str: Teks baru yang akan disisipkan menggantikan teks lamaPerintah create memungkinkan Claude untuk membuat file baru dengan konten yang ditentukan.
Parameter:
command: Harus "create"path: Jalur tempat file baru harus dibuatfile_text: Konten yang akan ditulis ke file baruPerintah insert memungkinkan Claude untuk menyisipkan teks pada lokasi tertentu dalam file.
Parameter:
command: Harus "insert"path: Jalur ke file yang akan dimodifikasiinsert_line: Nomor baris setelah teks akan disisipkan (0 untuk awal file)insert_text: Teks yang akan disisipkanContoh ini menunjukkan bagaimana Claude menggunakan alat editor teks untuk memperbaiki kesalahan sintaks dalam file Python.
Pertama, aplikasi Anda menyediakan Claude dengan alat editor teks dan prompt untuk memperbaiki kesalahan sintaks:
Claude menggunakan alat editor teks terlebih dahulu untuk melihat file:
{
"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"
}
}
]
}Aplikasi Anda kemudian harus membaca file dan mengembalikan isinya ke Claude:
Nomor baris
Dalam contoh di atas, hasil alat view mencakup isi file dengan nomor baris yang ditambahkan di depan setiap baris (misalnya, "1: def is_prime(n):"). Nomor baris tidak diperlukan, tetapi sangat penting untuk berhasil menggunakan parameter view_range untuk memeriksa bagian tertentu dari file dan parameter insert_line untuk menambahkan konten di lokasi yang tepat.
Claude mengidentifikasi kesalahan sintaks dan menggunakan perintah str_replace untuk memperbaikinya:
{
"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):"
}
}
]
}Aplikasi Anda kemudian harus membuat edit dan mengembalikan hasilnya:
Terakhir, Claude memberikan penjelasan lengkap tentang perbaikannya:
{
"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."
}
]
}Alat editor teks diimplementasikan sebagai alat tanpa skema. Saat menggunakan alat ini, Anda tidak perlu memberikan skema input seperti dengan alat lainnya; skema sudah tertanam dalam model Claude dan tidak dapat dimodifikasi.
Jenis alat adalah type: "text_editor_20250728" untuk model Claude 4.
Saat mengimplementasikan alat editor teks, perlu diingat:
Saat menggunakan alat editor teks, berbagai kesalahan dapat terjadi. Berikut adalah panduan tentang cara menanganinya:
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 |
Untuk informasi lebih detail tentang harga alat, lihat Harga penggunaan alat.
Alat editor teks dapat digunakan bersama dengan alat Claude lainnya. Saat menggabungkan alat, pastikan Anda:
| Tanggal | Versi | Perubahan |
|---|---|---|
| 28 Juli 2025 | text_editor_20250728 | Rilis alat editor teks yang diperbarui yang memperbaiki beberapa masalah dan menambahkan parameter max_characters opsional. Sebaliknya identik dengan text_editor_20250429. |
| 29 April 2025 | text_editor_20250429 | Rilis alat editor teks untuk Claude 4. Versi ini menghapus perintah undo_edit tetapi mempertahankan semua kemampuan lainnya. Nama alat telah diperbarui untuk mencerminkan arsitekturnya yang berbasis str_replace. |
| 13 Maret 2025 | text_editor_20250124 | Pengenalan dokumentasi alat editor teks mandiri. Versi ini dioptimalkan untuk Claude Sonnet 3.7 tetapi memiliki kemampuan identik dengan versi sebelumnya. |
| 22 Oktober 2024 | text_editor_20241022 | Rilis awal alat editor teks dengan Claude Sonnet 3.5 (). Menyediakan kemampuan untuk melihat, membuat, dan mengedit file melalui perintah , , , , dan . |
Berikut adalah beberapa ide tentang cara menggunakan alat editor teks dengan cara yang lebih nyaman dan kuat:
Alat editor teks memungkinkan Claude untuk bekerja langsung dengan basis kode Anda, mendukung alur kerja dari debugging hingga dokumentasi otomatis.
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)Sediakan Claude dengan alat editor teks dan prompt pengguna
Claude menggunakan alat untuk memeriksa file atau direktori
view untuk memeriksa konten file atau mendaftar konten direktoritool_use dengan perintah viewJalankan perintah view dan kembalikan hasil
max_characters ditentukan dalam konfigurasi alat, potong konten file ke panjang tersebutuser baru yang berisi blok konten tool_resultClaude menggunakan alat untuk memodifikasi file
str_replace untuk membuat perubahan atau insert untuk menambahkan teks pada nomor baris tertentu.str_replace, Claude membuat permintaan penggunaan alat yang diformat dengan benar dengan teks lama dan teks baru untuk menggantinyaJalankan pengeditan dan kembalikan hasil
Claude memberikan analisis dan penjelasannya
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)Inisialisasi implementasi editor Anda
Buat fungsi pembantu untuk menangani operasi file seperti membaca, menulis, dan memodifikasi file. Pertimbangkan untuk mengimplementasikan fungsionalitas cadangan untuk memulihkan dari kesalahan.
Tangani panggilan alat editor
Buat fungsi yang memproses panggilan alat dari Claude berdasarkan jenis perintah:
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
passImplementasikan langkah-langkah keamanan
Tambahkan validasi dan pemeriksaan keamanan:
Proses respons Claude
Ekstrak dan tangani panggilan alat dari respons 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,
}viewcreatestr_replaceinsertundo_edit