ajooda/laravel-ai-metering
Composer 安装命令:
composer require ajooda/laravel-ai-metering
包简介
AI usage metering and billing for Laravel apps (tokens, quotas, credits, billing).
README 文档
README
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.
Table of Contents
- Features
- Why Choose This Package?
- Requirements
- Quick Start
- Documentation
- Quick Links
- Contributing
- License
- Support
Features
- 🎯 Simple Developer Experience - Fluent API for metering AI usage
- 📊 Usage Tracking - Automatic token and cost tracking
- 🚦 Quota Management - Configurable limits (tokens, cost, per-plan, per-tenant)
- 💳 Billing Integration - Stripe/Cashier support with credit-based and subscription modes
- 🏢 Multi-Tenancy - Works with or without tenancy packages
- 🔌 Provider Agnostic - Support for OpenAI, Anthropic, and custom providers
- ⚡ Performance - Caching, queue support, and optimized queries
Why Choose This Package?
- Zero Configuration Required - Start tracking usage immediately, no plans or subscriptions needed
- Production Ready - Battle-tested with comprehensive test coverage and security features
- Automatic Processing - Automatic usage extraction, cost calculation, and billing integration
- Flexible Billing - Support for both subscription-based and credit-based billing models
- Multi-Tenant Ready - Built-in support for multi-tenant applications without coupling to specific packages
- Extensible - Easy to add custom providers, billing drivers, and tenant resolvers
- Well Documented - Comprehensive documentation with examples and troubleshooting guides
Requirements
- PHP >= 8.1 (PHP >= 8.2 for Laravel 11.x and 12.x)
- Laravel 10.x, 11.x, or 12.x
- Database (MySQL, PostgreSQL, SQLite, or SQL Server)
Version Compatibility
| 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 |
Optional Dependencies
- OpenAI:
openai-php/laravel(for OpenAI provider) - Anthropic:
anthropic-php/sdk(for Anthropic provider) - Stripe/Cashier:
laravel/cashier(for Stripe billing integration)
Quick Start
Note: This package supports Laravel Package Auto-Discovery, so you do not need to manually register the service provider.
Installation
# 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
Usage Example
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.
Documentation
Comprehensive documentation is available in the docs/ directory:
- Overview - What this package does and when to use it
- Installation - Detailed installation and setup guide
- Configuration - All configuration options explained
- Usage Guide - Common usage flows and examples
- Features - Detailed feature documentation
- Plans & Quotas - Setting up usage limits
- Credits - Credit-based billing
- Billing Integration - Stripe/Cashier integration
- Multi-Tenancy - Multi-tenant usage tracking
- Periods - Period configuration
- Advanced Topics - Extensibility, customization, events, hooks
- API Reference - Complete API documentation
- Troubleshooting - Common issues and solutions
Quick Links
Common Tasks
- Setting up plans and quotas
- Credit-based billing
- Stripe integration
- Multi-tenant setup
- Using middleware
- Querying usage
Artisan Commands
# 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.
Contributing
Contributions are welcome! Please feel free to submit a Pull Request.
Quick Start for Contributors
-
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.
License
The MIT License (MIT). Please see LICENSE for more information.
Support
Getting Help
- 🐛 Bug Reports: Open an issue
- 💬 Questions: GitHub Discussions
- 📖 Documentation: Full Documentation
- 🔒 Security Issues: Email abdalhadijouda@gmail.com or see SECURITY.md
Maintainers
Primary Maintainer: AbdAlhadi Jouda (abdalhadijouda@gmail.com)
Response Times:
- Security vulnerabilities: 48 hours initial response
- Bug reports: 3-5 business days
- Questions: 5-7 business days
- Feature requests: As time permits
ajooda/laravel-ai-metering 适用场景与选型建议
ajooda/laravel-ai-metering 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 1 次下载、GitHub Stars 达 3, 最近一次更新时间为 2025 年 12 月 09 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「laravel」 「billing」 「ai」 「openai」 「anthropic」 「metering」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 ajooda/laravel-ai-metering 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 ajooda/laravel-ai-metering 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 ajooda/laravel-ai-metering 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
PHP 8.0+ OpenAI API client with fully typed/documented requests+responses models, guzzle and symfony/http-client support and async/parallel requests.
Laravel Cashier provides an expressive, fluent interface to Braintree's subscription billing services.
Metamel Addresses is a polymorphic Laravel package, for address book management. You can add addresses to any eloquent model with ease.
Estimate AI API costs before making expensive calls
A powerful package that seamlessly integrates OpenAI's advanced AI capabilities into your Laravel applications. This package offers quick setup and intuitive configuration to leverage AI models for chat, embeddings, and more.
统计信息
- 总下载量: 1
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 3
- 点击次数: 28
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2025-12-09