adachsoft / model-pricing-lib
Versioned JSON price sheet catalog and BCMath cost estimator implementing adachsoft/model-pricing-contracts.
Requires
- php: ^8.3
- ext-bcmath: *
- ext-json: *
- adachsoft/model-pricing-contracts: ^0.4
Requires (Dev)
- adachsoft/php-code-style: ^0.7.3
- friendsofphp/php-cs-fixer: ^3.95
- phpstan/phpstan: ^2.3
- phpstan/phpstan-phpunit: ^2.0
- phpunit/phpunit: ^12.5
- rector/rector: ^2.8
- symplify/phpstan-rules: ^14.19
Suggests
None
Provides
None
Conflicts
None
Replaces
None
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-contractsfor 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.