ai_metering
AI Metering adds token usage tracking, cost estimation, per-user monthly quotas, and a usage dashboard to any Drupal site running the AI module. It hooks into drupal/ai's event system and meters every AI call automatically, with no integration work required per provider or per feature module.
The Drupal AI Initiative provides excellent provider abstraction, agentic workflows, and content generation tools, but no module currently tracks what those operations cost. Without cost governance, deploying AI in production means billing surprises and no visibility into usage patterns. AI Metering fills that gap.
Features
- Automatic interception of all AI calls. Subscribes to
PostGenerateResponseEventandPostStreamingResponseEvent, the same events fired by every provider throughai\Plugin\ProviderProxy. Any present or future provider registered withdrupal/aiis metered without additional configuration. - Per-user monthly token quotas. Set a global default, per-role defaults, or per-user overrides. When a user's budget is exhausted, the module can either silently reroute the call to a local fallback provider (e.g. Ollama) at zero cost, or throw an
AiQuotaExceptionto block the paid call entirely. - Pre-flight quota check. A lookahead estimate runs before the AI call is dispatched. If the estimated token cost of the incoming request would push the user over budget, the call is rerouted before the remote API is contacted.
- Multi-currency cost display. Costs are always tracked in USD (provider-native). Display currency is configurable: USD, CAD, EUR, GBP, AUD, JPY. Live exchange rates are fetched from Frankfurter (free, no API key, sourced from ECB reference rates), cached for one hour. Falls back to manually configured rates when the API is unreachable.
- Automated pricing sync. Model pricing is fetched from LiteLLM's public cost map (100+ models). Sync on demand via the admin UI or via
drush ai-metering:sync-pricing. Manual overrides are preserved across syncs. - Usage dashboard. Per-editor breakdown of token consumption, cost, call counts, and quota percentage for any calendar month. Distinguishes cloud providers (billed) from local providers such as Ollama, LM Studio, LocalAI, and KoboldCPP (tracked at zero cost). Exportable as CSV or JSON.
- Usage log. Per-call audit trail at
/admin/reports/ai-metering/log: user, timestamp, provider, model, operation type, input/output/cached token counts, and cost in USD at 8-decimal precision. Full visibility into every individual AI call on the site. Exportable as CSV. - Email alerts. Configurable threshold (default 75%). Alerts fire exactly once per threshold crossing per month, not on every call above the threshold. Per-user threshold overrides are supported.
- Context grouping. Tag AI events with
aim_context:UUIDto group all calls from a single agent run in the dashboard. - Optional LiteLLM proxy integration. When a LiteLLM proxy is configured, Drush commands expose per-user and global spend reports from the proxy's own billing data.
- Extensibility hooks.
hook_ai_metering_quota_exceeded()fires when a user's budget is hit.hook_ai_metering_record_presave_alter()allows altering usage records before persistence.MeteringRecordCreatedEvent(Symfony event) fires after every usage record is written.
Post-Installation
After enabling the module:
- Visit Admin → Configuration → AI → AI Metering Settings (
/admin/config/ai/ai-metering/settings) to configure display currency, quota defaults, fallback provider, alert emails, and alert threshold. - Sync model pricing: click Sync pricing from LiteLLM in the settings form, or run
drush ai-metering:sync-pricing. This step is optional because the module estimates costs from manual pricing entries if no sync has been run. - Visit Admin → Reports → AI Usage by Editor (
/admin/reports/ai-metering) to view the usage dashboard. - Explore the Usage log (
/admin/reports/ai-metering/log) for a per-call audit trail with full token breakdown and cost in USD at 8-decimal precision. - Assign the "Administer AI Metering" permission to site administrator roles and "View AI usage dashboard" to roles that should see the dashboard.
No configuration is needed per AI provider. The module meters any call that passes through drupal/ai's ProviderProxy, including streaming responses.
Additional Requirements
- Drupal 10.5 or 11.2 (as declared in
core_version_requirement) - AI module (
drupal/ai), the only hard dependency - At least one AI provider module enabled (e.g.
ai_provider_anthropic,ai_provider_openai,ai_provider_mistral, or any community provider)
Recommended modules/libraries
- Key module: the Anthropic token-counting adapter optionally uses
key.repositoryto resolve the Anthropic API key. If you use the Key module for API key management site-wide, the integration is detected automatically. - Ollama (local LLM runtime): required only if you configure a local fallback provider for quota enforcement. When a user's monthly budget is exhausted, calls are rerouted to the local model at zero API cost.
- LiteLLM proxy (optional): when configured, adds proxy-level spend reporting via Drush commands (
ai-metering:litellm-user-spend,ai-metering:litellm-report).
Similar projects
As of the time of this module's submission, there is no comparable contrib module in the Drupal AI Initiative ecosystem. The existing AI modules (ai, ai_provider_*, ai_assistant_api, ai_translate, ai_guardrails) handle provider abstraction, content operations, and access control, but none expose token usage counts, per-call cost estimates, or user-level budget governance to site administrators.
AI Metering is designed to complement, not replace, those modules. It is intentionally positioned as the "proof of cost" layer: transparent, passive interception with no changes required to existing AI-powered workflows.
Supporting this Module
Please use the issue queue on drupal.org for bug reports and feature requests.
This module is developed by Jérôme Tchania (CodeIt Wisely).
Community Documentation
- AI module: upstream provider abstraction this module builds on
- Frankfurter exchange rate API: open-source ECB-sourced rate service used for multi-currency display