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

agent-budget-guard

v1.0.0

Published

LLM API budget enforcement with real-time cost tracking, automatic model downgrade, and spending limits

Downloads

114

Readme

agent-budget-guard

LLM API budget enforcement with real-time cost tracking, automatic model downgrade, and spending limits.

Wraps fetch calls to OpenAI, Anthropic, and Google APIs. Tracks cumulative token costs per session and enforces three configurable thresholds:

  • WARN — fires a callback, continues
  • THROTTLE — auto-downgrades the model (e.g., Opus to Sonnet)
  • STOP — halts with a spending summary

Zero external runtime dependencies. Works as middleware (wraps fetch) or standalone (pass usage stats manually).

Install

npm install agent-budget-guard

Quick Start

import { BudgetGuard } from 'agent-budget-guard';

const guard = new BudgetGuard({
  budget: 5.00,  // $5.00 max
  thresholds: {
    warn: 0.50,      // warn at 50%
    throttle: 0.80,  // downgrade at 80%
    stop: 1.00,      // halt at 100%
  },
  throttleModel: 'claude-sonnet-4-6',
  onWarn: (stats) => console.log('Budget warning:', stats),
  onStop: (stats) => { throw new Error('Budget exceeded') },
});

// Wrap your fetch calls
const response = await guard.fetch('https://api.anthropic.com/v1/messages', {
  method: 'POST',
  headers: {
    'content-type': 'application/json',
    'x-api-key': process.env.ANTHROPIC_API_KEY,
    'anthropic-version': '2023-06-01',
  },
  body: JSON.stringify({
    model: 'claude-opus-4-6',
    max_tokens: 1024,
    messages: [{ role: 'user', content: 'Hello' }],
  }),
});

API

new BudgetGuard(options)

| Option | Type | Default | Description | |--------|------|---------|-------------| | budget | number | required | Maximum spend in dollars | | thresholds.warn | number | 0.50 | Fraction of budget to trigger warn | | thresholds.throttle | number | 0.80 | Fraction to trigger model downgrade | | thresholds.stop | number | 1.00 | Fraction to halt all calls | | throttleModel | string | — | Model to downgrade to at throttle | | pricing | Partial<PricingTable> | — | Custom per-model pricing overrides | | onWarn | (stats) => void | — | Callback on warn threshold | | onThrottle | (stats) => void | — | Callback on throttle threshold | | onStop | (stats) => void | — | Callback on stop threshold | | cocChainFile | string | — | Path to CoC JSONL file for provenance | | sessionId | string | auto | Session identifier for CoC entries |

guard.fetch(url, init?)

Drop-in replacement for fetch. Auto-detects provider from URL, parses usage from response, calculates cost. Downgrades model at throttle threshold. Throws BudgetExceededError at stop.

guard.wrap()

Returns a bound fetch function: const myFetch = guard.wrap();

guard.recordUsage(options)

Manual usage recording for streaming responses or non-fetch integrations:

guard.recordUsage({
  inputTokens: 1500,
  outputTokens: 800,
  model: 'claude-opus-4-6',
  provider: 'anthropic',
});

guard.getStats()

Returns current UsageStats: total tokens, cost, budget percent, per-model breakdown, threshold events.

guard.isStopped() / guard.reset()

Check if halted. Reset all state for a new session.

CLI

# Summarize spending from CoC entries
agent-budget-guard report chain.jsonl

Built-in Pricing ($/MTok)

| Provider | Model | Input | Output | |----------|-------|-------|--------| | Anthropic | claude-opus-4-7 | $15 | $75 | | Anthropic | claude-sonnet-4-6 | $3 | $15 | | Anthropic | claude-haiku-4-5 | $1 | $5 | | OpenAI | gpt-4 | $30 | $60 | | OpenAI | gpt-4o | $5 | $15 | | OpenAI | gpt-3.5-turbo | $0.50 | $1.50 | | Google | gemini-pro | $7 | $21 | | Google | gemini-1.5-flash | $0.075 | $0.30 |

Override with the pricing option. Unknown models are tracked (tokens counted) but at zero cost.

CoC Integration

If @absupport/coc-writer is installed, threshold events are recorded as BUDGET_EVENT entries in the specified chain file. This is optional — the package works standalone.

Deploy

  1. npm run build — compiles TypeScript to CJS + ESM
  2. npm test — runs 39 tests via node --test
  3. npm publish — publish to npm

License

Apache-2.0