AI usage metering and billing for Laravel apps (tokens, quotas, credits, billing).
ajooda/laravel-ai-metering is a Laravel package for ai usage metering and billing for laravel apps (tokens, quotas, credits, billing)..
It currently has 3 GitHub stars and 1 downloads on Packagist (latest version v1.0.0).
Install it with composer require ajooda/laravel-ai-metering.
Discover more Laravel packages by ajooda
or browse all Laravel packages to compare alternatives.
Last updated
Usage metering, quotas, and billing for AI applications.
A production-ready Laravel package that automatically tracks AI usage, enforces quotas, and integrates with Stripe for billing. Perfect for SaaS applications that need to meter and bill AI usage.
| Laravel Version | PHP Version | Package Version | Status | |----------------|-------------|----------------|--------| | 10.x | >= 8.1 | ^1.0 | ✅ Supported | | 11.x | >= 8.2 | ^1.0 | ✅ Supported | | 12.x | >= 8.2 | ^1.0 | ✅ Supported |
openai-php/laravel (for OpenAI provider)anthropic-php/sdk (for Anthropic provider)laravel/cashier (for Stripe billing integration)Note: This package supports Laravel Package Auto-Discovery, so you do not need to manually register the service provider.
# 1. Install the package
composer require ajooda/laravel-ai-metering
# 2. Publish and run migrations
php artisan vendor:publish --tag=ai-metering-migrations
php artisan migrate
# 3. Verify installation
php artisan ai-metering:validate
Full Example - Complete usage with all options:
use Ajooda\AiMetering\Facades\AiMeter;
use OpenAI\Laravel\Facades\OpenAI;
$response = AiMeter::forUser(auth()->user())
->billable(auth()->user())
->usingProvider('openai', 'gpt-4o-mini')
->feature('chat')
->call(function () {
return OpenAI::chat()->create([
'model' => 'gpt-4o-mini',
'messages' => [
['role' => 'user', 'content' => 'Hello!'],
],
]);
});
// Access the AI response
$aiResponse = $response->getResponse();
// Check usage
$usage = $response->getUsage();
echo "Tokens: {$usage->totalTokens}, Cost: \${$usage->totalCost}";
Minimal Example - Just track usage:
use Ajooda\AiMetering\Facades\AiMeter;
$response = AiMeter::forUser(auth()->user())
->billable(auth()->user())
->usingProvider('openai', 'gpt-4o-mini')
->call(fn() => OpenAI::chat()->create([...]));
$usage = $response->getUsage();
That's it! Usage is automatically tracked. No plans or subscriptions required for basic tracking.
Comprehensive documentation is available in the docs/ directory:
# Generate usage report
php artisan ai-metering:report
# Cleanup old usage records
php artisan ai-metering:cleanup
# Sync Stripe overages
php artisan ai-metering:sync-stripe-overages
# Validate configuration
php artisan ai-metering:validate
# Migrate plan
php artisan ai-metering:migrate-plan "App\Models\User" 1 "pro-plan"
# List plans
php artisan ai-metering:sync-plans
See API Reference for complete command documentation.
Contributions are welcome! Please feel free to submit a Pull Request.
Fork and clone the repository:
git clone https://github.com/ajooda/laravel-ai-metering.git
cd laravel-ai-metering
Install dependencies:
composer install
Run tests:
composer test
Run tests with coverage:
composer test-coverage
See CONTRIBUTING.md for detailed guidelines.
The MIT License (MIT). Please see LICENSE for more information.
Primary Maintainer: AbdAlhadi Jouda ([email protected])
Response Times: