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
- Open the AI Metering Settings page.
- There is no option to select a pricing source — LiteLLM is the only source and it is hardcoded.
- Pricing from
models.dev/api.jsoncannot 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
PricingSourceInterfaceandPricingSourcePluginManager. - Define
ModelPricevalue object. - Refactor existing LiteLLM sync into
LiteLLMPricingSourceplugin (default). - Implement
ModelsDotDevPricingSourceplugin with schema normalisation. - Add pricing source selector to the admin Settings form.
- Update
SyncPricingCommandand 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
Comment #20
codeitwisely commented