Model compatibility
Model | Tool Version |
---|---|
Claude Sonnet 4, Sonnet 4.5, Opus 4, and Opus 4.1 | text_editor_20250728 |
Claude Sonnet 3.7 | text_editor_20250124 |
Claude Sonnet 3.5 (deprecated) | text_editor_20241022 |
Claude Sonnet 3.5 (deprecated) requires the
computer-use-2024-10-22
beta header when using the text editor tool.The text editor tool is generally available in Claude 4 models and Claude Sonnet 3.7.The
text_editor_20250728
tool for Claude 4 models does not include the undo_edit
command. If you require this functionality, you’ll need to use Claude Sonnet 3.7 or Sonnet 3.5 with their respective tool versions.Older tool versions are not guaranteed to be backwards-compatible with newer models. Always use the tool version that corresponds to your model version.
When to use the text editor tool
Some examples of when to use the text editor tool are:- Code debugging: Have Claude identify and fix bugs in your code, from syntax errors to logic issues.
- Code refactoring: Let Claude improve your code structure, readability, and performance through targeted edits.
- Documentation generation: Ask Claude to add docstrings, comments, or README files to your codebase.
- Test creation: Have Claude create unit tests for your code based on its understanding of the implementation.
Use the text editor tool
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.1
Provide Claude with the text editor tool and a user prompt
- Include the text editor tool in your API request
- Provide a user prompt that may require examining or modifying files, such as “Can you fix the syntax error in my code?”
2
Claude uses the tool to examine files or directories
- Claude assesses what it needs to look at and uses the
view
command to examine file contents or list directory contents - The API response will contain a
tool_use
content block with theview
command
3
Execute the view command and return results
- Extract the file or directory path from Claude’s tool use request
- Read the file’s contents or list the directory contents
- If a
max_characters
parameter was specified in the tool configuration, truncate the file contents to that length - Return the results to Claude by continuing the conversation with a new
user
message containing atool_result
content block
4
Claude uses the tool to modify files
- After examining the file or directory, Claude may use a command such as
str_replace
to make changes orinsert
to add text at a specific line number. - If Claude uses the
str_replace
command, Claude constructs a properly formatted tool use request with the old text and new text to replace it with
5
Execute the edit and return results
- Extract the file path, old text, and new text from Claude’s tool use request
- Perform the text replacement in the file
- Return the results to Claude
6
Claude provides its analysis and explanation
- After examining and possibly editing the files, Claude provides a complete explanation of what it found and what changes it made
Text editor tool commands
The text editor tool supports several commands for viewing and modifying files:view
Theview
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.
Example view commands
Example view commands
str_replace
Thestr_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 text
Example str_replace command
Example str_replace command
create
Thecreate
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 file
Example create command
Example create command
insert
Theinsert
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)new_str
: The text to insert
Example insert command
Example insert command
undo_edit
Theundo_edit
command allows Claude to revert the last edit made to a file.
This command is only available in Claude Sonnet 3.7 and Claude Sonnet 3.5 (deprecated). It is not supported in Claude 4 models using the
text_editor_20250728
.command
: Must be “undo_edit”path
: The path to the file whose last edit should be undone
Example undo_edit command
Example undo_edit command
Example: Fixing a syntax error with the text editor tool
This example demonstrates how Claude 4 models use 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 will use the text editor tool first to view the file:Your application should then read the file and return its contents to Claude:Claude will identify the syntax error and use the Your application should then make the edit and return the result:Finally, Claude will provide a complete explanation of the fix:
Line numbersIn 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.str_replace
command to fix it:Implement the text editor tool
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 depends on the model version:- Claude 4:
type: "text_editor_20250728"
- Claude Sonnet 3.7:
type: "text_editor_20250124"
- Claude Sonnet 3.5 (deprecated):
type: "text_editor_20241022"
1
Initialize your editor implementation
Create helper functions to handle file operations like reading, writing, and modifying files. Consider implementing backup functionality to recover from mistakes.
2
Handle editor tool calls
Create a function that processes tool calls from Claude based on the command type:
3
Implement security measures
Add validation and security checks:
- Validate file paths to prevent directory traversal
- Create backups before making changes
- Handle errors gracefully
- Implement permissions checks
4
Process Claude's responses
Extract and handle tool calls from Claude’s responses:
When implementing the text editor tool, keep in mind:
- Security: The tool has access to your local filesystem, so implement proper security measures.
- Backup: Always create backups before allowing edits to important files.
- Validation: Validate all inputs to prevent unintended changes.
- Unique matching: Make sure replacements match exactly one location to avoid unintended edits.
Handle errors
When using the text editor tool, various errors may occur. Here is guidance on how to handle them:File not found
File not found
If Claude tries to view or modify a file that doesn’t exist, return an appropriate error message in the
tool_result
:Multiple matches for replacement
Multiple matches for replacement
If Claude’s
str_replace
command matches multiple locations in the file, return an appropriate error message:No matches for replacement
No matches for replacement
If Claude’s
str_replace
command doesn’t match any text in the file, return an appropriate error message:Permission errors
Permission errors
If there are permission issues with creating, reading, or modifying files, return an appropriate error message:
Follow implementation best practices
Provide clear context
Provide clear context
When asking Claude to fix or modify code, be specific about what files need to be examined or what issues need to be addressed. Clear context helps Claude identify the right files and make appropriate changes.Less helpful prompt: “Can you fix my code?”Better prompt: “There’s a syntax error in my primes.py file that prevents it from running. Can you fix it?”
Be explicit about file paths
Be explicit about file paths
Specify file paths clearly when needed, especially if you’re working with multiple files or files in different directories.Less helpful prompt: “Review my helper file”Better prompt: “Can you check my utils/helpers.py file for any performance issues?”
Create backups before editing
Create backups before editing
Implement a backup system in your application that creates copies of files before allowing Claude to edit them, especially for important or production code.
Handle unique text replacement carefully
Handle unique text replacement carefully
The
str_replace
command requires an exact match for the text to be replaced. Your application should ensure that there is exactly one match for the old text or provide appropriate error messages.Verify changes
Verify changes
After Claude makes changes to a file, verify the changes by running tests or checking that the code still works as expected.
Pricing and token usage
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) | 700 tokens |
text_editor_20250124 (Claude Sonnet 3.7) | 700 tokens |
text_editor_20241022 (Claude Sonnet 3.5 (deprecated)) | 700 tokens |
Integrate the text editor tool with other tools
The text editor tool can be used alongside other Claude tools. When combining tools, ensure you:- Match the tool version with the model you’re using
- Account for the additional token usage for all tools included in your request
Change log
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 (deprecated). Provides capabilities for viewing, creating, and editing files through the view , create , str_replace , insert , and undo_edit commands. |
Next steps
Here are some ideas for how to use the text editor tool in more convenient and powerful ways:- Integrate with your development workflow: Build the text editor tool into your development tools or IDE
- Create a code review system: Have Claude review your code and make improvements
- Build a debugging assistant: Create a system where Claude can help you diagnose and fix issues in your code
- Implement file format conversion: Let Claude help you convert files from one format to another
- Automate documentation: Set up workflows for Claude to automatically document your code