Search by

adachsoft / model-pricing-reconciliation-openai

Arkadiusz Adach

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

Issues

pkg:composer/adachsoft/model-pricing-reconciliation-openai

Statistics

Installs: 3

Dependents: 0

Suggesters: 0

Stars: 0

v0.1.0 2026-11-17 22:03 UTC

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 BilledCostSourceInterface and 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.