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

usage-meter-test

v0.1.0

Published

Usage metering core (credits + quota + holds + ledger) for AI and other compute costs.

Readme

usage-meter

A small TypeScript library for credits + quota + holds + ledger metering (e.g. AI token billing).

Core concepts

  • Credits: integer unit (bigint) deducted from a user account.
  • Hold: pre-authorize a maximum cost (freeze credits + reserve quota).
  • Capture: settle actual cost (deduct balance + release unused hold/quota).
  • Release: cancel a hold (unfreeze credits + unreserve quota).
  • Ledger: append-only, idempotent charge records.

Quick start (in-memory)

import { DEFAULT_PRICING_CONFIG, InMemoryMeteringPersistence, UsageMeter, bps, credits } from "usage-meter";

const persistence = new InMemoryMeteringPersistence({ quota: true, usage: true });

// Seed credits
await persistence.accounts.addCredits("u1", credits(10_000));

// Optional: quota policy (daily 5_000 credits)
persistence.quota?.setPolicy("u1", { policyId: "free-daily", period: "daily", limitCredits: credits(5_000) });

const meter = new UsageMeter(persistence, {
  pricing: {
    ...DEFAULT_PRICING_CONFIG,
    inputTokenCost: credits(1),
    outputTokenCost: credits(2),
    minCost: credits(1),
    defaultModelMultiplierBps: bps(10_000),
    modelRules: [{ model: "premium", multiplierBps: bps(20_000) }],
    toolRules: [{ tool: "web_search", unitCost: credits(50) }],
  },
});

const estimate = meter.estimate({
  userId: "u1",
  requestId: "req-1",
  model: "premium",
  tokens: { inputTokens: 100, outputTokens: 200 },
  maxOutputTokens: 800,
  tools: [{ tool: "web_search", units: 1 }],
});

// Hold max possible cost (freeze + reserve quota)
await meter.hold({
  userId: "u1",
  requestId: "req-1",
  amount: estimate.maxPossibleCost,
  idempotencyKey: "hold:req-1",
  expiresAt: new Date(Date.now() + 2 * 60 * 1000),
});

// Capture actual cost when done
await meter.capture({
  userId: "u1",
  requestId: "req-1",
  actualCost: estimate.estimatedCost,
  idempotencyKey: "charge:req-1",
  usage: {
    requestId: "req-1",
    model: "premium",
    tokens: { inputTokens: 100, outputTokens: 200 },
    tools: [{ tool: "web_search", units: 1 }],
  },
});

Prisma adapter (route A)

  • Add the models/enums from usage-meter/prisma/schema.prisma.snippet into your app's schema.prisma
  • Run prisma migrate dev (or prisma migrate deploy)
import { createPrismaPersistence, UsageMeter, DEFAULT_PRICING_CONFIG } from "usage-meter";
import { PrismaClient, Prisma } from "@prisma/client";

const prisma = new PrismaClient();
const persistence = createPrismaPersistence(prisma, {
  isolationLevel: Prisma.TransactionIsolationLevel.Serializable,
});

const meter = new UsageMeter(persistence, { pricing: DEFAULT_PRICING_CONFIG });