CodeHealth is an aggregated metric based on 25+ factors scanned from the source code. The code health factors correlate with increased maintenance costs and an increased risk for defects, and predicts whether AI agents will introduce defects or not. Code above 9.0 is human-readable, code above 9.5 is AI-ready.
Why do coding agents need deterministic guidance over probabilistic AI reviews?
An agent session rarely touches a file once. It plans, writes, then keeps iterating across the same files and commits, and every extra pass is another chance for the code to drift out of shape while still reading as locally sound. A model checking its own output does not fix this: sounding certain and being correct are two different things.
That is why independent checks such as linters, coverage, and security scanners are necessary, but none of them answer the question that predicts whether the next agent's edit will hold: is this code healthy enough for an agent to change it safely?
How do you know if a codebase is AI-ready?
CodeScene measures that with CodeHealth, a 1.0 to 10.0 code health score, validated in peer-reviewed research, Tornhill & Borg's 2022 “Code Red” study. The study found a clear link between code quality and business impact: 15x fewer defects and 124% faster feature delivery in healthy code versus unhealthy ("red") code, across 39 production codebases and 40,000 modules.
For AI specifically, CodeScene's research puts the number higher. The study Code for Machines, Not Just Humans defines “AI-friendliness” as the probability that AI-generated refactorings preserve behavior and improve maintainability. The study used the CodeHealth™ metric as a proxy for code quality.
AI-generated changes fail at least 60% more often in unhealthy code (CodeScene, AI-Ready Code, 2026).
"These results suggest that organizations can use CodeHealth to guide where AI interventions are lower risk and where additional human oversight is warranted. Investing in maintainability not only helps humans; it also prepares for large-scale AI adoption."

The study itself is a large-scale study of 5,000 real programs using six different LLMs to refactor code while keeping all tests passing. The results show:
- LLMs consistently perform better in healthy code.
- Unhealthy code triggered a higher rate of AI-introduced defects across the board.
- There’s a 60% higher defect risk when applying AI to problematic code.
What's even more interesting is that a 60% increase in AI-induced defects is severe. But the study only included code with CodeHealth ≥ 7.0. This means the research never touched the truly unhealthy code found in many legacy codebases: the modules scoring 4, 3, or even 1.
What would the AI error rate be on such code? Based on patterns observed across all CodeHealth research, the relationship is almost certainly non-linear:
- At Code Health 7, AI breaks code frequently
- At Code Health 3, breakage may become the default outcome
How will AI agents perform on enterprise codebases?
What's even more alarming is that, as our benchmarking report shows, in reality the average enterprise codebase is far from AI-ready. In that benchmarking report we show that the average Hotspot CodeHealth is at 5.15 of 10.0 in the Industrial & Technology sector.
Code that is easy for a human to understand starts at 9.0. AI-ready, the line that keeps AI-induced bugs in check, needs to be at least 9.5. Scaling agents across a codebase with 5.15 in code health is not an acceleration strategy, it's a costly investment.
How do I know where we have AI-ready code?
Understand the risk with CodeHealth analysis
Assess the AI-risk across your codebase, to understand where the your "unhealthy hotspots" are located. As evident from the following graph, you want to aim for a Code Health of at least 9.5, ideally a perfect 10.0, and you certainly want to make sure that agents or not declining your current code health.
For healthy code that's AI-friendly, you can safely scale AI agents across those areas and benefit from AI acceleration. Code that is not-yet AI-friendly needs to be refactored and uplifted before attempting to implement features via agents.
What guardrails do I need before letting agents write production code?
Treat quality as an enforcement layer, not a reviewer preference: one deterministic CodeHealth standard, applied automatically at three points - inside the agent session, on the developer's machine, and on the PR. Quality does not rely on the agent. Three failure modes show up as soon as agents run unattended:
- Step-by-step drift. Agents stay faithful to the plan they are executing, not to the structure of the code. Long methods, deep nesting, missing boundaries - the agent invents a plausible next step and keeps going.
code_health_reviewnames those smells before the next edit, another model reviewing the diff often does not. - Probabilistic validation. Asking the same (or a similar) model to “review this” produces a new sample. It can look thorough and still miss the code smells.
Code_health_reviewis not an LLM, it's deterministic. Same file on a second run will get the same score and the same code smell list is produced. That is the difference between a review pass and a gate. - No single enforcement point. CI, linters, and security scanners each see a slice of the problem. None of them share one decision about whether the code is still easy to work with. Code Health is that decision, enforced in three places - not only as MCP tools in the chat:
CodeHealth MCP + the AI agent.
Inside the session: code_health_review as snippets are generated, pre_commit_code_health_safeguard before commit, analyze_change_set before the PR. If Code Health regresses, the agent refactors until it is restored.
Local quality gate (CLI)
The individual developer's backstop before commit, for anything the agent skipped.
Team quality gate (PR/MR)
Automated Code Health review before merge, so the org standard holds even when AGENTS.md was ignored.
Simply put, one deterministic CodeHealth standard enforced automatically at every step of the workflow. Quality does not rely on the agent and the AI agent is held accountable to the same standards as human developers.

Add code coverage as a behavioral gate
Healthy code alone does not guarantee correct behavior. A common shortcut for an agent facing a failing test is to delete it, weakening behavioral safeguards without obvious signals.
Code coverage becomes an effective safeguard when used as a regression signal rather than a vanity metric. By setting the thresholds high, regressions surface early and cannot slip through unnoticed. Coverage gates act as behavioral checks alongside Code Health safeguards, ensuring code remains healthy and does the right thing.
How do we expand and scale AI agents across our codebase?
As we've seen from the research, and what we've so far have written in this blog post, we need to uplift and refactor unhealthy code that is not yet AI-ready. Without objective feedback, agents tend to reshuffle complexity and do minor polish rather than truly move the needle of code quality.
The workflow for refactoring and uplifting unhealthy code can also be agentic. As we've concluded so far, AI performs best on code that is healthy and modular. Refactoring is where objective feedback makes the biggest difference.
Refactoring loop: CodeHealth MCP + AI Agent
The MCP server gives the AI agent concrete guidance to:
- run focused Code Health reviews
- identify the specific design issues to address
- refactor in small, measurable steps, and verify progress with updated Code Health scores.
This workflow works with MCP alone and is often enough to safely improve legacy code.
The code_health_review changes that dynamic:
- The Code Health score gives agents an explicit, measurable goal
- The detailed review offers the direction via concrete maintainability issues, allowing the agent to formulate a structured refactoring plan rather than guessing at improvements

The workflow becomes straightforward:
review → plan → refactor → re-measure
Why can't we just use the AI agent to refactor?
We show "why" through our benchmarking study on refactoring tasks using Claude Code with and without code health-guidance. First we let Claude Code refactor on its own, then the same refactoring tasks were given, but this time Claude Code was guided by the CodeHealth MCP server.
On a public benchmark of 25,000 source files from competitive-programming problems, Claude Code with the CodeHealth MCP produced 2–5x more Code Health improvement than Claude Code off the shelf.
But what's even more interesting is the type of refactorings that Claude was able to do.
Unguided agents are conservative and tend to do shallow improvements that don’t really move the needle on code quality. Let’s illustrate the difference via a more detailed look at the type of refactorings that the agents performed in our benchmark study:
| Refactoring operation | Frequency of refactorings | Frequency of refactorings |
| Claude Code | Claude Code + CodeHealth MCP | |
| Extract Method (structural impact) |
7,550 | 21,702 |
| Rename Variable (shallow refactoring) |
54,094 | 8,640 |
The shift from mostly shallow "Rename Variable" edits to mostly "Structural Extract Method" edits is the point. Without an objective target, agents optimize for “looks cleaner.” With CodeHealth guidance, they restructure.
The score is not a vanity metric - healthier code is faster to change, less defective, and safer for the next agent to touch. That is why the benchmark improvements matter. They are not just numerical gains. They reflect a codebase that becomes progressively easier to change, and therefore progressively more AI-friendly. What stands out in practice is not just the improvement in Code Health, but the stability of the refactoring process.
The agent stops guessing. Instead, it iterates toward a clearly defined target, much like an experienced developer working with objective feedback. One-off uplift isn’t enough, though. The real improvement occurs when those feedback loops are embedded into everyday development. Only then can your recently uplifted code remain healthy, and continue to benefit from agentic speed.
Why does a second AI opinion not count as a gate?
Because the generator and the judge are in the same class of system. That is useful for surfacing issues. It is not an acceptance mechanism.
“AI-assisted review” usually means one of two things: the model that generated the code is asked to critique it, or a second model takes a follow-up pass. Both produce probabilistic output - neither runs against a fixed threshold, and neither gives the same answer for the same diff twice.
First-level review can be probabilistic. Enforcement cannot. Production-bound changes need a gate where the same input, under the same policy, produces the same pass/fail every time.
Code Health is that gate. It is not an LLM - it is an aggregated metric based on 25+ factors scanned from the source code, scored from 10.0 (healthy, easy to change) to 1.0 (overly complex). An agent can be told: raise this file from 6.2 to 9.5, or do not open the PR.
What is Code Health - and why is it a better signal than a pile of checks?
As we've already pointed out, CodeHealth is evidence-based and the effectiveness of the metric was evaluated in the Code Red paper. The code health factors correlate with increased maintenance costs and an increased risk for defects, as we've already pointed out.
Those factors include low cohesion, brain classes, nested complexity, complex conditionals, large methods, and duplication - each correlated with higher maintenance cost and higher defect risk. It is a deterministic metric, regardless of the LLM it checks.
| Threshold | CodeHealth | Meaning |
| Healthy for humans | ≥ 9.0 | Understandable, cheaper to change. |
| AI-ready | ≥ 9.5 | The line that keeps AI-induced bugs in check. |
Lint, tests, and coverage answer questions like does this violate a rule, did the suite pass, and is enough of the new code executed. CodeHealth answers a different question: how likely is the next change - human or agent - to succeed without adding complexity? Agents need both kinds of answers. Only one of them is a maintainability probability.
Summary of the safeguards
Safety here is not only permissions, audit logs, and model choice. Those matter, but none of them measure whether the code an agent just wrote got harder to work with. Safety also does not mean every team must refactor everything to Code Health 9.5 - there are two distinct use cases: safeguard and uplift.
| Use case | What it requires | When to use it |
| CodeHealth MCP + AI Agent Safeguard (mandatory) | New AI code must not add Code Health issues - the file's score must not drop. | Every AI-generated or modified file. This is the default safety bar. |
| CodeHealth MCP + AI Agent Uplift (optional) | Refactor toward Code Health ≥ 9.5, ideally 10.0. | When you want a larger AI-ready surface - not required by default. |
Safeguard runs at the same three checkpoints described above - holding the score is enough, you do not have to raise the file to 9.5. Uplift uses the same review → plan → refactor → re-measure loop until the file reaches the target.
Keep a bound: one or two correction loops, then a human. Keep tests honest so the agent cannot delete coverage to go green. Encode both use cases in AGENTS.md so the agent does not invent the workflow - the gate is the same for Claude Code, Cursor, Copilot, and a human reviewer.
Inner loop: The agent runs the MCP tools in session. AGENTS.md is the contract - if it cooperates, bad structure never leaves the laptop.
Outer loop: If the agent overrides AGENTS.md, misses a tool, or never calls the MCP, the CodeScene CLI still scores the change locally, and PR/MR quality gates catch it at team or path level. Same CodeHealth metric - the team gate does not care whether the agent cooperated. The agent stops guessing and iterates toward the target you actually chose.
Add code coverage as a behavioral gate: An agent facing a failing test can just delete it. Set thresholds high and treat coverage as a regression signal, not a vanity metric, so that shortcut surfaces immediately instead of slipping through.
Is agent observability the same as a quality gate?
No. Observability tells you what the agent did - tool calls, traces, handoffs. A quality gate tells you whether the code it produced is allowed to merge. You can have perfect traces of an agent that just nested a 400-line hotspot. Traces will not stop that merge. CodeHealth will.
Code Health vs. traditional quality gates
| Lint/static analysis/tests | AI-on-AI review |
CodeScene Code Health gate | |
| What it measures | Rule violations, defect/security patterns, test outcomes, coverage and configured quality metrics |
Heuristic issues, inferred intent and model-generated critique |
Structural maintainability + AI-readiness (1–10) |
| Deterministic? |
Generally yes* |
No | Yes |
| Tied to defects and speed? | Depends on the metric/tool |
No |
Yes, peer-reviewed |
| Tied to AI break probability? |
Not directly established |
No validated stable signal |
Yes; CH ≥ 9.5 tracks break rates |
| Tied to token cost? |
Not directly established | No |
Yes - 35–45% more tokens on unhealthy code (see source below) |
| Action on fail |
Rule/test-specific finding; can block a build or PR |
Another model- generated recommendation |
Named structural issues + measurable refactoring target via MCP |
| Runs in the inner loop (before PR)? | Depending on tooling and workflow |
Yes | Yes - code_health_review during the agent session |
Static analysis, security rules, and tests remain the deterministic stack for correctness and known vulnerabilities. CodeHealth is the missing layer for structure. Several disconnected checks still do not add up to one merge decision, but a single CodeHealth threshold does.
On the token-cost row: agents consume 35–45% more tokens on unhealthy code across C++, Java, and Python - and for Java specifically, iterative refactoring uses roughly 120% more output tokens once Code Health drops below 8.
What should engineering leaders ask before they scale agents?
1. Where is the risk?
Pull the risk forward. Before adding more agents, know where they are going to fail, how you are going to stop a quality drop, and which numbers you are going to trust when a diff looks fine. Map your CodeHealth Hotspots and track the effect as you scale agentic code.
2. How do I ensure the agent is doing what it should and not degrading quality?
Enforce quality check that guides the agent, give them context, a quality target and make them accountable.
Individuals. AGENTS.md plus the CodeHealth MCP: code_health_review as code is written, pre_commit_code_health_safeguard before commit, analyze_change_set before the PR. The developer's agent hits the gate in the inner loop, automated.
Team. The CodeScene CLI on the change, and PR quality gates at team or path level. If the agent skipped AGENTS.md or never called the MCP, the team gate still scores the diff. The inner loop is the agent; the outer loop is CLI plus the PR check. The merge does not care whether the agent cooperated.
Behavioral gate
As we've written above, coverage gates act as behavioral checks alongside Code Health safeguards, ensuring code remains healthy and does the right thing.
3. What is the measure of quality?
As we've already mentioned, CodeHealth predicts the AI performance, and that verdict is the same across the whole workflow, the standard is the same and agents need to follow the quality standard that the organization has set up via the CodeHealth KPI.
Human-readable code starts at CodeHealth 9.0.
AI-ready code is at least 9.5. That's the target that the existing code need to have if the AI agents or to perform feature work on it reliably.
Safeguard does not require 9.5; it requires no new issues. If the agent degrades the current code health of the file, it is mandatory to restore.
Without clear answers to those three, quality still depends on whoever happens to be reviewing, not on a system. Agents already solved the supply problem: code is now cheap to produce. They did not solve the harder one - knowing which of that code is safe to build on. That is the actual competition. Not who ships the most diffs, but who can put a number on which ones are safe.
FAQ
What is an independent quality gate for coding agents?
A check that is not the same system that generated the code, that returns a stable pass/fail, and that every agent and human hits before merge. CodeHealth is that deterministic check for maintainability, structural quality and AI-readiness.
Is Code Health deterministic?
Yes. The same code produces the same score and the same smell list every time. That is the difference between a gate and a review pass.
Can linters replace CodeHealth for AI-generated code?
No. Linters enforce local rules. They do not predict defect break rates or identify complexity, size, coupling, or other code health issues. The Code Health tools solve this by giving AI agents precise insight into design problems, as well as an objective way to assess the outcome: did the Code Health improve?
What Code Health score is AI-ready?
Aim for at least 9.5. Human-readable code starts at 9.0.
What is the evidence behind Code Health's defect-risk numbers?
General Code Health validity comes from Tornhill & Borg's 2022 peer-reviewed “Code Red” study (up to 15x fewer defects, 124% faster delivery in healthy vs. unhealthy code).
The AI-specific figure - unhealthy code carrying at least 60% higher AI-induced defect risk comes from CodeScene's AI-Ready Code research.
