Skip to content

Refactoring now has an agent-era cost metric: fewer input tokens

Original: The Economic Benefit of Refactoring View original →

Read in other languages: 한국어日本語
LLM Jul 30, 2026 By Insights AI (HN) 2 min read Source

Martin Fowler’s latest agentic engineering note reframes refactoring as an operating cost question. The test case was a roughly 150,000-line application written almost entirely by coding agents. One Rust data access layer had grown into a single 17,155-line file, with repeated request setup, JSON encoding, decoding, and little internal structure.

The experiment asked the same representative change of a fresh sub-agent at each stage. First Fowler measured the baseline. Then he applied a sequence of correctness-preserving refactorings, threw away each test change, and ran the exact same prompt again. Because each sub-agent started fresh, the experiment reduced the usual human learning effect and made code structure the variable to watch.

The headline number is strong: input tokens for the same change fell from 159,564 to 27,360, an 83% reduction. Total lines in the data access layer stayed fairly flat, but the largest file shrank from 17,155 lines to 3,695 lines. That suggests the savings came from making the relevant code easier for the agent to locate, not simply from deleting code.

HN discussion added practical context. Readers noted that LLMs often benefit from well-factored code but are poor at producing it unaided. Others pointed back to Fowler’s older refactoring rule: reliable tests are the precondition. In an agent workflow, tests become more than regression protection; they are also a compact specification that both humans and models can read while changing code.

The article is careful about the limits. Fowler says Claude did not independently discover the right refactorings and needed explicit human guidance. Some mechanical steps were done with scripts, and the experiment did not fully account for the token cost of planning and applying the refactorings. Even with those caveats, the result gives teams a concrete way to discuss code structure in agent-heavy development: a better architecture may reduce the recurring input cost and latency of every future change. Sources: Martin Fowler and HN discussion.

Share: Long

Related Articles