fr3on / laravel-ledger
Token cost accounting and budget enforcement for AI-heavy Laravel applications. Supports OpenAI, Anthropic, Gemini, and more.
0.0.1
2026-04-12 13:42 UTC
Requires
- php: ^8.2
- illuminate/support: ^10.0|^11.0|^12.0|^13.0
Requires (Dev)
- laravel/pint: ^1.0
- orchestra/testbench: ^8.0|^9.0|^10.0
- pestphp/pest: ^2.34|^3.0
README
Token cost accounting for AI-heavy Laravel applications. Tracks spend per user, per feature, and per model. Enforces budgets before calls are made.
Installation
composer require fr3on/laravel-ledger php artisan migrate
Usage
Wrap your AI calls with the Ledger facade:
use Fr3on\Ledger\Facades\Ledger; $result = Ledger::for(user: $user, feature: 'chat') ->budget()->dailyLimit(tokens: 50_000)->orFail() ->track(fn() => $ai->generate($prompt));
Expected Result
The ledger tracks token usage and calculates costs based on your pricing map. You can then generate reports:
php artisan ledger:report --period=month
Output Example:
Ledger Report — April 2026
Feature Requests Tokens Cost (USD)
──────────────────────────────────────────────────────
chat 1,240 4,820,000 $72.30
summarize 380 940,000 $14.10
embed 2,100 210,000 $4.20
──────────────────────────────────────────────────────
TOTAL 3,720 5,970,000 $90.60
Configuration
Publish the config file:
php artisan vendor:publish --tag=ledger-config
Define your model pricing and token drivers in config/ledger.php.