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

@bluefyn/ai-spend-sdk

v0.2.0

Published

Track the cost of your AI features per customer and feature. In-process, metadata-only usage telemetry for Bluefyn AI Spend — Vercel AI SDK, Anthropic and OpenAI adapters plus manual recording.

Readme

@bluefyn/ai-spend-sdk

Track what each AI model call costs you, per customer and per feature, in Bluefyn AI Spend.

The SDK records one usage event for each model call your Node.js backend makes and sends it to Bluefyn in the background. Bluefyn prices each call and shows the cost per customer, feature, model and run. The SDK sends metadata only: provider, model, token counts, status, latency and the ids you attach. It never reads or sends prompts, completions, tool arguments, embeddings or images.

  • Adapters for the Vercel AI SDK, the Anthropic SDK and the OpenAI SDK, plus record() for any other provider.
  • It never throws into your code, never blocks a call, and keeps its memory bounded.
  • Zero runtime dependencies. MIT licensed. Developed at github.com/bluefyn/ai-sdk and published from there with npm provenance.

Requirements

  • Node.js ^20.16.0 || >=22.3.0, tested on Node.js 20, 22 and 24. ESM and CommonJS.
  • A backend. In a browser the SDK turns itself off, so the key never reaches your users.
  • For an adapter, the library you already use: ai 7 or later, @anthropic-ai/sdk 0.40 or later, or openai 4.80 or later (4.87 or later for the Responses API). They are optional peer dependencies, so the SDK installs beside any version you have.

Vercel Functions, AWS Lambda and Cloudflare Workers each need one extra line. See Runtimes.

Get access and an SDK key

  1. Email [email protected] to turn on AI Spend and its Product lens for your workspace. Until Bluefyn turns them on, the AI spend page has no Product lens, and the ingest endpoint refuses events with 403 feature_disabled. The SDK then logs one warning and pauses sending.
  2. As a workspace Owner or Admin, open Payables → AI spend, switch to the Product lens, and open Settings → SDK keys.
  3. Click Create SDK key. Name the key and pick the environment you send from: production, staging, development, or a name of your own. Copy the key. It starts with bfu_ and is shown once.

An SDK key can send usage events and customer names. It cannot read any data. Bluefyn stores each event under the environment of the key that sent it.

Install

npm install @bluefyn/ai-spend-sdk

Set the key in the environment of every process that calls a model:

BLUEFYN_AI_SPEND_KEY=bfu_...

Create the client once

// ai-spend.ts
import { createAiSpend } from '@bluefyn/ai-spend-sdk';

export const aiSpend = createAiSpend(); // reads BLUEFYN_AI_SPEND_KEY

Import this one client wherever you call a model. Without a key, the client logs one warning and does nothing, so your code runs unchanged in local development.

Record your AI calls

Pick the library you call models with. Each example records one event per model call.

Vercel AI SDK

// telemetry.ts
import { anthropic } from '@ai-sdk/anthropic';
import { generateText, registerTelemetry } from 'ai';
import { aiSpendTelemetry } from '@bluefyn/ai-spend-sdk/vercel';
import { aiSpend } from './ai-spend';

export const aiSpendIntegration = aiSpendTelemetry(aiSpend);

registerTelemetry(aiSpendIntegration); // once, at startup

const { text } = await generateText({ model: anthropic('claude-opus-5-5'), prompt: 'Hi' });

generateText, streamText, generateObject, streamObject, embed and embedMany are recorded. A call that passes its own telemetry.integrations replaces the registered ones, so add AI Spend to that list with withAiSpendIntegration(aiSpendIntegration, [yours]).

Anthropic SDK

// anthropic.ts
import Anthropic from '@anthropic-ai/sdk';
import { wrapAnthropic } from '@bluefyn/ai-spend-sdk/anthropic';
import { aiSpend } from './ai-spend';

export const anthropic = wrapAnthropic(new Anthropic(), aiSpend);

const message = await anthropic.messages.create({
  model: 'claude-opus-5-5',
  max_tokens: 1024,
  messages: [{ role: 'user', content: 'Hi' }],
});

The wrapped client has the same type, methods and return values as your client. Use it everywhere instead of new Anthropic(), and wrap each client once. Streams, messages.stream(), messages.parse(), and the Amazon Bedrock and Google Vertex AI clients are recorded too.

OpenAI SDK

// openai.ts
import OpenAI from 'openai';
import { wrapOpenAI } from '@bluefyn/ai-spend-sdk/openai';
import { aiSpend } from './ai-spend';

export const openai = wrapOpenAI(new OpenAI(), aiSpend);

const response = await openai.responses.create({ model: 'gpt-5', input: 'Hi' });
const completion = await openai.chat.completions.create({ model: 'gpt-5', messages: [{ role: 'user', content: 'Hi' }] });

Chat Completions, Responses and embeddings are recorded, streaming included. Wrap an Azure OpenAI client, or a client of an OpenAI-compatible host, the same way.

Any other provider

import { fromOpenAIChatUsage } from '@bluefyn/ai-spend-sdk';
import { aiSpend } from './ai-spend';

const startedAt = Date.now();
const completion = await callModel(request); // a client no adapter covers

aiSpend.record({
  provider: 'openai',
  model: completion.model,
  usage: fromOpenAIChatUsage(completion.usage),
  latencyMs: Date.now() - startedAt,
});

Build usage with a normalizer (fromAnthropicUsage, fromOpenAIChatUsage, fromOpenAIResponsesUsage, fromOpenAIEmbeddingsUsage or fromVercelUsage), so cached tokens are never counted twice.

Say which customer and feature each call is for

import { aiSpend } from './ai-spend';

export async function POST(request: Request) {
  const account = await auth(request);

  // Every AI call inside, however deep, is attributed to this customer and feature.
  return aiSpend.withContext({ customer: account.id, feature: 'support.reply' }, () => handleChat(request));
}
  • customer is your own opaque id for the account, never an email address.
  • feature is a lowercase dot path such as support.reply. Costs roll up by prefix, so support includes support.reply.
  • A call without a customer or a feature is still recorded, under Unattributed.

For Express middleware, NestJS interceptors, background jobs and per-call attribution, see Attribution.

Send what is buffered before the process ends

The SDK sends a batch every 5 seconds, and its timers never keep your process alive. Send the rest before the process ends:

import { waitUntil } from '@vercel/functions';
import { aiSpend } from './ai-spend';

// A long-running server, when the platform stops it:
process.on('SIGTERM', async () => {
  await aiSpend.shutdown();
  process.exit(0);
});

// Vercel Functions, before the handler returns. In Next.js, after(() => aiSpend.flush()) works too.
waitUntil(aiSpend.flush());

// AWS Lambda, before the handler returns:
await aiSpend.flush();

A script or a cron job calls await aiSpend.shutdown() before it ends.

Check the first event

import { aiSpend } from './ai-spend';

// After one AI call, send now instead of waiting for the next batch:
await aiSpend.flush();

console.log(aiSpend.enabled, aiSpend.stats()); // true { queued: 0, sent: 1, accepted: 1, ... }

In Bluefyn, Settings in the Product lens shows the event as soon as it arrives. Its cost appears on the Overview, Customers and Features tabs about 5 minutes later. If accepted stays at 0, see Troubleshooting.

Learn more

License

MIT © Bluefyn