Sub topic 7
The usual toolbox: read, search, edit, run
This chapter assumes you know what a tool definition is and how a tool call moves between the model and the harness. It covers the four kinds of tools most coding agents ship with and the mistakes each one invites.
Four verbs cover most of the work
A coding agent needs to look at code, find code, change code, and check that the change works. Each job maps to a tool category.
| Category | Typical tool names | What it does | Main risk |
|---|---|---|---|
| Read | `read_file`, `list_directory` | Returns file contents or a listing | Large files fill the context window |
| Search | `grep`, `glob` | Finds files or lines that match a pattern | Too many matches, or none because the pattern was wrong |
| Edit | `write_file`, `replace_in_file` | Changes files on disk | Wrong or stale edits, lost work |
| Run | `run_command` | Executes a shell command, such as a test suite | Side effects and unbounded output |
Read and search tools only observe. Edit and run tools change the world, so they are usually the ones that need approval (covered in a later chapter).
Read and run, with bounded output
Every tool result becomes text in the context window. A tool that returns a 2 MB log can crowd out the task itself. The usual defense is to cut output to a fixed length inside the tool.
import subprocess
from pathlib import Path
def read_file(path):
return Path(path).read_text()[:20000]
def run_command(command):
r = subprocess.run(command, shell=True, capture_output=True, text=True, timeout=30)
return (r.stdout + r.stderr)[-5000:]
TOOLS = {"read_file": read_file, "run_command": run_command}The two tools cut in different directions on purpose. read_file keeps the start of the file, because the top usually holds imports and definitions. run_command keeps the end of the output, because errors and test summaries appear last. The timeout=30 stops a hung command from blocking the loop. If a timeout fires, subprocess.run raises an exception, and the loop turns it into an error: message the model can read.
Search before read
A model that reads whole files to find one function wastes context. A better habit is to search first, then read only the relevant part. Searching by text pattern (grep) finds where a symbol is used. Searching by file name (glob) finds where a file lives.
The sequence below shows the usual order of tool use for a bug fix.
Edit tools: replace, don't rewrite
There are two common designs for editing. A write_file tool replaces the whole file. A replace_in_file tool takes an old string and a new string and swaps one for the other. Replacement is safer for large files, because the model only has to produce the changed lines and cannot accidentally drop the rest.
Good replacement tools fail loudly. If the old string is not found, or appears more than once, the tool should return an error instead of guessing.
from pathlib import Path
def replace_in_file(path, old, new):
text = Path(path).read_text()
count = text.count(old)
if count != 1:
raise ValueError(f"expected exactly 1 match for old text, found {count}")
Path(path).write_text(text.replace(old, new))
return "ok"
TOOLS["replace_in_file"] = replace_in_fileThe raised ValueError becomes an error: tool result. The model can then read the file again and retry with a more specific old string.
Common mistakes
- Giving the model one huge tool such as "do anything in the shell" and no search tool. It then reads whole files with
cat. - Returning empty output on success. The model cannot tell "nothing happened" from "it worked". Return a short confirmation such as
ok. - Silent truncation. Add a marker like
[truncated]so the model knows output was cut. The code above does not do this, which is a gap you can fix in the exercise.
Exercise
In run_command, the output is cut with [-5000:] instead of [:5000]. Why is that choice sensible for test output, and what small change would tell the model the output was cut?
Show answer
Test runners print failures and the pass/fail summary at the end, so keeping the last 5000 characters preserves the most useful part. To signal truncation, check the length first: if the combined output is longer than 5000 characters, prefix the result with a marker such as "[truncated, showing last 5000 characters]".