AI Coding Cost per Refactor: Targeted Edits vs Full-File Rewrites
By Eric Bush · July 21, 2026 · 8 min read
When an AI agent refactors a file, it can do it two ways. It can emit a targeted edit, a diff or patch that changes only the lines that need changing, or it can regenerate the entire file from scratch. Both produce the same end result, but they cost wildly different amounts, because the difference lands almost entirely in output tokens, and output tokens are the expensive ones. Understanding this gap is one of the highest-leverage things you can know about your AI coding bill.
Why Output Tokens Are the Expensive Ones
On almost every frontier model, output is priced several times higher than input. Claude Sonnet 5 costs $2 per million tokens in but $10 per million out, a 5x ratio. That ratio is the whole story of refactoring cost. Reading a file into context is cheap. Writing a file back out is what you pay for. Any refactoring style that re-emits code you did not need to change is quietly overpaying on the most expensive side of the meter.
Why Full-File Rewrites Are Expensive
A full-file rewrite regenerates the whole file even when you are changing a single function. Take a 400-line module. At roughly 12 to 13 tokens per line of code, that is about 5,000 output tokens every time the model rewrites it. Change one variable name, get 5,000 output tokens. Adjust an import, get another 5,000. Do this across a realistic refactoring session, say 8 iterations, and you have emitted 40,000 output tokens to make changes that touched maybe a few hundred.
The waste scales with file size. The bigger the file, the more untouched code the model re-emits on every pass, and the more you pay for text that did not change.
Why Targeted Edits Are Cheaper
A targeted edit emits only the changed lines plus a little surrounding context to anchor the patch. For a small change in a 400-line file, that might be 250 to 500 output tokens instead of 5,000, commonly 10x to 20x fewer output tokens. The model still reads the file as input, which is cheap, but it writes back only the delta.
The catch is reliability. A patch has to apply cleanly against the real file. If the model miscounts lines or the surrounding context has drifted, the edit fails to apply, and the agent retries, sometimes falling back to a full rewrite anyway. A failed patch that triggers a retry can erase the savings for that step, which is why edit-application quality matters as much as the token math.
Worked Example: 400-Line Module, 6 Iterations
Refactor a 400-line module across 6 iterations. In both paths the model reads the file each time (about 5,000 input tokens per iteration, 30,000 total). The paths diverge on output. The full-rewrite path emits about 5,000 output tokens per iteration, 30,000 total. The diff-edit path emits about 400 output tokens per iteration, 2,400 total. Here is the bill on Claude Sonnet 5 (in $2/M, out $10/M):
| Path (Sonnet 5) | Input cost | Output cost | Total |
|---|---|---|---|
| Full rewrite (30K out) | $0.060 | $0.300 | $0.360 |
| Diff edits (2.4K out) | $0.060 | $0.024 | $0.084 |
The diff-edit path costs about $0.084 versus $0.360 for full rewrites, roughly a 4x reduction, and the entire gap comes from output tokens. Run the same session on Haiku 4.5 (in $1/M, out $5/M) and the shape holds: full rewrite lands near $0.18, diff edits near $0.042, again driven almost entirely by the output side.
Now scale it. One module across one session is pocket change, but a team refactoring dozens of files a day, many of them larger than 400 lines, multiplies that per-file gap into a serious monthly difference. The rewrite tax is small per action and large in aggregate.
Practical Guidance
- Prefer tools that patch, not rewrite. Modern coding agents default to diff-style edits for exactly this reason. If your tool regenerates whole files by default, look for a patch or apply-edit mode.
- Keep files small. If files stay under a few hundred lines, even a full rewrite is cheap, and the rewrite-versus-edit gap shrinks. Large monolithic files make every rewrite maximally expensive.
- Watch for silent edit failures. A patch that fails to apply and triggers a retry, or a fallback to full rewrite, quietly erases your savings. Malformed diffs are a real cost, not just an annoyance.
- Match the model to the task. For mechanical edits, a cheaper model applying clean diffs beats a premium model rewriting whole files on both cost and speed.
The Takeaway
Refactoring cost is an output-token problem. Full-file rewrites re-emit code you never touched, and you pay the premium output rate for every line. Targeted edits emit only the delta, typically 10x to 20x fewer output tokens, as long as the patches apply cleanly. Before you standardize on a tool or a model for refactor-heavy work, price your own file sizes and iteration counts in the AI cost calculator to see the gap in dollars.
Want to calculate exact costs for your project?
Frequently Asked Questions
Why do full-file rewrites cost more than targeted edits?
A full rewrite re-emits the entire file as output even for a one-line change, and output tokens are the expensive side of the bill (for example, Sonnet 5 charges $10/M out versus $2/M in). Targeted edits emit only the changed lines, typically 10x to 20x fewer output tokens, so you pay far less on the priciest part of the meter.
How much cheaper are diff edits in practice?
In a worked example of a 400-line module over 6 iterations on Sonnet 5, diff edits cost about $0.084 versus $0.360 for full rewrites, roughly a 4x reduction. The exact ratio depends on file size and how much of the file each change touches, but the savings always come from the output side.
Do targeted edits have any downside?
Yes. Patches must apply cleanly against the real file. If the model miscounts lines or context has drifted, the edit fails and the agent retries or falls back to a full rewrite, which can erase the savings for that step. Reliable edit application is what makes diff editing pay off.
Does keeping files small reduce refactoring cost?
It does. Smaller files mean even a full rewrite emits fewer output tokens, so the rewrite-versus-edit gap shrinks. Large monolithic files make every rewrite maximally expensive because the model re-emits a lot of untouched code each pass.
Which is better for cheap models like Haiku 4.5?
The same logic holds. On Haiku 4.5 (in $1/M, out $5/M) the 6-iteration example lands near $0.18 for full rewrites and $0.042 for diff edits. Diff editing is cheaper at every price tier because output is always priced above input; cheaper models just shrink the absolute numbers.
Related Articles
Kimi K2.5's Linear Attention: What It Means for Long-Context Coding Costs
Kimi K2.5's linear attention attacks the KV-cache cost driver behind long-context surcharges. Token math on why repo-scale AI coding gets cheaper.
How to Estimate AI Coding Cost Before You Start a Task
Estimate AI coding cost before you run an agent: count input and output tokens, multiply by iterations and model rate. Worked example plus a free calculator.
AI Coding Cost per Dockerfile: Generating Docker and Compose Configs
What does it really cost to have AI write Dockerfiles and docker-compose configs? Real token math across Haiku, Sonnet 5, DeepSeek, and Qwen3 Coder, plus a free calculator.