PHP code example of token27 / nexus-ai-pricing

1. Go to this page and download the library: Download token27/nexus-ai-pricing 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/ */

    

token27 / nexus-ai-pricing example snippets


use Token27\NexusAI\Pricing\Engine\PricingEngine;

$result = PricingEngine::for('claude-sonnet-4-6')->calculate(
    inputTokens: 250,
    outputTokens: 400,
    cacheWriteTokens: 800,  // Anthropic 5-minute surcharge
    cacheReadTokens: 5000   // 90% discounted block
);

echo $result->totalCostUsd();     // Output standard metrics seamlessly
echo $result->cacheSavingsUsd();  // Output exactly what was saved against baselines

// Processes offline immediately avoiding Network HTTP limits
$estimated = PricingEngine::for('gpt-4o-mini')
    ->estimate('Generate a dense historical assessment regarding the Roman Empire.');

echo $estimated->format(); 

use Token27\NexusAI\Pricing\ValueObject\ImageAttachment;

// Handles internal tile mapping explicitly
$visionPrice = PricingEngine::for('gpt-4o')->estimateWithImages(
    text: 'Analyze the architectural defect visible here',
    images: [
        ImageAttachment::highDetail(1920, 1080)
    ]
);

echo $visionPrice->imageCostUsd();

use Token27\NexusAI\Pricing\Engine\PricingEngine;
use Token27\NexusAI\Pricing\PriceTable\ArrayPriceTable;
use Token27\NexusAI\Pricing\ValueObject\ModelPrice;
use Token27\Tokenizer\Contract\ImageTokenEstimatorInterface;
use Token27\Tokenizer\Contract\TokenCountInterface;
use Token27\Tokenizer\Registry\TokenizerRegistry;
use Token27\Tokenizer\ValueObject\TokenCount;
use Token27\Tokenizer\Vision\AnthropicImageEstimator;
use Token27\Tokenizer\Vision\GeminiImageEstimator;
use Token27\Tokenizer\Vision\OpenAIImageEstimator;

// Proprietary model: flat 500 tokens per image regardless of size/detail
$myEstimator = new class implements ImageTokenEstimatorInterface {
    public function estimateImageTokens(int $widthPx, int $heightPx, string $detail, string $model): TokenCountInterface
    {
        return new TokenCount(count: 500, model: $model, strategy: 'flat_500', approximate: false);
    }
    public function supports(string $model): bool
    {
        return str_starts_with($model, 'my-vision-');
    }
};

$engine = new PricingEngine(
    tokenizer:       TokenizerRegistry::createDefault(),
    priceTable:      new ArrayPriceTable([
        new ModelPrice('my-vision-model', inputPerMillion: 1.00, outputPerMillion: 4.00, imageInputPerMillion: 2.00),
    ]),
    imageEstimators: [
        $myEstimator,                  // my-vision-* → 500 tokens flat (checked first)
        new OpenAIImageEstimator(),    // gpt-4o, o1, o3, …  (built-in)
        new AnthropicImageEstimator(), // claude-*           (built-in)
        new GeminiImageEstimator(),    // gemini-*           (built-in)
    ],
);

$result = $engine->make('my-vision-model')->estimateWithImages(
    text:   'Analyse this diagram.',
    images: [ImageAttachment::highDetail(1920, 1080), ImageAttachment::lowDetail(800, 600)],
);

echo $result->imageTokens();   // 1000 (2 images × 500 flat)
echo $result->imageCostUsd();