Install
Works with Drupal: ^10.5 || ^11.2Using Composer to manage Drupal site dependencies
Alternative installation files
Download tar.gz
81.33 KB
MD5: ac772a636605ce7a9624a9232061e10e
SHA-1: a6b48272d2f49d30a766e9a7f0931926f1cf6fe6
SHA-256: bc44b29ed9b78720849adb16e325658c4c06947ca54d470645397092570a0d59
Download zip
121.12 KB
MD5: 46ae43a97a50ce58e879e00c03ba5794
SHA-1: e3fca09c846bb41c9deb8578070efd0e3a27106d
SHA-256: 4d45b9ec130bad41ca751cd22e55002492e546f9fcfa57de07a3c5619f13e269
Release notes
This is the first public alpha of AI Metering — a module that tracks token usage and cost for every AI operation dispatched through the Drupal AI module (chat, translation, summarization, and any other operation type). It is intended for developers and early adopters. The API may change before the stable release.
Requirements: Drupal 10.5+ or 11.2+, AI module, at least one provider module (e.g. ai_provider_anthropic, ai_provider_openai, ai_provider_ollama).
Token tracking and cost calculation
- Intercepts every AI call dispatched through the
aimodule — no per-feature instrumentation required. Works with chat, translate_text, summarize_text, and all other operation types. - Records provider, model, operation type, input tokens, output tokens, cached tokens (prompt cache hits), and estimated cost in USD to a dedicated
ai_metering_usagetable. - Model pricing synced from the LiteLLM public cost map (100+ models). Manual price overrides are preserved across syncs.
- Optional integration with a self-hosted LiteLLM proxy for authoritative cost data via Drush.
Per-user monthly quotas and Ollama fallback
- Configurable default monthly token budget per user, with per-role defaults and individual overrides settable inline from the admin dashboard.
- When a user's quota is exceeded, requests are transparently rerouted to a configurable fallback provider — for example a local Ollama model — with no error shown to the user.
- Fallback works correctly across all operation types, including
translate_text. Token estimation coversTranslateTextOutputand all other output types, not justChatOutput. - Email notifications sent to the user and/or site admin when the fallback activates.
- Configurable alert threshold (default 75%) triggers email alerts before the quota is fully consumed. Per-user threshold overrides available from the dashboard.
Usage dashboard and reporting
- Per-editor dashboard at
/admin/reports/ai-metering— token counts, estimated cost in display currency, quota progress bar, and inline budget and threshold overrides via AJAX (no page reload). - Per-call usage log at
/admin/reports/ai-metering/log— full audit trail with provider, model, operation type, tokens, cost, and fallback status. - CSV and JSON export of the full usage log for billing and reporting.
- Two optional Views shipped as
config/optional— activate automatically when the Views UI module is enabled.
Multi-currency display
- Costs are stored in USD (provider-native). Display currency is configurable: USD, CAD, EUR, GBP, AUD, JPY.
- Live exchange rates from Frankfurter (free, no API key required, ECB-sourced), cached for 1 hour by default.
- Manual fallback rates configurable in the admin form for offline resilience.
Developer API
hook_ai_metering_record_alter(array &$record)— modify a usage record before it is written to the database (add custom fields, adjust costs, tag for external billing).hook_ai_metering_quota_exceeded(int $uid, string $providerId, array $quota)— react when a user's monthly quota is reached (push to external systems, trigger workflows).- Drush commands:
drush aim-quota-reset,drush aim-budget,drush aim-sync,drush aim-user,drush aim-report.
Coming in 1.0.0 — Role-based budget pools
- Collective role pools: instead of a per-user default budget, assign a shared monthly token pool to a role (e.g. 500 000 tokens for all Editors). When the pool is exhausted, all users in that role fall back to Ollama automatically — no per-user configuration required. Designed for sites with dozens or hundreds of editors.
- Role-centric dashboard: the current per-user table will be replaced by a role-level chart (one progress bar per role showing pool utilization), with a drill-down page per role listing individual user consumption. Per-user overrides move to a dedicated sub-page.
- Pool-level alerts: threshold notifications fire at the pool level (e.g. "the Editor pool is at 80%") and are sent to configurable per-role recipients — one signal per role per day, not one per user.
Known limitations in this alpha
- Slack/webhook budget alerts are not yet implemented (email only).
- No usage forecasting based on historical data.
- Budget-based circuit breaking (hard-blocking AI calls when quota is exhausted) is planned but not yet available.