What complexity costs AI agents
A study measures how many tokens AI agents use to review and fix code. But even in a healthy codebase, why can changing a single business rule send an agent through half the system?

I came across Markus Borg’s write-up through Martin Fowler’s Fragments. The headline caught my attention: “Does code quality affect AI token costs?” The study looked at how many tokens agents use to review and fix pull requests, and whether code quality helps explain that beyond the size of the change. My first thought: finally, we have some numbers.
In a study of 465 agent-authored pull requests from open-source repositories, the researchers used CodeScene’s CodeHealth metric. It combines signals such as file length, deeply nested logic, and complex methods into a score from 1 to 10. Before review, they scored each whole file the PR touched as it stood with the changes applied. They then averaged those scores, giving more weight to files with more changed lines.
The researchers accounted for how many lines each pull request changed. Their model estimated that reviewing and fixing a pull request with a CodeHealth score of 9.7 would use 22–35% fewer tokens than one scoring 2.8. Those were the typical scores in the highest and lowest groups. That’s a comparison between pull requests, not a measurement of what refactoring saves.
The researchers also looked at three common code problems. After accounting for the size of the change, they found that complex methods and conditions were linked to more tokens when the agent applied the review feedback. That held for all three AI models. They didn’t find a clear link for duplicated code. During review, agents made more model calls on code with lower CodeHealth scores, but they didn’t leave more comments.
The agents could look beyond the changed files within the same repository, and that work counted toward the pull request’s token total. But the study didn’t measure how much of that total went into tracing a business rule across components.
In the legacy systems I work on, an agent can usually help me follow the logic in a file. The harder question comes once I’ve found the rule: what else depends on it working the way it does today? Every function involved can be easy to read, and that question can still take me across components or repositories.
On a recent project, I designed the architecture for a telecom data platform and rebuilt it with the client’s team. The business logic was scattered across stored procedures and multiple repositories. We moved it into services with clear responsibilities and explicit contracts. That worked well when we wrote the code ourselves, and it worked well when we brought in coding agents. In both cases, a change started in the same place: the service that owned the rule, the specification that described its behavior, and the contracts the change had to respect.
Keeping Complexity Down
At a recent meetup, I got talking with a software practitioner whose former team had split a monolith into smaller components. Each component was easier to read on its own, but the system wasn’t any easier to change. To change one rule, the team still had to figure out which components were involved and what else might break. Give a coding agent that same change and it has more places to look before it knows what to edit. Now the pendulum swings back: he’s bringing related use cases back together.
We can scatter a business rule across components without ever splitting a monolith. It can happen one use case at a time, when we fit new work into the components we already have without looking at how the use cases belong together. The next time the rule changes, a developer or an agent has to find every piece of it. Whether we’re starting from scratch or extending an existing system, we need to look at several use cases together and decide which behavior should change together and which needs to change independently.
What static analysis can and can’t tell us
Even when a rule is in the right place, the code that implements it should be easy to follow. On a project I worked on, we used Detekt to measure cyclomatic complexity, a measure of how much a function branches. Some of the highest scores came from functions in our engines. That’s where we put the rules for how a business activity is performed, so we expected more branching there. We used the scores to decide which functions to read more closely. The chart below shows the final distribution.

Cyclomatic complexity in production Kotlin code.
A low score tells you something about the function, not how far you’ll have to go to change a rule.
My favorite is when a team says they’ve modernized a system because they put a new API in front of the same legacy stored procedures. Detekt can tell us the Kotlin API has little branching. It doesn’t count the branches inside the stored procedures. If a business rule changes, the agent still has to follow the call into the database and work through the branches there. The score isn’t wrong. It just stops before the hard part.
Limiting the ripple effects of change
Everything looks simple on day one. The test is whether the system is still easy to change after years of new rules, integrations, and exceptions. The point isn’t to make every component simple. It’s to keep the complexity of one part from becoming something every other part has to deal with. That way of drawing boundaries is central to Juval Löwy’s IDesign Method. Juval and Monty Montgomery have taught it for well over a decade, alongside other practices for designing well-factored systems. We design the system up front, taking into account how the business rules, external systems, and processes around it might change. Then we build in increments and see where those boundaries hold up and where we need to adjust them.
For years, many teams treated the cost of tangled code as part of the job. A developer could spend half a day chasing a change through the system. We saw that half day as the cost of making the change, not as a cost of the design.
The study measured one part of it in tokens. But even if tokens were free, I’d still want to change a business rule without having to understand half the system or risk changing behavior somewhere I never meant to touch. An agent can help us make the change. The architecture shapes how far that change can reach.