1. Go to this page and download the library: Download nsd7/laravel-ai-meter 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/ */
$decision = Meter::check(BudgetScope::user($id));
if (! $decision->allowed) {
// over a blocking budget — stop before the next expensive step
}
$left = Meter::remaining(BudgetScope::user($id)); // USD remaining, or null
// config/ai-meter.php
'alerts' => [
'enabled' => true,
'thresholds' => [0.8, 1.0], // alert at 80% and 100% of the limit
'notify' => [
'mail' => env('AI_METER_ALERT_MAIL'), // address or array of addresses
'slack' => env('AI_METER_ALERT_SLACK_WEBHOOK'), // Slack incoming-webhook URL
],
],
use Nsd7\AiMeter\Facades\Meter;
use Nsd7\AiMeter\Core\Budget\BudgetScope;
$run = Meter::run(BudgetScope::user(auth()->id()), [
'label' => 'support-agent',
'budget' => 2.00, // USD for this whole run
'max_tool_calls' => 10, // runaway-loop cap
'max_wall_clock' => 120, // seconds
]);
try {
while ($agent->thinking()) {
$run->guard(); // throws before the next step if over a cap/budget
$response = $agent->step();
$run->recordPrism($response); // or $run->record([...]) for tool calls
}
$run->finish();
} catch (\Nsd7\AiMeter\Runs\Exceptions\RunLimitExceededException
| \Nsd7\AiMeter\Budget\Exceptions\BudgetExceededException $e) {
$run->fail($e->getMessage()); // loop stopped safely
}
// config/ai-meter.php
'spend_store' => ['driver' => 'cache'], // or AI_METER_SPEND_STORE=cache
bash
php artisan ai-meter:prune # delete call records older than prune.keep_days
php artisan ai-meter:update-prices # refresh the price book so token costs don't drift
php artisan ai-meter:export # export the (filtered) call log as CSV or JSON