Download the PHP package backtik-ch/laravel-ai-usage without Composer

On this page you can find all versions of the php package backtik-ch/laravel-ai-usage. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.

FAQ

After the download, you have to make one include require_once('vendor/autoload.php');. After that you have to import the classes with use statements.

Example:
If you use only one package a project is not needed. But if you use more then one package, without a project it is not possible to import the classes with use statements.

In general, it is recommended to use always a project to download your libraries. In an application normally there is more than one library needed.
Some PHP packages are not free to download and because of that hosted in private repositories. In this case some credentials are needed to access such packages. Please use the auth.json textarea to insert credentials, if a package is coming from a private repository. You can look here for more information.

  • Some hosting areas are not accessible by a terminal or SSH. Then it is not possible to use Composer.
  • To use Composer is sometimes complicated. Especially for beginners.
  • Composer needs much resources. Sometimes they are not available on a simple webspace.
  • If you are using private repositories you don't need to share your credentials. You can set up everything on our site and then you provide a simple download link to your team member.
  • Simplify your Composer build process. Use our own command line tool to download the vendor folder as binary. This makes your build process faster and you don't need to expose your credentials for private repositories.
Please rate this library. Is it a good library?

Informations about the package laravel-ai-usage

Laravel AI Usage

Track and log AI usage (tokens, cost, duration) for any Laravel application, with optional auto-discovery for the laravel/ai SDK and an optional Filament panel integration.

Requirements

Installation

Publish the config file and run the migrations:

How It Works

This package hooks into the laravel/ai SDK lifecycle via two events:

  1. PromptingAgent — fired when an agent starts. The package creates a pending record with the agent class, label, and an invocation ID.
  2. AgentPrompted — fired when the agent completes. The package locates the pending record, fills in token counts, duration, model/driver info, and snapshots the token prices from your config.

The result is a complete, immutable log of every AI call — no manual code required if you use laravel/ai.

If you're not using laravel/ai, you can log calls manually via the fluent AiUsage facade (see Manual logging).

Configuration

The config file (config/ai-usage.php) exposes the following options:

Key Default Description
table ai_usage Database table name
queue_logs false Dispatch log writes as queued jobs
log_system_prompt false Store system prompts in the log
log_response_text true Store AI response bodies
max_text_length 10_000 Truncate prompt/response text (chars); null disables
stale_timeout_minutes 60 Minutes before a processing record is considered stale
auto_discover true Auto-listen to laravel/ai events
prices (see below) Token prices (USD / 1 M tokens) per driver & model

Token prices

Prices are snapshotted into each log row at write time so historical cost estimates remain accurate after you update the config.

Supported price keys: prompt, completion, cache_read, cache_write, reasoning.

The package ships with indicative prices for common OpenAI, Anthropic and Gemini models. Always verify against your provider's current pricing page.

Usage

[!TIP] Using laravel/ai? You're already done. If auto-discovery is enabled (default) and laravel/ai is installed, every AI agent call is logged automatically — no code needed. The sections below are for manual logging or custom integrations.

Auto-discovery mode

When auto_discover is true (default) and laravel/ai is installed, the package automatically listens for Laravel\Ai\Events\PromptingAgent and Laravel\Ai\Events\AgentPrompted. A pending record is created when the agent starts, then updated with tokens, duration, model info, and cost data when the agent finishes.

To disable auto-discovery, set auto_discover to false in config/ai-usage.php.

Manual logging

Use the AiUsage facade with a fluent builder:

Attach to a model (polymorphic owner)

Override prices at log time

If costPrices() is not called, prices are resolved automatically from config('ai-usage.prices').

How cost estimation works

Token prices (USD per 1 million tokens) are snapshotted from your config at log time and stored alongside each record. This means historical cost data remains accurate even when you update prices later.

The AiUsageLog model exposes an estimated_cost computed attribute derived from the stored token counts and prices:

Returns null when no price data is available (i.e., the model isn't listed in your prices config).

To add a new model, simply add its prices to config('ai-usage.prices.{driver}.{model}'). Supported price keys: prompt, completion, cache_read, cache_write, reasoning.

Status values

Logs use the AiUsageStatus enum: pending, processing, completed, failed.

Filament integration

Requires filament/filament. Register the plugin in your panel provider:

This registers:

Summary widget (standalone)

AiUsageSummaryWidget is a full-width Livewire widget with a period selector and per-driver / per-model token breakdowns. Add it to any page:

Artisan Commands

Prune old records

Mark stale records as failed

If an AI call throws an exception, the laravel/ai SDK never fires AgentPrompted, so the record stays in processing status indefinitely. Run this command on a schedule to mark those stale records as failed:

The timeout is controlled by stale_timeout_minutes in your config (default: 60). You can also override it per-call:

Add both commands to your scheduler in routes/console.php:

Testing

License

MIT — see LICENSE.


All versions of laravel-ai-usage with dependencies

PHP Build Version
Package Version
Requires php Version ^8.3
illuminate/support Version ^12.0
illuminate/database Version ^12.0
illuminate/console Version ^12.0
illuminate/events Version ^12.0
Composer command for our command line client (download client) This client runs in each environment. You don't need a specific PHP version etc. The first 20 API calls are free. Standard composer command

The package backtik-ch/laravel-ai-usage contains the following files

Loading the files please wait ...