Giles Edwards-Alexander built a 150,000-line application without reading most of the code. As CTO for Europe, Middle East and India at Thoughtworks, he relied on agents powered by Claude and Cursor. The system shipped a high-quality web UI, machine learning components, and automated deployment. Then one file in the data access layer hit 17,155 lines. HTTP request setups repeated everywhere. Duplication ran through the whole layer.
He decided to measure what that bloat was actually costing him, not in developer pain, but in token bills. The results, published July 30, 2026 on martinfowler.com as part of Thoughtworks’ Exploring Gen AI series, are concrete enough to change how teams think about code hygiene in an agentic workflow.
The Experiment Setup
The test required a representative task: adding a new trait to the Firestore layer with three specific methods. Edwards-Alexander sent the same prompt to a fresh sub-agent each time. Agents forget everything between runs, which made clean comparisons possible. No human learning curve to control for.
Token counts came from character approximations divided by four using the tiktoken library. Direct reporting from Claude proved unreliable, so this approximation method replaced it.
The refactoring followed a disciplined sequence: extract classes, pull out helper functions, split files on logical boundaries. Each step happened one at a time. After each step, the same representative change ran again and token counts were recorded. The experiment isolated the effect of structure on cost.
What the Numbers Showed

Input tokens for the identical task dropped from 159,564 to 27,360. An 83 percent reduction. That is not a one-time saving. Every future modification to that layer costs 83 percent less to run.
The largest single file in the data access layer shrank from 17,155 lines to a maximum of 3,695 lines. Total lines in the layer stayed roughly the same. The gain came from focus: the agent could now read only the relevant file instead of ingesting the entire monolith.
Input tokens stayed flat until the largest file crossed a size threshold. Then they dropped sharply. Edwards-Alexander wrote that the results suggest the agent successfully narrowed its focus once natural boundaries existed. Random file splitting would not achieve this. Proper concept extraction created the boundaries that let the model ignore irrelevant code.
Output tokens behaved differently. They held around 2,000 per change with some variation. At current Sonnet 5 pricing, output tokens cost roughly five times input tokens. But the volume stayed low enough that the overall economics still favor refactoring. One representative change saved about 39.7 cents. Multiply that across thousands of modifications in a growing codebase and the cumulative effect reaches thousands, possibly tens of thousands, of dollars.
⏱️ What the Refactoring Cost
Edwards-Alexander spent roughly eight hours on the experiment, much of it unattended. He estimates the refactoring itself consumed no more than five million tokens including planning. He notes that future studies should track refactoring costs more precisely to give teams a cleaner payback calculation.
The experiment ran on slow hotel WiFi. Cargo build caches bloated and slowed tests. Indentation errors caused headaches during mechanical steps run with Python scripts using grep and sed. One key step was missed initially and had to be reapplied. Even with those friction points, the core signal held.
Where Agents Still Need Human Direction
Claude could not identify suitable extractions on its own. Human guidance directed each refactoring step. The agent handled execution. Edwards-Alexander also found Claude performed better on Claude.ai than in the Code interface for the planning phase.
The codebase was greenfield, built and maintained largely by one developer using agents. That limits how far these findings generalize. Legacy systems with tangled dependencies may show different patterns, particularly in output token behavior on complex changes. Edwards-Alexander calls for more experiments: continuous refactoring, wider scope across entire applications, and comparisons across different refactoring techniques.
The Broader Context

These findings fit a pattern others have been tracking. A July 25, 2026 discussion on X noted that a typical retrieval-augmented generation pipeline costs $2,275 monthly on proprietary APIs but drops to $168 with optimized open models, a 93 percent reduction. Karina Nguyen, who worked on post-training for Claude 3 Haiku at Anthropic, described in a March 2024 thread how 32x cost reductions in some token prices enabled more experiments and faster research cycles.
Aaron Levie, CEO of Box, posted on January 8, 2026 about a related angle:
A deeply under-appreciated economic benefit of AI agents is the ability to experiment and throw away things at near 0 cost.
Refactored code makes those cheap experiments even cheaper. Teams already shipping AI-generated code at scale can route complex refactoring tasks to powerful models and simple queries to cheaper ones. Provider discounts can cut costs another 40 percent, according to discussions tracked in the source. Smart token routing treats compute as a scarce resource.
Not everyone is optimistic about where this leads. An X user with 17 years building search infrastructure at Google warned in February 2026 about the hidden costs accumulating in AI-generated codebases: technical debt, security vulnerabilities, and bug patterns that developers do not fully understand because they never read the code. Refactoring is one answer to that concern, though it demands upfront investment and human direction.
The Replication Playbook
If you’re running an agent-heavy codebase and want to test this yourself, the approach is reproducible:
- Identify your largest files in the data access layer or any high-churn module.
- Define a representative change you make frequently: adding a method, modifying a trait, updating an interface.
- Record input token counts before refactoring using character approximations divided by four.
- Refactor one step at a time: extract classes, pull helper functions, split on logical boundaries.
- Re-run the same representative change after each step and record tokens again.
- Track refactoring token costs alongside the savings to calculate payback period.
Edwards-Alexander’s experiment gives the industry a starting point for putting real numbers on code quality decisions. The 83 percent input token reduction is not a benchmark to copy directly, but the methodology is. Teams that measure token costs alongside traditional quality metrics will make better decisions about when refactoring pays.

