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

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

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

  [ home ](https://www.msaied.com)    [ articles ](https://www.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://www.msaied.com/articles?category=composer-pacakge) [  AI ](https://www.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%2Fwww.msaied.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%2Fwww.msaied.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://www.msaied.com/articles) 

 [ ![Octane Worker Lifecycle, State Leakage, and Memory Management in Production](https://cdn.msaied.com/554/8cc265358b47e59601a66d1e247eba9a.png) laravel octane performance 

### Octane Worker Lifecycle, State Leakage, and Memory Management in Production

Laravel Octane keeps workers alive across requests, which means static state, resolved singletons, and stale d...

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

 16 Aug 2026     3 min read  

  Read    

 ](https://www.msaied.com/articles/octane-worker-lifecycle-state-leakage-and-memory-management-in-production-2) [ ![Job Batching with Laravel Horizon: Reliable Async Workflows at Scale](https://cdn.msaied.com/553/b794b736bfd84f3cbcc6218319916544.png) laravel queues horizon 

### Job Batching with Laravel Horizon: Reliable Async Workflows at Scale

Learn how to combine Laravel's job batching API with Horizon's queue supervision to build fault-tolerant async...

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

 15 Aug 2026     3 min read  

  Read    

 ](https://www.msaied.com/articles/job-batching-with-laravel-horizon-reliable-async-workflows-at-scale) [ ![Contextual Eloquent Casts: Custom Cast Classes, Value Objects, and Inbound-Only Transforms](https://cdn.msaied.com/552/a7825c0c6f53d934f84fce522573eafb.png) laravel eloquent value-objects 

### Contextual Eloquent Casts: Custom Cast Classes, Value Objects, and Inbound-Only Transforms

Go beyond primitive casts. Learn how to build custom Eloquent cast classes that hydrate value objects, handle...

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

 15 Aug 2026     1 min read  

  Read    

 ](https://www.msaied.com/articles/contextual-eloquent-casts-custom-cast-classes-value-objects-and-inbound-only-transforms) 

   [  ![Mohamed Said](https://cdn.msaied.com/01KT78WE565VEMM3PSNQAAB0MH.png)   Mohamed Said Laravel Backend Engineer  ](https://www.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://www.msaied.com)
- [Projects](https://www.msaied.com/projects)
- [Articles](https://www.msaied.com/articles)
- [Certificates](https://www.msaied.com/certificates)
- [Contact](https://www.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)
