Weave Code
Weaver
Helps Laravel developers discover, compare, and choose open-source packages. See popularity, security, maintainers, and scores at a glance to make better decisions.
Feedback
Share your thoughts, report bugs, or suggest improvements.
Subject
Message

Llm Cost Tracking Bundle Laravel Package

lunetics/llm-cost-tracking-bundle

Lunetics LLM Cost Tracking Bundle helps Symfony/PHP apps monitor LLM API usage and estimate spend across providers and models. Track token counts, attribute costs to users or features, set budgets, and review usage via dashboards and reports.

View on GitHub
Deep Wiki
Context7

Getting Started

lunetics/llm-cost-tracking-bundle is a Symfony bundle (not a Laravel package) that tallies token usage and computes per-model LLM costs, with a Web Profiler panel for inspecting them. In Laravel you get the cost-tracking core, but not the profiler UI — plan to build your own surfacing (logs, Telescope, a dashboard).

  1. composer require lunetics/llm-cost-tracking-bundle
  2. There is no package auto-discovery. Register the services you need in a small LlmCostServiceProvider, or instantiate the tracker directly from the container.
  3. The entry point is CostTrackerInterface. In v0.6.0 this interface changed (see Gotchas) — read its signature in the source before writing your first implementation.
  4. Locate the model pricing map in the bundle config and copy it to config/llm-cost-tracking.php so you can maintain prices yourself.

First use case: a thin decorator around your OpenAI/Anthropic client that records model, prompt_tokens, completion_tokens on each response and returns a computed cost you can log alongside the request.

Implementation Patterns

  • Decorator over your LLM client. Don't sprinkle tracker calls through controllers. Wrap your existing client (or Laravel AI SDK binding) so every completion is recorded in one place, including failures.
  • Bind the interface, not the class. In your service provider: $this->app->bind(CostTrackerInterface::class, ...). This keeps you insulated from the v0.6.0 interface break and lets you swap in a null/no-op tracker in tests.
  • Implement the skipped-call surface. v0.6.0 adds reporting for skipped calls — calls the tracker could not price (unknown model, missing usage in a streamed response, malformed payload). Your implementation must return these; in Symfony they render in the profiler panel, in Laravel you should pipe them into Log::warning() or a metrics counter.
  • Per-request and per-job scoping. Cost accumulates across a long-lived worker. Reset/flush the tracker at the start of each queued job and persist the total to your own llm_costs table (user, tenant, job id, model, tokens, cost).
  • Config layering. Keep a base price table in config and override via env for premium/discounted contracts; store prices as integers (micro-dollars) to avoid float drift in aggregates.
  • Testing. Bind a fake CostTrackerInterface that records calls and returns a fixed cost; assert against that in feature tests rather than mocking HTTP.

Gotchas and Tips

  • Breaking change in v0.6.0: CostTrackerInterface changed. Any custom implementation (including test doubles and decorators) will fatal until it implements the new skipped-call methods. Check PR #12 for the exact signature before upgrading; this is a minor-version bump with a BC break, so pin ^0.5 if you can't migrate immediately.
  • The profiler panel is Symfony-only. The headline v0.6.0 feature ("surface skipped calls in the profiler panel") gives you nothing in Laravel out of the box — retrieve the skipped-call data from the tracker API yourself.
  • Skipped calls are the debugging gold. A spike in skipped calls usually means: a model name missing from the pricing map, a streamed response with no usage block, or an error path recorded without tokens. Alert on it.
  • No auto-discovery / no Laravel config merge. You own publishing, config merging and service wiring. Expect to write ~30 lines of glue.
  • Double counting. Retries, streaming re-sends, and tool-call loops inflate totals. Key each recorded call by a request/response id and dedupe on write.
  • Pricing drift is on you. The bundled table goes stale after provider price changes; treat it as a seed and keep your own overrides under version control.
  • Extension point: decorate CostTrackerInterface rather than extending the concrete tracker — it survives upstream class renames and is the cleanest place to add tenant tagging or metrics export.
Weaver

How can I help you explore Laravel packages today?

Conversation history is not saved when not logged in.
Prompt
Add packages to context
No packages found.
skyraptor/chatter
edlugz/sasapay
abbasudo/artisan-route
mirvan/ippanel
ignibyte/aic-framework
glavweb/data-schema-bundle
glavweb/datagrid-bundle
tacticmedia/scim-server
uni-method/frame
neika-ai/sdk
adachsoft/php-code-style
pharaonic/laravel-notification-dependencies
beautinow/translug
dev-brainiac/bot-cloaked
octoberrain/html
octoberrain/extension
octoberrain/exception
octoberrain/contracts
adachsoft/api-version-bundle
cgv/localized-resource