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

@semanticguard/ai-sdk

v0.4.0

Published

SemanticGuard AI SDK. Two things in one package: (1) a fetch wrapper for semantic caching, cost tracking, and analytics on any AI provider; (2) Claude Code guardrails CLI (sg-guardrails-install + sg-guardrails-hook) that blocks destructive tool calls.

Downloads

228

Readme

@semanticguard/ai-sdk

Guard your agents. Cut your LLM bill in half. 1 line of code.

Two features in one package:

  1. Semantic caching + cost tracking for any AI provider via a custom fetch wrapper (withSemanticGuard).
  2. Claude Code guardrails CLI (sg-guardrails-install + sg-guardrails-hook) that blocks destructive tool calls with a PreToolUse hook.

Install

npm install @semanticguard/ai-sdk

Claude Code guardrails (one-liner)

Blocks destructive tool calls (rm -rf, drop table, git push --force, PRs to main, terraform apply) using your tenant's policy engine.

npx --package=@semanticguard/ai-sdk sg-guardrails-install
export SG_API_KEY=sg-your-key-here      # get one at https://semanticguard.dev/dashboard/keys
export SG_WORKSPACE_TAG=prod            # optional attribution
export SG_GUARDRAILS_MODE=enforce       # or "warn" | "observe" for rollout

That's it. Every tool call now shows up in your /dashboard/agents audit feed with a policy verdict. Fail-open on any error so a guardrails outage never blocks a developer. Uninstall with the same command plus uninstall.

Semantic caching (SDK wrapper)

Add fetch: withSemanticGuard(...) to any AI SDK provider:

Google Vertex AI

import { createVertex } from "@ai-sdk/google-vertex";
import { withSemanticGuard } from "@semanticguard/ai-sdk";

const vertex = createVertex({
  project: "your-gcp-project",
  location: "us-central1",
  fetch: withSemanticGuard({
    gatewayUrl: "https://your-sg-instance.vercel.app",
  }),
});

OpenAI

import { createOpenAI } from "@ai-sdk/openai";
import { withSemanticGuard } from "@semanticguard/ai-sdk";

const openai = createOpenAI({
  apiKey: "your-openai-key",
  fetch: withSemanticGuard({
    gatewayUrl: "https://your-sg-instance.vercel.app",
  }),
});

Anthropic

import { createAnthropic } from "@ai-sdk/anthropic";
import { withSemanticGuard } from "@semanticguard/ai-sdk";

const anthropic = createAnthropic({
  apiKey: "your-anthropic-key",
  fetch: withSemanticGuard({
    gatewayUrl: "https://your-sg-instance.vercel.app",
  }),
});

How It Works

withSemanticGuard() returns a custom fetch function that:

  1. Intercepts AI API calls (POST to model endpoints)
  2. Routes them through SemanticGuard's passthrough proxy
  3. The proxy checks exact match cache, then semantic cache (embedding similarity)
  4. On cache hit: returns cached response instantly (no upstream call)
  5. On cache miss: forwards to the provider, caches the response, logs metadata

Non-AI requests (GET, non-model endpoints) pass through directly without proxying.

Options

withSemanticGuard({
  gatewayUrl: "https://...",    // Your SemanticGuard instance URL (or set SEMANTICGUARD_URL env var)
  apiKey: "sg-...",             // SemanticGuard API key (or set SG_API_KEY env var)
  projectId: "my-project",     // Optional -- project ID for per-project isolation
  enabled: true,                // Optional -- toggle on/off (default: true)
  failOpen: true,               // Optional -- fall back to direct provider call if gateway is down (default: false)
  gatewayTimeoutMs: 10000,      // Optional -- timeout before fail-open kicks in, in ms (default: 10000)
  onLog: (entry) => { ... },   // Optional -- callback for each proxied request
});

Production configuration

For production, enable failOpen so your app continues working if the gateway is unreachable:

const openai = createOpenAI({
  apiKey: "sk-...",
  fetch: withSemanticGuard({
    failOpen: true,
    gatewayTimeoutMs: 5000,
  }),
});

When failOpen is true and the gateway returns a 429/5xx or times out, the SDK retries the request directly against your AI provider. The onLog callback receives a type: "fail-open" entry so you can track these events.

Data Flow

Requests are routed through SemanticGuard for caching and analytics. Prompts and responses are processed in-memory and cached according to your tenant settings. You can control what is stored via the dashboard:

  • Cache learning -- toggle whether responses are cached for future matching
  • Prompt storage -- toggle whether prompt text is persisted in request logs
  • Semantic cache -- toggle vector-based similarity matching on or off

Metadata always logged: model name, token counts, latency, cache status, cost estimate.

Zero Dependencies

This package has no runtime dependencies. It only uses globalThis.fetch.

License

MIT