PHP code example of sanjayacloud / ai-model-usage-tracker

1. Go to this page and download the library: Download sanjayacloud/ai-model-usage-tracker 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/ */

    

sanjayacloud / ai-model-usage-tracker example snippets


use AiModelUsageTracker\AiModelUsageTracker\Facades\AiModelUsageTracker;

AiModelUsageTracker::track()
    ->provider('openai')
    ->model('gpt-4o')
    ->tokens(prompt: 1200, completion: 350)
    ->record();

use AiModelUsageTracker\AiModelUsageTracker\Reporting\UsageReporter;

app(UsageReporter::class)->totals(); // ['records' => 1, 'total_tokens' => 1550, 'total_cost' => 0.0065, ...]

use Illuminate\Support\Facades\Schedule;

Schedule::command('ai-usage:fetch-pricing')->daily();

'instrumentation' => [
    'laravel-ai' => true,
    'prism' => false,
    'http' => false,
],

use AiModelUsageTracker\AiModelUsageTracker\Facades\AiModelUsageTracker;
use AiModelUsageTracker\AiModelUsageTracker\Enums\Operation;

AiModelUsageTracker::for($user) // attribute to any Eloquent model
    ->provider('openai')
    ->model('gpt-4o')
    ->operation(Operation::Chat)
    ->tokens(prompt: 1200, completion: 350, cacheRead: 800, reasoning: 120)
    ->conversation($conversationId) // optional, for per-conversation reporting
    ->feature('support-bot')        // optional, for grouping by product feature
    ->latency(840)
    ->meta(['ticket_id' => 42])
    ->record();

$response = Prism::text()->using('openai', 'gpt-4o')->withPrompt('...')->generate();

AiModelUsageTracker::capturePrism($response, $user);

use AiModelUsageTracker\AiModelUsageTracker\Facades\AiModelUsageTracker;

// e.g. in a service provider or middleware
AiModelUsageTracker::resolveTrackableUsing(fn () => auth()->user());

use AiModelUsageTracker\AiModelUsageTracker\Reporting\UsageReporter;

$summary = app(UsageReporter::class)->forConversation($conversationId);

$summary['totals'];   // ['records' => 5, 'total_tokens' => 8120, 'total_cost' => 0.0123, ...]
$summary['by_model']; // per-model breakdown for that conversation

use AiModelUsageTracker\AiModelUsageTracker\Facades\AiModelUsageTracker;
use AiModelUsageTracker\AiModelUsageTracker\Http\Resources\UsageResource;

$record = AiModelUsageTracker::forInvocation($invocationId); // or ::latest()

return response()->json([
    'answer' => $answer,
    'usage' => $record ? (new UsageResource($record))->toArray($request) : null,
]);

use Illuminate\Support\Facades\Schedule;

Schedule::command('ai-usage:fetch-pricing')->daily();

'pricing' => [
    'currency' => 'USD',
    'models' => [
        'gemini' => [
            'gemini-3.1-flash-lite' => ['input' => 0.25, 'output' => 1.50, 'cache_read' => 0.025],
        ],
    ],
],

use AiModelUsageTracker\AiModelUsageTracker\Reporting\UsageReporter;

$reporter = app(UsageReporter::class);

$reporter->totals();                 // records, tokens, cost, failures
$reporter->forConversation($id);     // totals + per-model breakdown for one conversation
$reporter->byModel();                // grouped by model
$reporter->byProvider();             // grouped by provider
$reporter->byOperation();            // grouped by operation
$reporter->dailyTrend();             // cost/tokens per day
$reporter->topConsumers();           // biggest spenders (attributed models)

'budgets' => [
    'monthly' => ['period' => 'month', 'limit' => 500.0, 'thresholds' => [0.8, 1.0]],
],

use AiModelUsageTracker\AiModelUsageTracker\Events\BudgetThresholdReached;

Event::listen(BudgetThresholdReached::class, function (BudgetThresholdReached $event) {
    // notify your team
});

'recording' => [
    'mode' => 'queue',
    'queue' => ['connection' => null, 'queue' => null],
],

use Illuminate\Support\Facades\Schedule;

Schedule::command('ai-usage:prune')->daily();

use Illuminate\Support\Facades\Gate;

Gate::define('viewAiUsageDashboard', fn ($user) => $user->isAdmin());
bash
php artisan migrate
bash
php artisan vendor:publish --tag="ai-model-usage-tracker-migrations"
bash
php artisan vendor:publish --tag="ai-model-usage-tracker-config"
bash
php artisan ai-usage:fetch-pricing
php artisan ai-usage:reprice
bash
php artisan ai-usage:fetch-pricing
bash
php artisan ai-usage:reprice          # zero-cost / pricing_missing only
php artisan ai-usage:reprice --all
php artisan ai-usage:reprice --dry-run
bash
php artisan ai-usage:report --days=30
bash
php artisan ai-usage:prune --days=90
bash
php artisan ai-usage:install
bash
php artisan vendor:publish --tag="ai-model-usage-tracker-inertia-vue"
php artisan vendor:publish --tag="ai-model-usage-tracker-inertia-react"