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

llm-cost-guard

v1.5.0

Published

Drop-in spend tracking, rolling budgets, and kill switches for LLM API calls.

Downloads

288

Readme

llm-cost-guard

Drop-in spend tracking, rolling budgets, and kill switches for LLM API calls.

npm version license: MIT node >=18

Why this exists

LLM bills can spike from runaway loops, prompt explosions, or accidental load. llm-cost-guard gives you hard limits and visibility without locking you into one provider SDK.

What it does

  • Tracks spend from token usage with built-in model pricing
  • Enforces rolling-window budgets globally or by user/feature
  • Triggers threshold alerts (80/90/100%) and kill events
  • Wraps SDK clients (OpenAI/Anthropic/Gemini-style usage fields)
  • Supports manual usage tracking for any provider
  • Provides Express/Fastify precheck guards
  • Uses pluggable storage (in-memory included)

Quickstart

npm install llm-cost-guard
import { createGuard } from "llm-cost-guard";

const guard = createGuard({
  budgets: [{ id: "global-hourly", limitUsd: 50, windowMs: 60 * 60 * 1000 }]
});

await guard.track({
  model: "gpt-5",
  inputTokens: 1200,
  outputTokens: 800,
  userId: "u_123",
  feature: "chat"
});

Real examples

import OpenAI from "openai";
import { createGuard } from "llm-cost-guard";

const guard = createGuard({
  budgets: [
    { id: "global", limitUsd: 100, windowMs: 3_600_000 },
    { id: "user-daily", limitUsd: 10, windowMs: 86_400_000, scopeBy: "user" }
  ]
});

const wrapped = guard.wrap(new OpenAI(), { userId: "u_42", feature: "assistant" });
await wrapped.responses.create({ model: "gpt-5", input: "Summarize this transcript" });
guard.onBudgetAlert((event) => {
  console.log(`[${event.thresholdPercent}%] ${event.scopeKey}: $${event.usageUsd.toFixed(4)} / $${event.limitUsd}`);
});

guard.onKill((event) => {
  console.error(`Kill switch: ${event.scopeKey} exceeded $${event.limitUsd}`);
});

Config / options

| Option | Type | Default | Description | |---|---|---|---| | budgets | BudgetRule[] | required | Rules to enforce | | pricing | PricingCatalog | built-in | Override/add model pricing | | storage | StorageAdapter | MemoryStorageAdapter | Persist usage events | | throwOnKill | boolean | true | Throw BudgetExceededError when exceeded | | onUnknownModel | "error" \| "zero" | "error" | Unknown model behavior | | now | () => number | Date.now | Injectable clock for testing |

Architecture / flow

  1. Ingest usage via guard.wrap(...) or guard.track(...).
  2. Resolve model pricing and compute cost.
  3. Store usage event in selected adapter.
  4. Evaluate matching budget scopes/windows.
  5. Emit alert/kill events and optionally throw.

Benchmarks / perf notes

  • Zero runtime dependencies.
  • Default MemoryStorageAdapter keeps chronological events and optimizes time-window queries.
  • For multi-instance deployments, use a shared StorageAdapter (Redis/Postgres/etc.) to keep budget state consistent.

Limitations + roadmap

Current limitations

  • Built-in pricing is static; provider pricing changes require updates/overrides
  • SDK auto-wrap depends on providers exposing token usage in response payloads
  • In-memory adapter is process-local (not distributed)

Roadmap

  • Additional ready-made storage adapters
  • More provider-specific usage extractors
  • Optional sampled telemetry/export hooks

License + links