mohammed94 / ai-ledger
Cost & observability ledger for AI/LLM calls in Laravel — track tokens, dollar cost, latency and budgets across Prism, the official Laravel AI SDK, OpenAI and Anthropic.
Requires
- php: ^8.2
- illuminate/contracts: ^10.0|^11.0|^12.0
- illuminate/database: ^10.0|^11.0|^12.0
- illuminate/support: ^10.0|^11.0|^12.0
Requires (Dev)
- orchestra/testbench: ^8.0|^9.0|^10.0
- pestphp/pest: ^2.0|^3.0
README
The cost & observability meter for every AI/LLM call in your Laravel app — tokens, dollar cost, latency and budgets, across Prism, the official Laravel AI SDK, OpenAI and Anthropic.
Most teams ship AI features blind: nobody knows which feature — or which user — is burning the OpenAI bill until it arrives. AI Ledger is the meter for your app's AI usage. One call records a trace; you get spend reports, per-user / per-feature attribution, and budget guards that can stop a request before it blows the budget.
Why it exists: Prism, the most popular Laravel AI package, ships no built-in observability. AI Ledger fills that gap and stays provider-agnostic.
Features
- 💵 Dollar cost per call from a configurable pricing table (OpenAI, Anthropic, Gemini, …)
- 🔌 Provider-agnostic — works with Prism, the Laravel AI SDK, the OpenAI/Anthropic SDKs, or anything, through one
record()call - 🏷️ Attribution by user, tenant and feature tag — answer "who is spending my AI budget?"
- 📊 Spend reports — today, this month, by model, by user, by tag
- 🛡️ Budget guards — know when you're over budget and block calls before they cost money
- 🔒 PII-safe — prompt/response content is not stored unless you opt in
Installation
composer require mohammed94/ai-ledger php artisan vendor:publish --tag=ai-ledger-migrations php artisan vendor:publish --tag=ai-ledger-config php artisan migrate
Usage
Record a call (any provider)
use Mohammed94\AiLedger\Facades\AiLedger; AiLedger::record( provider: 'openai', model: 'gpt-4o', inputTokens: 1_200, outputTokens: 350, latencyMs: 840, tag: 'chatbot', // which feature this call belongs to userId: auth()->id(), // who triggered it );
Record straight from a provider response
recordUsage() understands the usage payloads returned by Prism, the Laravel AI SDK and the OpenAI/Anthropic SDKs — no manual token counting:
// Prism $response = Prism::text()->using('openai', 'gpt-4o')->withPrompt($prompt)->generate(); AiLedger::recordUsage($response->usage, provider: 'openai', model: 'gpt-4o', tag: 'chatbot'); // OpenAI SDK $result = OpenAI::chat()->create([...]); AiLedger::recordUsage($result->usage->toArray(), provider: 'openai', model: 'gpt-4o');
Spend reports
AiLedger::cost()->today(); // 12.46 (USD) AiLedger::cost()->thisMonth(); // 318.90 AiLedger::cost()->forUser(5)->thisMonth(); // 7.21 AiLedger::cost()->tag('chatbot')->total(); // 96.40 AiLedger::cost()->byModel(); // ['gpt-4o' => 210.5, 'gpt-4o-mini' => 12.3] AiLedger::cost()->byUser(); // ['5' => 7.21, '9' => 3.10]
Budget guards
Set ceilings in config (or via AI_LEDGER_BUDGET_MONTHLY), then gate expensive calls:
if (AiLedger::overBudget()) { abort(429, 'AI budget exhausted for this period.'); } AiLedger::remainingBudget(); // 41.60 (USD left this period)
Configuration
The published config/ai-ledger.php holds the pricing table, budgets, store driver and PII switch:
'capture_content' => env('AI_LEDGER_CAPTURE_CONTENT', false), // store raw prompt/response? 'budgets' => [ 'daily' => env('AI_LEDGER_BUDGET_DAILY'), 'monthly' => env('AI_LEDGER_BUDGET_MONTHLY'), ], 'pricing' => [ 'openai' => [ 'gpt-4o' => ['input' => 2.50, 'output' => 10.00], // USD per 1M tokens 'gpt-4o-mini' => ['input' => 0.15, 'output' => 0.60], ], // ... ],
Roadmap
- Filament dashboard plugin (charts: spend over time, by model, by user)
- Auto-capture via Prism events (zero call-site changes)
- Slack / mail alerts on budget thresholds (80% / 100%)
- Prompt-caching savings tracking
Testing
composer test
License
The MIT License (MIT). See LICENSE.