Skip to content

Hooks API

/**
 * Alter a usage record before it is written to ai_metering_usage.
 *
 * @param array $record
 *   Keys: uid, timestamp, provider_id, model_id, operation, input_tokens,
 *   output_tokens, cached_tokens, estimated_cost_usd, status.
 */
function hook_ai_metering_record_alter(array &$record): void {}

/**
 * React when a user's monthly AI token quota is exceeded.
 *
 * Informational only — fires before the fallback reroute or
 * AiQuotaException; it does not change routing behavior.
 *
 * @param int $uid
 * @param string $providerId
 * @param array $quota
 *   Keys: monthly_budget, tokens_used, tokens_remaining, pct_used,
 *   exceeded, alert_threshold.
 */
function hook_ai_metering_quota_exceeded(int $uid, string $providerId, array $quota): void {}

\Drupal\ai_metering\Event\MeteringRecordCreatedEvent (event name ai_metering.record_created) is also dispatched after every usage record is persisted, exposing getUid(), getProviderId(), getModelId(), getOperationType(), getInputTokens(), getOutputTokens(), getCachedTokens(), and getCostUsd() — useful for pushing data to an external analytics service or triggering a webhook outside the alert-channel system.