@reaatech/llm-router-telemetry
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
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 metrics — MetricsCollector 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
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.
import { CostTracker, createCostTracker } from "@reaatech/llm-router-telemetry" ;
const tracker = createCostTracker ();
Methods
Method Returns Description 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
Field Type Description 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
Field Type Description totalCostnumberSum of all costs totalRequestsnumberNumber of requests totalInputTokensnumberTotal input tokens totalOutputTokensnumberTotal output tokens modelIdstringWhich model these aggregates apply to
CostByModel
Field Type Description modelIdstringModel identifier costnumberTotal cost percentagenumberShare of total cost (0–100) requestsnumberRequest count
BudgetManager
Enforces daily spending limits with alert thresholds.
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
Method Returns Description 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
Field Type Description 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
Field Type Description 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.
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
Method Returns Description getReport(options)CostReportGenerate a cost report for a budget and time period
CostReportOptions
Field Type Description budgetIdstringBudget to report on periodtoday" | "week" | "monthTime window modelIdstringOptional filter to a single model
CostReport
Field Type Description 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.
import { MetricsCollector } from "@reaatech/llm-router-telemetry" ;
const collector = new MetricsCollector ({ enabled: true });
await collector. initialize ();
Methods
Method Description 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 collectorshutdown()Promise<void> — shuts down and clears metrics
MetricsConfig
Field Type Default Description serviceNamestringllm-routerService identifier for metrics enabledbooleanfalseWhether to collect metrics exportIntervalMsnumber60000Export interval in milliseconds
TelemetryMetrics
Convenience facade over MetricsCollector for telemetry-specific events.
import { TelemetryMetrics } from "@reaatech/llm-router-telemetry" ;
const metrics = new TelemetryMetrics (collector);
metrics. recordCost ( "glm-edge" , "cost-optimized" , 0.123 , 42 );
Related Packages
@reaatech/llm-router-core — Shared types including BudgetConfig, BudgetState, and CostTelemetry
@reaatech/llm-router-engine — Main routing engine that consumes telemetry
License
MIT