# Stolen access to a model shows up as spend. Azure counts each account's # tokens, prompt plus completion, as the platform metric TotalTokens, with no # diagnostic setting needed. One alert per account fires when an hour's total # passes tokens_per_hour, checked every 15 minutes. # # Azure checks the metric name and namespace when the alert is created, so a # misspelling fails at apply; terraform plan cannot catch it. resource "azurerm_monitor_metric_alert" "tokens" { for_each = toset(var.ai_account_ids) name = "ai-tokens-${element(split("/", each.value), 8)}" resource_group_name = data.azurerm_resource_group.security.name scopes = [each.value] description = "The account ${element(split("/", each.value), 8)} processed more than ${var.tokens_per_hour} tokens in an hour. The saved query ai-calls-by-caller shows who called which deployment." severity = 2 frequency = "PT15M" window_size = "PT1H" tags = local.tags criteria { metric_namespace = "Microsoft.CognitiveServices/accounts" metric_name = "TotalTokens" aggregation = "Total" operator = "GreaterThan" threshold = var.tokens_per_hour } action { action_group_id = azurerm_monitor_action_group.ai.id } }