3 min read

LoopGain cuts agent loop costs by 92.8%

An open-source Python library aims to stop iterative agent loops when they actually converge, not at a fixed iteration cap.

Image: Hacker News

LoopGain is a new open-source library built to solve a familiar agent problem: loops that keep burning tokens after they’ve stopped getting better. Instead of relying on a fixed max_iterations setting, the project measures an iterative workflow in real time and stops when it detects convergence, or rolls back to the best prior output if quality starts to degrade.

According to the project’s public benchmark, based on 2,000 paired trials across 10 workload cells, that approach cut total API spend by 92.8% versus max_iter=20, dropping benchmark cost from $27.05 to $1.94. Median wall-clock time fell from 30.9s to 2.1s, or about 15× faster. The project says quality was preserved rather than traded away, citing a 0.50–0.63 judge win rate on natural-distribution workloads (W1–W4), 0.92–0.95 on engineered-failure workloads (W5), and a 0.678 weighted preference across 1,800 judge comparisons. It also says zero of six pre-registered kill criteria fired.

The package is pure Python, has no runtime dependencies, supports Python 3.10+, and ships adapters for LangGraph, CrewAI, AutoGen, LangChain, OpenAI Agents SDK, and Claude Agent SDK. It can also be used through a raw API in custom stacks. The core implementation is small: developers provide a non-negative error signal for each iteration, and LoopGain watches the trend rather than the output semantics.

That makes it suitable for workflows such as:

  • verify-revise loops
  • refinement passes
  • tool-use retry chains
  • RAG with self-correction
  • code generation with linter or test feedback
  • multi-step reasoning loops

Under the hood, the library measures empirical loop gain as Aβ = E(n) / E(n-1) and classifies the trajectory using four features: cumulative error reduction, the OLS slope of log10(E), the slope’s t-test p-value, and residual oscillation. It then routes the loop into one of five states: FAST_CONVERGE, CONVERGING, STALLING, OSCILLATING, or DIVERGING. A short-circuit TARGET_MET state stops the loop as soon as observed error reaches target_error.

Recommended reading

Chrome’s Gemini tools reach UK desktop users

The project says the default multi-feature classifier reached 98.8% macro-averaged accuracy across 5 regimes on N=1000 deterministic-mock trajectories. It also keeps a best-so-far buffer, so if a loop oscillates or diverges, the returned result is the lowest-error iteration, not simply the last one.

Limits and verifier quality

The project is unusually explicit about what the library cannot do. LoopGain detects convergence, not correctness: it can tell when more iterations are unlikely to help, but not whether the answer is actually right. That depends entirely on the verifier behind the error signal.

The benchmark highlights two separate limits. First, savings vary by workload: fast-success loops saved about 96%, while adversarial or failure-prone loops saved around 78–84%. Second, verifier quality sets the ceiling. In the benchmark’s code-generation workload, 4.5% of converged runs (16/355) passed every in-loop check but still failed the full held-out test suite.

For teams that want a broader operational view, the library also offers opt-in telemetry plus an open-source, self-hostable dashboard and receiver.

LoopGain dashboard — loop health, convergence, waste, and rollbacks across a fleet
LoopGain dashboard — loop health, convergence, waste, and rollbacks across a fleet

There’s also a Claude Code plugin that scans repositories for wrappable loops — including literal, recursive, graph-cycle, and semantic loops — and proposes reviewed diffs one file at a time, never auto-applying changes.

Tomas Berg

Computing Editor

Tomas lives in the terminal. He covers chips, laptops, and operating systems with a focus on performance and efficiency. He reads kernel changelogs the way other people read fiction, and he's always on the hunt for the perfect mechanical keyboard switch. If it processes data, Tomas has an opinion on it.

via Hacker News

// Keep reading