Skip to content
reaatechREAATECH

@reaatech/otel-cost-exporter-core

pending npm

Provides shared TypeScript types, Zod schemas, and GenAI semantic conventions for modeling and validating LLM cost data. It serves as the foundational library for the otel-cost-exporter ecosystem, requiring `zod` and `pino` at runtime.

@reaatech/otel-cost-exporter-core

npm version License: MIT CI

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

Core domain types, Zod schemas, GenAI semantic conventions, and shared utilities for the @reaatech/otel-cost-exporter-* ecosystem. This package is the single source of truth for all LLM cost domain shapes used across the exporter monorepo.

Installation

terminal
npm install @reaatech/otel-cost-exporter-core
# or
pnpm add @reaatech/otel-cost-exporter-core

Feature Overview

  • Domain typesPriceEntry, CostBreakdown, CostSpan, AggregationKey, TelemetryContext define the canonical shapes for LLM cost data
  • Zod schemasPriceEntrySchema, CostSpanSchema, ConfigSchema for runtime validation at system boundaries
  • GenAI semantic conventionsGEN_AI_SYSTEM, GEN_AI_REQUEST_MODEL, GEN_AI_USAGE_INPUT_TOKENS, GEN_AI_USAGE_OUTPUT_TOKENS, and cache token attribute constants
  • Structured logging — Pino-based createLogger() factory with configurable level and format (JSON or pretty-print)
  • Interval parsingparseIntervalMs() converts human-readable duration strings ("24h", "5m", "30s") to milliseconds
  • ConstantsTOKENS_PER_UNIT (1,000,000), roundTo() precision helper used throughout the cost calculation pipeline
  • Zero runtime dependencies beyond pino and zod — lightweight and tree-shakeable
  • Dual ESM/CJS output — works with import and require

Quick Start

typescript
import { CostSpanSchema, type CostSpan, createLogger } from "@reaatech/otel-cost-exporter-core";
 
// Validate a CostSpan at the boundary
const rawSpan = JSON.parse(incomingJson);
const span: CostSpan = CostSpanSchema.parse(rawSpan);
 
// Create a logger
const logger = createLogger("info", "json");
logger.info({ spanId: span.spanId, model: span.model }, "Processing span");

Exports

Domain Types

ExportDescription
PriceEntryPricing for a single model: inputTokenPrice, outputTokenPrice, optional cacheReadPrice, cacheCreationPrice, effectiveDate
CostBreakdownComputed costs: inputCostUsd, outputCostUsd, optional cacheReadCostUsd, cacheCreationCostUsd
CostSpanNormalized span: spanId, traceId, provider, model, inputTokens, outputTokens, cacheReadTokens?, cacheCreationTokens?, costUsd, costBreakdown, telemetry, timestamp, startTime, endTime, durationMs, status, errorMessage?
AggregationKey{ provider, model, environment?, ns? } — key for grouping spans by dimensions
TelemetryContextDeployment metadata: environment?, ns (namespace), service?

Zod Schemas

ExportDescription
PriceEntrySchemaValidates pricing entries — positive token prices, ISO 8601 effective date
CostSpanSchemaValidates span structure with nested cost breakdown and telemetry context
ConfigSchemaFull configuration validation — pricing, metrics, export, and logging sections

GenAI Semantic Conventions

ExportAttribute Name
GEN_AI_SYSTEMgen_ai.system
GEN_AI_REQUEST_MODELgen_ai.request.model
GEN_AI_USAGE_INPUT_TOKENSgen_ai.usage.input_tokens
GEN_AI_USAGE_OUTPUT_TOKENSgen_ai.usage.output_tokens
GEN_AI_USAGE_CACHE_READ_TOKENSgen_ai.usage.cache_read_input_tokens
GEN_AI_USAGE_CACHE_CREATION_TOKENSgen_ai.usage.cache_creation_input_tokens
SEMCONV_VERSIONPinned semantic conventions version

Constants

ExportValueDescription
TOKENS_PER_UNIT1000000Tokens per pricing unit (1M)
roundTo(value, decimals)FunctionRound a number to N decimal places

Logging

ExportDescription
createLogger(level, format)Pino logger factory — accepts `“debug"

Utilities

ExportDescription
parseIntervalMs(value)Parse a duration string (24h, 5m, 30s, 1h30m) into milliseconds

Usage Pattern

Every schema export has a matching type export. Use the schema for runtime validation and the type for compile-time checking:

typescript
import { PriceEntrySchema, type PriceEntry } from "@reaatech/otel-cost-exporter-core";
 
// Parse at the boundary — throws ZodError on invalid data
function validatePrice(raw: unknown): PriceEntry {
  return PriceEntrySchema.parse(raw);
}

License

MIT