PHP code example of adam-dziuk / laravel-agent-cost

1. Go to this page and download the library: Download adam-dziuk/laravel-agent-cost 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/ */

    

adam-dziuk / laravel-agent-cost example snippets


use AdamDziuk\LaravelAgentCost\Facades\AiCost;
use App\Ai\Agents\SupportAgent; // any Laravel\Ai\Contracts\Agent

$response = SupportAgent::make()->prompt('What is a llama?'); // laravel/ai

AiCost::for($response); // 0.0075

AiCost::tokens('gpt-4o', inputTokens: 1000, outputTokens: 500); // 0.0075

AiCost::agent(SupportAgent::make())->monthFrom('2026-01')->total(); // total SupportAgent cost from January 2026 onward

return [

    // The URL `ai-prices:sync` downloads LiteLLM's pricing data from.
    'source_url' => env(
        'AGENT_COST_SOURCE_URL',
        'https://raw.githubusercontent.com/BerriAI/litellm/main/model_prices_and_context_window.json'
    ),

    // Where the synced pricing data is cached. A `store` of null uses
    // the application's default cache store.
    'cache' => [
        'store' => env('AGENT_COST_CACHE_STORE'),
        'key' => 'agent-cost::prices',
    ],

    // Manual price overrides, keyed the same way as the LiteLLM pricing
    // file. Prices are USD per single token.
    'overrides' => [
        // 'gpt-4o' => [
        //     'input_cost_per_token' => 0.0000025,
        //     'output_cost_per_token' => 0.00001,
        // ],
    ],

    // Automatically records the cost of every laravel/ai agent
    // invocation, so `AiCost::agent()` can total it up. See "Tracking
    // the total cost of an agent" below.
    'agent_tracking' => [
        'enabled' => env('AGENT_COST_TRACK_AGENTS', true),
        'table' => env('AGENT_COST_TABLE', 'agent_cost_records'),
    ],

];

Schedule::command('ai-prices:sync')->daily();

use AdamDziuk\LaravelAgentCost\Facades\AiCost;
use App\Ai\Agents\SupportAgent; // any Laravel\Ai\Contracts\Agent, created with `php artisan make:agent`

$response = SupportAgent::make()->prompt('Summarize this document.');

$cost = AiCost::for($response); // e.g. 0.007500

use AdamDziuk\LaravelAgentCost\Facades\AiCost;

AiCost::tokens('gpt-4o', inputTokens: 1000, outputTokens: 500); // 0.0075

// Provider-prefixed keys, like LiteLLM uses for Azure, are resolved too.
AiCost::tokens('azure/o3', inputTokens: 1000, outputTokens: 500);

use AdamDziuk\LaravelAgentCost\Facades\AiCost;
use App\Ai\Agents\SupportAgent;

AiCost::agent(SupportAgent::make())->total(); // every recorded invocation of SupportAgent, ever
AiCost::agent(SupportAgent::class)->total(); // a class name works too, no instance needed

AiCost::agent(SupportAgent::make())
    ->dateFrom('2026-01-01')
    ->dateTo('2026-01-31')
    ->total();

AiCost::agent(SupportAgent::make())
    ->monthFrom('2026-01')
    ->monthTo('2026-03')
    ->total();

// config/agent-cost.php
'agent_tracking' => [
    'table' => 'my_custom_table_name', // or set AGENT_COST_TABLE in .env
],

// config/agent-cost.php
'agent_tracking' => [
    'enabled' => false,
],

// config/agent-cost.php
'overrides' => [
    'gpt-4o' => [
        'output_cost_per_token' => 0.000008, // a negotiated rate
    ],
    'my-fine-tuned-model' => [
        'input_cost_per_token' => 0.000003,
        'output_cost_per_token' => 0.000006,
    ],
],
bash
php artisan migrate
bash
php artisan vendor:publish --tag="agent-cost-config"
bash
php artisan ai-prices:sync