Model compatibility
| Model | Tool Version |
|---|---|
| Claude 4.x models | text_editor_20250728 |
| Claude Sonnet 3.7 (deprecated) | text_editor_20250124 |
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 (deprecated).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
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.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?”
Claude uses the tool to examine files or directories
- Claude assesses what it needs to look at and uses the
viewcommand to examine file contents or list directory contents - The API response will contain a
tool_usecontent block with theviewcommand
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_charactersparameter 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
usermessage containing atool_resultcontent block
Claude uses the tool to modify files
- After examining the file or directory, Claude may use a command such as
str_replaceto make changes orinsertto add text at a specific line number. - If Claude uses the
str_replacecommand, Claude constructs a properly formatted tool use request with the old text and new text to replace it with
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
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.
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
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"
Initialize your editor implementation
Handle editor tool calls
Implement security measures
- Validate file paths to prevent directory traversal
- Create backups before making changes
- Handle errors gracefully
- Implement permissions checks
Process Claude's responses
- 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
tool_result:Multiple matches for replacement
Multiple matches for replacement
str_replace command matches multiple locations in the file, return an appropriate error message:No matches for replacement
No matches for replacement
str_replace command doesn’t match any text in the file, return an appropriate error message:Permission errors
Permission errors
Follow implementation best practices
Provide clear context
Provide clear context
Be explicit about file paths
Be explicit about file paths
Create backups before editing
Create backups before editing
Handle unique text replacement carefully
Handle unique text replacement carefully
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
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.x) | 700 tokens |
text_editor_20250124 (Claude Sonnet 3.7 (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 (retired). 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