Drupal is a registered trademark of Dries Buytaert
cms 2.1.3 Update released for Drupal core (2.1.3)! drupal 10.5.11 Update released for Drupal core (10.5.11)! drupal 11.3.11 Update released for Drupal core (11.3.11)! drupal 11.2.13 Update released for Drupal core (11.2.13)! drupal 10.6.10 Update released for Drupal core (10.6.10)! cms 2.1.2 Update released for Drupal core (2.1.2)! drupal 11.1.10 Update released for Drupal core (11.1.10)! drupal 10.5.10 Update released for Drupal core (10.5.10)! drupal 10.4.10 Update released for Drupal core (10.4.10)! drupal 11.2.12 Update released for Drupal core (11.2.12)! drupal 11.3.10 Update released for Drupal core (11.3.10)! drupal 10.6.9 Update released for Drupal core (10.6.9)! drupal 10.6.8 Update released for Drupal core (10.6.8)! drupal 11.3.9 Update released for Drupal core (11.3.9)! drupal 11.3.8 Update released for Drupal core (11.3.8)! drupal 11.3.7 Update released for Drupal core (11.3.7)! drupal 11.2.11 Update released for Drupal core (11.2.11)! drupal 10.6.7 Update released for Drupal core (10.6.7)! drupal 10.5.9 Update released for Drupal core (10.5.9)! cms 2.1.1 Update released for Drupal core (2.1.1)!

ai_metering

No security coverage
View on drupal.org

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 PostGenerateResponseEvent and PostStreamingResponseEvent, the same events fired by every provider through ai\Plugin\ProviderProxy. Any present or future provider registered with drupal/ai is 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 AiQuotaException to 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:UUID to 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:

  1. 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.
  2. 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.
  3. Visit Admin → Reports → AI Usage by Editor (/admin/reports/ai-metering) to view the usage dashboard.
  4. 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.
  5. 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)
  • Key module: the Anthropic token-counting adapter optionally uses key.repository to 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

Activity

Total releases
1
First release
Jun 2026
Latest release
22 hours ago
Release cadence
Stability
0% stable

Releases

Version Type Release date
1.0.0-alpha1 Pre-release Jun 2, 2026