laravel-ai-usage

Versie
v0.1.8
Downloads
146 downloads
Licentie
Apache-2.0
GitHub

Installation

Requirements

  • PHP 8.4+
  • Laravel 12 or 13, Livewire 4
  • laravel/ai 0.9 or newer, including 1.x

Installation

composer require spaanproductions/laravel-ai-usage

Publish the migrations and run them. The ai_models table (the price list) has its own tag, so skip it if your application already has that table:

php artisan vendor:publish --tag=ai-usage-migrations
php artisan vendor:publish --tag=ai-usage-models-migrations
php artisan migrate

After an update, run the same vendor:publish commands and migrate again. Migrations you already published are skipped, so only new ones are added.

If one of the table names (ai_models, ai_usage_records, ai_usage_budget_alerts) is already taken in your application, publish the config (php artisan vendor:publish --tag=ai-usage-config) and change tables before running the migrations. The migrations and the models both read these names:

'tables' => [
    'models' => 'ai_models',
    'records' => 'ai_usage_records',
    'budget_alerts' => 'ai_usage_budget_alerts',
],

Sync the AI models and their prices. Every provider with an API key in config/ai.php is synced, plus the public OpenRouter catalogue, which is always included:

php artisan ai-usage:sync-models

Allow access to the pages by defining the gate, for example in AppServiceProvider::boot(). Until you do, the package denies everyone:

use Illuminate\Support\Facades\Gate;

Gate::define('viewAiUsage', fn ($user): bool => $user->isAdmin());

The dashboard is now available at /ai-usage.

Schedule the price sync and, if you configure a retention period (rows, content, traces or attachments), the pruning, in routes/console.php:

use Illuminate\Support\Facades\Schedule;
use SpaanProductions\LaravelAiUsage\Models\AiUsageRecord;
use SpaanProductions\LaravelAiUsage\Console\Commands\SyncAiModelsCommand;

Schedule::command(SyncAiModelsCommand::class)->weekly();
Schedule::command('model:prune', ['--model' => [AiUsageRecord::class]])->daily();

Een fout gevonden of een vraag? Maak een issue aan op GitHub.