Claude can use Anthropic’s text editor tool to view and modify text files, helping you debug, fix, and improve code or other text documents. This enables Claude to interact directly with your files, providing practical assistance rather than just suggestions.

Preparation Before Using the Text Editor Tool

Select a Compatible Model Anthropic’s text editor tool only supports the following Claude models:

  • Claude 3.7 Sonnet: text_editor_20250124
  • Claude 3.5 Sonnet: text_editor_20241022 Both versions provide the same functionality - you need to choose the version that matches your model.

Evaluating Use Cases

Here are some typical scenarios for using the text editor tool:

Code Debugging: Help identify and fix issues in code, from syntax errors to logical problems. Code Refactoring: Improve code structure, readability, and performance through targeted edits. Documentation Generation: Add docstrings, comments, or README files to your codebase. Test Creation: Create unit tests based on implementation understanding.

Using the Text Editor Tool

Provide the text editor tool (named str_replace_editor) to Claude through the Messages API: You need to install the anthropic package:

pip install anthropic

Usage Example:

import anthropic

client = anthropic.Anthropic(
    api_key="AIHUBMIX_API_KEY", # Replace with the key you generated in AiHubMix
    base_url="https://aihubmix.com"
)

response = client.messages.create(
    model="claude-3-7-sonnet-20250219",
    max_tokens=1024,
    tools=[
        {
            "type": "text_editor_20250124",
            "name": "str_replace_editor"
        }
    ],
    messages=[
        {
            "role": "user", 
            "content": "There's a syntax error in my primes.py file. Can you help me fix it?"
        }
    ]
)

print("Response content:")
for message in response.content:
    print(message.text)

Response Example:

{
  "id": "msg_bdrk_012xyNaFCQg4zsVcTk5VkDAe",
  "type": "message",
  "role": "assistant",
  "content": [
    {
      "type": "text",
      "text": "I'd be happy to 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",
      "text": "",
      "id": "toolu_bdrk_01P6jQG6suDSsDjzugqGVHWC",
      "name": "str_replace_editor",
      "input": {
        "command": "view",
        "path": "/repo/primes.py"
      }
    }
  ],
  "model": "claude-3-7-sonnet-20250219",
  "stop_reason": "tool_use",
  "usage": {
    "input_tokens": 1042,
    "output_tokens": 115
  }
}