PHPackages                             mohammed94/ai-ledger - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Utility &amp; Helpers](/categories/utility)
4. /
5. mohammed94/ai-ledger

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

mohammed94/ai-ledger
====================

Cost &amp; 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.

v0.1.0(1mo ago)00MITPHP ^8.2

Since Jun 21Compare

[ Source](https://github.com/mohammed1981994/ai-ledger)[ Packagist](https://packagist.org/packages/mohammed94/ai-ledger)[ Docs](https://github.com/mohammed1981994/ai-ledger)[ RSS](/packages/mohammed94-ai-ledger/feed)WikiDiscussions Synced 2w ago

READMEChangelogDependencies (5)Versions (2)Used By (0)

AI Ledger
=========

[](#ai-ledger)

> The cost &amp; 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.

[![Tests](https://camo.githubusercontent.com/cbea254072688a33647eda01ea28fd57fc2bd3c51c1df8a35801b4014e0bb027/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f616374696f6e732f776f726b666c6f772f7374617475732f6d6f68616d6d6564313938313939342f61692d6c65646765722f63692e796d6c3f6272616e63683d6d61696e266c6162656c3d7465737473)](https://github.com/mohammed1981994/ai-ledger/actions)[![Latest Version](https://camo.githubusercontent.com/5268fdcff2cc77bc0491e2d970584611a2e278deb31e1ecdfeb02b23c2c32f77/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f6d6f68616d6d656439342f61692d6c6564676572)](https://packagist.org/packages/mohammed94/ai-ledger)[![License](https://camo.githubusercontent.com/c6653316b02b8cfb79d767928e6d6fb86aa2ee1ac10d5fdd2d063bc435d65793/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f6c2f6d6f68616d6d656439342f61692d6c6564676572)](LICENSE)

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](https://prismphp.com), the most popular Laravel AI package, ships no built-in observability. AI Ledger fills that gap and stays provider-agnostic.

Features
--------

[](#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
------------

[](#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
-----

[](#usage)

### Record a call (any provider)

[](#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

[](#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

[](#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

[](#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
-------------

[](#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
-------

[](#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
-------

[](#testing)

```
composer test
```

License
-------

[](#license)

The MIT License (MIT). See [LICENSE](LICENSE).

###  Health Score

34

—

LowBetter than 74% of packages

Maintenance90

Actively maintained with recent releases

Popularity0

Limited adoption so far

Community2

Small or concentrated contributor base

Maturity36

Early-stage or recently created project

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Unknown

Total

1

Last Release

45d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/c2807f189686982f165d2b203d97a35cff07a7ce9850fc24c949b7d943ef8e8b?d=identicon)[mohammed1981994](/maintainers/mohammed1981994)

---

Tags

laraveltokensaiopenaiprismobservabilityllmanthropiccostfinops

###  Code Quality

TestsPest

### Embed Badge

![Health badge](/badges/mohammed94-ai-ledger/health.svg)

```
[![Health](https://phpackages.com/badges/mohammed94-ai-ledger/health.svg)](https://phpackages.com/packages/mohammed94-ai-ledger)
```

###  Alternatives

[psalm/plugin-laravel

Psalm plugin for Laravel

3355.4M352](/packages/psalm-plugin-laravel)[laravel/ai

The official AI SDK for Laravel.

1.1k4.6M277](/packages/laravel-ai)[api-platform/laravel

API Platform support for Laravel

58174.6k18](/packages/api-platform-laravel)[pressbooks/pressbooks

Pressbooks is an open source book publishing tool built on a WordPress multisite platform. Pressbooks outputs books in multiple formats, including PDF, EPUB, web, and a variety of XML flavours, using a theming/templating system, driven by CSS.

45844.8k1](/packages/pressbooks-pressbooks)[spectra-php/laravel-spectra

Comprehensive observability for AI/LLM operations in Laravel applications

182.9k](/packages/spectra-php-laravel-spectra)[forjedio/inertia-table

Backend-driven dynamic tables for Laravel + Inertia.js

272.0k](/packages/forjedio-inertia-table)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
