Download the PHP package lunetics/llm-cost-tracking-bundle without Composer

On this page you can find all versions of the php package lunetics/llm-cost-tracking-bundle. 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 llm-cost-tracking-bundle

LuneticsLlmCostTrackingBundle

CI codecov Latest Stable Version Total Downloads License

A Symfony bundle that tracks LLM API costs and displays them in the Web Debug Toolbar and Profiler.

Hooks into symfony/ai-bundle's TraceablePlatform to calculate per-request costs based on token usage, with support for input, output, cached, and thinking tokens.

Requirements

Installation

If you are not using Symfony Flex, register the bundle manually:

Features

Model Pricing

The bundle resolves pricing for a model using the following priority order:

  1. Your YAML configmodels: entries take precedence over everything else
  2. Dynamic pricing from models.dev — the bundle fetches live pricing from models.dev (thousands of models) and caches it for 24 hours. When models.dev is unreachable, a bundled snapshot of ~3000 models is used as a fallback so known models are still priced correctly during outages
  3. Not found — cost is shown as zero with a warning in the profiler

This means most models work out of the box with no configuration. Your own entries always win.

All prices are in USD. The models.dev feed and any prices you configure are all treated as USD. There is no currency conversion; the $ prefix shown in the profiler is a literal dollar sign.

Configuration

Logging

Logging is disabled by default. Enable it explicitly to have per-request LLM cost data written via Monolog:

Each request that makes at least one AI call produces:

Logs are emitted on kernel.terminate — after the response is sent — so there is no latency impact.

To route AI cost logs to a dedicated file, configure a Monolog handler for the ai channel:

If the ai channel is not explicitly configured, logs fall through to your default handler.

To use a different channel name:

Disabling Dynamic Pricing

If you want fully offline/air-gapped operation, or prefer explicit control over every model's price:

When disabled, the bundled snapshot continues to provide model coverage as a read-only baseline — no live HTTP requests are made, and the lunetics:llm:update-pricing command is removed from the container. Your explicit models: config always takes priority over the snapshot. This is the right choice for air-gapped or reproducible-pricing environments where you want stable costs without live API calls.

Upgrade note (v0.3): The old static list of bundled defaults has been replaced with a versioned pricing snapshot (resources/pricing_snapshot.json). When dynamic_pricing.enabled: false, the snapshot serves as an always-on baseline — models that previously returned null pricing may now resolve from the snapshot. If you need strict "only my config" behaviour where unknown models always return nothing, add models: entries for every model you use and configure your app to treat unresolved models as an error.

Adjusting the Cache TTL

The dynamic pricing response is cached to avoid unnecessary HTTP requests on every page load. The default TTL is 24 hours. To refresh more or less frequently:

Minimum: 1 second. Maximum: 604800 (7 days).

Console Command

To manually refresh the cached pricing from models.dev:

This clears the cache and immediately fetches fresh pricing. Add --verbose to see the full model table:

The command exits with a non-zero status if the API is unreachable or returns no models, making it safe to use in deployment pipelines.

Model Coverage

The bundle ships a versioned snapshot of the models.dev catalogue (~3000 models across ~100 providers) as resources/pricing_snapshot.json. This snapshot is used as a fallback when the live API is unreachable, so pricing works correctly even in offline or air-gapped environments.

The snapshot is regenerated before each release. To regenerate it locally:

The model string passed to $platform->invoke() (e.g. 'gpt-5') is the same string the bundle uses to look up pricing.

Example: OpenAI GPT

No lunetics_llm_cost_tracking config is needed — gpt-5 is covered by dynamic pricing (or the bundled snapshot when offline). Costs appear automatically in the profiler toolbar.

Overriding or Adding Model Pricing

If you use a model that isn't on models.dev (e.g. a fine-tuned or self-hosted model), add it to your config:

Your models: entries always take precedence over bundle defaults and dynamic pricing.

Using Cost Data in Your Services

The CostTrackerInterface service is available for dependency injection. Use it to access cost data outside the profiler — for example, in middleware, event listeners, or API responses:

Available methods: getCalls(), getTotals(), getByModel(), getUnconfiguredModels(), getSnapshot().

The ModelRegistryInterface and CostCalculatorInterface are also available for injection if you need lower-level access to model definitions or cost calculation.

How It Works

The bundle collects data from all services tagged with ai.traceable_platform (provided by symfony/ai-bundle). The CostTracker service iterates over all recorded LLM calls, extracts token usage metadata, and calculates costs using the configured model pricing. Results are memoized for the lifetime of the request, so repeated calls to any getter return the same data without recomputation.

The LlmCostCollector (Symfony Profiler data collector) delegates to CostTracker via getSnapshot(), which returns all cost data in a single atomic call. This separation keeps business logic in a standalone service that can be injected anywhere, while the data collector focuses on profiler integration.

Cost formula per call:

Where regular_input_tokens = max(0, input_tokens - cached_tokens).

Development

A Docker-based Makefile is provided for local development:

Override the PHP version with PHP_VERSION=8.2 make test.

License

MIT License. See LICENSE for details.


All versions of llm-cost-tracking-bundle with dependencies

PHP Build Version
Package Version
Requires php Version >=8.2
symfony/ai-bundle Version >=0.8.0 <2.0.0
symfony/ai-platform Version >=0.8.0 <2.0.0
symfony/cache Version ^7.0|^8.0
symfony/config Version ^7.0|^8.0
symfony/dependency-injection Version ^7.0|^8.0
symfony/framework-bundle Version ^7.0|^8.0
symfony/http-client Version ^7.0|^8.0
symfony/http-kernel Version ^7.0|^8.0
symfony/twig-bundle Version ^7.0|^8.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 lunetics/llm-cost-tracking-bundle contains the following files

Loading the files please wait ...