Grafy News
Try Grafy free

Metrics-in-the-loop reduces complexity in agent-written code

Researchers introduce a method to inject real-time code metrics into AI agents, preventing the accumulation of complex, unmaintainable codebases.

Illustration for the news story: Metrics-in-the-loop reduces complexity in agent-written codeSource: Grafy Research Lab

Autonomous coding agents are increasingly capable of writing entire codebases, but they often prioritize passing tests over long-term maintainability. By injecting real-time structural metrics directly into the agent's context, researchers have found a way to steer these models toward writing simpler, more organized code without sacrificing functional correctness.

What is the metrics-in-the-loop approach?

According to a study by Nicolas Chapados, Jean-François Gagné, and Nouha Dziri, the "metrics-in-the-loop" (MITL) method is an inference-time intervention designed to provide AI agents with immediate, actionable feedback during a coding task. Currently, most coding agents operate based on whether their code passes a suite of tests. However, passing tests do not necessarily indicate that the code is well-structured or easy to maintain.

The MITL approach uses a lightweight plugin to measure the specific code an agent has just modified. It then injects a short report into the agent's conversation history before the next turn. This report focuses on two key areas: cyclomatic complexity and test coverage. Cyclomatic complexity measures the number of independent paths through a function, effectively flagging code that is becoming too convoluted. Coverage feedback identifies lines of newly modified code that are not being executed by the agent's own tests.

How does complexity feedback affect AI agents?

In their experiments, the researchers tested several different types of feedback across four models, including DeepSeek V4.1 Flash, GLM-5.3 Flash, Claude Haiku 5.5, and GPT-6 Luna. They compared a baseline of no feedback against a "reminder" baseline (instructions without measurements), and then against specific metrics like complexity feedback (C), coverage feedback (T), or both (CT).

The results indicated that complexity feedback—either alone or combined with coverage feedback—significantly reduces "erosion." The researchers define erosion as the share of complexity mass concentrated in functions that exceed a specific complexity threshold. Specifically, they used an erosion index EτE_{\tau} defined as:

Eτ=∑fcfnf 1{cf>τ}∑fcfnf\displaystyle E_{\tau}=\frac{\sum_{f}c_{f}\sqrt{n_{f}}\,\mathbf{1}\{c_{f}>\tau\}}{\sum_{f}c_{f}\sqrt{n_{f}}}

where cfc_{f} is the cyclomatic complexity of a function ff, nfn_{f} is its count of non-blank, non-comment lines, and τ\tau is the threshold. By providing this metric, the agents were able to recognize when they had written a function that was too complex and were prompted to refactor it to bring the complexity below the limit.

Does this method impact code correctness?

One of the primary concerns with intervening in an agent's reasoning process is whether such constraints might prevent the agent from solving the actual task. However, the study found that providing these structural metrics had no detectable negative effect on the held-out correctness of the code. The agents were able to maintain their ability to pass tests while simultaneously producing code that was structurally simpler and more maintainable.

This suggests that the feedback acts as a "re-railing" mechanism. Instead of the agent blindly building on top of messy or overly complex foundations, the timely measurements redirect the agent toward better engineering practices before those weaknesses become deeply embedded in the codebase.

Why is this important for large-scale software?

As AI agents scale to handle codebases containing millions of lines of code, the risks associated with accumulating complexity grow exponentially. Complex code is harder to debug, more prone to vulnerabilities, and significantly more difficult for human developers to maintain. The researchers note that while current benchmarks may not yet fully capture the long-term benefits of improved maintainability on correctness, these interventions are critical for the future of autonomous software development.

By treating code quality as a real-time metric rather than a post-hoc review process, developers can create agents that produce professional-grade software that is sustainable over long development cycles.

How to apply these concepts in Grafy

While the specific MITL plugin is a research intervention, the principle of using AI to manage and improve code is central to modern development workflows. Developers looking to build or refine complex logic can utilize Grafy Code to interact with various models. For those researching the mathematical foundations of these improvements, Grafy Reader provides an ideal environment to analyze technical papers and extract key insights using AI-driven research tools.

Frequently asked questions

Does complexity feedback make the code less accurate?

No, the research indicates that complexity feedback does not negatively impact the correctness of the code. The models were able to maintain their ability to pass tests and fulfill the original programming requirements while simultaneously producing code that was structurally simpler and more organized.

What is cyclomatic complexity in this context?

Cyclomatic complexity is a metric used to measure the number of independent paths through a function. In the MITL study, it is used to flag functions that have become too difficult to follow or test. The feedback encourages the agent to refactor these functions to keep them below a set threshold.

How does coverage feedback work?

Coverage feedback identifies specific lines of code that the agent has modified but which are not being executed by the tests the agent wrote. This information is presented to the agent as a report, allowing it to either add new tests or modify its existing code to ensure all changes are properly verified.


Source: Grafy Research Lab — Metrics-in-the-Loop: Complexity Feedback Reshapes Agent-Written Code