PHPackages                             mubseoul/laravel-llm-observability - 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. [Logging &amp; Monitoring](/categories/logging)
4. /
5. mubseoul/laravel-llm-observability

ActiveLibrary[Logging &amp; Monitoring](/categories/logging)

mubseoul/laravel-llm-observability
==================================

Laravel-native observability and cost tracking toolkit for LLM calls (OpenAI, Claude, etc.)

v1.0.0(4mo ago)00MITPHPPHP ^8.2CI failing

Since Feb 21Pushed 4mo agoCompare

[ Source](https://github.com/mubseoul/laravel-llm-observability)[ Packagist](https://packagist.org/packages/mubseoul/laravel-llm-observability)[ RSS](/packages/mubseoul-laravel-llm-observability/feed)WikiDiscussions main Synced today

READMEChangelog (1)Dependencies (12)Versions (2)Used By (0)

Laravel LLM Observability
=========================

[](#laravel-llm-observability)

[![Latest Version on Packagist](https://camo.githubusercontent.com/40326c88805b8df0f48daff1496077fd30333d9310c29331095a7cd643938a40/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f6d756273656f756c2f6c61726176656c2d6c6c6d2d6f62736572766162696c6974792e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/mubseoul/laravel-llm-observability)[![GitHub Tests Action Status](https://camo.githubusercontent.com/5bf272bb1d40e90c0eeb6a7f536b286592f7d6bba51673e6de6a89e57da844ec/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f616374696f6e732f776f726b666c6f772f7374617475732f6d756273656f756c2f6c61726176656c2d6c6c6d2d6f62736572766162696c6974792f63692e796d6c3f6272616e63683d6d61696e266c6162656c3d7465737473267374796c653d666c61742d737175617265)](https://github.com/mubseoul/laravel-llm-observability/actions?query=workflow%3Aci+branch%3Amain)[![Total Downloads](https://camo.githubusercontent.com/c50d295cfd5763f6183cd91fa746f640f8d5834e66293783d1e01853141c61cc/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f6d756273656f756c2f6c61726176656c2d6c6c6d2d6f62736572766162696c6974792e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/mubseoul/laravel-llm-observability)

A Laravel-native observability and cost tracking toolkit for LLM calls (OpenAI, Claude, Ollama, and more). Track every LLM request in your Laravel application with detailed metrics, enforce quotas, monitor costs, and gain insights through a beautiful Filament dashboard.

Features
--------

[](#features)

✨ **Comprehensive Tracking**

- Track tokens, latency, provider/model, cost estimates, and success/failure status
- User and team attribution for multi-tenant applications
- Detailed metadata capture (environment, route, IP, user agent)
- Optional storage of request/response bodies (with privacy controls)

💰 **Cost Management**

- Built-in pricing tables for OpenAI, Anthropic, and other providers
- Automatic cost calculation based on token usage
- Custom pricing providers for specialized models
- Cost aggregation by user, team, time period, or globally

🚦 **Quota Enforcement**

- Request, token, and cost limits (per day/month)
- Scoped quotas (global, per-user, per-team, per-API-key)
- Automatic quota resets via scheduled jobs
- Middleware for blocking requests when quotas exceeded

📊 **Filament Dashboard**

- Overview page with key metrics (requests, costs, latency, error rates)
- Detailed request logs with filtering and search
- Usage analytics by provider, model, user, and team
- Quota management interface
- Alert rule configuration

🔔 **Alerts &amp; Webhooks**

- Configurable alert rules (cost thresholds, error rates, latency spikes)
- Webhook delivery with retry logic
- Laravel Notifications integration (mail, Slack, etc.)
- Audit trail for webhook deliveries

⚡ **Performance**

- Async recording via queue jobs
- Sampling support for high-volume applications
- Efficient data aggregation
- Configurable data retention

🔒 **Privacy &amp; Security**

- Optional PII redaction
- Configurable sensitive data patterns
- Body storage disabled by default
- Secure webhook delivery

Requirements
------------

[](#requirements)

- PHP 8.2 or higher
- Laravel 10.x or 11.x
- (Optional) Filament 3.2+ for dashboard UI

Installation
------------

[](#installation)

Install the package via Composer:

```
composer require mubseoul/laravel-llm-observability
```

Publish the configuration file:

```
php artisan vendor:publish --tag="llm-observability-config"
```

Run the migrations:

```
php artisan migrate
```

(Optional) If using Filament dashboard, install Filament:

```
composer require filament/filament:"^3.2"
php artisan filament:install --panels
```

Configuration
-------------

[](#configuration)

The config file `config/llm-observability.php` provides extensive customization options:

### Recording Settings

[](#recording-settings)

```
'recording' => [
    'enabled' => true,
    'mode' => 'async', // 'sync' or 'async'
    'sampling_rate' => 1.0, // 1.0 = 100%, 0.1 = 10%
    'store_bodies' => false, // Privacy: store raw prompts/responses
],
```

### Pricing Configuration

[](#pricing-configuration)

Update pricing as providers change their rates:

```
'pricing' => [
    'openai' => [
        'gpt-4' => ['input' => 30.00, 'output' => 60.00],
        'gpt-4o' => ['input' => 5.00, 'output' => 15.00],
        // ... more models
    ],
    'anthropic' => [
        'claude-3-opus-20240229' => ['input' => 15.00, 'output' => 75.00],
        // ... more models
    ],
],
```

Prices are in USD per 1M tokens.

### Quota Defaults

[](#quota-defaults)

```
'quotas' => [
    'enabled' => true,
    'defaults' => [
        'user' => [
            'requests_per_day' => 1000,
            'cost_per_day' => 10.00,
        ],
    ],
],
```

Usage
-----

[](#usage)

### Basic Recording

[](#basic-recording)

```
use Mubseoul\LLMObservability\Facades\LLM;

$response = LLM::record([
    'provider' => 'openai',
    'model' => 'gpt-4',
    'prompt_tokens' => 150,
    'completion_tokens' => 75,
])->send(function () {
    return OpenAI::chat()->create([
        'model' => 'gpt-4',
        'messages' => [
            ['role' => 'user', 'content' => 'Hello!'],
        ],
    ]);
});
```

### With User Context

[](#with-user-context)

```
LLM::record([
    'provider' => 'anthropic',
    'model' => 'claude-3-sonnet-20240229',
])
->withUser(auth()->id())
->withTeam(auth()->user()->currentTeam->id)
->send(function () {
    return Anthropic::messages()->create([...]);
});
```

### With Metadata

[](#with-metadata)

```
LLM::record([
    'provider' => 'openai',
    'model' => 'gpt-4',
])
->withMetadata([
    'feature' => 'chat',
    'session_id' => session()->getId(),
])
->send(function () {
    // Your LLM call
});
```

### Manual Recording (without callback)

[](#manual-recording-without-callback)

```
LLM::record([
    'provider' => 'openai',
    'model' => 'gpt-4',
    'prompt_tokens' => 100,
    'completion_tokens' => 50,
    'latency_ms' => 1250,
    'status' => 'success',
])->recordRequest([
    'request_id' => 'custom-id',
]);
```

### Enforcing Quotas (Middleware)

[](#enforcing-quotas-middleware)

Add the middleware to your routes:

```
Route::middleware(['llm.quota'])->group(function () {
    Route::post('/api/chat', [ChatController::class, 'send']);
});
```

When a quota is exceeded, the middleware returns a 429 response:

```
{
  "error": "Quota exceeded",
  "message": "Daily request limit exceeded",
  "quota_type": "requests_per_day"
}
```

### Programmatic Quota Checking

[](#programmatic-quota-checking)

```
use Mubseoul\LLMObservability\Services\QuotaEnforcer;

$enforcer = app(QuotaEnforcer::class);

$result = $enforcer->checkQuota(
    userId: auth()->id(),
    estimatedTokens: 1000,
    estimatedCost: 0.05
);

if (!$result['allowed']) {
    throw new Exception($result['reason']);
}
```

### Managing Quotas

[](#managing-quotas)

```
use Mubseoul\LLMObservability\Models\LLMQuota;

// Create a custom quota for a user
LLMQuota::create([
    'scope' => 'user',
    'scope_id' => '123',
    'requests_per_day' => 500,
    'cost_per_month' => 50.00,
    'enabled' => true,
]);

// Get or create quota with defaults
$quota = app(QuotaEnforcer::class)->getOrCreateQuota('user', '123');
```

### Creating Alert Rules

[](#creating-alert-rules)

```
use Mubseoul\LLMObservability\Models\LLMAlertRule;

LLMAlertRule::create([
    'name' => 'High Daily Cost Alert',
    'type' => 'cost_threshold',
    'scope' => 'global',
    'threshold_config' => [
        'cost_usd' => 100.00,
        'period' => 'day',
    ],
    'send_webhook' => true,
    'webhook_url' => 'https://your-app.com/webhooks/llm-alert',
    'enabled' => true,
]);
```

Artisan Commands
----------------

[](#artisan-commands)

### Prune Old Logs

[](#prune-old-logs)

```
# Prune all log types
php artisan llm:prune --all

# Prune specific types
php artisan llm:prune --requests --webhooks

# Force without confirmation
php artisan llm:prune --all --force
```

### Recalculate Aggregates

[](#recalculate-aggregates)

```
# Recalculate last 90 days
php artisan llm:recalc-aggregates

# Custom date range
php artisan llm:recalc-aggregates --from=2024-01-01 --to=2024-12-31
```

### View Pricing Table

[](#view-pricing-table)

```
# Show all pricing
php artisan llm:pricing

# Show specific provider
php artisan llm:pricing openai
```

Dashboard
---------

[](#dashboard)

The Filament dashboard provides a comprehensive UI for monitoring and managing LLM usage.

Access the dashboard at: `/llm-observability` (configurable)

**Dashboard Features:**

- **Overview**: Real-time metrics for the last 24h, 7d, and 30d
- **Request Logs**: Searchable, filterable table of all LLM requests
- **Usage Analytics**: Breakdown by provider, model, user, and team
- **Quota Management**: Configure and monitor quotas
- **Alert Rules**: Create and manage alert thresholds

Configure dashboard settings in `config/llm-observability.php`:

```
'dashboard' => [
    'enabled' => true,
    'prefix' => 'llm-observability',
    'middleware' => ['web', 'auth'],
],
```

Scheduled Tasks
---------------

[](#scheduled-tasks)

The package automatically registers these scheduled tasks:

- **Daily Quota Reset**: Runs at 00:01 daily
- **Monthly Quota Reset**: Runs on the 1st of each month at 00:05
- **Log Pruning**: Runs at 02:00 daily (if retention configured)

No manual scheduler configuration needed.

Integration Examples
--------------------

[](#integration-examples)

### OpenAI PHP SDK

[](#openai-php-sdk)

```
use OpenAI\Laravel\Facades\OpenAI;
use Mubseoul\LLMObservability\Facades\LLM;

$result = LLM::record([
    'provider' => 'openai',
    'model' => 'gpt-4',
])->send(function () {
    $response = OpenAI::chat()->create([
        'model' => 'gpt-4',
        'messages' => [
            ['role' => 'user', 'content' => 'Tell me a joke'],
        ],
    ]);

    return $response;
});

// Extract tokens from response if available
$tokens = $result->usage->totalTokens ?? null;
```

### Laravel HTTP Client

[](#laravel-http-client)

```
use Illuminate\Support\Facades\Http;
use Mubseoul\LLMObservability\Facades\LLM;

$result = LLM::record([
    'provider' => 'anthropic',
    'model' => 'claude-3-sonnet-20240229',
])->send(function () {
    $response = Http::withHeaders([
        'x-api-key' => config('services.anthropic.key'),
        'anthropic-version' => '2023-06-01',
    ])->post('https://api.anthropic.com/v1/messages', [
        'model' => 'claude-3-sonnet-20240229',
        'max_tokens' => 1024,
        'messages' => [
            ['role' => 'user', 'content' => 'Hello!'],
        ],
    ]);

    return $response->json();
});
```

### Custom Provider

[](#custom-provider)

```
LLM::record([
    'provider' => 'ollama',
    'model' => 'llama2',
    'input_chars' => strlen($prompt),
    'output_chars' => strlen($response),
])->send(function () use ($prompt) {
    return Http::post('http://localhost:11434/api/generate', [
        'model' => 'llama2',
        'prompt' => $prompt,
    ])->json();
});
```

Testing
-------

[](#testing)

```
composer test
```

Run with coverage:

```
composer test-coverage
```

Run static analysis:

```
composer analyse
```

Format code:

```
composer format
```

Security Considerations
-----------------------

[](#security-considerations)

### Privacy

[](#privacy)

- **Bodies Disabled by Default**: Raw prompts and responses are NOT stored unless explicitly enabled
- **Redaction Patterns**: Sensitive data (API keys, passwords) is automatically redacted from metadata
- **Configurable**: Add custom redaction patterns in config

```
'recording' => [
    'store_bodies' => false, // Keep disabled for production
    'redact_patterns' => [
        '/api[_-]?key["\s:=]+([a-zA-Z0-9_\-]+)/i',
        '/bearer\s+([a-zA-Z0-9_\-\.]+)/i',
    ],
],
```

### Recommendations

[](#recommendations)

1. **Review stored data**: Understand what metadata is captured
2. **Enable sampling**: Use sampling in high-volume environments
3. **Set retention limits**: Configure `retention.requests_days` to auto-prune old data
4. **Secure webhooks**: Use HTTPS and validate webhook signatures
5. **Audit access**: Restrict dashboard access with appropriate middleware

Roadmap
-------

[](#roadmap)

- **Enhanced Analytics**: More dashboard widgets and charts
- **Export Functionality**: CSV/Excel export for finance teams
- **Budget Alerts**: Proactive budget threshold notifications
- **Multi-tenancy**: Enhanced team/organization isolation
- **Rate Limiting**: Built-in rate limiting by provider
- **Streaming Support**: Track streaming LLM responses
- **Cache Integration**: Track cache hits/misses for LLM responses
- **Provider Auto-detection**: Automatically detect provider from API calls
- **Prompt Templates**: Track and version prompt templates
- **A/B Testing**: Compare different models/prompts

Contributing
------------

[](#contributing)

Please see [CONTRIBUTING](CONTRIBUTING.md) for details.

Security Vulnerabilities
------------------------

[](#security-vulnerabilities)

If you discover a security vulnerability, please send an email to . All security vulnerabilities will be promptly addressed.

Credits
-------

[](#credits)

- [Mubseoul](https://mubseoul.com)
- [All Contributors](../../contributors)

License
-------

[](#license)

The MIT License (MIT). Please see [License File](LICENSE) for more information.

###  Health Score

34

—

LowBetter than 75% of packages

Maintenance75

Regular maintenance activity

Popularity0

Limited adoption so far

Community6

Small or concentrated contributor base

Maturity47

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

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

133d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/9cdeee878ffcba362afaf1ed18ded1f4b5ce83a2ecb50d7de6014e7d443f7175?d=identicon)[mubseoul](/maintainers/mubseoul)

---

Top Contributors

[![mubseoul](https://avatars.githubusercontent.com/u/177406116?v=4)](https://github.com/mubseoul "mubseoul (3 commits)")

---

Tags

analyticsanthropiccost-trackingfilamentlaravellaravel-packagellmmonitoringobservabilityopenaiphplaravelmonitoringaiopenaiclaudeobservabilityllmanthropiccost-tracking

###  Code Quality

TestsPest

Static AnalysisPHPStan

Code StyleLaravel Pint

Type Coverage Yes

### Embed Badge

![Health badge](/badges/mubseoul-laravel-llm-observability/health.svg)

```
[![Health](https://phpackages.com/badges/mubseoul-laravel-llm-observability/health.svg)](https://phpackages.com/packages/mubseoul-laravel-llm-observability)
```

###  Alternatives

[spatie/laravel-health

Monitor the health of a Laravel application

87512.0M167](/packages/spatie-laravel-health)[psalm/plugin-laravel

Psalm plugin for Laravel

3355.3M346](/packages/psalm-plugin-laravel)[laravel/ai

The official AI SDK for Laravel.

1.0k3.2M200](/packages/laravel-ai)[simplestats-io/laravel-client

Server-side analytics for Laravel that follows the full funnel from visit to registration to payment, attributed to the channel that drove it. Revenue, MRR, churn and ad-spend profit (ROAS/CAC) per channel. GDPR compliant, ad-blocker proof.

5022.0k](/packages/simplestats-io-laravel-client)[api-platform/laravel

API Platform support for Laravel

58171.6k14](/packages/api-platform-laravel)[spectra-php/laravel-spectra

Comprehensive observability for AI/LLM operations in Laravel applications

182.8k](/packages/spectra-php-laravel-spectra)

PHPackages © 2026

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