USAIGE: Laravel AI SDK Token &amp; Cost Tracking | Mohamed Said        [  ![Mohamed Said](https://cdn.msaied.com/01KT78WE565VEMM3PSNQAAB0MH.png)   Mohamed Said Laravel Backend Engineer  ](https://msaied.com) [ Home ](https://msaied.com) [ Projects ](https://msaied.com/projects) [ Articles  ](https://msaied.com/articles) [ Certificates ](https://msaied.com/certificates) [ Contact ](https://msaied.com#contact-section) 

       [  ](https://github.com/EG-Mohamed)       

 [ Home ](https://msaied.com) [ Projects ](https://msaied.com/projects) [ Articles ](https://msaied.com/articles) [ Certificates ](https://msaied.com/certificates) [ Contact ](https://msaied.com#contact-section) 

  [ home ](https://msaied.com)    [ articles ](https://msaied.com/articles)    USAIGE: Track Token Usage and Costs for Laravel AI SDK Requests        On this page       1. [  What Is USAIGE? ](#what-is-usaige)
2. [  Two Helpers, Three Lines of Code ](#two-helpers-three-lines-of-code)
3. [  Provider, Model, and Cost Resolution ](#provider-model-and-cost-resolution)
4. [  User Tracking and Metadata ](#user-tracking-and-metadata)
5. [  Built-In Dashboard ](#built-in-dashboard)
6. [  Installation ](#installation)
7. [  Key Takeaways ](#key-takeaways)

  ![USAIGE: Track Token Usage and Costs for Laravel AI SDK Requests](https://cdn.msaied.com/298/4bde9881e94264e1805ff443a90199d3.png)

 [  Composer Pacakge ](https://msaied.com/articles?category=composer-pacakge) [  AI ](https://msaied.com/articles?category=ai)  #Laravel   #AI   #Laravel AI SDK   #Observability   #Token Tracking   #Composer Package  

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

     26 Jun 2026      3 min read    ![Mohamed Said](https://cdn.msaied.com/01KT78WE565VEMM3PSNQAAB0MJ.jpg)  Mohamed Said  

       Table of contents

1. [  01   What Is USAIGE?  ](#what-is-usaige)
2. [  02   Two Helpers, Three Lines of Code  ](#two-helpers-three-lines-of-code)
3. [  03   Provider, Model, and Cost Resolution  ](#provider-model-and-cost-resolution)
4. [  04   User Tracking and Metadata  ](#user-tracking-and-metadata)
5. [  05   Built-In Dashboard  ](#built-in-dashboard)
6. [  06   Installation  ](#installation)
7. [  07   Key Takeaways  ](#key-takeaways)

 What Is USAIGE?
---------------

As AI features become standard in Laravel applications, keeping tabs on token consumption and API spend quickly becomes a real operational concern. [USAIGE](https://github.com/ludoguenet/usaige) is a Laravel package that attaches observability directly to the [Laravel AI SDK](https://github.com/laravel/ai). It records every AI request as a "run" — capturing token counts, USD costs, provider and model details, request timing, and error status — then surfaces everything through a built-in web dashboard at `/usaige`.

Two Helpers, Three Lines of Code
--------------------------------

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

```php
$run = ai_run('summarize-document');

$response = Ai::text('Summarize: ' . $document->content);

$usage = ai_usage($run, $response);

```

USAIGE automatically detects the response shape, handling responses from the Laravel AI SDK, the OpenAI PHP SDK, and plain arrays without extra configuration. If none of those shapes match, you can pass token counts directly:

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

```

Provider, Model, and Cost Resolution
------------------------------------

USAIGE reads `config/ai.php` to populate the provider and model on each run automatically. Both values can be overridden per call, or you can pass a `Lab` enum directly:

```php
$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` Eloquent model.

User Tracking and Metadata
--------------------------

By default, each run is associated with `auth()->id()`. You can override the resolver globally when the default does not fit your application's auth model:

```php
use Laraveljutsu\Usaige\Facades\Usaige;

Usaige::resolveUsersUsing(fn () => auth()->user()?->team_id);

```

Runs also accept arbitrary JSON metadata, which is useful for attaching tenant identifiers, ticket references, or any other contextual data:

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

```

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

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

```

Built-In Dashboard
------------------

The package registers a dashboard at `/usaige` listing 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 simple callback:

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

```

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

Installation
------------

USAIGE requires PHP 8.5+, Laravel 11+, and `laravel/ai ^0.8.1`:

```bash
composer require laraveljutsu/usaige
php artisan migrate

```

Key Takeaways
-------------

- Two global helpers (`ai_run()` and `ai_usage()`) are all you need to start tracking.
- Automatically detects response shapes from the Laravel AI SDK, OpenAI PHP SDK, and plain arrays.
- Stores costs with sub-cent precision, queryable by feature, user, model, or date.
- Supports per-run metadata and custom user resolvers for multi-tenant or team-based apps.
- Ships with a built-in `/usaige` dashboard protected by configurable middleware.
- Explicit failure recording keeps your observability data complete even when AI calls throw errors.

Find the source code and full documentation on [GitHub](https://github.com/ludoguenet/usaige).

---

*Source: [Laravel News — USAIGE: Track Token Usage and Costs for Laravel AI SDK Requests](https://laravel-news.com/usaige-track-token-usage-and-costs-for-laravel-ai-sdk-requests)*

 Found this useful?

          [  ](https://twitter.com/intent/tweet?url=https%3A%2F%2Fmsaied.com%2Farticles%2Fusaige-track-token-usage-and-costs-for-laravel-ai-sdk-requests&text=USAIGE%3A+Track+Token+Usage+and+Costs+for+Laravel+AI+SDK+Requests) [  ](https://www.linkedin.com/sharing/share-offsite/?url=https%3A%2F%2Fmsaied.com%2Farticles%2Fusaige-track-token-usage-and-costs-for-laravel-ai-sdk-requests) 

 Frequently Asked Questions 
----------------------------

  3 questions  

     Q01  Does USAIGE work with AI providers other than OpenAI?        USAIGE reads provider and model information from `config/ai.php` and supports overriding both per call. It automatically detects response shapes from the Laravel AI SDK and the OpenAI PHP SDK, and you can pass token counts manually for any other provider. 

      Q02  How do I restrict access to the USAIGE dashboard?        Access is controlled through middleware settings in `config/usaige.php`. You can also pass a callback via `Usaige::auth(fn ($request) =&gt; $request-&gt;user()?-&gt;isAdmin())` to apply custom authorization logic. 

      Q03  What happens if my AI call fails before ai\_usage() is called?        You can record the failure explicitly by calling `$run-&gt;fail('Your error message')`, ensuring the run is still logged with an error status rather than left incomplete in the database. 

  Continue reading

 More Articles 
---------------

 [ View all    ](https://msaied.com/articles) 

 [ ![Help Make Filament Faster: Beta Versions of v4 and v5 Now Available for Testing](https://cdn.msaied.com/299/b7163ad1d319ebdd0e7128cc976053bf.png) Filament Laravel Performance 

### Help Make Filament Faster: Beta Versions of v4 and v5 Now Available for Testing

The Filament team has released performance-focused beta versions of both v4 and v5. Install them today, test i...

  ![Mohamed Said](https://cdn.msaied.com/01KT78WE565VEMM3PSNQAAB0MJ.jpg)  Mohamed Said 

 26 Jun 2026     3 min read  

  Read    

 ](https://msaied.com/articles/help-make-filament-faster-beta-versions-of-v4-and-v5-now-available-for-testing) [ ![Filament v4 Schema-Based Forms, Infolists, and the Unified Schema API](https://cdn.msaied.com/297/6eb3a7aaf7148fd21116eea870bd004e.png) filament laravel filament-v4 

### Filament v4 Schema-Based Forms, Infolists, and the Unified Schema API

Filament v4 replaces scattered form and infolist definitions with a single Schema API. Learn how unified schem...

  ![Mohamed Said](https://cdn.msaied.com/01KT78WE565VEMM3PSNQAAB0MJ.jpg)  Mohamed Said 

 26 Jun 2026     3 min read  

  Read    

 ](https://msaied.com/articles/filament-v4-schema-based-forms-infolists-and-the-unified-schema-api-1) [ ![Laravel Pipeline Pattern: Building Custom Pipelines Beyond Middleware](https://cdn.msaied.com/296/afbac95c7f4aac1cee83eb2c87541369.png) laravel pipeline clean-architecture 

### Laravel Pipeline Pattern: Building Custom Pipelines Beyond Middleware

The Pipeline pattern in Laravel is far more powerful than middleware alone. Learn how to build typed, composab...

  ![Mohamed Said](https://cdn.msaied.com/01KT78WE565VEMM3PSNQAAB0MJ.jpg)  Mohamed Said 

 26 Jun 2026     4 min read  

  Read    

 ](https://msaied.com/articles/laravel-pipeline-pattern-building-custom-pipelines-beyond-middleware-1) 

   [  ![Mohamed Said](https://cdn.msaied.com/01KT78WE565VEMM3PSNQAAB0MH.png)   Mohamed Said Laravel Backend Engineer  ](https://msaied.com)Senior Backend Engineer specializing in Laravel, scalable SaaS platforms, APIs, and cloud infrastructure. I build secure, high-performance web applications that help businesses grow.

Explore

- [Home](https://msaied.com)
- [Projects](https://msaied.com/projects)
- [Articles](https://msaied.com/articles)
- [Certificates](https://msaied.com/certificates)
- [Contact](https://msaied.com#contact-section)

Connect

- [   hello@msaied.com ](mailto:hello@msaied.com)
- [   +20 109 461 9204 ](tel:+201094619204)

© 2026 Mohamed Said. All rights reserved.

 [  ](https://github.com/EG-Mohamed) [  ](https://www.linkedin.com/in/msaiedm/) [  ](https://wa.me/201094619204) [  ](mailto:hello@msaied.com) [  ](https://drive.google.com/file/u/0/d/1MF20IPRJyzfy32mhEutjL5EpSls0w2Q8/view)
