The Economic Benefit of Refactoring in Agentic Engineering

The Economic Benefit of Refactoring in Agentic Engineering

I built a sophisticated application entirely using AI agents, only to discover a massive 17,000-line file in the data access layer. By systematically refactoring this code, I conducted an experiment to measure token consumption for future changes. The results were striking: breaking down the monolithic file reduced input token costs by 83%, proving that strategic refactoring significantly lowers the ongoing expense of working with AI agents.

The saving is not because there is less code to read, but because the agent must be able to successfully identify the smallest subset of files necessary to read.
  1. Viliam1234

    I find it funny how the best practices for programmers, ignored in most IT companies, get reinvented as the best practices for AIs.

    Boring: The documentation should be in code, not in external Word documents uploaded to the company SharePoint server.

    Exciting: The documentation for the AI should be in code, not in external Word documents uploaded to the company SharePoint server.

    Boring: You should give your developers the big picture of the project, not just micromanage them using Jira tasks.

    Exciting: You should give your AI the big picture of the project in CLAUDE.md, not just micromanage it using prompts.

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

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

  2. rocky_raccoon

    I really enjoy refactoring. Like, doing it by hand (5 miles uphill in the snow both ways) rather than AI. I don't even understand why I enjoy it, because if done right, there's no visible change. When people ask me why I'm so pumped up after having worked on the codebase all day long, I can't give them any sort of answer that makes sense (coming from a small business with a smaller dev team). It's just... "I'm future-proofing our website and we won't see any direct results today but things will be so much easier going forward..."

    There's something about the puzzle. Looking at my old, deranged coding workarounds that tried to solve problems that have already been solved a thousand times before with established paradigms; and then moving them toward said best practices; and doing it in a way that no NEW technical debt is created. It's just satisfying.

    I think one of the best learning experiences for me has been the fact that I created a bunch of sloppy shit by hand, auth and all, which forced me to learn things the hard way. All along the way, people were shouting from the rooftops: "use established libraries, dummy!", which is the same advice I would give to somebody today. But by doing things the hard way, I learned so much more about the inner workings. And, I've also given myself a decade's worth of refactoring work, which I really enjoy!

  3. whats_a_quasar

    This is such a nice piece, this is how people should write about AI. Specific, grounded to how the tools are actually being used, and quantitative. There is so much bad AI commentary that is incredibly vague, divorced from any actual use cases, or written by people who don't actually use the tools. It is good to see a critique that is "here is a thing AI is bad at and measurements to show it" rather than gesturing "here is why I think AI is problematic for society."

    It is rather different but another piece of research I liked for the same reason was this report that interviewed Boko Haram members about how they used AI to assist terrorism. You get these interminable online debates that are so unproductive and reporting that is specific is such a breath of fresh air.

    https://casp.ac/reports/ai-enabled-terrorism

  4. firasd

    I think this is one of those things where a human in the loop is indispensable

    An agentic refactoring pass does make sense cause one LLM reviewing work can spot things the ‘generator’ LLM missed while focused on the initial task output

    But can the reviewer agent ever actually have in mind what this project actually is? And how the code all comes together to do the work involved? In other words what parts of the code are redundant or can be made more elegant

    Asking coding agents to refactor your codebase is maybe like asking trauma surgeons to increase your exercise capacity. The agents are gonna need a really holistic POV to do this properly

    I guess part of my point is that just splitting big files into multiple files is only refactoring in a superficial sense without having a theory of what code belongs together and what can be extracted into utility functions etc. Is splitting files actually like decomposing factors or is it like splitting a larger number into smaller numbers that still eventually get added together

    A good example of what I mean is that agents often don’t ~actually~ understand the whole system anyway. They might implement a system to store and calculate something that is already being fetched via API. Humans often have a dual perspective — a holistic sense of the project and (when applying our mind to a task) a precise scalpel: ‘oh if we just look at this this JSON it has a key with this data already’

  5. BenoitEssiambre

    The benefits go beyond reducing token consumption. Compact contexts also foster better reasoning, enable intelligence across more layers if you can load them in a single context, and writing software to enable this, results in more correct software, software that _generalizes_, that has higher probability of being correct not just for tested cases, but for the interpolations and extrapolations of these cases.

    Refactoring towards good abstractions is more powerful than people realize. There's information theoretic bayesian math to back this up.

    It's a bit of a divine coincidence that software that is more economically and energy efficient to process and run tends to also be more correct.

    It's all about reducing the entropy of your code. https://benoitessiambre.com/entropy.html

  6. pmg101

    It's very interesting to have some data on this.

    It matches my experience which is that LLMs greatly benefit from well factored code, but are not particularly adept at creating such code.

    Much like most human developers I suppose!

  7. benrutter

    Tangent, but I have a theory that refactoring is one of the best symptoms of a healthy dev team.

    It's partly that refactors themselves have benefits, but I think more that the benefits to refactoring aren't visible to something like product-owners, feature tickets, etc.

    If teams are refactoring to ensure the health of the overall software, it's a tell-tale sign that developers are happy making recommendations for good software, and that those recommendations are being taken seriously.

    I think Martin Folwer might have actually coined the term "software rot" - either way, as an issue it happens most severely when a team either aren't motivated or empowered to build their vision of high quality software. When a team can follow their judgement of excellence, that's usually a great sign!

    (and yes, obviously this can go to far, there are probably some teams who rewrote all their stuff in Ruby then Node then Rust and now something else to be "agent native", but in the coorporate world, I see a lot less of this than teams who just don't feel like they have permission to improve things)

  8. holtkam2

    Great piece, but it misses the elephant in the room: the lion's share of economic benefit from refactoring will come from the fact that it makes it easier for humans to understand. That means 3am pages get resolved faster, fewer bugs will end up in prod, and your team can ship faster than your competition - gaining a leg up in the market. Most importantly, folks will be more comfortable accepting responsibility and ownership of a system when they understand it... this means that if/when something goes wrong, people will more quickly jump in and fix it, and when things could be better, folks will jump in and improve it.

More from this day

2026-07-30