SDK

SDK overview

The LLM Cost Tracker SDK is a lightweight TypeScript package that wraps your existing Anthropic, OpenAI, Google Gemini, xAI, DeepSeek, Meta, and Perplexity calls and logs cost data to your dashboard — without changing your response handling or adding latency to your requests. Tag calls with a group to attribute spend across features, teams, or workflows. It also handles budget enforcement, checking spend limits and blocking or warning before a call is made.

Installation

npm install @llmcosttracker/sdk

Two functions

The SDK exports two functions depending on how you call your LLM:

What the SDK does on each call

  1. Checks the active budget for this user — per-user budget first, tier template as fallback
  2. If the limit is reached and action is block, throws LLMBudgetExceededError before making the API call
  3. Makes the underlying LLM API call directly to the provider — no proxy, no extra hop
  4. Reads token counts and latency from the response
  5. Posts the cost event to your dashboard asynchronously — never blocks the response
  6. Increments the spend counter for this user

Design principles

  • Non-blocking logging — cost events are posted asynchronously and never throw. A logging failure will never surface to your users or break your app.
  • Zero response change — both functions return the exact same value as the underlying LLM call. Drop-in replacement.
  • No proxy — your API calls go directly to the provider. The SDK reads metadata from the response object after the call resolves. No latency added, no new failure mode.
  • No framework required — works with any Node.js or TypeScript project. No LangChain, no special setup.
  • Enforcement is optional — if no budget is configured, the SDK behaves exactly as before. Enforcement only activates when you configure a limit in the dashboard.

Exports

trackedCall()Wraps a standard non-streaming LLM call. Returns the identical provider response.
trackedStream()Wraps a streaming LLM call. Returns the identical stream. Logs cost after the stream completes.
LLMBudgetExceededErrorError class thrown when a budget is configured with action: "block" and the limit is reached. Import and catch this specifically.
BudgetConfigTypeScript type for per-call budget configuration passed to trackedCall() or trackedStream().
BudgetCheckResultTypeScript type passed to the onBudgetWarning callback. Contains current spend, limit, and budget config.

Next: trackedCall() reference →