Download the PHP package backtik-ch/laravel-ai-usage without Composer
On this page you can find all versions of the php package backtik-ch/laravel-ai-usage. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download backtik-ch/laravel-ai-usage
More information about backtik-ch/laravel-ai-usage
Files in backtik-ch/laravel-ai-usage
Package laravel-ai-usage
Short Description Track and log AI usage (tokens, cost, duration) for any Laravel application, with optional auto-discovery for laravel/ai SDK.
License MIT
Informations about the package laravel-ai-usage
Laravel AI Usage
Track and log AI usage (tokens, cost, duration) for any Laravel application, with optional auto-discovery for the laravel/ai SDK and an optional Filament panel integration.
Requirements
- PHP 8.3+
- Laravel 12.x
Installation
Publish the config file and run the migrations:
How It Works
This package hooks into the laravel/ai SDK lifecycle via two events:
PromptingAgent— fired when an agent starts. The package creates a pending record with the agent class, label, and an invocation ID.AgentPrompted— fired when the agent completes. The package locates the pending record, fills in token counts, duration, model/driver info, and snapshots the token prices from your config.
The result is a complete, immutable log of every AI call — no manual code required if you use laravel/ai.
If you're not using laravel/ai, you can log calls manually via the fluent AiUsage facade (see Manual logging).
Configuration
The config file (config/ai-usage.php) exposes the following options:
| Key | Default | Description |
|---|---|---|
table |
ai_usage |
Database table name |
queue_logs |
false |
Dispatch log writes as queued jobs |
log_system_prompt |
false |
Store system prompts in the log |
log_response_text |
true |
Store AI response bodies |
max_text_length |
10_000 |
Truncate prompt/response text (chars); null disables |
stale_timeout_minutes |
60 |
Minutes before a processing record is considered stale |
auto_discover |
true |
Auto-listen to laravel/ai events |
prices |
(see below) | Token prices (USD / 1 M tokens) per driver & model |
Token prices
Prices are snapshotted into each log row at write time so historical cost estimates remain accurate after you update the config.
Supported price keys: prompt, completion, cache_read, cache_write, reasoning.
The package ships with indicative prices for common OpenAI, Anthropic and Gemini models. Always verify against your provider's current pricing page.
Usage
[!TIP] Using
laravel/ai? You're already done. If auto-discovery is enabled (default) andlaravel/aiis installed, every AI agent call is logged automatically — no code needed. The sections below are for manual logging or custom integrations.
Auto-discovery mode
When auto_discover is true (default) and laravel/ai is installed, the package automatically listens for Laravel\Ai\Events\PromptingAgent and Laravel\Ai\Events\AgentPrompted. A pending record is created when the agent starts, then updated with tokens, duration, model info, and cost data when the agent finishes.
To disable auto-discovery, set auto_discover to false in config/ai-usage.php.
Manual logging
Use the AiUsage facade with a fluent builder:
Attach to a model (polymorphic owner)
Override prices at log time
If costPrices() is not called, prices are resolved automatically from config('ai-usage.prices').
How cost estimation works
Token prices (USD per 1 million tokens) are snapshotted from your config at log time and stored alongside each record. This means historical cost data remains accurate even when you update prices later.
The AiUsageLog model exposes an estimated_cost computed attribute derived from the stored token counts and prices:
Returns null when no price data is available (i.e., the model isn't listed in your prices config).
To add a new model, simply add its prices to config('ai-usage.prices.{driver}.{model}'). Supported price keys: prompt, completion, cache_read, cache_write, reasoning.
Status values
Logs use the AiUsageStatus enum: pending, processing, completed, failed.
Filament integration
Requires filament/filament. Register the plugin in your panel provider:
This registers:
AiUsageResource— browsable, filterable list of all log entries with a detail view.AiUsageStatsWidget— stats overview: total calls, prompt/completion tokens, average duration, failed count.
Summary widget (standalone)
AiUsageSummaryWidget is a full-width Livewire widget with a period selector and per-driver / per-model token breakdowns. Add it to any page:
Artisan Commands
Prune old records
Mark stale records as failed
If an AI call throws an exception, the laravel/ai SDK never fires AgentPrompted, so the record stays in processing status indefinitely. Run this command on a schedule to mark those stale records as failed:
The timeout is controlled by stale_timeout_minutes in your config (default: 60). You can also override it per-call:
Add both commands to your scheduler in routes/console.php:
Testing
License
MIT — see LICENSE.
All versions of laravel-ai-usage with dependencies
illuminate/support Version ^12.0
illuminate/database Version ^12.0
illuminate/console Version ^12.0
illuminate/events Version ^12.0