A practical guide to using agentic AI for code refactoring without losing speed. Healthy code improves model performance.
The promise of agentic AI is tempting: completing development tasks two to three times faster. However, recent research shows that these speed gains evaporate in just two months if the resulting code accumulates complexity. In his talk at GOTO Copenhagen, Adam Tornhill explained why healthy code not only benefits humans but also drastically reduces the token consumption of language models.
A study of over a thousand open-source projects showed that, with AI assistance, initial speed increases during the first few weeks, but complexity grows so much that the benefit completely disappears by the second month. Tornhill summarizes it as: AI operates in self-harm mode, writing code that it then cannot reliably maintain. This guide outlines the practical steps he proposed to break that cycle.
What you need
Before you start, make sure you have a way to measure code health. Tornhill recommends combining metrics from three levels: module design (cohesion and responsibilities), function design (expressive APIs), and implementation (avoiding deeply nested logic or excessive copy-paste). Additionally, you'll need an MCP (Model Context Protocol) that exposes this health data to the agent, and a reliable test suite to act as a safety net.
- Code analysis tool that calculates health metrics at the module, function, and implementation levels.
- A local MCP server or CLI tool that the agent can query.
- Test suite with sufficient coverage to detect behavioral regressions.
- Access to a language model like Claude with iterative refactoring capabilities.
Step-by-step agentic refactoring process
The key is to provide the agent with an objective feedback loop. Without concrete health data, Claude or another LLM tends to become conservative and make small changes that don't move the quality needle. According to the research presented, with an MCP supplying health information, the correction rate increases from 20-50% to 90-100%.
- Establish a baseline by measuring current code health; identify modules and functions with low scores.
- Build a safety net with automated tests: the agent needs to know if a change breaks existing behavior.
- Configure the MCP so the agent can query health metrics in each iteration.
- Launch refactoring in short cycles: the agent proposes a change, runs tests, and verifies if the health metric improves.
- Allow the agent to learn: according to the large-scale refactoring study, instructing the agent to build a playbook of successful repository-specific patterns accelerates subsequent iterations.
A relevant economic fact: healthy code allows the same tasks to be performed with 40-45% fewer tokens. For teams using paid APIs, this directly translates into cost savings. Furthermore, pre-AI era research already showed that working on healthy code can be up to ten times faster and reduces the risk of defects fifteen times.

Automating safeguards
Tornhill insisted that all safeguards must be fully automated in an agentic context. It's not enough to have a linter or code coverage if the agent cannot execute them and read their results. The specific tools he mentioned include linting, coverage, mutation testing to ensure that tests actually prove something, and high-level metrics like code health. All of this is exposed to the agent via local MCP servers or CLI commands.
An often-overlooked aspect: the average codebase has a health score of 5.15, while truly AI-friendly code starts at 9.5. This gap explains why many organizations see diminishing returns when adopting coding agents without first addressing existing technical debt.

Tornhill's conclusion points to the future: code quality at the module level is a problem practically solved with current automation. The next challenge is scaling AI-friendliness to the design and architecture level, where decisions impact multiple components and require a broader view of the system. For now, starting with existing code and making it healthy is the first step to prevent AI from becoming a source of accelerated technical debt.
To learn more: Chinese edition.
Original source: infoq.com
Article generated with AI.larebelion



Comments (0)
No comments yet. You can be the first.
Write a comment