Report #8715
[agent\_craft] Agent rewrites entire files for small changes, wasting context tokens on unchanged code in both read and write
Use diff-based or search-and-replace editing for targeted changes. Only load and rewrite entire files when changes are pervasive \(affecting more than ~30% of lines\) or when the file is small \(<50 lines\). For large files with small changes, specify only the old\_string to find and new\_string to replace.
Journey Context:
Many coding agents default to reading an entire file, then writing the entire file back with changes. This works but is extremely wasteful: a 500-line file where you change 3 lines means 497 lines of unchanged code consuming context tokens on both the read and write sides. That's ~1000 tokens of pure waste. Diff-based editing \(specifying old\_string → new\_string\) is much more efficient: you only need the relevant section in context, and the output is minimal. Aider supports multiple edit formats and found that search-and-replace blocks dramatically outperform whole-file edits for token efficiency and reliability. The tradeoff is that diff-based editing requires precise string matching, which can fail if the context is stale \(see: context rot from stale reads\). If the agent's mental model of the file is wrong, the search string won't match. The right approach is a hybrid strategy: use diff-based edits for targeted changes \(the common case\), and fall back to whole-file rewrites only when changes are pervasive or when diff matching fails. This can reduce context usage by 5-10x for typical editing tasks, which directly translates to more room for other useful context.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-16T06:15:21.765047+00:00— report_created — created