Search by

adachsoft / model-pricing-lib

Arkadiusz Adach

Versioned JSON price sheet catalog and BCMath cost estimator implementing adachsoft/model-pricing-contracts.

Package info

gitlab.com/a.adach/model-pricing-lib

Issues

pkg:composer/adachsoft/model-pricing-lib

Statistics

Installs: 13

Dependents: 2

Suggesters: 0

Stars: 0

v0.1.0 2026-11-17 18:16 UTC

This package is auto-updated.

Last update: 2026-11-17 17:26:21 UTC


README

adachsoft/model-pricing-lib provides JSON-backed versioned price catalogs and decimal cost estimation for applications implementing adachsoft/model-pricing-contracts.

Features

  • Store append-only price sheet versions as one JSON file per provider.
  • Resolve model aliases and select the price sheet effective at a requested time.
  • Estimate costs for token and per-unit usage with BCMath decimal arithmetic.
  • Reconcile billed costs and verify estimated costs against provider-reported amounts.

Requirements

  • PHP 8.3 or later.
  • The BCMath extension.
  • adachsoft/model-pricing-contracts for request, price-sheet, and result DTOs.

Installation

composer require adachsoft/model-pricing-lib

Basic usage

use AdachSoft\ModelPricingLib\ModelPricingFactory;

$catalog = ModelPricingFactory::createJsonCatalog(__DIR__ . '/var/model-prices');
$estimator = ModelPricingFactory::createEstimator($catalog);

$estimate = $estimator->estimate($request); // CostEstimateRequestDto from the contracts package

The estimate is null when no matching price sheet is available. Missing prices for individual usage kinds are reported in the estimate rather than priced using a different tier. See the usage example for constructing a request and storing a price sheet.

Price catalog format

The repository stores files at <directory>/<providerId>.json. Each file contains a provider ID, optional aliases, and price sheet versions with effective dates and components. Writes are made through a temporary file and atomic rename. Invalid JSON or sheet data raises PriceCatalogUnavailableException.

A price version is appended only when its components or long-context threshold differ from the latest version for that model. Existing versions are not edited or removed. The first observed version is treated as effective from the Unix epoch; subsequent versions take effect at their fetch time.

Additional services

CostReconciliationService compares billed cost lines with the effective price sheet. ProviderCostVerificationService aggregates estimated and provider-reported cost pairs from a ProviderCostCheckJournalInterface; JsonlProviderCostCheckJournal is the append-only JSONL implementation.