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

@reaatech/agent-budget-spend-tracker

v0.1.0

Published

Real-time spend tracking for agent-budget-controller

Readme

@reaatech/agent-budget-spend-tracker

npm version License: MIT CI

Status: Pre-1.0 — APIs may change in minor versions. Pin to a specific version in production.

Real-time spend tracking with a circular-buffer store, multi-index lookups, sliding-window aggregation, and anomaly detection. Designed for sub-millisecond per-scope spend queries at scale — the core data layer behind every budget enforcement check.

Installation

npm install @reaatech/agent-budget-spend-tracker
# or
pnpm add @reaatech/agent-budget-spend-tracker

Feature Overview

  • Circular buffer store — configurable up to 500K entries, oldest evicted automatically
  • O(1) per-scope spend lookup — instant total computation via pre-aggregated indexes
  • Three indexes — by scope, by time bucket (per-minute), and by model ID
  • Sliding-window spend rategetRate() returns spend-per-minute over configurable windows
  • Cost projectionprojectTotal() estimates spend over a configurable time horizon
  • Spike detection — standard-deviation-based anomaly detection for unexpected spend bursts
  • Multi-scope queries — filter by scope type, time range, model, or any combination
  • Zero external dependencies — pure in-memory, no database required

Quick Start

import { SpendStore } from '@reaatech/agent-budget-spend-tracker';
import { BudgetScope } from '@reaatech/agent-budget-types';

const store = new SpendStore({ maxEntries: 100_000 });

store.record({
  requestId: 'req-abc123',
  scopeType: BudgetScope.User,
  scopeKey: 'user-42',
  cost: 0.045,
  inputTokens: 1500,
  outputTokens: 300,
  modelId: 'claude-sonnet-4',
  provider: 'anthropic',
  timestamp: new Date(),
});

const spent = store.getSpend(BudgetScope.User, 'user-42');
// → 0.045

const rate = store.getRate(BudgetScope.User, 'user-42', 5); // last 5 minutes
// → 0.009 per minute

API Reference

SpendStore

| Method | Description | | ------------------------------------------------------------ | ---------------------------------------------------------------------- | | record(entry) | Record a spend event. Returns an auto-incremented entry ID. | | getSpend(scopeType, scopeKey) | Get total spend for a scope. O(1). | | getRate(scopeType, scopeKey, windowMinutes?) | Spend per minute over the last N minutes (default: 1). | | projectTotal(scopeType, scopeKey, windowHours?) | Projected total spend over N hours (default: 1) based on current rate. | | detectSpikes(scopeType, scopeKey, thresholdStdDev?) | Detect abnormal spend bursts. Returns spike entries. | | getEntriesInRange(scopeType, scopeKey, startTime, endTime) | Query entries in a time range. | | getRecentEntries(count) | Get the N most recent entries across all scopes. | | getEntriesByModel(modelId) | Get entries for a specific model across all scopes. | | getAllScopes(scopeType) | List all scope keys that have entries for a given scope type. | | size | Total number of entries currently in the store. |

Constructor

new SpendStore(options?: {
  maxEntries?: number; // default: 500_000
})

SpendAccumulator

Internal interface exposed for inspection:

interface SpendAccumulator {
  total: number;
  entryIds: number[];
}

Usage Patterns

Spike Detection

const store = new SpendStore();

// Record normal traffic...
for (const entry of normalEntries) store.record(entry);

// Detect anomalies — entries more than 2 standard deviations above the mean
const spikes = store.detectSpikes(BudgetScope.User, 'user-42', 2);
for (const spike of spikes) {
  console.warn(`Spike: $${spike.cost} — model ${spike.modelId}`);
}

Multi-Scope Budget Tracking

// Track spend per-user, per-org, and globally
store.record({ ...entry, scopeType: BudgetScope.User, scopeKey: 'user-42', cost: 0.01 });
store.record({ ...entry, scopeType: BudgetScope.Org, scopeKey: 'org-acme', cost: 0.01 });

const userSpend = store.getSpend(BudgetScope.User, 'user-42'); // → 0.01
const orgSpend = store.getSpend(BudgetScope.Org, 'org-acme'); // → 0.01

Integration with Budget Engine

import { SpendStore } from '@reaatech/agent-budget-spend-tracker';
import { BudgetController } from '@reaatech/agent-budget-engine';

const store = new SpendStore({ maxEntries: 500_000 });
const controller = new BudgetController({ spendTracker: store });

// BudgetController automatically records spend via store.record()
await controller.defineBudget({
  scopeType: BudgetScope.User,
  scopeKey: 'user-42',
  limit: 10.0,
  policy: { softCap: 0.8, hardCap: 1.0 },
});

await controller.record({
  requestId: 'req-1',
  scopeType: BudgetScope.User,
  scopeKey: 'user-42',
  cost: 2.5,
  inputTokens: 800,
  outputTokens: 200,
  modelId: 'claude-opus-4-1',
  provider: 'anthropic',
  timestamp: new Date(),
});

Related Packages

License

MIT