Agentic AI can speed up velocity, but the gains vanish within months as code complexity grows. In his presentation on AI-friendly code at GOTO Copenhagen, Adam Tornhill showed that healthy code makes AI perform better and costs fewer tokens. Safeguarding means keeping code health high through agentic refactoring, with an MCP supplying code health data, so AI can maintain the code it generates.
Developing software with agents is significantly harder than writing code manually, Tornhill argued. But with agentic AI, they can complete tasks two to three times faster, he added. It also helps them unblock themselves and reduces coordination needs.
Tornhill mentioned a study done on 1000+ open-source projects which showed that with AI, velocity went up during the first few weeks, but after two months the velocity gains were fully canceled out due to a massive increase in code complexity:
AI operates in a self-harm mode, often writing code it cannot reliably maintain later.
Code health measures how easy code is to understand, change, and maintain. Research done on human coding (before AI) shows that if you have healthy code, you can be up to 10x faster than someone who works on similar problems in unhealthy code, Tornhill said. The defect risk is reduced 15 times, and code reviews go twice as quickly, he added.
Building a prototype was never the hard problem in software; 95% of the costs for any software project come after the first release. This is where code quality, maintainability, and architecture start to matter, Tornhill said.
Recent research shows that large language models perform consistently better in healthy code. AI won’t generate perfect code; if we want to survive in the Agentic age, we need to focus on safeguarding, Tornhill said.
The average codebase has a code health of 5.15, where AI-friendly code starts at 9.5. Technical depth is a bottleneck for enterprise adoption of agentic coding; what’s needed is an agentic refactoring, Tornhill argued. Claude can be used to fix code health issues, but only to a certain extent:
As a best-case scenario, an agent can fix roughly 50% of the code health issues for lower-health code. Once the code health improves, the AI performance drops sharply, and Claude can only correctly refactor 20% of the code. AI tends to become conservative, doing small changes that don’t really move the needle on code quality.
Adding an MCP with code health data can lead to a 90%-100% fix rate, Tornhill said. Agentic refactoring turned out to be a predictable and mechanical process at scale, where AI was capable of doing codebase-specific refactorings.
Healthy code is also financially viable code, with healthy code, you can do things with 40% to 45% fewer tokens, Tornhill said.
By refactoring code, modularizing it and making it healthy, we can also reduce organizational friction, Tornhill said. Code quality at the module level is a largely solved problem with the automation we now have. Scaling AI friendliness to the design and architectural level is the next big challenge, he concluded.
InfoQ interviewed Adam Tornhill after his talk. InfoQ: How can we measure code health?
Adam Tornhill: Code Health is a multifacetted concept, so we need to combine multiple metrics. We’ve found that it works best when combining measures from three levels of software design:
- Module level design: e.g. are your modules cohesive and do one thing well? Or are there multiple, unrelated responsibilities?
- Function level design: e.g. how well-designed are the individual function blocks? How expressive are their APIs?
- Implementation level: where you want to avoid any accidental complexity in how the code is written such as deeply nested logic, excessive copy-paste, etc?
InfoQ: How do you safeguard AI-generated code?
Tornhill: Safeguarding needs to happen at multiple levels, and all safeguards need to be completely automated in an agentic context. Examples of concrete safeguards include linting, code coverage, mutation testing to ensure your tests actually test, and higher-level metrics like Code Health. In practice, these safeguards are exposed to agents as local MCP servers or command-line tools.
InfoQ: How can we do refactoring at scale with agents?
Tornhill: The main steps are to establish a baseline, build a safety-net for behavior, and provide a feedback loop to the agents refactoring the code. The whole process is highly iterative.
- The baselining step focuses on measuring code quality to identify what needs improving.
- The safety-net is a reliable test suite that helps preserve the existing behavior of the codebase.
- The agentic feedback loop builds on objective code health information and test outcomes so that the AI can determine whether a change moves the code quality closer to the goal.
- Finally, enable the agents to learn. In our study on Refactoring at Scale with Agents, we instructed agents to build an evolving playbook of successful codebase-specific refactoring patterns.