The Economic Benefit of Refactoring
Points and comments are a snapshot, not live.
Refactoring a 17,000-line Rust file cut AI agent token consumption by 83% per change.
Giles Edwards-Alexander built a 150,000-line Rust/TypeScript app entirely with AI coding agents (Claude Code, Cursor). The data access layer became a single 17,155-line Rust file. He ran a controlled experiment applying 15 refactoring steps, measuring token costs for a representative change at each step. Input tokens dropped from 159,564 to 27,360 (83% savings) after splitting the monolithic file into 19 smaller files, though total lines of code stayed nearly constant. The refactoring required significant human guidance and took about 8 hours; Claude was poor at both planning and executing refactorings autonomously. At $3/MTok, each saved change costs about 40 cents less.
What commenters are saying
Commenters broadly agreed that well-factored code benefits AI agents, but noted that current LLMs are poor at refactoring autonomously. Several observed that total lines of code remained roughly constant, despite the largest file shrinking from 17K to 3.7K lines. One commenter quipped that the dollar savings per change was only 40 cents, questioning whether the human-guided refactoring cost was worth it. Others noted that cyclomatic complexity or cognitive complexity likely correlates with token usage, and that enforcing good structure helps both humans and AI. A few shared tactics for guiding AI toward cleaner code, such as providing examples of well-factored code or budgeting explicit 'slop removal' time.