TL;DR: Cleaner code reduced Claude Code usage by 7.1% fewer input tokens, 8.5% fewer output tokens, and 11.1% less reasoning effort, while pass rates stayed effectively unchanged, according to Sonar’s controlled study. The finding matters because code structure now influences both developer productivity and AI operating cost, turning maintainability into a FinOps and governance issue.
NHIMG editorial — based on content published by Sonar: how code cleanliness affects AI coding agent bills and token usage
By the numbers:
- Agents working on cleaner code used 7.1% fewer input tokens in Sonar’s controlled study.
- Teams mixing inline completion and agentic coding tools now spend $200 to $600 per engineer per month, according to DX research.
- The FinOps Foundation says AI spend moved from 31% of teams actively managing it two years ago to 98% today.
Questions worth separating out
Q: How should security teams manage AI coding agents in repositories with poor code structure?
A: Start by reducing the amount of code an agent must inspect to complete a task.
Q: Why does code structure affect AI coding agent costs so much?
A: Agentic tools do not just generate text.
Q: What do teams get wrong about using comments to help coding agents?
A: Comments can help, but they do not fix a hard-to-navigate structure.
Practitioner guidance
- Apply cognitive complexity thresholds to agent-heavy repositories Set explicit thresholds for methods and modules that are frequently touched by coding agents, then refactor the highest-density paths first.
- Prioritise naming and boundary cleanup before broad refactoring Target the functions, files, and interfaces that agents repeatedly search for, especially in multi-module workflows.
- Treat AI coding cost as part of FinOps reviews Include token spend, session length, and reread frequency in your cost management process, especially for teams that use agentic tools daily.
What's in the full report
Sonar's full analysis covers the experimental design and task-level measurements this post intentionally leaves for the source:
- Matched repository-pair methodology showing how clean and degraded codebases were built and compared
- Task-by-task token and reasoning deltas across 660 Claude Code trials, including variance by repository pair
- The comments normalisation ablation and why structure, not documentation volume, drove the footprint difference
- The long-horizon hypothesis comparing per-task savings with structural drift over repeated agent edits
👉 Read Sonar’s analysis of how code cleanliness affects AI coding agent cost →
Code quality and AI agent bills: what practitioners need to know?
Explore further