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

trystub

v1.2.0

Published

Drop-in budget gate for autonomous AI agents: reserve, pay once, settle. Exactly-once spend control over a strongly-consistent double-entry ledger.

Readme

trystub

One budget your agents can't break.

npm version npm downloads install size types license

A drop-in budget gate for AI agents that spend real money: paid APIs, x402 micropayments, and LLM tokens. Put one enforceable budget in front of your whole fleet and keep an exact, double-entry record of what every agent spent. Zero dependencies, TypeScript-first, works with any runtime that has fetch.

The hard part it solves: a naive retry around an irreversible payment double-charges. trystub uses a reserve → pay → settle flow, so the payment fires exactly once, even under concurrent writers, conflict retries, or a crash mid-flight.

npm install trystub

Three lines in front of a paid call

import { StubClient } from "trystub";

const stub = new StubClient({ apiKey: process.env.STUB_API_KEY, baseUrl: process.env.STUB_URL });

if (await stub.guard({ vendorAccountId, amountUsd: 0.02, intent: "fetch market data" })) {
  await doThePaidThing();
}

Exactly-once around an irreversible payment

When the payment itself can't be taken back, reserve first, pay once, then settle the real cost:

import { StubClient } from "trystub";
import { payThroughStub } from "trystub/x402";

const stub = new StubClient({ apiKey: process.env.STUB_API_KEY, baseUrl: process.env.STUB_URL });

const data = await payThroughStub(stub, vendorAccountId, {
  status: 402,
  priceUsd: 0.04,
  intent: "fetch market data",
  costCenter: "Marketing",
  pay: async () => {
    const res = await fetchPaidResource();
    return { result: res.body, actualUsd: res.chargedUsd };
  },
});

payThroughStub reserves the estimate against the budget, runs pay() exactly once, then settles for the actual amount and refunds the difference. If the reservation is denied it throws BudgetDeniedError and never pays; if pay() throws, the hold is released.

Metering LLM tokens

Tokens are the spend most fleets actually have. meterLLM holds an estimate, runs the call once, then settles the real token cost:

import { StubClient } from "trystub";
import { meterLLM, estimateUsd } from "trystub/llm";

const pricing = { inputPerMTok: 3, outputPerMTok: 15 };

const response = await meterLLM(
  stub,
  {
    vendorAccountId,
    pricing,
    estimatedUsd: estimateUsd(promptTokens, maxOutputTokens, pricing),
    model: "your-model",
    costCenter: "Marketing",
  },
  () => callTheModel(),
);

usageFrom reads both OpenAI-shaped (prompt_tokens / completion_tokens) and Anthropic-shaped (input_tokens / output_tokens) responses, including cached-input tokens, and costUsd prices them. You supply the rates. This package ships no price table on purpose: published prices go stale, and a stale number in a dependency silently mis-bills you. If the response carries no usage, the hold settles at the full estimate rather than guessing low.

Reliability

Every call has a timeout and bounded retries on network errors, 429, and 5xx. Retries are safe because the client attaches a generated idempotencyKey to each spend and reserve, so a request that is retried after the server already committed it resolves to that same single ledger entry instead of charging twice. Denials and 4xx are never retried.

new StubClient({ baseUrl, apiKey, timeoutMs: 10_000, maxRetries: 2, retryDelayMs: 200 });

Set maxRetries: 0 to disable retries; the client then sends no generated key.

Operator actions

Two calls are for whoever runs the Stub deployment, not an agent's runtime: construct the client with the server's ADMIN_TOKEN as apiKey instead of an agent's scoped key.

const admin = new StubClient({
  apiKey: process.env.STUB_ADMIN_TOKEN,
  baseUrl: process.env.STUB_URL,
});

await admin.sweep(); // reclaim reservations left held past the server's TTL; run on a schedule
await admin.provisionShards(accountId); // split a hot budget across shard rows for throughput

provisionShards shards whatever the account's balance is right now, and refuses to run twice on the same account. If you shard a budget account, shard every vendor account it pays too — a sharded spend errors if either side of the pair isn't sharded.

API

  • new StubClient({ apiKey?, baseUrl?, fetch?, timeoutMs?, maxRetries?, retryDelayMs? })
  • stub.guard(input): Promise<boolean>: true only if the spend committed
  • stub.spend(input): Promise<SpendResult>: single-shot debit
  • stub.reserve(input): Promise<ReserveResult>: hold funds against the cap
  • stub.settle(reservationId, actualUsd?): Promise<SettleResult>: book the real cost
  • stub.release(reservationId): Promise<ReleaseResult>: return a hold
  • stub.sweep(): Promise<SweepResult>: operator-only, reclaim expired holds
  • stub.provisionShards(accountId, shards?): Promise<ShardAccountResult>: operator-only, shard an account

input accepts { vendorAccountId, amountUsd, intent?, costCenter?, idempotencyKey?, budgetAccountId?, receipt? }. A scoped API key pins spends to one budget account; without one, pass budgetAccountId and authenticate as an admin.

Ships as both ESM and CommonJS, so import and require both work.

Links

License

MIT