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

@substrat-run/engine-metering

v0.3.3

Published

Substrat engine: billable usage — an append-only, idempotent meter ledger (counters and gauges) whose closed periods are frozen billing evidence

Readme

@substrat-run/engine-metering

Metering engine for Substrat — billable usage as an append-only, idempotent meter ledger whose closed periods are frozen billing evidence.

It counts what happened — tokens, requests, bytes stored — and knows nothing about what any of it costs: no prices, no currency, no plans. The vertical maps meter keys to rates and feeds invoicing from the fat metering.period-closed event. It is the billable metering plane, deliberately separate from the platform's high-volume telemetry plane (Analytics Engine): durable, transactional, in the scope's own SQL — because an invoice needs evidence a customer can dispute.

What it owns

  • The ledger is append-only. A usage entry is never edited or deleted; an over-recorded counter is corrected by a compensating negative entry.
  • Ingest is idempotent by construction. Every recordUsage names a caller-supplied dedupe key, unique per meter (UNIQUE (meter_key, dedupe_key)). A replay with the same quantity returns the existing entry — no second row, no second event, no double bill; the same key with a different quantity throws.
  • Counters and gauges aggregate differently. A counter (tokens, requests) is a flow you sum — signed deltas, corrections compensate. A gauge (bytes stored) is a level you sample — max-in-window, and a silent window carries the last level forward. Kind and unit live on the meter definition and are frozen after creation.
  • A closed period is evidence. closePeriod freezes a half-open [from, to) UTC window into immutable per-meter lines and emits one fat event. Closes are monotonic, and the latest closed to is a hard horizon no new entry may land behind — closed lines stay reproducible from their entries forever.
  • Entries may carry an opaque attribution ref (subject: EntityRef — a project, a site) for bill-splitting and filtering. The engine never dereferences it, and takes no DataSubjectId: meters count machines and bytes, not people (piiClass: 'none' throughout).

Install

pnpm add @substrat-run/engine-metering
import { meteringModule, configureMeter, recordUsage, PERM } from '@substrat-run/engine-metering';

host.registerModule(meteringModule);

// A vertical composes the in-scope functions inside its own operations — same
// transaction, its own permission check:
host.defineOperation('builder/complete-turn', async (ctx, input) => {
  assertAllowed(await ctx.check(MY_PERM.turn));
  // … the turn's own work …
  recordUsage(ctx, {
    meter: 'ai.tokens.input',                 // registered once via configureMeter
    qty: String(input.usage.inputTokens),
    subject: projectRef(input.projectId),     // attribution: whose bill line is this
    dedupeKey: input.turnId,                  // idempotency: a retried turn never double-bills
  });
  return …;
});

High-volume sources pre-aggregate before recording (one counter entry per hour/day, bucket id as the dedupe key); the raw firehose stays on the platform telemetry plane.

Documentation

https://substrat.net/engines — the domain model and ledger invariants, the full operation/permission surface, the event contracts, and how a vertical composes pricing on top.

The docs site is the single source of truth; this README deliberately doesn't restate it.

Related packages

Status

Pre-release (0.x): surfaces change without notice until the first vertical ships.