DEV Community

Cover image for Your Messy Codebase Is Secretly Costing You More: How Code Cleanliness Shapes AI Coding Agent Efficiency
Manoranjan Rajguru
Manoranjan Rajguru

Posted on

Your Messy Codebase Is Secretly Costing You More: How Code Cleanliness Shapes AI Coding Agent Efficiency

Your Messy Codebase Is Secretly Costing You More: How Code Cleanliness Shapes AI Coding Agent Efficiency

Meta Description: New 2026 research reveals that messy codebases cost 7–8% more in AI tokens and cause 34% more file revisitations when using autonomous coding agents. Discover what the science says and how to make your codebase AI-agent ready.

Clean vs Messy Code - AI Agent Navigation


Table of Contents

  1. Introduction — The Hidden Tax of Technical Debt in the AI-Agent Era
  2. The Agent Economy: Why Token Cost Matters Now
  3. The Study: Minimal Pairs and Controlled Science
  4. Key Findings — What Clean Code Changes (and What It Doesn't)
  5. The File Revisitation Signal: Why Agents Keep Coming Back
  6. Track-Level Breakdown: Multi-Module vs. Cognitive Hotspots
  7. The Real Cost: Running the Numbers at Production Scale
  8. Practical Playbook: Making Your Codebase Agent-Ready
  9. The "Vibeclean" Experiment: Can Agents Clean Themselves?
  10. Limitations and Open Questions
  11. Conclusion: Your SOLID Principles Are Now Your AI Budget

1. Introduction — The Hidden Tax of Technical Debt in the AI-Agent Era

Here's a question your sprint planning meetings probably haven't asked yet: how much does your technical debt cost you in AI tokens?

You already know the human cost. Messy codebases slow down onboarding, inflate cognitive load, and turn routine bug fixes into afternoon-long archaeological digs. But as autonomous AI coding agents — tools like Claude Code, GitHub Copilot Workspace, and a growing zoo of agentic scaffolding frameworks — become first-class members of your engineering team, that messy codebase is now billing you twice: once in developer productivity, and again in API costs every time an agent has to navigate it.

A research paper published in May 2026 by engineers at SonarSource (arXiv:2605.20049) set out to answer a deceptively simple question: does the structural quality of your code affect how efficiently an AI coding agent navigates and modifies it? The answer, backed by 660 controlled trials, is nuanced but actionable: clean code doesn't make agents smarter, but it makes them meaningfully cheaper and significantly less confused.

This post breaks down the research in full, draws out the engineering implications, and gives you a concrete playbook for tuning your codebase for the agents that are already running on it.


2. The Agent Economy: Why Token Cost Matters Now

Before diving into the research, it's worth grounding the stakes. We're no longer talking about AI pair-programming as a novelty.

A 2026 survey of 128,018 GitHub projects found traces of autonomous AI agent activity in 22–29% of all repositories — in codebases of every size and age — less than a year after the first practical coding agents shipped at scale. Agentic software development is not a future state. It is happening now, at volume, across the industry.

Running these agents is expensive. According to a 2026 analysis of token consumption on SWE-bench Verified (Bai et al., 2026), a single task averages around 4 million tokens across frontier LLMs — with input tokens (the code the agent reads) dominating the bill. At typical API pricing of $3–15 per million tokens, that's $12–$60 per task. Run a thousand tasks a month — a reasonable baseline for a mid-size engineering org that has leaned into agentic workflows — and you're looking at $12,000–$60,000 in monthly API spend before you've written a single line of application logic.

And here's the core problem: most teams evaluate their agents purely on pass rate — whether the agent completed the task correctly. Nobody is asking what it cost to complete the task, or why the same task sometimes costs 2.5× more in tokens on one run versus another on the same codebase.

That's exactly the gap this research fills.


3. The Study: Minimal Pairs and Controlled Science

The central methodological challenge: in the wild, you can't separate code quality from code functionality. A messy codebase usually has messy behavior too. To isolate the variable cleanly, the SonarSource team invented a clever experimental apparatus: minimal pairs.

A minimal pair is two versions of the same repository that are:

  • Architecturally identical
  • Written in the same language, framework, and with the same dependencies
  • Externally identical — same test suite, same API surface, same observable behavior
  • But differing on cleanliness alone, measured by SonarQube static-analysis rule violations and cognitive complexity density

Slopify and Vibeclean Pipeline Diagram

Six such pairs were constructed across Java and Python codebases, split between private SonarSource repos (to prevent the model from having trained on them) and public open-source projects (Apache Commons BCEL, Netflix Genie, CKAN). The pair construction itself was agentic — two pipelines were designed:

Slopify takes a clean, well-maintained codebase and degrades it — inlining helpers back into callers, duplicating logic across code paths, padding files with dead code, occasionally merging modules into single bloated files. The goal is to produce code that plausibly grew on a team without code review or linting — not deliberately sabotaged, just neglected.

Vibeclean takes an organically messy codebase and resolves its SonarQube violations mechanically — deduplicating string literals, deleting commented-out code, replacing legacy collection idioms, removing dead branches, and breaking up god structures (200+ line dispatch switches, 2,800-line classes) into named helpers.

Across the six pairs, the difference in code quality was dramatic. The sonar-caas-poc pair went from 16 SonarQube issues to 855 after Slopify. The CKAN pair went from 1,006 to 3,632. These are not trivially different codebases — they represent the real spectrum from actively maintained to years of accumulated neglect.

Thirty-three tasks were authored across the six pairs — add a feature, fix a behavior, extend an interface — all described in purely external terms with no mention of internal structure. The agent had to explore and navigate on its own. Each task was run 10 times per side, yielding 660 trials total, using Claude Code backed by Claude Sonnet 4.6.


4. Key Findings — What Clean Code Changes (and What It Doesn't)

AI Agent Token Usage: Clean vs Messy Code

Pass Rate: Unchanged

The first and most important finding: clean code does not make agents better at their job. Pass rate — the fraction of hidden tests that the agent's output passes — moves by less than a percentage point between clean and messy sides: 91.3% on cleaner code vs. 92.1% on messier code (−0.9 pp). Statistically negligible.

This is essential context. The research is not claiming clean code produces fewer bugs or more correct agent outputs. It's saying something subtler and, for engineering economics, arguably more important.

Token Footprint: A Consistent 7–8% Reduction

Across the 660 trials, agents working on cleaner code consistently consumed fewer resources:

Metric Change (Clean vs. Messy)
Input tokens -7.1%
Output tokens -8.5%
Reasoning characters -11.1%
Conversation messages -7.0%
Turns before first edit -3.6%

Seven percent might not make your jaw drop on a single task. But applied consistently across all your agentic workloads at scale, it's a meaningful reduction in your monthly AI bill — and the downstream effects are larger than the token count suggests.

File Revisitation: The 34% Effect

The most striking number in the study has nothing to do with tokens. It's about behavior: clean code reduces file revisitations by 34%.

File revisitation is how often an agent re-reads a file it has already edited. The typical pattern: read file → make edit → do other work → come back and re-read the same file. The researchers interpret this as uncertainty about a previous edit — the agent isn't confident its change was correct, so it checks again.

On clean code, this uncertainty-driven behavior drops by a third. On commons-bcel specifically, the effect reaches 68.5% fewer revisitations. Crucially, every single repo in the study showed a reduction in revisitation on the cleaner side — it's the most consistent and interpretable finding in the entire dataset.


5. The File Revisitation Signal: Why Agents Keep Coming Back

AI Agent File Navigation: Clean vs Messy Codebase

To understand why revisitation drops on clean code, think about how a coding agent actually navigates a codebase.

Agents like Claude Code don't hold the entire codebase in context. They explore by reading files, building a working model of relevant code, formulating a plan, making changes, and then — sometimes — second-guessing those changes. When they second-guess, they re-read.

In a messy codebase, the sources of second-guessing multiply:

  • God methods (500+ lines, deep nesting) make side effects genuinely hard to reason about. Did the edit on line 340 interact with the branching logic at line 480?
  • Duplicated logic spread across three files means the agent can never be sure it's edited all the right places.
  • Opaque naming (_xfm_q2, proc2, handleStuff) forces the agent to read more of every file just to understand its purpose.
  • Dead code and unreachable branches introduce noise — the agent can't reliably distinguish live logic from vestigial artifacts.

Clean code acts as living documentation. Small, single-purpose functions with descriptive names convey intent explicitly. Low cognitive complexity means edits have bounded, predictable side effects. The agent can read less, understand more, and move on confidently.

This is the same reason clean code helps human developers. But where humans get habituated to a messy codebase — we stop seeing the chaos — LLM agents have no such adaptation. Every context window is a fresh read. The mess costs the same computational attention every single time.


6. Track-Level Breakdown: Multi-Module vs. Cognitive Hotspots

The study divided its 33 tasks into three tracks. The per-track analysis reveals important nuances obscured by the headline numbers.

Multi-Module Tasks: Where Cleanliness Pays Most

Tasks requiring changes that span two or more module boundaries show the most dramatic effects:

Metric Multi-Module Effect
Input tokens -10.7%
File revisitations -50.8%

When a task requires the agent to understand how two parts of a system interact, messy module seams become brutal. Leaky abstractions, accidental coupling, unclear dependencies — the agent loops: modifies Module A, suspects Module B might be affected, reads Module B, edits it, then worries about Module A again and re-reads it...

On clean codebases with well-factored modules and explicit interfaces, this loop tightens dramatically. A 50% reduction in revisitations on multi-module tasks is not noise — it's a real behavioral signal with direct cost implications.

Key insight: If you're going to optimize one thing for agentic workloads, clean module boundaries give you the highest return on investment.

Cognitive Hotspot Tasks: A Surprising Twist

Tasks routed through regions of high cognitive complexity — god methods, deeply nested control flow, large dispatch switches — tell a different story:

Metric Cognitive Hotspot Effect
Input tokens +1.8% (effectively neutral)
Files read +11.2%
File revisitations -20.2%

Clean hotspots don't reduce token footprint — the agent reads more files (+11.2%). Why? Because Vibeclean extracts large methods into smaller named helpers, distributing complexity across more files rather than eliminating it. The agent now navigates a wider spread of smaller functions.

Revisitations still drop (less per-file uncertainty), but the overall token footprint is roughly neutral. Refactoring god methods is still valuable — for human understandability, for team velocity, for maintainability — but don't expect it to meaningfully reduce your AI token bills. That ROI lives at the module boundary level.


7. The Real Cost: Running the Numbers at Production Scale

Let's run the math that matters for engineering leaders signing off on AI infrastructure.

Baseline assumptions:

  • 1,000 agentic tasks per month (mid-size engineering org)
  • 4M tokens per task (SWE-bench 2026 baseline)
  • Input token cost: $3/million (approximate frontier model pricing — verify before publishing)
  • 7.1% token reduction from clean code (dataset-level average from arXiv:2605.20049)
# Token cost model: clean vs. messy codebase at scale

TASKS_PER_MONTH = 1_000
TOKENS_PER_TASK = 4_000_000        # average from SWE-bench 2026 baseline
INPUT_TOKEN_COST = 3.00 / 1_000_000  # $ per token (approx frontier pricing)
CLEANLINESS_REDUCTION = 0.071      # 7.1% input token reduction (arXiv:2605.20049)

messy_cost = TASKS_PER_MONTH * TOKENS_PER_TASK * INPUT_TOKEN_COST
clean_cost  = messy_cost * (1 - CLEANLINESS_REDUCTION)
savings     = messy_cost - clean_cost

print(f"Monthly cost (messy codebase):    ${messy_cost:>10,.2f}")
print(f"Monthly cost (clean codebase):    ${clean_cost:>10,.2f}")
print(f"Monthly savings from cleanliness: ${savings:>10,.2f}")
print(f"Annual savings:                   ${savings * 12:>10,.2f}")

print("\n--- Savings at scale ---")
for scale in [1_000, 10_000, 100_000]:
    annual = scale * TOKENS_PER_TASK * INPUT_TOKEN_COST * CLEANLINESS_REDUCTION * 12
    print(f"  {scale:>7,} tasks/month  →  ${annual:>12,.0f} / year saved")
Enter fullscreen mode Exit fullscreen mode
Monthly cost (messy codebase):    $ 12,000.00
Monthly cost (clean codebase):    $ 11,148.00
Monthly savings from cleanliness: $    852.00
Annual savings:                   $ 10,224.00

--- Savings at scale ---
    1,000 tasks/month  →      $10,224 / year saved
   10,000 tasks/month  →     $102,240 / year saved
  100,000 tasks/month  →   $1,022,400 / year saved
Enter fullscreen mode Exit fullscreen mode

Beyond the dollar figures, account for the compounding qualitative cost of 34% extra revisitations:

  • Longer wall-clock time per task — agent loops waste real seconds
  • Increased context window saturation on long-running tasks
  • Higher probability of agent derailment or contradictory edits as context fills
  • Harder to debug agent trajectories when revisitation patterns are erratic

8. Practical Playbook: Making Your Codebase Agent-Ready

The research gives a clear signal. Here's how to act on it today.

Step 1: Run Static Analysis as a Hard CI Gate

The study used SonarQube as its cleanliness proxy. If you're not already running static analysis on every PR, now is the moment — not just for human readability, but as a direct investment in agent efficiency.

Python:

# Ruff: fast linter + formatter
pip install ruff
ruff check . --select ALL --fix
ruff format .

# Pylint: deeper analysis with a quality gate
pip install pylint
pylint src/ --fail-under=8.0
Enter fullscreen mode Exit fullscreen mode

Java (SonarQube via Docker):

docker run --rm \
  -e SONAR_HOST_URL="http://sonarqube:9000" \
  -e SONAR_LOGIN="${SONAR_TOKEN}" \
  -v "$(pwd):/usr/src" \
  sonarsource/sonar-scanner-cli
Enter fullscreen mode Exit fullscreen mode

TypeScript/JavaScript:

# .eslintrc.json — add these agent-oriented rules
# "complexity":              ["error", 10]
# "max-lines-per-function":  ["error", {"max": 50}]
# "max-depth":               ["error", 4]

npx eslint src/ --max-warnings 0
Enter fullscreen mode Exit fullscreen mode

Step 2: Enforce Cognitive Complexity — With a Real Example

Here's the exact transformation that Vibeclean applies — and that you should apply to your highest-traffic agent-touched modules:

# BEFORE: High cognitive complexity — expensive for agents AND humans
def process_order(order, user, config, state):
    if order:
        if order.status == 'pending':
            if user:
                if user.is_premium:
                    if config.get('premium_fast_track'):
                        if state.queue_length < 10:
                            return fast_track_process(order)
                        else:
                            return standard_process(order, priority='high')
                    else:
                        return standard_process(order)
                else:
                    return standard_process(order, delay=True)
            else:
                raise ValueError("User required")
        elif order.status == 'cancelled':
            return None
    return None


# AFTER: Low cognitive complexity — clear contracts, agent-navigable
def process_order(order, user, config, state):
    """Route an order to the appropriate processing pipeline."""
    if not order or order.status == 'cancelled':
        return None
    _validate_user(user)
    return _route_to_pipeline(order, user, config, state)


def _validate_user(user):
    """Raise if user context is missing for order processing."""
    if not user:
        raise ValueError("User required for order processing")


def _route_to_pipeline(order, user, config, state):
    """Select the processing pipeline based on user tier and queue state."""
    if user.is_premium and _can_fast_track(config, state):
        return fast_track_process(order)
    return standard_process(
        order,
        priority='high' if user.is_premium else 'normal',
        delay=not user.is_premium
    )


def _can_fast_track(config, state) -> bool:
    """Return True if the fast-track lane is configured and available."""
    return config.get('premium_fast_track', False) and state.queue_length < 10
Enter fullscreen mode Exit fullscreen mode

When an agent needs to modify routing logic, it reads _route_to_pipeline and immediately knows it doesn't need to understand validation or queue availability unless those are the actual concern. The cognitive boundary is explicit.

Step 3: Enforce Module Boundaries With Import Linting

The highest ROI fix (50.8% fewer revisitations on multi-module tasks) is clean module contracts. Enforce them formally:

# Python: import-linter
pip install import-linter

# .importlinter config
[importlinter]
root_packages = myapp

[importlinter:contract:layers]
name = Feature layer independence
type = layers
layers =
    myapp.api
    myapp.services
    myapp.repositories
    myapp.models

# Run in CI
lint-imports
Enter fullscreen mode Exit fullscreen mode

Step 4: Kill Dead Code Systematically

Dead code is agent poison — it can't reliably distinguish an unused code path from an intentional fallback. Make dead code impossible to hide:

# Python: Vulture for dead code detection
pip install vulture
vulture src/ --min-confidence 80

# Add to CI as a hard gate
vulture src/ --min-confidence 80 || exit 1
Enter fullscreen mode Exit fullscreen mode

Step 5: Wire It Into Pre-Commit

# .pre-commit-config.yaml — agent-oriented quality gates
repos:
  - repo: https://github.com/astral-sh/ruff-pre-commit
    rev: v0.4.5
    hooks:
      - id: ruff
        args: [--fix]
      - id: ruff-format

  - repo: https://github.com/PyCQA/pylint
    rev: v3.2.0
    hooks:
      - id: pylint
        args: ["--fail-under=8.0"]

  - repo: local
    hooks:
      - id: vulture
        name: Dead code check
        entry: vulture src/ --min-confidence 80
        language: system
        types: [python]
      - id: import-linter
        name: Module boundary enforcement
        entry: lint-imports
        language: system
        pass_filenames: false
Enter fullscreen mode Exit fullscreen mode

9. The "Vibeclean" Experiment: Can Agents Clean Themselves?

One of the more fascinating aspects of this research is that it uses agents to construct the minimal pairs it then evaluates agents on. The Vibeclean pipeline is a working demonstration that AI can be used to improve a codebase for AI.

The pipeline is practical and directly replicable. Here's a minimal wrapper you can use today with the Anthropic API:

import anthropic


def vibeclean_module(module_path: str, sonar_issues: list[dict]) -> str:
    """
    Run an agentic cleanup pass on a module given a SonarQube issue list.

    Args:
        module_path: Path to the module to clean (e.g., "src/orders/processor.py")
        sonar_issues: List of SonarQube issues, each a dict with
                      'rule', 'message', and 'line' keys.

    Returns:
        A summary string of changes made by the cleanup agent.
    """
    client = anthropic.Anthropic()

    issue_list = "\n".join(
        f"  - Line {i['line']}: [{i['rule']}] {i['message']}"
        for i in sonar_issues
    )

    prompt = f"""You are a precision code cleanup agent. Your goal is to resolve
the following SonarQube violations in `{module_path}` WITHOUT changing any
externally observable behavior.

Violations to fix:
{issue_list}

Constraints:
1. Fix each listed issue; do not modify anything else.
2. Run the test suite after each module-level edit to verify behavioral parity.
3. Do NOT redesign the architecture or change public interfaces.
4. If an issue cannot be fixed safely, mark it 'wontfix' and move on.
5. Return a brief summary of each change made.

Start by reading `{module_path}`, then address each violation in order."""

    response = client.messages.create(
        model="claude-sonnet-4-5",
        max_tokens=8192,
        messages=[{"role": "user", "content": prompt}],
    )

    return response.content[0].text


# Example usage — target your most agent-touched module
if __name__ == "__main__":
    issues = [
        {
            "rule": "python:S3776",
            "message": "Cognitive Complexity too high (15, max allowed: 10)",
            "line": 42,
        },
        {
            "rule": "python:S1192",
            "message": "String literal 'PENDING' is duplicated 4 times",
            "line": 87,
        },
        {
            "rule": "python:S1481",
            "message": "Remove unused local variable 'tmp_result'",
            "line": 103,
        },
    ]
    summary = vibeclean_module("src/orders/processor.py", issues)
    print(summary)
Enter fullscreen mode Exit fullscreen mode

The practical workflow: identify the files your agents touch most frequently (look at your agent trajectory logs), export their SonarQube issue lists, run Vibeclean on them, verify tests pass, and measure your agent token costs before and after. The research predicts the effect will be strongest on files that sit at architectural seams — your service boundary adapters, your cross-module orchestrators, your repository layer interfaces.


10. Limitations and Open Questions

Good science acknowledges its edges, and this paper is admirably candid.

Single agent configuration. All 660 trials used Claude Code with Claude Sonnet 4.6. Claude Haiku 4.5 was swept but excluded due to low baseline pass rate. Whether GPT-5, Gemini 2.5 Ultra, or local models (Llama 4, Mistral Large) show the same pattern is unknown. The effect size may vary significantly across model families.

Static analysis as a proxy for cleanliness. SonarQube can detect rule violations, cognitive complexity, and dead code. It cannot detect bad domain modeling, inappropriate abstraction levels, or misleading API design. The study's "clean" code is clean in a specific, measurable sense — not necessarily in the holistic sense a principal engineer would mean.

Enormous trial-to-trial variance. The same task on the same codebase can cost 2.5× more tokens on one run vs. another. On one CKAN task, 10 cleaner-side trials spanned 1.4M to 10.6M input tokens. The 7.1% aggregate holds because it pools hundreds of trials — but at the individual task level, cleanliness is hard to distinguish from noise for small-volume workloads.

The hotspot tension. Refactoring god methods distributes complexity across more files without eliminating it, showing neutral token footprint. The optimal refactoring strategy for agent efficiency may differ from the optimal strategy for human readability — a tension not yet resolved.

Open questions surfaced by the paper:

  • Does the effect generalize across non-Claude agents and local models?
  • Is there a quality threshold below which gains are dramatic, and above which they plateau?
  • Does agentic scaffolding type (single-pass, multi-agent, tree-of-thought) modulate the effect?
  • How does the cleanliness effect differ for greenfield vs. brownfield agentic tasks?

The benchmark (6 minimal pairs, 33 tasks, open Harbor-based infrastructure) is explicitly designed for reuse — these are the right questions to test against it next.


11. Conclusion: Your SOLID Principles Are Now Your AI Budget

Here's the punchline: everything your team has argued for in code reviews for the past decade just got a new justification — one denominated in dollars.

Clean code, small functions, clear module boundaries, dead code removal, low cognitive complexity — these are not aesthetic preferences. They are not bureaucratic overhead. They are not CTO theater. And in 2026, they are not just for the humans on your team.

They are the configuration space of your AI agent's operational cost.

The research from SonarSource gives us the first controlled, quantified answer to a question every engineering organization running agentic workflows should be asking: what is the hidden cost of neglecting code quality in the agent era?

The answer: 7–8% more in tokens, 34% more in file revisitations, and up to 50% more revisitations on multi-module tasks — precisely the work where agents are most useful, most expensive, and most likely to spiral when the code underneath them is unclear.

These numbers will only compound in importance. AI coding agents are not going away. The 22–29% of GitHub repos already showing agent activity will become 50%, then 80%. The cost per token will decline — but the volume of agentic tasks will rise to fill every budget available. Code quality will remain a first-order lever on your AI spend.

Write clean code. Enforce your module boundaries. Kill your dead branches. Your static analysis pipeline is not a formality — it is infrastructure.

Your SOLID principles are your AI compute budget. Treat them accordingly.


→ Three things to do this week:

  1. Audit your agent touchpoints. Pull your agent trajectory logs and identify the top 10 files your agents read most frequently. These are your highest-ROI cleanup targets.

  2. Run the scanner. Execute ruff check . --select ALL (Python) or your language's equivalent static analyzer on those files and count the violations. Sort by cognitive complexity density.

  3. Run a Vibeclean sprint. Use the Claude API snippet from Section 9, point it at your top-violation files, and benchmark agent token costs before and after.

→ Read the original paper: arXiv:2605.20049 — "Does Code Cleanliness Affect Coding Agents? A Controlled Minimal-Pair Study"


Published July 6, 2026 | Estimated read time: 15 minutes

Tags: ai-agents code-quality llm claude software-engineering devops clean-code technical-debt

Top comments (0)