{"slug": "producing-healthy-code-with-artificial-intelligence-costs-fewer-tokens", "title": "Producing Healthy Code with Artificial Intelligence Costs Fewer Tokens", "summary": "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.", "body_md": "Agentic AI can speed up velocity, but the gains vanish within months as code complexity grows. In his presentation on [AI-friendly code](https://www.youtube.com/watch?v=ciRTdOIhNCc) at [GOTO Copenhagen](https://gotocph.com/), 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.\n\nDeveloping 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.\n\nTornhill 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:\n\nAI operates in a self-harm mode, often writing code it cannot reliably maintain later.\n\nCode 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.\n\nBuilding 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.\n\nRecent 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.\n\nThe 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:\n\nAs 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.\n\nAdding 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.\n\nHealthy code is also financially viable code, with healthy code, you can do things with 40% to 45% fewer tokens, Tornhill said.\n\nBy 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.\n\nInfoQ interviewed [Adam Tornhill](https://www.linkedin.com/in/adam-tornhill-71759b48/) after his talk.\n\n**InfoQ: How can we measure code health?**\n\n**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:\n\n- Module level design: e.g. are your modules cohesive and do one thing well? Or are there multiple, unrelated responsibilities?\n- Function level design: e.g. how well-designed are the individual function blocks? How expressive are their APIs?\n- 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?\n\n**InfoQ: How do you safeguard AI-generated code?**\n\n**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.\n\n**InfoQ: How can we do refactoring at scale with agents?**\n\n**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.\n\n- The baselining step focuses on measuring code quality to identify what needs improving.\n- The safety-net is a reliable test suite that helps preserve the existing behavior of the codebase.\n- 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.\n- Finally, enable the agents to learn. In our study on\n[Refactoring at Scale with Agents](https://codescene.com/blog/case-study-refactoring-at-scale-with-agents), we instructed agents to build an evolving playbook of successful codebase-specific refactoring patterns.", "url": "https://wpnews.pro/news/producing-healthy-code-with-artificial-intelligence-costs-fewer-tokens", "canonical_source": "https://www.infoq.com/news/2026/11/healthy-code-AI-tokens/?utm_campaign=infoq_content&utm_source=infoq&utm_medium=feed&utm_term=global", "published_at": "2026-11-19 11:09:00+00:00", "updated_at": "2026-11-19 11:45:25.350578+00:00", "lang": "en", "topics": ["artificial-intelligence", "ai-agents", "ai-tools", "agent-protocols", "developer-tools"], "entities": ["Adam Tornhill", "GOTO Copenhagen", "InfoQ", "Claude", "MCP"], "also_reported_by": [], "takeaway": "Refactoring code to a code health score of at least 9.5 before agent work cuts token use by 40% to 45%, and supplying code health data through an MCP server lifts an agent's fix rate from about 50% to 90%-100%.", "recommendation": {"advice": "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.", "playbook": "llm-cost", "playbook_url": "https://wpnews.pro/playbooks/llm-cost"}, "alternates": {"html": "https://wpnews.pro/news/producing-healthy-code-with-artificial-intelligence-costs-fewer-tokens", "markdown": "https://wpnews.pro/news/producing-healthy-code-with-artificial-intelligence-costs-fewer-tokens.md", "text": "https://wpnews.pro/news/producing-healthy-code-with-artificial-intelligence-costs-fewer-tokens.txt", "jsonld": "https://wpnews.pro/news/producing-healthy-code-with-artificial-intelligence-costs-fewer-tokens.jsonld"}}