This module tracks AI token usage, estimates costs, and allows setting per-user monthly quotas. It automatically records every AI call the system makes, providing a dashboard for usage overviews and a detailed log for auditing.
AI Metering adds token usage tracking, cost estimation, per-user monthly quotas, and an admin dashboard to any Drupal site running the AI module. It subscribes to drupal/ai's event system and records every AI call automatically — any provider registered with the AI module is covered with no per-provider configuration.
Features
- Automatic call interception. Subscribes to
PostGenerateResponseEventandPostStreamingResponseEvent, fired by every provider throughai\Plugin\ProviderProxy. No per-provider or per-feature configuration needed. - Per-user monthly quotas. Set a global default, per-role defaults, or per-user overrides. When a user's budget is reached, the module either reroutes the call to a local fallback provider (e.g. Ollama) at zero cost, or throws an
AiQuotaExceptionto block the paid call. A pre-flight estimate runs before dispatch — if the request would push the user over budget, the call is rerouted before the remote API is contacted. - Multi-currency cost display. Costs are tracked in USD. Display currency is configurable: USD, CAD, EUR, GBP, AUD, JPY. Exchange rates are fetched from Frankfurter (ECB reference rates, no API key), cached for one hour, with a fallback to manually configured rates.
- Pluggable pricing sources. The
PricingSourceplugin type ships withLiteLLMPricingSource(100+ models) andModelsDotDevPricingSource. Third-party modules can register additional sources. Sync on demand via the admin UI ordrush 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. Local providers (Ollama, LM Studio, etc.) appear tracked at zero cost, separate from billed cloud providers. Exportable as CSV or JSON.
- By-role dashboard. Aggregates per-user usage into role buckets at
/admin/reports/ai-metering/by-role. Includes CSV and JSON exports. - Usage log. Per-call audit trail at
/admin/reports/ai-metering/log: user, timestamp, provider, model, operation type, input/output/cached token counts, cost in USD at 8-decimal precision, and atoken_detailscolumn for provider-specific breakdowns (reasoning tokens, provider-reported totals). Filterable by provider, model, and period. Exportable as CSV or JSON. - Email alerts. Fires once per threshold crossing per month (default 75%). Per-user threshold overrides supported.
- Context grouping. Tag AI events with
aim_context:UUIDto group all calls from a single agent run. Calls sharing the same UUID appear as a single session in the usage log. - Extensibility hooks.
hook_ai_metering_quota_exceeded(),hook_ai_metering_record_alter(), andMeteringRecordCreatedEvent.
Post-Installation
- Go to Admin → Configuration → AI → AI Metering (
/admin/config/ai/ai-metering) for the hub page linking to all sections: dashboard, settings, usage log, and permissions. - In Settings, configure display currency, quota defaults, fallback provider, pricing source, and alert threshold.
- Sync model pricing from the settings form or via
drush ai-metering:sync-pricing. Optional — the module falls back to manually entered rates if no sync has run. - Assign Administer AI Metering to administrator roles and View AI usage dashboard to roles that should see the dashboard.
Additional Requirements
- Drupal 10.5+ or 11.2+
- PHP 8.1+ (8.3+ on Drupal 11)
- AI module (
drupal/ai) - At least one AI provider module (e.g.
ai_provider_anthropic,ai_provider_openai, or any community provider)
Recommended modules/libraries
- Key module: the Anthropic token-counting adapter uses
key.repositorywhen available. If you manage API keys with Key site-wide, the integration is detected automatically. - Ollama provider (
ai_provider_ollama): exposes a local Ollama runtime as a provider, useful as the fallback for quota-exhausted calls at zero cost. - LiteLLM proxy (optional): when configured, Drush commands expose proxy-level spend reports (
ai-metering:litellm-user-spend,ai-metering:litellm-report).
Similar projects
Two other contrib modules address AI usage governance:
- AI Usage Limits: per-provider token limits with hard blocking.
- AI Budget Control: declarative limit rules, scopes, sliding time windows, and anti-flood protection.
AI Metering overlaps with both on core metering, and is closest in scope to AI Budget Control. The main differences are cost transparency across providers (pricing from a 100+ model cost map, multi-currency), a graceful local-model fallback instead of a hard block, and a per-editor dashboard backed by a full per-call audit trail. There is an active conversation about consolidating this overlapping functionality: #3595756.
Supporting this Module
Use the issue queue for bug reports and feature requests. When filing a bug, include your Drupal and module versions and any relevant output from drush watchdog:show --type=ai_metering.
This module is developed by Jérôme Tchania (CodeIt Wisely).
Community Documentation
- AI module: upstream provider abstraction this module builds on
- LiteLLM cost map: source for automated pricing sync
- Models.Dev: source for automated pricing sync
- Frankfurter: ECB exchange rates used for multi-currency display
- Module wiki: installation guide, permissions, Drush commands, and hooks API reference
Significant portions of this module's code were written with AI coding assistance under developer supervision.