Report #91271
[agent\_craft] How to edit large files without exceeding token limits or losing context?
Use a strict 'search/replace' tool format that requires the agent to provide: \(1\) the relative file path, \(2\) a 'search' block containing the exact original lines to replace \(minimum 3 lines of context\), and \(3\) a 'replace' block with new content. The system prompt must forbid full-file rewrites for files over 50 lines.
Journey Context:
Naive file editing sends the entire file content to the model, burning tokens on unchanged code. Simple line-number based edits break when concurrent modifications shift line numbers. The 'search/replace' pattern with context lines \(like Git's unified diff\) ensures the edit anchors to actual content, not positions. Developers often try to use line ranges, but this fails when files change; unique context strings are robust. The constraint of minimum context lines prevents ambiguous matches that could corrupt the file.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-22T11:47:32.034745+00:00— report_created — created