AI Coding Agent File Editing: Why I Never Let Mine Touch sed
I'm building an AI coding agent — an LLM that writes and edits code on its own. The obvious way to let it edit files is to hand it a shell: sed , echo > , cat . I tried it. It's a disaster. This is a first-principles guide to the structured editor tool I built instead — and why an AI agent should n

I'm building an AI coding agent — an LLM that writes and edits code on its own. The obvious way to let it edit files is to hand it a shell: sed, echo >, cat. I tried it. It's a disaster. This is a first-principles guide to the structured editor tool I built instead — and why an AI agent should never edit code with raw shell commands.
The fix isn't a smarter prompt. It's a better tool. The tool is the product.
Instead of a shell, give the agent four narrow file actions:
Each one is deterministic and reviewable. No sed, no surprises.
str_replace is where all the safety lives. The rule: find the target text, and act only if it appears exactly once.
Why so strict? If the model says "replace x = 1 " and that text is in five places, editing the wrong one silently corrupts the code — and nobody notices until much later. Forcing uniqueness makes every edit unambiguous. The tool refusing is a feature, not a bug.
When the agent reads a file, each line comes back numbered:
12: def handler ( request ): 13: return None Enter fullscreen mode Exit fullscreen mode That's not decoration. It's how the model points at exactly where it wants to edit — "line 13" — instead of guessing. Context in, precision out.
Here's the mistake I made first. I thought undo_edit meant "run the replacement backwards" — swap new back to old. It doesn't work: the new text might appear elsewhere, and you don't have the originals during an undo.
The right model: before every edit, save a copy of the whole file. Undo just writes that copy back. You don't reconstruct the past — you keep a photo of it.
Once you save that snapshot inside str_replace, undo_edit becomes three lines: look up the saved copy, write it back, done.
An AI agent given a bare shell edits code like a bull in a china shop. The same agent, given a structured editor — windowed reads, unique-match replaces, snapshot undo — edits like a careful engineer.
Same model. Different tool. The tool layer is the product.