TLDRocket
Sign in

The Economic Benefit of Refactoring

martinfowler.com

A dev refactored a 17K-line AI-written Rust file and measured the token cost of every step. Input tokens for the same task dropped 83%, proving cleanup pays for itself.

Based on reporting by martinfowler.com — read the original for the full story.

Summary, retelling and take written by AI under human oversight; images are AI-generated illustrations. How we work · Report an error

Someone building a fairly ambitious app almost entirely with Claude Code and Cursor hit an odd milestone: a single Rust file in the data access layer had ballooned to 17,155 lines. No deduplication, barely any function extraction, just endless repeated HTTP setup and JSON handling for every query. So they decided to treat it as an experiment rather than a chore.

The method was clever precisely because agents don't retain memory between sessions. That let the developer run the exact same representative prompt — adding a new watch-store trait to the Firestore layer — against a fresh sub-agent after every single refactoring step, with no risk of the agent "learning" from prior runs. Fifteen steps later, following strict Fowler-style refactoring discipline, the file had been split into 19 Rust files, with the biggest single file down to a few thousand lines.

The payoff shows up almost entirely in input tokens. The baseline run consumed 159,564 input tokens to complete the task; the final run needed just 27,360. That's a drop of 132,204 tokens, or 83%, for producing functionally the same change. Output tokens barely moved across all fifteen steps, since the agent still had to write roughly the same amount of code. But it had to read dramatically less of it to figure out where that code belonged.

What's notable is where the savings kicked in. Input tokens stayed roughly flat for most of the process and only fell sharply once the largest file itself started shrinking — described in the piece as tokens "falling off a cliff." That timing matters: the early steps that extracted duplication weren't wasted, they were groundwork that let the later file-splitting actually pay off. The author is careful to note the total code in the data layer didn't shrink much overall; the saving came from the agent successfully narrowing its search to fewer, smaller files rather than scanning everything.

The experiment also surfaced some blunt criticism of the tools involved. Claude Code, on its own, couldn't identify which refactorings were worth doing — a human had to steer it, and even then its mechanical script-based edits kept tripping over indentation. Claude.ai apparently did better at spotting the bigger structural fix (extracting an entire client class) than Claude Code did. The whole exercise took about eight hours, mostly unattended, complicated at one point by a bloated build cache slowing test runs on hotel WiFi.

The author is upfront that this is a single case study on one greenfield app maintained by one person, and they never tracked the token cost of designing the refactoring plan itself, only estimating an upper bound of five million tokens for that surrounding work. Still, the core result — that refactoring lowers the ongoing cost of every future change touching that code — reads as a real, measurable economic argument rather than just good hygiene.

My take — AI-written commentary, not fact-checked reporting

This is the kind of experiment more teams leaning on agentic coding should be running instead of just shipping whatever the model spits out. An 83% drop in input tokens for the same task is a genuine cost signal, not a vibes-based argument for clean code, and it suggests that letting AI-generated codebases sprawl unchecked is going to quietly tax every future feature. The bigger tell, though, is that Claude needed a human to spot which refactorings mattered at all — anyone hoping agents will tidy up after themselves without guidance is going to be waiting a while.

Read more about this at: martinfowler.com

Related stories

The daily briefing

Every AI story that matters, in your inbox by 8am.

TLDRocket reads all relevant sources, removes duplicate coverage, and summarises the day in two minutes. Follow companies and topics for alerts, or get the briefing in Slack. Free, no spam, unsubscribe anytime.