cd /news/artificial-intelligence/producing-healthy-code-with-artifici… · home › topics › artificial-intelligence › article
[ARTICLE · art-172505] src=infoq.com ↗ pub= topic=artificial-intelligence verified=true sentiment=↑ positive

Producing Healthy Code with Artificial Intelligence Costs Fewer Tokens

Healthy code lets AI agents complete refactoring tasks with 40% to 45% fewer tokens, Adam Tornhill reported at GOTO Copenhagen, citing a study of more than 1,000 open-source projects in which AI-driven velocity gains were fully canceled out after two months by rising code complexity. Tornhill said an agent can fix roughly 50% of code health issues in lower-health code but only 20% once health improves, while adding an MCP server with code health data raised the fix rate to 90%-100%. The average codebase scores 5.15 on code health against the 9.5 threshold Tornhill calls AI-friendly, and he argued agentic refactoring is the next bottleneck for enterprise adoption.

recommendation

Raise code health to 9.5 or above and expose code health metrics to the agent via an MCP server; Tornhill measured 40%-45% fewer tokens and a 90%-100% fix rate versus 50% without it.

more in playbook: Controlling LLM cost
by read4 min views2 publishedNov 19, 2026
Producing Healthy Code with Artificial Intelligence Costs Fewer Tokens
Image: source

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.
── more in #artificial-intelligence 4 stories · sorted by recency
── more on @adam tornhill 3 stories trending now
sponsored brought to you by zahid.host 4,200+ EU-deployed projects
reading about agents? ship yours in a single git push.

Run your AI side-project on zahid.host

EU-based hosting, git-push deploys, automatic HTTPS, no cold starts. Free tier with a custom domain — perfect for shipping the agent you just read about.

$git push zahid main
→ Live at https://your-agent.zahid.host ✓
Get free account → Pricing
from €0/mo · no card required
LIVE [news/producing-healthy-co…] indexed:0 read:4min 2026-11-19 · —