What it does
agent-watchdog detects loops in multi-agent AI systems built with LangGraph. It runs inside LangGraph conditional edges. It collects the last N×2 messages from the graph state on every cycle. The window size is configurable. It computes the average pairwise word-set overlap between the recent window and the previous window using Jaccard similarity. If the similarity score exceeds the configured threshold, which defaults to 55%, it signals loop_detected. If the cycle count exceeds the limit, it signals limit_reached. The router then returns END before the next agent fires. It generates an intervention report for the logging pipeline. Setup has two steps. First, create an AgentWatchdog instance with your thresholds. Second, replace a conditional edge with the watchdog router. The package requires @langchain/langgraph as a peer dependency.
Capabilities
- Detects a tool being called repeatedly with near-identical arguments, even while the surrounding message text keeps changing, and signals tool_loop_detected.
- Similarity threshold, window size and iteration cap can be tuned independently per graph or per edge.
- The optional onIntervene callback connects to your own logger, alerting system or monitoring pipeline.
- Each intervention report lists the trigger, iteration count, similarity score, last exchanges and recommended action in a structured, machine-readable form.
- All detection runs in-process with no external calls, and the watchdog itself needs no API keys.
When to use it
- Two agents debate the same point in different words indefinitely with no exit condition.
- A tool is called over and over with near-identical arguments.
- An uncapped loop burns through your API budget before anyone notices the graph is stuck.