1. Go to this page and download the library: Download 1manfactory/ai-costs library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
1manfactory / ai-costs example snippets
declare(strict_types=1);
use AiCosts\CostCalculator;
use AiCosts\Enum\BillingMode;
use AiCosts\OpenAI\OpenAIResponsesUsageExtractor;
use AiCosts\OpenAI\OpenAIToolPricing;
use AiCosts\Pricing\StaticPriceProvider;
use AiCosts\Value\BillingContext;
$payload = [
'model' => 'gpt-5.4',
'usage' => [
'input_tokens' => 1200,
'input_tokens_details' => ['cached_tokens' => 200],
'output_tokens' => 300,
'output_tokens_details' => ['reasoning_tokens' => 50],
],
];
$extractor = new OpenAIResponsesUsageExtractor();
$usage = $extractor->extract($payload);
$calculator = new CostCalculator(StaticPriceProvider::default());
$breakdown = $calculator->calculate(
$usage,
new BillingContext(
billingMode: BillingMode::STANDARD,
additionalCharges: [
OpenAIToolPricing::webSearchCalls(3),
],
),
);
echo $breakdown->inputCostInUsdMicrocent;
echo $breakdown->cachedInputCostInUsdMicrocent;
echo $breakdown->outputCostInUsdMicrocent;
echo $breakdown->additionalChargesInUsdMicrocent;
echo $breakdown->totalCostInUsdMicrocent;
use AiCosts\Value\UsageBreakdown;
$usage = new UsageBreakdown(
model: 'gpt-5.6',
inputTokens: 300_000,
cachedInputTokens: 20_000,
outputTokens: 10_000,
cacheWriteInputTokens: 30_000,
);
use AiCosts\Enum\ContextPricingMode;
new BillingContext(
billingMode: BillingMode::STANDARD,
contextPricingMode: ContextPricingMode::LONG,
);
use DateTimeImmutable;
$provider = StaticPriceProvider::default(
new DateTimeImmutable('2026-08-31'),
);
$provider = StaticPriceProvider::default(
new DateTimeImmutable('2026-09-01'),
);