Skip to content
mahimathackerPublic

About

Runtime guardrails for LangGraph agents that detect loops, repeated tool calls, and no-progress runs.

Topics

Resources

Contributing

Stars

0 stars

Watchers

0 watching

Forks

Repository files navigation

LoopGuard

PyPI version Python versions PyPI monthly downloads

Docs PyPI Demo GitHub

Runtime guardrails for AI agents that detect loops, repeated failures, and runs that stop making progress.

same tool + useful new result   -> continue
same tool + same empty result   -> stop
503 -> 503 -> success           -> continue
401 -> 401 -> 401               -> stop

Why LoopGuard

Agents can keep spending time and tokens without getting closer to an answer. LoopGuard watches LangGraph and LangChain runs and checks repeated actions against results, progress, and failures. Useful polling can continue; repeated empty results can provide evidence to stop.

It complements your agent and tracing tools by turning recorded behavior into runtime decisions. The examples above illustrate patterns; thresholds and evidence determine the actual decision.

See it in action

LoopGuard catching a real search agent in a semantic loop and interrupting the run

Screenshot from a LoopGuard demo run showing repeated intent and an interrupted run. The four visual examples pair detection explanations with Python SDK code so you can inspect the same behavior in your own program.

Watch the demo · Run it locally

Quick example

Prerequisites: Python 3.11+ and pip. This integration assumes an existing LangGraph/LangChain agent and its framework dependencies.

pip install loopguard-runtime

The package is loopguard-runtime; the import is loopguard. With your existing agent and my_input:

from loopguard import LoopGuardCallbackHandler, LoopGuardInterrupt

handler = LoopGuardCallbackHandler()
handler.raise_error = True

try:
    result = agent.invoke(my_input, config={"callbacks": [handler]})
except LoopGuardInterrupt as exc:
    print("LoopGuard stopped the run:", exc)

print(handler.report())

LangChain must propagate callback errors, and the agent's tool-error handling must let LoopGuardInterrupt escape. Use interrupt_on_fatal=False to collect reports without raising. REPLAN and PAUSE are recorded recommendations today.

Run a complete example without a model or API key →

What it detects

Pattern Evidence
Exact and semantic loops Repeated calls or repeated intent in different words.
Stalls and no progress Unchanged, empty, or error-like tool results.
Repeated failures Retryable errors versus likely permanent failures.
Action and handoff cycles Repeating sequences or closed multi-agent handoffs.
Budget overruns Configurable limits on recorded steps and tool calls.

Detection · Configuration

How it works

Agent event -> Tracer -> Detectors -> Signals -> Policy -> Decision

Repetition alone can warn; repetition plus no progress can stop. The policy combines signals into CONTINUE, WARN, REPLAN, PAUSE, or STOP.

How LoopGuard works →

Live runs and saved traces

Use callbacks during a live run, or inspect an exported trace without rerunning the agent. From a repository checkout:

python -m loopguard.ingest examples/sample_trace.json
python -m loopguard.check examples/sample_trace.json

ingest reports findings; check gives pass/fail behavior for scripts or CI. The sample includes a loop, so its check exits with code 1.

Integration guide · Saved traces

Demo scenarios

Compare offline exact loops, paraphrased searches, healthy polling, temporary 503 recovery, permanent 401 failures, empty results, and real calculation/search agents. Only exact runs without provider credentials; other scenarios need a model, embeddings, or both.

Scenario walkthroughs →

Evaluation

The core suite uses fake agents and embeddings and runs offline:

python -m unittest discover -v

The repo also includes labeled progress-aware traces and a small detector evaluation harness. Evaluation and its limits →

Documentation

Hosted documentation: Read the LoopGuard docs.

The Mintlify source covers the full developer journey. The concept guides explain agent loops, tests and traces, and evaluating the path as well as the answer. See preview and deployment instructions, troubleshooting, or the Python API. For changes to the project, read Contributing.

Package and project status

loopguard-runtime v0.2.1 is an alpha release. Python 3.11+ is required; package classifiers list 3.11 and 3.12. Gemini support and controlled demo scenarios currently require the source checkout. JavaScript support and GitHub check annotations remain roadmap ideas.

Status and roadmap · Report an issue

About

Runtime guardrails for LangGraph agents that detect loops, repeated tool calls, and no-progress runs.

Topics

Resources

Contributing

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages