Skip to content
reaatechREAATECH

@reaatech/multi-tenant-mcp-cost-accounting

npm v0.1.0

Calculates and tracks multi-tenant usage costs for MCP tool invocations using per-call, per-token, and tiered pricing models. It provides classes for cost calculation, in-memory state tracking, and asynchronous event emission to external billing pipelines.

@reaatech/multi-tenant-mcp-cost-accounting

npm version License: MIT CI

Status: Pre-1.0 — APIs may change in minor versions. Pin to a specific version in production.

Track and report per-tenant usage costs with per-call, per-token, and tiered pricing models.

Installation

terminal
npm install @reaatech/multi-tenant-mcp-cost-accounting
# or
pnpm add @reaatech/multi-tenant-mcp-cost-accounting

Feature Overview

  • Per-call pricing — Assign a flat cost to each tool invocation.
  • Per-token pricing — Charge independently for input and output tokens.
  • Tiered discounts — Volume discounts applied progressively as call counts grow.
  • Usage emitters — Forward UsageEvent records to your billing pipeline via a callback. Emissions are non-blocking and never fail the request.

Quick Start

typescript
import {
  DefaultCostCalculator,
  InMemoryCostTracker,
  CallbackUsageEmitter,
} from '@reaatech/multi-tenant-mcp-cost-accounting';
 
const calculator = new DefaultCostCalculator({
  perCall: { 'tool-premium': 0.05, 'tool-standard': 0.005 },
  perToken: { input: 0.001, output: 0.002 },
  tiers: [
    { upTo: 1000, discount: 0 },
    { upTo: 10_000, discount: 0.1 },
    { upTo: Number.POSITIVE_INFINITY, discount: 0.2 },
  ],
});
 
const tracker = new InMemoryCostTracker({ calculator });
 
const emitter = new CallbackUsageEmitter(async (event) => {
  await billingPipeline.record(event);
});
 
const event = {
  tenantId: 'acme-corp',
  itemName: 'tool-premium',
  itemType: 'tool',
  inputTokens: 1500,
  outputTokens: 800,
  timestamp: new Date(),
};
 
const cost = calculator.calculate(event, tracker.getAccount('acme-corp'));
await tracker.record(event); // accumulates into the tenant's CostAccount

Exports

ExportKindDescription
CostCalculatorInterfacecalculate(event, account) → number
CostTrackerInterfacerecord(event) + getAccount(tenantId)
UsageEventEmitterInterfaceemit(event) — async, non-blocking
CostAccountInterfacetenantId, totalCost, totalCalls, totalInputTokens, totalOutputTokens
UsageEventInterfaceEvent payload: tenantId, itemName, itemType, inputTokens, outputTokens, timestamp
DefaultCostCalculatorClassPer-call + per-token + tiered cost calculation
InMemoryCostTrackerClassLRU-bounded in-memory cost accumulator
CallbackUsageEmitterClassInvoke a user callback for each usage event

Pricing Evaluation Order

  1. Per-call cost (pricing.perCall[itemName])
  2. Per-token cost (pricing.perToken.input × count + pricing.perToken.output × count)
  3. Tiered discount applied to the subtotal based on account.totalCalls

License

MIT