adachsoft / model-pricing-reconciliation-openai
Reads billed costs from the OpenAI Costs API so model-pricing estimates can be reconciled with real invoices.
Package info
gitlab.com/a.adach/model-pricing-reconciliation-openai
pkg:composer/adachsoft/model-pricing-reconciliation-openai
Requires
- php: ^8.3
- ext-json: *
- adachsoft/model-pricing-contracts: ^0.4
- adachsoft/model-pricing-lib: ^0.1.0
- psr/http-client: ^1.0
- psr/http-factory: ^1.0
Requires (Dev)
- adachsoft/php-code-style: ^0.7.3
- friendsofphp/php-cs-fixer: ^3.95
- guzzlehttp/psr7: ^2.0
- 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 21:03:58 UTC
README
This library connects the OpenAI organization Costs API to the shared model-pricing billed-cost reconciliation flow. It retrieves cost buckets, parses OpenAI line-item labels, and exposes billed lines for comparison with catalog price estimates.
Features
- Fetches daily cost buckets across the requested half-open time range
[from, to)and follows API pagination. - Parses model, component, service-tier, and context-tier details from OpenAI cost labels.
- Implements
BilledCostSourceInterfaceand returns the shared billed-cost line collection. - Uses PSR-18 for HTTP requests and PSR-17 for request creation.
Requirements
- PHP 8.3 or later.
- PSR-18 HTTP client and PSR-17 request factory implementations provided by your application.
- An OpenAI organization admin API key with the Owner role for Costs API access.
Installation
composer require adachsoft/model-pricing-reconciliation-openai
Provide a PSR-18 HTTP client and PSR-17 request factory in your application. The Costs API requires an OpenAI organization admin API key with the Owner role. Store it in an environment variable such as ADMIN_OPENAI_API_KEY; do not commit or log its value.
Usage
use AdachSoft\ModelPricingReconciliationOpenAi\Http\OpenAiCostsApiClient;
use AdachSoft\ModelPricingReconciliationOpenAi\Parser\OpenAiCostLineItemParser;
use AdachSoft\ModelPricingReconciliationOpenAi\Source\OpenAiBilledCostSource;
$apiClient = new OpenAiCostsApiClient(
$httpClient,
$requestFactory,
getenv('ADMIN_OPENAI_API_KEY') ?: '',
);
$source = new OpenAiBilledCostSource($apiClient, new OpenAiCostLineItemParser());
$billedLines = $source->fetchBilledLines($from, $to);
See the usage example for date-range and label details.
Cost labels
Labels follow [<tier> | ]<model>[, <kind>][, long context]. Missing tiers resolve to standard; fast and priority resolve to priority; flex, batch, and ultrafast remain distinct. Recognized component labels are input, cached input, cache writes, cache writes - 12hr, and output. An absent or unrecognized component remains unpriced. long context selects context_tier=long; otherwise the line uses context_tier=short.
Reconciliation results
coveragePercent measures the share of billed amounts for which the catalog supplied a matching price. deviationPercent compares expected cost with billed cost for covered lines only; unpriced items are listed separately, aggregated by raw provider label. isWithinThreshold() is true only when no priced line exceeds the threshold and the aggregate covered-cost deviation is within it.