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

@spendcap/anthropic

v0.1.0

Published

The Anthropic SDK behind a spend guard: policy decides, budget reserves, ledger settles at real usage.

Readme

@spendcap/anthropic

The Anthropic SDK behind a spend guard. One import changes; every call is then decided by your policy before it is sent, reserved against your budget, and settled at the token count the API actually reports.

npm install @spendcap/anthropic @spendcap/policy @anthropic-ai/sdk
import Anthropic from "@anthropic-ai/sdk";
import { SpendGuard, MemoryLedgerStore, singlePolicy } from "@spendcap/policy";
import { guardMessages, SpendRefusedError } from "@spendcap/anthropic";

const policy = {
  account: "acct:research", version: 1,
  rules: [
    { id: "daily", kind: "WINDOW_BUDGET", scope: { kind: "ANY" },
      asset: "anthropic:tokens", windowMs: 86_400_000, maxTotal: 2_000_000n },
    { id: "models", kind: "DESTINATION_ALLOWLIST", scope: { kind: "ANY" },
      destinations: ["anthropic:claude-opus-5"] },
  ],
};

const guard = new SpendGuard({ store: new MemoryLedgerStore(), policyFor: singlePolicy(policy) });
const claude = guardMessages(new Anthropic(), {
  guard, account: "acct:research", requester: { kind: "AGENT", agentId: "researcher" },
});

try {
  const message = await claude.create({
    model: "claude-opus-5", max_tokens: 16_000,
    messages: [{ role: "user", content: "Summarise this quarter's incidents." }],
  });
} catch (error) {
  if (error instanceof SpendRefusedError) console.log(error.message); // "DENY  ...  (BUDGET_EXHAUSTED)"
  else throw error;
}

What is counted. Input, output, cache creation and cache read tokens, summed, in the asset anthropic:tokens. Pass measure to weight them or to settle in a currency instead; pass asset to name it.

The destination is the model. Each call is recorded against anthropic:<model>, so a destination allowlist is a model allowlist and a destination denylist blocks a model. Override with destination.

Before the call, an estimate; after it, the truth. The reservation is about four characters per token of input plus max_tokens. Set estimate: "count" to use the token counting endpoint instead. Either way the ledger settles at message.usage.

When the API says no, budget comes back. An error with an HTTP status reverses the reservation, since nothing was billed. A connection reset or a timeout leaves it open, because a response lost after the tokens were spent looks identical from here; reconcile those against your usage report.

Streaming. await claude.stream(params) returns the SDK's own MessageStream plus a settled promise that resolves once the final message has arrived and the ledger is updated.

Retries are free. Pass requestId to derive a stable id from your own request, and a retry returns the original decision without spending twice.

The SDK is a peer dependency used for its types only. Nothing here imports it at runtime; any object with the same messages shape works.

Ships without a test suite. npm run check at the repository root exercises the claims above against the built output.

Apache-2.0.