Refactoring AI-Generated Code Cut Agent Token Use by 83%
An engineer took one file of 17,155 lines, split it into 19 smaller ones, and measured what the same change cost an AI agent before and after.
Writing on Martin Fowler's site on Thursday, Giles Edwards-Alexander reported the numbers. To make the change in the big file, the agent had to read 159,564 tokens of code. In the refactored version, 27,360.[1] Tokens are the units these models are billed in, so that gap is an invoice line, not a metaphor.
Same change. Same instructions. Eighty-three percent less to read.
At the price he cites for the model doing the work, the difference is 39.7 cents. Forty cents is nothing. Forty cents on every future change to that part of the codebase is a cost curve.
What did the experiment actually measure?
One representative change, made over and over. Edwards-Alexander wrote a 15-step refactoring plan, had agents carry out each step, and re-ran the identical task after every one. The task never changed. Only the shape of the code around it did.
What makes the comparison clean is a property of agents that usually reads as a limitation. They do not learn between runs. A human developer would get quicker on the second attempt out of sheer familiarity, and the measurement would be worthless. "Precisely because agents never learn this was now possible to run as an experiment," Edwards-Alexander writes.
The codebase deserves a sentence of its own. Roughly 150,000 lines, most of it Rust, and written by AI agents in the first place. So this is not a story about tidying up old human code so the machines can cope. This is AI-written code that had grown expensive for AI to work in.
Why does messy code cost more when an AI agent works on it?
Because an agent has to read the relevant code before it can safely change anything, and a sprawling file forces it to read far more to find the right spot. A well-separated module lets it load only what it needs. Reading is the meter, and how the code is arranged is what decides how long the meter runs.
The tell in the data is the thing that did not move. Output tokens, the code the agent actually wrote, were largely unaffected across all 15 steps. The work being done stayed the same size the whole way through. What shrank was everything the agent had to take in first.
The common read since agents got good is that code quality matters less now. If a file is a mess, regenerate it, or let the agent grind through it. This measurement points the other way.
When humans made every change, mess cost human attention, which no finance system has ever tracked. When agents make the changes, mess costs tokens, and tokens arrive as a bill at the end of the month.
Does refactoring code that already works pay for itself?
In this case yes, and the arithmetic is not complicated. The refactoring cost tokens once. The saving comes back on every future change to that code. The more you keep building on a given piece of a system, the more that one-time spend returns.
The goal of refactoring an agentic code base is to spend tokens now in refactoring to make token consumption for future work lower.
He is careful about how far it generalizes, and so are we. His own words: "This is just one experiment, on a significant application that is still greenfield and built and maintained by a single developer." One file, one task, one codebase, one developer. The 83% is not a law of nature.
The mechanism underneath it is the part that travels. What an agent has to read scales with how the code is arranged, and reading is where the money goes.
Cheaper tokens do not dissolve it either. OpenAI cut prices on two of its models by up to 80% the same week, and the cut lands on the tiers most agent work does not run on. A lower price lowers the dollars. It does not touch the ratio between reading a tangled file and reading a clean one.
The ratio is the part that compounds.
What this changes about how you build
For two years the case for keeping AI-generated code coherent has been a quality case. This is the same argument with a price attached, which tends to travel further.
The quality evidence was already there. AI-generated code accrues debt you did not write and cannot see. A Carnegie Mellon study of 53,600 real in-editor edits found developers delete the AI's completion outright in 31% of edit trajectories.[2] The security of that code has been flat for two years while the models got dramatically better at everything else.
What this adds is a meter running on the same problem:
- The prototype that works but sprawls is not free to keep. It gets more expensive to change every week you build on it.
- The expense is invisible in the way that matters. Nobody gets an alert that says "this file is why your agent bill went up."
- The fix is a one-time spend with a repeating return, which is the rarest shape a technical investment can have.
None of that is work an agent decides to do on its own. Somebody has to look at a system and say this belongs here and that belongs there, which is judgment about the domain, not typing.
It is the same point we keep landing on from different directions: the code was never the hard part, and the parts that were hard are now the parts that set your operating costs.
AI made writing code close to free. It never made reading it free. What changed on Thursday is that somebody put a number on the difference.
References
- ^1.Giles Edwards-Alexander, martinfowler.com, “The Economic Benefit of Refactoring (exploring-gen-ai)” (July 30, 2026)
- ^
Frequently asked
Does refactoring AI-generated code actually save money?›In at least one careful test, yes. Writing on Martin Fowler's site in July 2026, an engineer split a 17,155-line file into 19 smaller ones and found the tokens an AI agent had to read to make the same change fell 83%, from 159,564 to 27,360.
Why does messy code cost more when an AI agent works on it?›Because an agent has to read the relevant code before it can change anything, and a sprawling, tangled file forces it to read far more to find and safely edit the right part.
If AI writes the code, does code quality still matter?›It matters more, and now there is a number on it. When humans made every change, messy code cost human time, which is hard to see on a budget.
Is it worth spending agent tokens to refactor code that already works?›Often yes, because the refactoring is a one-time cost and the saving repeats on every future change.
What should a builder do about this?›You do not need to read the code to act on it. Keeping a codebase well organized is the cheapest way to keep your agents fast and affordable as you add features.
Let’s build it together.
We turn clever prototypes into production systems people can rely on. If you’re building with agents and want a hand making it real, leave your email and we’ll be in touch.