Documentation

Installation

Install the MarginDash SDK for your language.

npm
npm install ai-cost-calc

Usage

TypeScript SDK reference: npmjs.com/package/ai-cost-calc

TypeScript
import { AiCostCalc } from "ai-cost-calc";

const md = new AiCostCalc({ apiKey: "YOUR_API_KEY_HERE" }); // Get from Settings page

const response = await md.guardedCall({
  customerId: String(user.id),
  eventType: "summarize",
}, () => providerCall());

// No prompts or responses ever leave your servers
md.addUsage({
  model: response.model,
  inputTokens: response.usage?.prompt_tokens,
  outputTokens: response.usage?.completion_tokens,
});

md.track({
  customerId: String(user.id), // "8291" (must be a string)
  eventType: "summarize",
  revenueAmountInCents: 500,
});

// Events are flushed automatically in the background.
// Before your process exits:
await md.shutdown();

Privacy first: Only the model name and token counts are sent to MarginDash — no request or response content ever leaves your infrastructure. Pass model and token counts via addUsage(), and MarginDash calculates cost from live pricing data. For agent sessions with multiple AI calls, call addUsage() once per call, then track() once to attach them all.

Supported Models

Click a vendor to see valid slugs for the model field. You can also fetch this list programmatically via GET /api/v1/models.