@reaatech/multi-tenant-mcp-cost-accounting
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-accountingFeature 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
UsageEventrecords 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 CostAccountExports
| Export | Kind | Description |
|---|---|---|
CostCalculator | Interface | calculate(event, account) → number |
CostTracker | Interface | record(event) + getAccount(tenantId) |
UsageEventEmitter | Interface | emit(event) — async, non-blocking |
CostAccount | Interface | tenantId, totalCost, totalCalls, totalInputTokens, totalOutputTokens |
UsageEvent | Interface | Event payload: tenantId, itemName, itemType, inputTokens, outputTokens, timestamp |
DefaultCostCalculator | Class | Per-call + per-token + tiered cost calculation |
InMemoryCostTracker | Class | LRU-bounded in-memory cost accumulator |
CallbackUsageEmitter | Class | Invoke a user callback for each usage event |
Pricing Evaluation Order
- Per-call cost (
pricing.perCall[itemName]) - Per-token cost (
pricing.perToken.input × count+pricing.perToken.output × count) - Tiered discount applied to the subtotal based on
account.totalCalls
