← In the News

Refactoring an agent-written codebase cut the token cost of the same change by 83%

The Economic Benefit of Refactoring · Giles Edwards-Alexander, CTO for Europe, Middle East and India, Thoughtworks · martinfowler.com, 30 July 2026

Machine-readable Download Markdown

Edwards-Alexander built a 150,000-line application, roughly 120 kLoC of it Rust, without reading it: "I didn't read or review any of the code, except occasionally, out of interest." Its data access layer reached 17,155 lines in one file. He ran fifteen refactoring steps, re-executing one identical feature prompt in a fresh sub-agent after each, and input tokens for that change fell from 159,564 to 27,360, "a saving of 132,204 tokens, or 83%." The design turns on a property of the subject: "Precisely because agents never learn this was now possible to run as an experiment." The layer barely shrank, at 16,608 lines, so "this saving is because the agent has to read less code. But it is not because there is less code to read"; the money saved is 39.7 cents at Sonnet 5 pricing, "not a lot"; and "Claude was not good at refactoring." He never counted what the refactoring itself cost.

Why it matters: Edwards-Alexander makes the first published attempt to price refactoring in the unit agents are billed in, with the prompts in the appendices so you can re-run it. The payoff comes from the agent reading a smaller subset, not from there being less code, so splitting a large file arbitrarily will not buy it.