Skip to content
AgenticAIWiki
  1. Learn
  2. Part 7
  3. 37. Built-in tools
  4. 37.1

Lesson 37.1 · 1 of 5 · 5 min read · Reviewed 24 Nov 2026

File tools: Read, Write, Edit

By the end, you can

  • Use Read, Write and Edit appropriately.
  • Explain why Edit needs a unique match and a prior Read.

Claude Code and the Agent SDKClaude Agent SDK: Anthropic’s library for building agents on the same loop that powers Claude Code, with tools, hooks, subagents and MCP. come with built-in tools for working on code. Knowing how each behaves helps you read traces and set permissionsPermission rule: An allow, ask or deny rule that controls which tools, commands or paths a coding agent may use..

Read

Returns a file’s contents with line numbers. For big files it can read a range, such as lines 400 to 600, which saves context.

Edit

Replaces an exact string with a new one.

  • The old string must appear exactly once, or the edit fails. Add surrounding lines to make it unique.
  • The file must have been read in the session first, so Claude edits what’s really there, not what it remembers.

Write

Creates a file or replaces it entirely. Good for new files; risky for big existing ones.

Rules of thumb

Task Tool
Look at a file Read
Change a few lines Edit
Create a new file Write
Rewrite a small file completely Write

In permission rules you can scope these: Edit(./src/**) allows edits only under src/.

Key points

  • Read returns a file's contents, and can return a range of lines for large files.
  • Edit replaces an exact string, which must be unique in the file; Write replaces the whole file.
  • Prefer Edit for small changes: it's cheaper and less likely to clobber other content.
Hands-on labRuns in the Playground

Build a safe Edit tool

Implement Read, Edit and Write over an in-memory file system with the same safety rules as an agent's built-in tools.

You will have: A tiny file tool module whose Edit refuses unread files and ambiguous matches, with a printed test run.

Steps

  1. Read the starter: files live in a dictionary and read_files records what has been read.
  2. Make read() return numbered lines and, optionally, a line range.
  3. Make edit() fail if the file was not read first.
  4. Make edit() fail unless old appears exactly once, and say how many matches it found.
  5. Run the test script: an ambiguous edit, a fix with more context, and an edit before reading.

Done when

  • Editing before reading gives a clear error.
  • An old string that appears twice is rejected with the match count.
  • Adding the section header as context makes the same edit succeed.

Open in the Playground No API key needed.

Starter code (Python)
files = {"config.py": "[database]\ntimeout = 30\n\n[http]\ntimeout = 30\n"}
read_files = set()

def read(path, start=1, end=None):
    read_files.add(path)
    lines = files[path].splitlines()
    # TODO: return only lines start..end, each prefixed with its line number
    return files[path]

def edit(path, old, new):
    # TODO: refuse if path not in read_files
    # TODO: refuse unless old appears exactly once (report the count)
    files[path] = files[path].replace(old, new)
    return "edited"

def write(path, content):
    files[path] = content
    return "written"

print(edit("config.py", "timeout = 30", "timeout = 60"))
print(read("config.py"))
Show a solution
files = {"config.py": "[database]\ntimeout = 30\n\n[http]\ntimeout = 30\n"}
read_files = set()

def read(path, start=1, end=None):
    read_files.add(path)
    lines = files[path].splitlines()
    end = end or len(lines)
    return "\n".join(f"{i:>3}  {lines[i - 1]}" for i in range(start, end + 1))

def edit(path, old, new):
    if path not in read_files:
        return f"error: read {path} before editing it"
    count = files[path].count(old)
    if count != 1:
        return f"error: old string found {count} times; add context to make it unique"
    files[path] = files[path].replace(old, new)
    return "edited"

def write(path, content):
    files[path] = content
    return "written"

print("1.", edit("config.py", "timeout = 30", "timeout = 60"))
print(read("config.py", 1, 2))
print("2.", edit("config.py", "timeout = 30", "timeout = 60"))
print("3.", edit("config.py", "[http]\ntimeout = 30", "[http]\ntimeout = 60"))
print(read("config.py"))
Quick checkClaude must change one line in a 2,000-line file. Which tool fits best?
How sure are you?
Pick an answer and how sure you are