Code cleanliness cuts Claude Code token usage by eight percent
A controlled minimal-pair study by SonarSource shows that while code quality does not affect an agent's task pass rate, it reduces token usage by up to 8% and file revisitations by 34%. Keeping your codebase clean directly lowers agent execution costs.
Impact: Medium
Why it matters
Developers can reduce AI coding bills and navigation cycles simply by enforcing traditional static analysis and refactoring rules.
TL;DR
- 01Enforcing codebase cleanliness does not directly improve LLM task pass rates, but significantly improves computational efficiency.
- 02Cleaner code bases reduce file revisitations by 34%, preventing agents from looping redundantly.
- 03Using clean code reduces overall token usage by 7% to 8%, offering a direct saving on API consumption.
Key facts
- Token reduction in clean code
- 7% to 8%
- File revisitations reduction
- 34%
- Total trials in study
- 660
- Evaluation model
- Claude Code
Designing the Minimal-Pair Benchmark
To isolate code cleanliness from agent capability, the study constructed minimal-pair repositories. These pairs matched exactly on public architecture, external APIs, and dependencies, but diverged on code health indicators. One set of repositories was systematically cleaned of static-analysis violations and cognitive complexity, while the other was deliberately degraded. Researchers then authored 33 tasks evaluated via hidden end-to-end tests.
Token Savings and Navigation Efficiency
The researchers executed 660 runs utilizing Claude Code. While the messy code did not prevent Claude Code from passing the functional tests, it exacted a heavy operational toll:
- Token Cost: Working in clean repositories resulted in a 7% to 8% reduction in total tokens consumed.
- File Revisitations: Agents working on messy code frequently lost track of context, leading to a 34% increase in the number of times they had to reopen and reread files.
Practical Team Enforcement
As a developer, this means maintaining traditional code quality is no longer just for human readability—it is an economic optimization for agentic coding. To exploit these findings, team leads can integrate rigid static checks into pre-commit workflows. Using tools like tach for import boundaries or standard linters ensures that autonomous agents do not waste expensive API tokens fighting avoidable structural noise.
Try it in 2 minutes
# Example pre-commit config to enforce code cleanliness before agent run
repos:
- repo: https://github.com/pre-commit/mirrors-autopep8
rev: v2.0.4
hooks:
- id: autopep8
- repo: https://github.com/gauge-sh/tach
rev: v0.1.0
hooks:
- id: tachyaml
✓ When to use
- When trying to control escalating API costs of large-scale agentic code refactoring projects.
- To optimize repositories for smoother execution when using autonomous agentic tools like Cursor or Claude Code.
✕ When NOT to use
- When your team does not use autonomous agents and relies solely on manual developers who are unaffected by micro-token fluctuations.
- In rapid prototyping phases where structure is highly volatile and static checkers impede velocity.
What to do today
- Integrate static code analysis tools like flake8 or autopep8 into your CI pipeline.
- Configure pre-commit hooks to enforce import boundaries and limit cognitive complexity before letting agentic tools run.
What the community says
“You can put many agent constraints in precommit hooks if they're static checks... When an agent is prepped to make commits themselves, it will catch pre-commit failing and correct itself.”
“To be sure, we had hidden tests that validate whether agent implemented the task appropriately... What we didn't do... was to ensure that there are no regressions in the remaining tests”
Sources