Refactoring an Agent-Written Codebase Cut Token Costs 83% Per Change
A Thoughtworks technologist built a 150,000-line application—mostly Rust, plus TypeScript and Terraform—almost entirely with coding agents like Claude Code and Cursor, without reviewing most of the output. Left unsupervised, the agents let the data access layer balloon into a single 17,155-line Rust file that repeated the same HTTP setup and JSON encoding for every query, with no deduplication or meaningful function extraction. That made it an ideal test bed for measuring whether refactoring pays for itself in an agentic workflow.
Because agents don’t retain learning between runs, the author could re-run an identical feature-change prompt in a fresh sub-agent after each refactoring step and get a clean before/after comparison. The result: input tokens for the same change fell from roughly 159,564 to 27,360—an 83% reduction. The saving came not from having less code overall (the data layer’s total size stayed roughly constant) but from splitting it so the agent could read only the small subset of files relevant to a given task. Simply chopping the file into arbitrary pieces wouldn’t help; the gains depended on disciplined extraction of duplication before breaking things apart. Output tokens, which cost five times more, barely moved.
The practical dollar savings on one change were tiny—about 40 cents at Sonnet 5 pricing—but they recur on every future edit that touches the layer, raising the question of whether aggressive, codebase-wide refactoring could compound into real savings. The author is blunt about the tooling’s limits: Claude couldn’t independently judge which refactorings to apply and needed active human direction, its script-based grep-and-sed edits kept tripping over indentation, and the single most valuable refactoring was missed on the first pass. Notably, Claude.ai produced a better refactoring plan than Claude Code, spotting an entire client class to extract where the CLI only found a function.
Read the full article
Continue reading at Hacker News →This is an AI-generated summary. Read the original for the full story.