npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

@p-vbordei/loop-guardrail

v1.0.0

Published

Sliding-window tool-call loop guardrails for LLM agents

Downloads

123

Readme

loop-guardrail

A pure, side-effect-free sliding-window loop detection and recovery guardrail system for LLM agent tool calls.

License

Apache License 2.0 (100% independent and open-source).

Features

  • Exact Failure Tracking: Detects when an LLM retries the exact same tool call with identical arguments and it continues to fail.
  • Same-Tool Failure Tracking: Detects when a specific tool fails repeatedly with different arguments (indicates a general tool path failure or incorrect environment assumptions).
  • Idempotent No-Progress Tracking: Detects when read-only (idempotent) tool calls (like search, read_file) return the exact same content repeatedly, preventing useless duplicate reads.
  • Circuit Breaker halts: Support for warning thresholds (returns action guidance) or hard block/halt stops.
  • Synthetics and Guidance Helpers: Standard formatting to inject guardrail feedback directly back to the LLM agent context to break the loop programmatically.

Installation

npm install loop-guardrail

Usage

1. Initialize and Manage Turn State

Instantiate a stateful ToolCallGuardrailController for your agent execution session, and reset it at the start of every message turn.

import { 
  ToolCallGuardrailController, 
  ToolCallGuardrailConfig,
  toolguardSyntheticResult,
  appendToolguardGuidance
} from 'loop-guardrail';

const config = new ToolCallGuardrailConfig({
  hardStopEnabled: true, // Circuit breaker mode
});
const guardrail = new ToolCallGuardrailController(config);

// Inside your agent message loop:
async function runTurn() {
  guardrail.resetForTurn();

  const toolCalls = getToolCallsFromLlm();
  for (const call of toolCalls) {
    // 1. Run pre-execution checks
    const decision = guardrail.beforeCall(call.name, call.arguments);
    
    if (decision.shouldHalt) {
      // Return synthetic block result to the LLM
      const synthResult = toolguardSyntheticResult(decision);
      sendToLlm(synthResult);
      continue;
    }

    // 2. Execute tool
    const result = await executeTool(call.name, call.arguments);

    // 3. Register outcome
    const afterDecision = guardrail.afterCall(call.name, call.arguments, result);
    
    if (afterDecision.action === "warn") {
      // Append warnings/hints directly to the tool results
      const guidedResult = appendToolguardGuidance(result, afterDecision);
      sendToLlm(guidedResult);
    } else {
      sendToLlm(result);
    }
  }
}

2. Failure Classification

By default, the controller auto-classifies failures from output text (checking for standard JSON errors, non-zero shell exit codes, and "error" / "failed" substrings).

You can also pass an explicit failed flag if your orchestrator handles errors natively:

guardrail.afterCall("read_file", { path: "missing.txt" }, null, { failed: true });