An experiment measuring the economic impact of refactoring on AI agent token consumption. A 17,155-line Rust data access layer was refactored in 15 steps using Claude Code, while a sub-agent repeatedly applied the same representative change after each step to measure token cost. Input tokens for the same task dropped from 159,564 to 27,360 — an 83% reduction — as the largest file shrank from 17K to 3.7K lines. The key insight: agents read less code when it's well-structured, but only if the structure allows them to identify the minimal relevant subset. The post also notes Claude's limitations in autonomous refactoring — it required human guidance to identify and apply refactorings, and missed the most impactful step on the first pass.
Table of contents
An experiment in refactoringResultsRefactoring reduces token consumptionNotes on the processFurther work and broader implicationsAppendices520 Impressions