USAIGE: Track Token Usage and Costs for Laravel AI SDK Requests

Published on by

USAIGE: Track Token Usage and Costs for Laravel AI SDK Requests image

USAIGE is a Laravel package that attaches observability to the Laravel AI SDK. It records every AI request as a run with token counts, costs, provider and model details, timing, and error status — then surfaces all of it through a built-in web dashboard.

Two Helpers, Three Lines

The integration is built around two global helpers. ai_run() opens a tracking context tied to a feature identifier, and ai_usage() records what the SDK response consumed:

$run = ai_run('summarize-document');
$response = Ai::text('Summarize: ' . $document->content);
$usage = ai_usage($run, $response);

The package detects the response shape automatically. It handles responses from the Laravel AI SDK, the OpenAI PHP SDK, and plain arrays, pulling token counts without any extra configuration. When none of those shapes match, you can pass the counts directly:

$usage = ai_usage($run, promptTokens: 200, completionTokens: 80);

Provider, Model, and Cost Resolution

USAIGE reads config/ai.php to fill in the provider and model on each run. Both can be overridden per call or by passing a Lab enum directly:

$run = ai_run('classify-ticket', model: 'gpt-4o-mini', provider: 'openai');

Costs are stored with sub-cent precision. The ai_usages table keeps both prompt and completion token counts alongside the USD total per run, so you can query spend by feature, user, model, or date range using the AiRun model.

User Tracking and Metadata

By default, the run is associated with auth()->id(). You can override this globally when the default resolver does not fit:

use Laraveljutsu\Usaige\Facades\Usaige;
 
Usaige::resolveUsersUsing(fn () => auth()->user()?->team_id);

Or pass a user ID per call. Runs also accept arbitrary JSON metadata for attaching context like ticket IDs or tenant identifiers:

$run = ai_run('generate-report', metadata: [
'tenant_id' => $tenant->id,
'ticket' => 'PROJ-1042',
]);

When an AI call fails before ai_usage() runs, you can record the failure explicitly:

$run->fail('Rate limit exceeded');

Dashboard

The package registers a dashboard at /usaige that lists all runs with their status, provider, model, token counts, costs, and duration. Access is controlled through middleware configuration in config/usaige.php, or with a callback:

Usaige::auth(fn ($request) => $request->user()?->isAdmin());

The dashboard path, middleware, and database table names are all configurable through the published config file.

Installation

The package requires PHP 8.5+, Laravel 11+, and laravel/ai ^0.8.1:

composer require laraveljutsu/usaige
php artisan migrate

You can find the source and full documentation on GitHub.

Paul Redmond photo

Staff writer at Laravel News. Full stack web developer and author.

Cube

Laravel Newsletter

Join 40k+ other developers and never miss out on new tips, tutorials, and more.

image
Tinkerwell

Enjoy coding and debugging in an editor designed for fast feedback and quick iterations. It's like a shell for your application – but with multi-line editing, code completion, and more.

Visit Tinkerwell
Lucky Media logo

Lucky Media

Get Lucky Now - the ideal choice for Laravel Development, with over a decade of experience!

Lucky Media
PhpStorm logo

PhpStorm

The go-to PHP IDE with extensive out-of-the-box support for Laravel and its ecosystem.

PhpStorm
No Compromises logo

No Compromises

Joel and Aaron, the two seasoned devs from the No Compromises podcast, are now available to hire for your Laravel project. ⬧ Flat rate of $9500/mo. ⬧ No lengthy sales process. ⬧ No contracts. ⬧ 100% money back guarantee.

No Compromises
Tinkerwell logo

Tinkerwell

The must-have code runner for Laravel developers. Tinker with AI, autocompletion and instant feedback on local and production environments.

Tinkerwell
Laravel Cloud logo

Laravel Cloud

Easily create and manage your servers and deploy your Laravel applications in seconds.

Laravel Cloud
Kirschbaum logo

Kirschbaum

Providing innovation and stability to ensure your web application succeeds.

Kirschbaum
Acquaint Softtech logo

Acquaint Softtech

Acquaint Softtech offers AI-ready Laravel developers who onboard in 48 hours at $3000/Month with no lengthy sales process and a 100 percent money-back guarantee.

Acquaint Softtech
SaaSykit: Laravel SaaS Starter Kit logo

SaaSykit: Laravel SaaS Starter Kit

SaaSykit is a Multi-tenant Laravel SaaS Starter Kit that comes with all features required to run a modern SaaS. Payments, Beautiful Checkout, Admin Panel, User dashboard, Auth, Ready Components, Stats, Blog, Docs and more.

SaaSykit: Laravel SaaS Starter Kit
Shift logo

Shift

Running an old Laravel version? Instant, automated Laravel upgrades and code modernization to keep your applications fresh.

Shift
Harpoon: Next generation time tracking and invoicing logo

Harpoon: Next generation time tracking and invoicing

The next generation time-tracking and billing software that helps your agency plan and forecast a profitable future.

Harpoon: Next generation time tracking and invoicing

The latest

View all →
Turn PHP Attributes Into Docs With Signal image

Turn PHP Attributes Into Docs With Signal

Read article
Help make Filament faster! image

Help make Filament faster!

Read article
Yammi Audit Log: Track Who Really Made a Change Across Jobs and Queues image

Yammi Audit Log: Track Who Really Made a Change Across Jobs and Queues

Read article
Route Metadata Support in Laravel 13.17 image

Route Metadata Support in Laravel 13.17

Read article
Ship AI with Laravel: Failover, Queues, and Middleware for AI Agents image

Ship AI with Laravel: Failover, Queues, and Middleware for AI Agents

Read article
Monitor and Control Schedules, Queues, and Errors in Laravel with Watchtower image

Monitor and Control Schedules, Queues, and Errors in Laravel with Watchtower

Read article