News

Clean Code Doesn't Help AI Agents Pass Tests, But It Saves Tokens

A new study from SonarSource shows that code cleanliness has no effect on an AI agent's pass rate, but significantly reduces token usage and file revisits.

July 6, 2026· 2 min read· Source: arXiv.org
Clean Code Doesn't Help AI Agents Pass Tests, But It Saves Tokens

A paper from researchers at SonarSource tackles a question that's been nagging teams adopting AI coding agents: does the cleanliness of your codebase matter when an LLM is the one making changes? The short answer is yes—but not in the way you might expect.

The Minimal-Pair Protocol

The authors, Priyansh Trivedi and Olivier Schmitt, built six repository pairs that were functionally identical but differed in code quality. Each pair matched on architecture, dependencies, and external behavior, but diverged on static-analysis rule violations and cognitive complexity. The pairs were constructed in both directions: some started clean and were degraded, others started messy and were cleaned up.

They then authored 33 tasks across these pairs and ran 660 trials using Claude Code. The evaluation measured pass rates against hidden tests at the application's public surface, along with token consumption and file revisitation patterns.

Pass Rates Are Flat, But Costs Drop

The headline finding: code cleanliness did not change the agent's pass rate. Clean or messy, the agent completed tasks at the same rate. However, the operational footprint changed dramatically. Agents working on cleaner code used 7–8% fewer tokens and reduced file revisitations by 34%.

That's a meaningful cost saving, especially for teams running agents at scale. Token consumption directly translates to API costs, and fewer file revisits means faster iteration cycles.

What This Means for Engineering Teams

The results suggest that traditional maintainability principles aren't obsolete in the age of AI-driven development—they just serve a different purpose. Clean code doesn't make the agent smarter, but it makes the agent cheaper and more efficient. This joins model choice, harness design, and prompting as a factor that materially affects agent behavior.

For teams already investing in code quality tooling (SonarQube, linters, etc.), this is validation that those investments pay off even when an AI is doing the coding. For teams that have been lax about code hygiene, this is a data point that cleanliness has a real, measurable impact on operational costs.

The paper is a welcome dose of empirical rigor in a space that's been dominated by vibes and vendor claims. It's not a blockbuster result, but it's a useful one—and it points toward a more systematic approach to evaluating how codebase characteristics affect AI agent performance.