Skip to content
reaatechREAATECH

@reaatech/llm-router-telemetry

npm v1.0.0

Tracks LLM request costs, enforces budget limits, and exports OpenTelemetry-compatible metrics. It provides a set of classes including `CostTracker`, `BudgetManager`, and `CostReporter` designed to integrate with the `llm-router` ecosystem.

@reaatech/llm-router-telemetry

npm version License: MIT CI

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

Cost telemetry and budget management for llm-router. Real-time cost tracking per model, per-request, per-strategy with budget enforcement (soft/hard limits), cost aggregation, reporting, and OpenTelemetry-compatible metrics collection.

Installation

terminal
npm install @reaatech/llm-router-telemetry
# or
pnpm add @reaatech/llm-router-telemetry

Feature Overview

  • Per-request cost tracking — record cost, tokens, model, strategy, and budget ID for every routed call
  • Budget enforcement — daily limits with configurable alert thresholds and hard/soft enforcement modes
  • Cost aggregation — group costs by model, strategy, budget, and time period
  • Cost reporting — generate structured reports with per-model breakdowns and trend data
  • OTel-compatible metricsMetricsCollector provides a facade over OTel for request counts, latency histograms, and cost gauges
  • Budget alerts — callbacks fire at configured thresholds (50%, 75%, 90%) to integrate with your alerting stack

Quick Start

typescript
import {
  CostTracker,
  BudgetManager,
  CostReporter,
} from "@reaatech/llm-router-telemetry";
 
// Track costs across all routing decisions
const tracker = new CostTracker();
 
tracker.record({
  requestId: "req-abc123",
  modelId: "glm-edge",
  cost: 0.0012,
  inputTokens: 500,
  outputTokens: 200,
  strategy: "cost-optimized",
  budgetId: "team-alpha",
});
 
// Get per-model cost summaries
const aggregated = tracker.getAggregation("glm-edge");
console.log(aggregated.totalCost, aggregated.totalRequests);

API Reference

CostTracker

Tracks per-request costs and provides aggregation queries.

typescript
import { CostTracker, createCostTracker } from "@reaatech/llm-router-telemetry";
 
const tracker = createCostTracker();

Methods

MethodReturnsDescription
record(entry: CostEntry)voidRecord a single request’s cost and token usage
getAggregation(modelId)CostAggregationGet cost summary for a model
getAllAggregations()Map<string, CostAggregation>Get summaries for all models
getAllEntries()CostEntry[]Get every recorded cost entry
calculateCost(model, inputTokens, outputTokens)numberEstimate cost using the model’s per-million-token rates
reset()voidClear all recorded entries

CostEntry

FieldTypeDescription
requestIdstringUnique request identifier
modelIdstringWhich model was used
costnumberActual cost in USD
inputTokensnumberInput token count
outputTokensnumberOutput token count
strategystringWhich strategy selected the model
budgetIdstringWhich budget this cost counts against
timestampDateWhen the request was recorded

CostAggregation

FieldTypeDescription
totalCostnumberSum of all costs
totalRequestsnumberNumber of requests
totalInputTokensnumberTotal input tokens
totalOutputTokensnumberTotal output tokens
modelIdstringWhich model these aggregates apply to

CostByModel

FieldTypeDescription
modelIdstringModel identifier
costnumberTotal cost
percentagenumberShare of total cost (0–100)
requestsnumberRequest count

BudgetManager

Enforces daily spending limits with alert thresholds.

typescript
import { BudgetManager, createBudgetManager } from "@reaatech/llm-router-telemetry";
 
const manager = createBudgetManager();
 
manager.register({
  id: "team-alpha",
  dailyLimit: 100,
  alertThresholds: [0.5, 0.75, 0.9],
  hardLimit: true,
});

Methods

MethodReturnsDescription
register(config: BudgetConfig)voidRegister a new budget
registerAll(configs: BudgetConfig[])voidRegister multiple budgets at once
getConfig(id)BudgetConfig | undefinedGet a budget’s configuration
getState(id)BudgetState | undefinedGet a budget’s current state
checkBudget(id, estimatedCost)BudgetCheckResultCheck if a cost fits within the budget
recordSpending(id, cost)voidDeduct cost from the budget
getRemaining(id)number | undefinedGet remaining budget
resetBudget(id)voidReset daily spending (for midnight cron)
getAllStates()Map<string, BudgetState>Get all budget states
onAlert(id, callback)voidRegister a callback for budget threshold alerts

BudgetCheckResult

FieldTypeDescription
allowedbooleanWhether the expense is within budget
reasonstring | undefinedHuman-readable reason if rejected
remainingAfternumberProjected remaining budget if expense goes through
thresholdsTriggerednumber[]Which alert thresholds (if any) this expense would cross

BudgetAlert

FieldTypeDescription
budgetIdstringWhich budget triggered the alert
thresholdnumberWhich threshold was crossed (e.g., 0.75 = 75%)
spentTodaynumberCurrent spend amount
dailyLimitnumberThe budget’s total daily limit

CostReporter

Generates structured cost reports.

typescript
import { CostReporter } from "@reaatech/llm-router-telemetry";
 
const reporter = new CostReporter(tracker, manager);
 
const report = reporter.getReport({
  budgetId: "team-alpha",
  period: "today",
});
console.log(report.totalCost, report.byModel);

Methods

MethodReturnsDescription
getReport(options)CostReportGenerate a cost report for a budget and time period

CostReportOptions

FieldTypeDescription
budgetIdstringBudget to report on
periodtoday" | "week" | "monthTime window
modelIdstringOptional filter to a single model

CostReport

FieldTypeDescription
totalCostnumberTotal spend in the period
totalRequestsnumberNumber of requests
budgetIdstringBudget being reported on
periodstringTime window
byModelCostByModel[]Per-model breakdown
byStrategyRecord<string, number>Cost grouped by strategy
remainingBudgetnumberRemaining in the budget

MetricsCollector

OTel-compatible metrics collector for routing observability.

typescript
import { MetricsCollector } from "@reaatech/llm-router-telemetry";
 
const collector = new MetricsCollector({ enabled: true });
await collector.initialize();

Methods

MethodDescription
recordRequest(attributes)Tracks a request by strategy, status, model, cost, and latency
recordFallbackActivation(chainName)Counts fallback chain activations
updateBudgetRemaining(budgetId, remaining)Updates the budget remaining gauge
recordTokenUsage(modelId, inputTokens, outputTokens)Accumulates token usage per model
updateCircuitBreakerState(modelId, state)Tracks circuit breaker state changes
getMetrics()Returns a Map<string, number> of current metric values
initialize()Promise<boolean> — initializes the collector
shutdown()Promise<void> — shuts down and clears metrics

MetricsConfig

FieldTypeDefaultDescription
serviceNamestringllm-routerService identifier for metrics
enabledbooleanfalseWhether to collect metrics
exportIntervalMsnumber60000Export interval in milliseconds

TelemetryMetrics

Convenience facade over MetricsCollector for telemetry-specific events.

typescript
import { TelemetryMetrics } from "@reaatech/llm-router-telemetry";
 
const metrics = new TelemetryMetrics(collector);
metrics.recordCost("glm-edge", "cost-optimized", 0.123, 42);

License

MIT