Problem/Motivation

ai_metering syncs model pricing from LiteLLM
(model_prices_and_context_window.json). The source URL is hardcoded
in the sync service and SyncPricingCommand. There is no way to use
an alternative source without patching the module.

In the AI Initiative issue #3586519, Marcus Johansson mentioned
models.dev/api.json as the preferred pricing source for the Initiative.
Albert Albala suggested independently that the pricing source should be a Drupal
Plugin so the community can add sources without forking. Sites with air-gapped or
on-prem deployments may also need a local static file. LiteLLM remains the best
default, but the architecture should not prevent alternatives.

Steps to reproduce

  1. Open the AI Metering Settings page.
  2. There is no option to select a pricing source — LiteLLM is the only source and it is hardcoded.
  3. Pricing from models.dev/api.json cannot be used without a code change.

Environment

  • Drupal version: 11.3.11
  • ai_metering version: 1.0.0-alpha1
  • AI provider module + version: any (e.g. ai_provider_anthropic 1.2.2)
  • AI feature module tested: N/A

Proposed resolution

Introduce a PricingSourceInterface as a Drupal annotated Plugin type
with a PricingSourcePluginManager. Each implementation returns a
list of ModelPrice value objects
(provider, model, input_per_million,
output_per_million, cache_read_per_million,
cache_write_per_million, currency).

Ship two implementations: LiteLLMPricingSource (current behaviour,
becomes the default) and ModelsDotDevPricingSource (fetches and
normalises models.dev/api.json). The active source is selected on the
Settings page. Third-party modules can provide additional sources by declaring a
plugin in their namespace.

Remaining tasks

  • Define PricingSourceInterface and PricingSourcePluginManager.
  • Define ModelPrice value object.
  • Refactor existing LiteLLM sync into LiteLLMPricingSource plugin (default).
  • Implement ModelsDotDevPricingSource plugin with schema normalisation.
  • Add pricing source selector to the admin Settings form.
  • Update SyncPricingCommand and the cron sync service to use the plugin manager.
  • Add unit tests for both implementations against fixture responses.
  • Review.

User interface changes

Yes, the Settings page gains a "Pricing source" selector (LiteLLM or models.dev, with the option for contributed sources).

API changes

Yes, introduces a new PricingSourceInterface Plugin type and PricingSourcePluginManager. Third-party modules can implement the interface to provide custom pricing sources.

Data model changes

N/A, pricing data is stored in Drupal State; the structure is unchanged.

AI assistance

N/A

Comments

codeitwisely created an issue. See original summary.

  • fb658783 committed on 1.0.x-3604256-pricing-source-plugin
    Issue #3604256 by codeitwisely: Add PricingSource plugin type with...

  • 1aba864f committed on 1.0.x-3604256-pricing-source-plugin
    Issue #3604256 by codeitwisely: Add PricingSource plugin type with...

  • d8b7d2cb committed on 1.0.x-3604256-pricing-source-plugin
    Issue #3604256 by codeitwisely: Add PricingSource plugin type with...

  • d2d8dbc5 committed on 1.0.x-3604256-pricing-source-plugin
    Issue #3604256 by codeitwisely: Add PricingSource plugin type with...

  • edd2a48a committed on 1.0.x-3604256-pricing-source-plugin
    Issue #3604256 by codeitwisely: Add PricingSource plugin type with...

  • b9591a79 committed on 1.0.x-3604256-pricing-source-plugin
    Issue #3604256 by codeitwisely: Add PricingSource plugin type with...

  • 708ed266 committed on 1.0.x-3604256-pricing-source-plugin
    Issue #3604256 by codeitwisely: Add PricingSource plugin type with...

  • 2841ec39 committed on 1.0.x-3604256-pricing-source-plugin
    Issue #3604256 by codeitwisely: Add PricingSource plugin type with...

  • 1366a6d5 committed on 1.0.x-3604256-pricing-source-plugin
    Issue #3604256 by codeitwisely: Add PricingSource plugin type with...

  • de3225bc committed on 1.0.x-3604256-pricing-source-plugin
    Issue #3604256 by codeitwisely: Add PricingSource plugin type with...

  • bc1ae5e2 committed on 1.0.x-3604256-pricing-source-plugin
    Issue #3604256 by codeitwisely: Add PricingSource plugin type with...

  • codeitwisely committed 884bebbc on 1.0.x
    Issue #3604256: Add PricingSource plugin type with LiteLLM and models....

  • codeitwisely committed 884bebbc on 1.0.x-3604238-test-litellm-rate-path
    Issue #3604256: Add PricingSource plugin type with LiteLLM and models....

  • codeitwisely committed 884bebbc on 1.0.x-3604229-skip-wrapper-provider-double-count
    Issue #3604256: Add PricingSource plugin type with LiteLLM and models....

  • codeitwisely committed 884bebbc on 1.0.x-3604228-uid-filter-in-usage-log
    Issue #3604256: Add PricingSource plugin type with LiteLLM and models....

  • codeitwisely committed 884bebbc on 1.0.x-3604255-token-details-column
    Issue #3604256: Add PricingSource plugin type with LiteLLM and models....

  • codeitwisely committed 884bebbc on 1.0.x-3604240-toolbar-icon
    Issue #3604256: Add PricingSource plugin type with LiteLLM and models....

  • codeitwisely committed 884bebbc on 1.0.x-3604346-by-role-dashboard
    Issue #3604256: Add PricingSource plugin type with LiteLLM and models....
codeitwisely’s picture

Status: Active » Fixed

Now that this issue is closed, review the contribution record.

As a contributor, attribute any organization that helped you, or if you volunteered your own time.

Maintainers, credit people who helped resolve this issue.

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.