« All posts

Refactoring Cuts AI Coding Agent Token Costs by 83%

An experiment shows refactoring an AI-agent-built codebase cut input token costs for repeated code changes by 83%.

An engineer building a 150,000-line Rust/TypeScript application entirely through AI agents (Claude Code, Cursor) noticed a data access layer balloon to 17,155 lines in a single file, full of duplicated HTTP and JSON boilerplate with almost no abstraction.

To measure whether refactoring pays off in agentic development, they ran a controlled experiment: after each refactoring step, a fresh sub-agent—with no memory of prior steps—executed the same representative code change, and its token consumption was logged. Over 15 incremental steps, the monolithic file was split into 19 Rust files, and input tokens for the identical task dropped from 159,564 to 27,360, an 83% reduction.

Output tokens barely changed, meaning the savings came entirely from agents needing to read less code, not from less code existing overall. Because total lines of code stayed roughly constant, the gain depended on agents successfully narrowing their reads to the smallest relevant file subset—something well-structured, incrementally split files enabled far better than the original giant file. The result suggests refactoring in agent-driven codebases is not a one-time cost but a compounding saving applied to every future change.