TL;DR: Cleaner code reduced agent cost and churn in a study of 540 Claude Code runs, with 7.2% fewer input tokens, 8.5% fewer output tokens, and 34% fewer file revisits, while task completion stayed essentially unchanged, according to Sonar. The finding reframes code quality as an AI infrastructure cost control, not just a developer experience issue.
Editorial analysis by NHI Mgmt Group, based on content published by Sonar: “A cleaner codebase results in less token usage”.
By the numbers:
- The study covered 540 coding runs across six matched repository pairs.
Key questions
Q: Why does messy code increase the cost of AI-assisted development?
A: Messy code forces agents to read more, revisit earlier edits, and spend extra turns rebuilding context before they can safely change anything.
Q: How should teams measure whether AI coding agents are efficient?
A: Use a mix of outcome and effort metrics.
Q: What signs show that a codebase is difficult for AI agents to work in?
A: Look for repeated file revisits, long delays before the first edit, and high token usage on otherwise similar tasks.
Practitioner guidance
- Measure agent effort alongside completion rate Track input tokens, output tokens, file revisits, and turns to first edit for each recurring coding workflow.
- Prioritise repository clean-up in agent-heavy codebases Focus refactoring on deeply nested logic, inconsistent naming, and large functions in repositories where AI assistants already do meaningful work.
- Set governance thresholds for agentic runtime efficiency Create internal thresholds for acceptable token spend, revisit rates, and edit latency on standard tasks so engineering leaders can distinguish normal variance from structural inefficiency.
Bottom line: Cleaner code reduces the operational cost of AI agents even when it does not change whether the task is completed.
Explore further
View Full Forum → | NHI Foundation Course → | Our Services → | Read the full analysis →
Cleaner code is now part of AI governance, not just software engineering hygiene. Sonar's result shows that code structure changes the operating economics of AI agents, which makes maintainability a control variable for any programme deploying agentic development at scale. The governance question is no longer only whether the codebase is readable to humans, but whether it is legible enough for machine workflows to operate predictably. Practitioners should treat code quality as a runtime dependency for agent oversight.
A few things that frame the scale:
- 70% of organisations grant AI systems more access than they would give a human employee performing the exact same job, according to the 2026 Infrastructure Identity Survey.
- 19% of organisations give AI systems dramatically more access than human employees, nearly one in five granting unrestricted privilege, according to the 2026 Infrastructure Identity Survey.
A question worth separating out:
Q: Should security teams treat agent permissions differently in messy repositories?
A: Yes, because broader permissions in a hard-to-navigate codebase can amplify both cost and risk. If an agent must inspect or modify large parts of the repository to complete small tasks, keep its access narrowly scoped and pair that with stronger change review and traceability. Complexity should reduce, not expand, the agent's operating envelope.
👉 Read our full editorial: Cleaner code lowers AI agent costs without improving task success