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

rag-guard

v1.0.0

Published

Lightweight, deterministic backend guard for RAG pipelines. Detects irrelevant context, ungrounded answers, and generic responses without LLM calls.

Readme

rag-guard

A lightweight, deterministic backend guard for Retrieval-Augmented Generation (RAG) pipelines.

rag-guard acts as a runtime safety and quality gate for production AI systems. It evaluates the relationship between your user's query, the retrieved context, and the LLM's answer to ensure reliability.

It is a pure npm package with no LLM calls, no network requests, and no UI.

What problems does it solve?

RAG pipelines often fail silently.

  • Irrelevant Context: The retriever finds documents, but they don't answer the specific question.
  • Hallucinations: The LLM ignores the context and makes up an answer.
  • Generic Answers: The model politely refuses to answer ("I don't know") but the system doesn't detect this as a retrieval failure.
  • Empty Retrievals: Pipelines sometimes pass empty or malformed strings to the model.

Key characteristics

  • Deterministic: Same input always yields same output. No "AI judging AI".
  • Fast: Runs in milliseconds using lexical overlap and set theory (Jaccard similarity, asymmetric coverage).
  • Privacy-First: Data never leaves your server.
  • Inspectable: Returns clear, human-readable reasons for failure.

Badges

npm version license TypeScript

Author: Divesh Sarkar

Installation

npm install rag-guard
# or
yarn add rag-guard
# or
pnpm add rag-guard

Quick Start

import { guardRAG, RagGuardInput } from 'rag-guard';

const input: RagGuardInput = {
  query: "What is the battery life of the X200?",
  context: "The X200 features a 12-hour battery life under normal usage conditions.",
  answer: "The X200 lasts for about 12 hours."
};

const result = guardRAG(input);

if (result.isSafe) {
  console.log("Safe to return to user:", result.confidence);
} else {
  console.error("Unsafe response:", result.reasons);
}

Core API

Input

interface RagGuardInput {
  query: string;
  context: string | string[];
  answer: string;
}

Output

interface RagGuardEvaluation {
  isSafe: boolean;
  confidence: number;
  reasons: string[];
  metrics: {
    contextRelevance: number;
    answerGrounding: number;
    contextCoverage: number;
    infoDensity: number;
    contextRedundancy?: number;
  };
}

How evaluation works (high-level)

  • Text is cleaned and vectorized using bag-of-words.
  • Context Relevance: Cosine similarity between query and context.
  • Answer Grounding: Cosine similarity between answer and context (highest chunk).
  • Context Coverage: Overlap between answer terms and full context.
  • Additional heuristics: Length checks, info density, redundancy, generic phrases.
  • Zero dependencies · Pure TypeScript · < 5ms runtime

Configuration Options

You can customize strictness by passing an options object:

import { guardRAG } from 'rag-guard';

const result = guardRAG(input, {
  relevanceThreshold: 0.25,     // Min context relevance
  groundingThreshold: 0.4,      // Min answer grounding
  coverageThreshold: 0.2,       // Min query coverage in context
  minContextLength: 50,         // Min chars for context
  maxContextLength: 10000,      // Max chars for context
  genericAnswerPhrases: [       // Custom refusal phrases
    "i don't know",
    "not sufficient information",
    "cannot provide"
  ]
});

Production Use Cases

Safety gate example

Block answers that aren't supported by your internal data to prevent hallucinations in customer support chat bots.

Monitoring & observability example

Log the metrics object for every RAG interaction. Over time, visualize contextRelevance to measure the quality of your vector retriever, and answerGrounding to measure how well your LLM adheres to instructions.

Performance

rag-guard is designed to add negligible latency (< 5ms) to your pipeline. It uses efficient set operations and string manipulation, avoiding heavy embedding models or external API calls.

Where rag-guard fits

User Query 
    │
    ▼
[Retriever] ──► (Documents)
    │
    ▼
[LLM] ──► (Answer)
    │
    ▼
[rag-guard] ◄── (Query, Docs, Answer)
    │
    ├──► ✅ Safe: Return to User
    └──► ❌ Unsafe: Return fallback / Retry

Why use rag-guard?

Using an LLM to evaluate another LLM (LLM-as-a-judge) is slow, expensive, and non-deterministic. rag-guard provides a baseline logic layer that catches 80% of common RAG failures instantly and cheaply.

Contributing

Contributions are welcome! Please ensure all logic remains deterministic and dependency-free.

Author

Divesh Sarkar

License

MIT © 2026 Divesh Sarkar