npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

@reaatech/llm-cost-telemetry

v0.2.0

Published

Core types, schemas, utilities, and configuration for LLM cost telemetry

Readme

@reaatech/llm-cost-telemetry

npm version License: MIT CI

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

Core types, Zod schemas, shared utilities, and configuration loaders for the @reaatech/llm-cost-telemetry-* ecosystem. This package is the foundation that every other package in the monorepo depends on.

Installation

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

Feature Overview

  • 40+ domain typesCostSpan, CostBreakdown, AggregationKey, BudgetConfig, TelemetryContext, and more
  • 35 Zod schemas — runtime validation for every domain type at API boundaries
  • 25 utility functions — ID generation, time window math, cost calculation helpers, retry with backoff, hashing
  • Configuration loaders — typed, validated env-var loaders for telemetry, budgets, and all three exporters
  • Zero runtime dependencies beyond zod — lightweight and tree-shakeable
  • Dual ESM/CJS output — works with import and require

Quick Start

import {
  generateId,
  now,
  loadConfig,
  calculateCostFromTokens,
  type CostSpan,
  type TelemetryContext,
} from "@reaatech/llm-cost-telemetry";

const span: CostSpan = {
  id: generateId(),
  provider: "openai",
  model: "gpt-4",
  inputTokens: 500,
  outputTokens: 200,
  costUsd: calculateCostFromTokens(700, 30), // $0.021
  tenant: "acme-corp",
  feature: "chat-support",
  timestamp: now(),
};

const config = loadConfig();
console.log(config.budget.global.daily); // 100.0

Exports

Domain Types

| Export | Description | |--------|-------------| | CostSpan | Single LLM API call record: id, provider, model, tokens, cost, timing, telemetry context | | CostBreakdown | Input, output, cache read, and cache creation cost breakdown | | AggregationKey | Aggregation dimension key: tenant, feature, route, provider, model, time window | | CostRecord | Aggregated cost record with dimension metadata | | PricingTier | Model pricing per 1M tokens: input, output, cache read, cache creation | | BudgetConfig / BudgetLimits | Per-tenant budget limits with cascading defaults | | BudgetStatus | Current budget utilization: daily/monthly percentages, triggered alerts | | TelemetryConfig | OTel configuration: endpoint, service name, sampling rate | | TelemetryContext | Telemetry context attached to API calls: tenant, feature, route, metadata | | CostSummary | Aggregated cost summary grouped by dimension |

Zod Schemas

Every domain type has a matching Zod schema for runtime validation:

import { CostSpanSchema, TelemetryContextSchema } from "@reaatech/llm-cost-telemetry";

const span = CostSpanSchema.parse(rawData);     // throws on invalid input
const ctx = TelemetryContextSchema.parse(meta); // validates tenant/feature/route

43 schemas are exported, including typed input variants (CostSpanInput, AggregationKeyInput, etc.) inferred from partial schemas.

Utilities

| Function | Description | |----------|-------------| | generateId() | UUID v4 via crypto.randomUUID | | now() / nowMs() | Current timestamp as Date or milliseconds | | sleep(ms) | Promise-based delay | | getWindowStart(date, window) | Start of a time window (minute/hour/day/week/month) | | getWindowEnd(date, window) | End of a time window | | formatISODate(date) / parseISODate(iso) | ISO date formatting and parsing | | clamp(value, min, max) | Number clamping | | percentage(part, total) | Percentage calculation | | roundTo(value, decimals) | Decimal rounding | | calculateCostFromTokens(tokens, pricePerMillion) | (tokens / 1,000,000) × price | | deepClone(obj) | JSON-based deep clone | | isEmpty(obj) | Empty object check | | deepMerge(target, source) | Recursive object merge | | retryWithBackoff(fn, options) | Exponential backoff retry loop | | batchArray(arr, batchSize) | Array batch chunking | | sanitizeLabel(value) | Sanitize string for use as a metric label | | simpleHash(str) | String hashing for grouping | | getEnvVar(name, default?) / getEnvInt / getEnvFloat / getEnvBool | Environment variable loaders |

Configuration

| Export | Description | |--------|-------------| | loadConfig() | Loads complete AppConfig from environment variables | | loadTelemetryConfig() | OTel settings from OTEL_* env vars | | loadBudgetConfig() | Budget limits with optional TENANT_BUDGETS JSON | | loadCloudWatchConfig() | AWS CloudWatch exporter settings | | loadCloudMonitoringConfig() | GCP Cloud Monitoring exporter settings | | loadPhoenixConfig() | Grafana Loki/Phoenix exporter settings | | DEFAULT_CONFIG | Hardcoded fallback configuration |

Usage Patterns

Runtime Validation at Boundaries

import { CostSpanSchema, type CostSpan } from "@reaatech/llm-cost-telemetry";

function ingestSpan(raw: unknown): CostSpan {
  return CostSpanSchema.parse(raw); // throws ZodError on invalid data
}

Time Window Arithmetic

import { getWindowStart, getWindowEnd } from "@reaatech/llm-cost-telemetry";

const dayStart = getWindowStart(new Date(), "day");  // today at 00:00:00
const dayEnd = getWindowEnd(new Date(), "day");      // tomorrow at 00:00:00

Configuration from Environment

import { loadConfig } from "@reaatech/llm-cost-telemetry";

const config = loadConfig();
// Reads: OTEL_SERVICE_NAME, DEFAULT_DAILY_BUDGET, TENANT_BUDGETS,
//        AWS_REGION, GCP_PROJECT_ID, LOKI_HOST, and more

Related Packages

License

MIT