Skip to main content
You need Python 3.11+ and pip in your existing Python project.

Install the package

The PyPI package is loopguard-runtime. The Python import is loopguard. The core package has no required third-party dependencies or API key requirement. For the working tool example below, install the framework extra if your project doesn’t already include LangChain/LangGraph:

Catch a stuck tool loop

Save this as first_run.py. It uses a real LangChain tool with scripted output, so you can exercise the callback path without calling a model or external service.
Run it:
The final summary shows:
Three identical calls returned no useful new information, so LoopGuard requests a stop. The callback raises LoopGuardInterrupt, which the example catches. LangChain may also log the callback exception; that is expected for this run.
handler.raise_error = True lets LangChain propagate the interrupt. Your agent’s tool-error handlers must also let it escape. Follow the integration guide when attaching LoopGuard to your own agent.
To record decisions without stopping execution, replace the handler construction:
All three calls finish, while the report still records a stop recommendation. Its interrupted flag records that recommendation, not proof that execution stopped. See observe-only mode and the report reference for details.

Next steps