mohammed94/ai-ledger 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

mohammed94/ai-ledger

Composer 安装命令:

composer require 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.

README 文档

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.

Tests Latest Version 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, 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.

统计信息

  • 总下载量: 0
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 2
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 0
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2026-06-21

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固