Skip to content
packstubPublic

About

Credits and usage metering for Laravel: expiring credits, holds for AI calls, exact meters, plan allowances without a cron

Resources

Security policy

Stars

1 star

Watchers

0 watching

Forks

Repository files navigation

Credits for Laravel

Latest Version on Packagist Tests Total Downloads License Sponsor

Credits for Laravel: a Usage & credits page with the available, held and expiring balances, the lots in the order spends take them, and the ledger with a grant, a spend, its refund, a metered capture and a pending hold

Credits and usage metering for Laravel apps that charge per use: AI features, exports, SMS, API calls. Give each user or team a balance, let plans top it up every month, charge measured usage, and know, at any time, where every credit came from and where it went.

$team->credits()->grant(1_000, 'Pro plan', expiresAt: now()->endOfMonth());

$hold = $team->credits()->hold(200, 'AI turn');          // reserve before the call
$response = $agent->prompt($question);
$hold->settleUsage([                                      // charge what it actually cost
    'ai.input_tokens' => $response->usage->uncachedInputTokens(),
    'ai.output_tokens' => $response->usage->outputTokens,
]);

$team->credits()->available();                            // whole credits; a fraction carries to the next call

In php artisan tinker a team gets 500 credits and 1,000 that expire at month end, an AI turn holds 200 and settles at 137, the charge comes out of the expiring lot, then credits:audit reports the wallet balanced

Features

Integer credits throughout (pick your scale: 1 credit = 1/1000 of a cent works), no floats, SQLite, MySQL, MariaDB and Postgres.

Compatibility

Credits Laravel PHP
1.x 13.x 8.3+

SQLite with several processes writing at once needs PHP 8.4+ (Laravel's transaction_mode works only there); MySQL, MariaDB and Postgres run on 8.3 as well. See SQLite.

Installation

composer require packstub/credits
php artisan migrate

Add the trait to whatever holds credits:

use Packstub\Credits\Concerns\HasCredits;

class Team extends Model
{
    use HasCredits;
}

Schedule the catch-up for idle wallets (optional: every wallet catches up when it is used):

Schedule::command('credits:expire')->hourly();

Documentation

Testing

composer test

Security

Report vulnerabilities to security@packstub.dev, not through a public issue: see SECURITY.md, which also lists what to keep on your side of the line (wallets from the authenticated holder, holds and transactions looked up through the wallet, quantities from the provider's response).

License

MIT. See LICENSE.md.

About

Credits and usage metering for Laravel: expiring credits, holds for AI calls, exact meters, plan allowances without a cron

Resources

Security policy

Stars

1 star

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages