The Economic Benefit of Refactoring in Agentic Engineering

Refactoring directly reduces AI token costs

Refactoring agent-generated codebases provides a measurable economic benefit by reducing the number of input tokens an AI agent must process to implement new features. In a controlled experiment, refactoring a single 17,155-line Rust file into a modular structure reduced the input tokens required for a representative change from 159,564 to 27,360, representing an 83% saving in token consumption.

This saving occurs not because the total amount of code decreases—the overall line count of the data access layer remained relatively constant—but because modularity allows the agent to identify and read only the smallest necessary subset of files. When code is consolidated into a single massive file, the agent is forced to ingest the entire file to maintain context, leading to higher costs and slower execution.

The Refactoring Experiment

To quantify the impact of code structure on token usage, an application of approximately 150,000 lines of code (primarily Rust) was used. The application was written entirely by agents (Claude Code and Cursor) without human code review. Over time, the data access layer grew into a single file exceeding 17,000 lines, characterized by high duplication and a lack of internal abstraction.

Methodology

The experiment used a "fresh" agent for each test to ensure that no learning from previous steps tainted the results. The process followed these steps:

  1. Establish a Baseline: A representative change (adding a new trait and implementation) was prompted to a sub-agent to record the initial token cost.
  2. Iterative Refactoring: A series of 15 refactoring steps were applied based on strict refactoring discipline (referencing Martin Fowler's Refactoring 2nd edition).
  3. Measurement: After each refactoring step, the exact same representative change was prompted to a new sub-agent to measure the change in input tokens, output tokens, and execution time.

Quantitative Results

Metric Baseline After Step 15 Change
Data Access Layer LoC 17,155 16,608 -2.9%
Largest File LoC 17,155 3,695 -78.4%
Input Tokens per Change 159,564 27,360 -83%
Output Tokens per Change 1,705 2,113 +23.9%
Time per Change (s) 342 454 +32.7%

While input tokens "fell off a cliff" as the largest file size decreased, output tokens remained relatively stable. This indicates that while refactoring makes the code easier for the AI to read, it does not necessarily make the resulting code shorter to write.

Key Technical Insights

Modularity vs. Simple Splitting

Randomly splitting a large file into smaller ones is insufficient to realize these savings. The experiment showed that the most significant token reductions occurred only after the code was logically refactored to extract duplication and establish a repeating core. This structure enables the agent's retrieval mechanisms to successfully isolate the relevant files, rather than reading multiple small files in a search for the necessary logic.

The "Agentic Gap" in Refactoring

Despite the ability to generate large volumes of code, the experiment revealed that current AI agents (specifically Claude) struggle with the act of refactoring itself:

  • Lack of Initiative: Agents did not independently suggest or apply refactorings to improve the codebase; they required explicit human guidance and a detailed plan.
  • Execution Errors: The mechanical process of refactoring (moving code, updating imports) was often unreliable when handled by the agent, requiring the use of external Python scripts with grep and sed to ensure correctness.
  • Guidance Requirement: A human with deep software engineering knowledge was necessary to craft the prompts and the refactoring plan to achieve the desired architectural outcome.

Synthesis of Community Perspectives

Discussion among engineers regarding these findings highlights several critical points about the intersection of AI and software architecture:

The Return of "Boring" Best Practices

Many observers noted that the "exciting" new discovery—that refactoring helps AI—is simply a rediscovery of fundamental software engineering principles. As one contributor noted:

"Boring: Refactoring makes your developers more productive in long term. Exciting: Refactoring makes your AI more productive in long term."

Impact on Reasoning and Correctness

Beyond token costs, some argue that compact contexts improve the AI's reasoning capabilities. Reducing the "entropy" of the code through better abstractions may increase the probability that the AI generates correct software that generalizes well, rather than just passing specific test cases.

The Human-in-the-Loop Necessity

There is a consensus that while agents can write code, the high-level architectural vision remains a human responsibility. The ability to prompt for a specific async trait and its implementation across multiple store types requires a level of domain expertise that agents cannot yet synthesize independently. Some suggest that we are entering an era of "vibe coding" where the human acts as the architect and the AI as the high-speed implementer.

Sources