Download the PHP package adam-dziuk/laravel-agent-cost without Composer
On this page you can find all versions of the php package adam-dziuk/laravel-agent-cost. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download adam-dziuk/laravel-agent-cost
More information about adam-dziuk/laravel-agent-cost
Files in adam-dziuk/laravel-agent-cost
Package laravel-agent-cost
Short Description Calculate AI costs for Laravel AI SDK responses using up-to-date model pricing.
License MIT
Homepage https://github.com/adam-dziuk/laravel-agent-cost
Informations about the package laravel-agent-cost
Laravel Agent Cost
Know what your AI calls actually cost - now you can calculate AI costs for Laravel AI SDK. This package syncs LiteLLM's community-maintained model pricing data and uses it to calculate the USD cost of a Laravel AI SDK response, or of any token counts you already have on hand.
Installation
You can install the package via composer:
Run the migrations to create the agent_cost_records table used by AiCost::agent():
You can publish the config file with:
This is the contents of the published config file:
Before calculating any costs, sync the pricing data at least once:
If the download or the response fails to parse, the command exits with an error and leaves any previously cached pricing data untouched. You're never left without prices because of a temporary network hiccup. Schedule it to run regularly so pricing stays current, for example in routes/console.php:
Usage
Calculating the cost of a laravel/ai response
If you're using laravel/ai, pass any response that carries usage information straight to AiCost::for(). It understands text, agent, structured, streamed, image, transcription, and embeddings responses:
The model and provider are read from the response's own metadata, and prompt, completion, cached, and reasoning tokens are all billed at their correct rates, so there's no need to pass anything else. laravel/ai is not a hard dependency of this package: AiCost::for() only needs it installed when you actually call it with one of its response objects.
Manual calculation
If you already have token counts from somewhere else, calculate the cost directly:
Tracking the total cost of an agent
Every time a laravel/ai agent finishes a prompt() or stream() call, this package automatically records its cost to the database, keyed by the agent's class. AiCost::agent() lets you total that up, optionally narrowed down to a date or month range:
Costs are tracked per agent class, not per instance, since laravel/ai agents are typically stateless value objects created fresh on every call (SupportAgent::make()).
Run php artisan migrate to create the agent_cost_records table this relies on. Failed invocations (AgentFailed) aren't recorded, since they carry no usage information; invocations for a model with no pricing data are silently skipped rather than breaking the agent call.
If that table name clashes with something you already have, change it before running the migration:
If you don't want a permanent log of every agent call, turn tracking off:
When pricing data is missing
AiCost::tokens() and AiCost::for() throw AdamDziuk\LaravelAgentCost\Exceptions\UnknownModelException when there's no pricing data for a model. Run ai-prices:sync first, or add an override for it. AiCost::for() throws AdamDziuk\LaravelAgentCost\Exceptions\UnsupportedResponseException if you pass it a response that doesn't expose any token usage (audio and reranking responses, for example).
Overriding prices
Use the overrides config to correct a price, or to add pricing for a model that isn't in LiteLLM's file at all (a private deployment, for instance). Overrides are merged on top of the synced data, field by field, so you only need to specify what you want to change:
Testing
Changelog
Please see CHANGELOG for more information on what has changed recently.
Security Vulnerabilities
Please review our security policy on how to report security vulnerabilities.
Credits
- Adam Dziuk
- Pricing data is downloaded from LiteLLM, licensed under the MIT license. This package is not affiliated with or endorsed by LiteLLM.
- All Contributors
License
The MIT License (MIT). Please see License File for more information.
All versions of laravel-agent-cost with dependencies
spatie/laravel-package-tools Version ^1.16
illuminate/contracts Version ^11.0||^12.0||^13.0