Metrics-in-the-Loop: Complexity Feedback Reshapes Agent-Written Code

Nicolas Chapados, Jean-François Gagné, Nouha DziriarXiv 2026 · arXiv

TL;DR

Coding agents often produce "spaghetti code" that passes tests but is a nightmare to maintain. This paper introduces Metrics-in-the-Loop (MITL), a lightweight inference-time intervention that provides agents with real-time feedback on code complexity and test coverage, successfully steering them toward cleaner, more modular code without sacrificing functional correctness.

What Problem Does This Paper Address?

As AI coding agents move from writing snippets to building entire codebases, they face a "software aging" problem. Current agents are primarily guided by whether their code passes a test suite. While functional, this narrow focus often results in monolithic, poorly structured implementations that accumulate "erosion"—a concentration of high cyclomatic complexity and gaps in test coverage.

When an agent builds upon its own messy code in long autonomous runs, these weaknesses compound, making the codebase increasingly fragile and difficult to extend. Human developers can't keep pace with the volume of code generated, creating a critical need for automated "re-railing" mechanisms that ensure agent-written code remains maintainable over the long term.

Methods

The authors propose Metrics-in-the-Loop (MITL), a plugin for agent harnesses that requires no additional training or reference solutions. MITL operates during the agent's "trajectory" (its sequence of actions):

  1. Measurement: After any turn where the agent modifies code, MITL automatically calculates two key metrics:
  2. Cyclomatic Complexity (CC): A measure of the number of independent execution paths through a function. The authors set a target limit of 6.
  3. Test Coverage: Identifying specific lines of changed code that the agent's own tests failed to execute.
  4. Feedback Injection: If thresholds are exceeded, a short, actionable report is injected directly into the agent's context window before its next turn.
  5. Refactoring: The agent is given the opportunity to refactor the flagged functions or strengthen its tests before proceeding with the main task.

The researchers tested four state-of-the-art models (DeepSeek V4.1 Flash, GLM-5.3 Flash, Claude Haiku 5.5, and GPT-6 Luna) across various experimental "arms," ranging from no feedback to combined complexity and coverage reports.

The MITL feedback loop (a) and the resulting impact on erosion vs. correctness (b).

Datasets

The approach was evaluated on three long-horizon coding benchmarks designed to simulate real-world development:

  • Commit0: Agents must reconstruct entire Python libraries from scratch based on specifications.
  • SlopCodeBench: Evaluates iterative program development over successive checkpoints and evolving requirements.
  • SWE-EVO: A benchmark based on real-world software evolution, requiring agents to update published projects to their next release.

Key Insights and Ideas

  • Agents are Responsive to Mechanical Feedback: One of the most significant findings is that agents actually listen. When flagged, agents re-edited 54–88% of complex functions on Commit0 and nearly all (87–94%) on SlopCodeBench.
  • "Re-railing" Works: The intervention successfully prevents agents from building on a shaky foundation. By providing timely measurements, the system redirects the agent before complexity becomes entrenched.
  • The Correctness-Maintainability Decoupling: Interestingly, writing "cleaner" code did not immediately lead to higher pass rates on held-out tests. The authors suggest this is because current benchmarks are either too small or too saturated to show the long-term benefits of maintainability, which typically pays off as codebases scale to millions of lines.

Key Results

  • Significant Erosion Reduction: Complexity feedback (the 'C' and 'CT' arms) drastically reduced the erosion index ($E_{10}$) across all models and benchmarks. On SlopCodeBench, erosion was kept at or below 0.022 at every checkpoint.
  • Stable Correctness: The intervention achieved these structural improvements with no detectable negative effect on functional correctness. As shown in Figure 1b, the "arrows" move vertically (improving structure) while remaining stable horizontally (maintaining pass rates).
  • Cumulative Benefits: As seen in Figure 5, the gap in erosion between agents with feedback and those without grows over the course of a run, proving that MITL is particularly effective for long-horizon tasks.
Paired change in erosion over time for Commit0 (a) and SlopCodeBench (b), showing how feedback keeps complexity in check as the run progresses.

Limitations and Future Work

  • Language Scope: The current study is limited to Python. Future work is needed to see if these patterns hold for languages with different structural paradigms.
  • Benchmark Saturation: Current benchmarks may not be "long" enough. The authors call for new benchmarks with even longer sequences of change requests to truly measure the relationship between code cleanliness and long-term bug rates.
  • Semantic Complexity: Cyclomatic complexity is a "mechanical" metric. Future iterations of MITL could incorporate LLM-based semantic feedback to address logic-level maintainability issues that simple path-counting might miss.

Summary updated

Abstract

Coding agents now write whole codebases in long autonomous runs, guided mainly by whether their tests pass. Yet passing tests alone does not reveal accumulating complexity or gaps in test coverage, which then persist as agents extend their own code. We introduce metrics-in-the-loop, an inference-time intervention that gives agents in-flight feedback: cheap, automatically computed code metrics injected into the agent's context during the trajectory. After each code-changing agent turn, we insert short feedback messages naming changed functions whose cyclomatic complexity exceeds a limit (complexity feedback) or changed lines that are skipped by tests (coverage feedback), giving the agent the opportunity to refactor code or strengthen tests before continuing. We run extensive experiments and ablations comparing several interventions across three long-horizon coding benchmarks and four strong models. We find that complexity feedback significantly reduces erosion, a measure of complexity concentration of code, without affecting held-out correctness of the code. Broadly, our experiments investigate a practical form of agent "re-railing", where timely, actionable measurements redirect agents before weaknesses become the foundation they keep building on. As agents scale to codebases with millions of lines, such interventions become critical to sustaining effective development and long-term code maintainability.
BibTeX
@misc{chapados2026metrics,
  author = {Nicolas Chapados and Jean-François Gagné and Nouha Dziri},
  title = {{Metrics-in-the-Loop: Complexity Feedback Reshapes Agent-Written Code}},
  year = {2026},
  eprint = {2611.23817},
  archivePrefix = {arXiv},
  url = {https://arxiv.org/abs/2611.23817},
}
Figure