# LoopGain

An open-source Python cost controller for AI agent loops that measures real-time convergence and stops or rolls back a loop once it has converged, instead of a fixed iteration cap, with adapters for LangGraph, CrewAI, AutoGen and Claude Agent SDK.

LoopGain is a cost controller for AI agent loops such as verify-revise cycles, refinement passes, tool-use retries, self-correcting RAG, or code generation checked against linter feedback. It measures the loop in real time using a control-theoretic stop-and-rollback policy based on the Barkhausen criterion from feedback-oscillator analysis, and stops the loop once it detects actual convergence, rolling back to the best-so-far result before quality degrades, instead of running to a fixed max_iterations cap.

In a published benchmark of 2,000 paired trials across 10 workload cells, the project reports 92.8 percent less API spend and roughly 15 times faster median wall-clock time per trial than a fixed 20-iteration cap, with judge-scored quality preserved rather than traded away, and states that all six of its pre-registered kill criteria were met. It detects convergence, not correctness: it knows when more iterations will not help, not whether the answer is right, and its accuracy depends on the verifier behind the error signal it watches.

It is pure Python with no runtime dependencies, supports Python 3.10 and newer, installs with pip, and ships adapters for LangGraph, CrewAI, AutoGen, LangChain, OpenAI Agents SDK and Claude Agent SDK, plus a raw API for a custom stack and a separate Claude Code plugin that scans a repository for loops to wrap.

Website: https://loopgain.ai
