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.

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-agent-cost

Laravel Agent Cost

Latest Version on Packagist GitHub Tests Action Status GitHub Code Style Action Status Total Downloads

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

License

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


All versions of laravel-agent-cost with dependencies

PHP Build Version
Package Version
Requires php Version ^8.4
spatie/laravel-package-tools Version ^1.16
illuminate/contracts Version ^11.0||^12.0||^13.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 adam-dziuk/laravel-agent-cost contains the following files

Loading the files please wait ...