Your app's data
Node.js SDK
Wrap OpenAI, Anthropic and Google Gen AI clients or the Vercel AI SDK in Node 18+, with background batching and retries.
The Node.js SDK records token usage from the OpenAI, Azure OpenAI, Anthropic (including its Bedrock and Vertex clients) and Google Gen AI clients, and from the Vercel AI SDK, and sends it to the batch ingestion endpoint with an ingestion key. Node 18 or later, ESM and CommonJS with types, MIT licensed. The package is aispendbook; it installs from GitHub with npm install github:AISpendbook/aispendbook-node (source: AISpendbook/aispendbook-node).
install
npm install github:AISpendbook/aispendbook-node
export SPENDBOOK_API_KEY=afk_your_key # ingestion key, shown once
export SPENDBOOK_ENDPOINT=https://www.aispendbook.comwrap clients
import OpenAI from "openai";
import Anthropic from "@anthropic-ai/sdk";
import { init, wrapOpenAI, wrapAnthropic, withContext, flush } from "aispendbook";
init(); // reads SPENDBOOK_API_KEY and SPENDBOOK_ENDPOINT
const openai = wrapOpenAI(new OpenAI());
const anthropic = wrapAnthropic(new Anthropic());
await withContext({ customerId: "company_456", feature: "summarize", environment: "production" }, async () => {
await openai.chat.completions.create({ model: "gpt-4.1-mini", messages });
await anthropic.messages.create({ model: "claude-sonnet-4-5", max_tokens: 512, messages });
});
await flush(); // in a serverless function, before returning- Wrap clients with
wrapOpenAI,wrapAnthropicandwrapGoogleGenAI. Wrapped calls return exactly what the original client returns. withContext({ customerId, feature, environment, projectId, runId }, fn)follows the async call chain and keeps concurrent requests apart.- For anything else, call
record({ provider, model, inputTokens, outputTokens, ... }).
Vercel AI SDK
middleware
import { wrapLanguageModel } from "ai";
import { openai } from "@ai-sdk/openai";
import { spendbookMiddleware } from "aispendbook";
const model = wrapLanguageModel({ model: openai("gpt-4.1-mini"), middleware: spendbookMiddleware() });You can instead send the AI SDK's own OpenTelemetry spans: see Vercel AI SDK, LangChain and agents.
Delivery
- Events are queued in memory and sent in the background in batches of 100 by default (at most 500), or every 2 seconds.
- 429, 5xx and network errors are retried with backoff for up to 60 seconds. Retries keep the same event id, so the API removes duplicates.
- When the queue is full (10,000 by default), new events are dropped and counted. Queued events are flushed when the process is about to exit.
SPENDBOOK_DISABLED=1turns the SDK into a no-op.
Limits
- Streamed OpenAI chat completions only include usage when the request sets
stream_options: { include_usage: true }. Otherwise the event has zero tokens and is markedusage_missing. - Only chat completions, Responses, Anthropic messages and Gemini
generateContentandgenerateContentStreamare wrapped. Embeddings, images, audio, batch, realtime and Gemini chats needrecord(). - Vercel AI SDK 5 reports cached input differently per provider; the middleware subtracts cached tokens except for Anthropic. AI SDK 6 usage is read as is.
- Token counts are what the provider returned. AI Spendbook prices them as estimates; billed cost comes from the billing connectors.
- Tested with fakes only, not against live provider accounts.