Azure AI Foundry

The services_ai_foundry role deploys an Azure AI Foundry account (a dedicated resource group, virtual network and private endpoint) with one or more model deployments. This page covers choosing models, controlling spend (the cost ceiling and the key-rotation kill-switch), how output-token pricing is resolved, and change-detection alerting.

Finding deployable models

Each entry under services_ai_foundry.models must name a model, version and SKU that Azure actually offers in your deployment region. Rather than guess, list what is deployable with the Azure CLI and copy the values into your configuration.

List deployable models for a region

$ az cognitiveservices model list \
    --location westeurope \
    --query "[].{model:model.name, version:model.version, skus:join(',', model.skus[].name)}" \
    -o table

The model column is the value for model:, version for version: and any of the listed skus for sku: (commonly GlobalStandard, DataZoneStandard or Standard).

Prefer the current/default version. A version that has been retired for new deployments can still appear in this list, so a listed version is necessary but not always sufficient; if a deployment is rejected at apply time, pick the newest version the command reports.

Check what an existing account can deploy

Once an account exists, this shows exactly what that account can deploy in its region, which is the authoritative check:

$ az cognitiveservices account list-models \
    -g <resource-group> -n <account-name> -o table

Example configuration

services_ai_foundry:
  enabled: true
  cost_ceiling_usd: 100        # monthly USD ceiling for the whole account
  alert_at_percent: 80         # email heads-up
  kill_at_percent: 90          # rotate the keys (hard stop)
  guardian_interval_minutes: 1 # how often the kill-switch checks spend
  models:
    - name: gpt-4o
      model: gpt-4o
      version: "2024-11-20"
      sku: GlobalStandard
    - name: claude-sonnet-4
      format: Anthropic
      model: claude-sonnet-4
      sku: GlobalStandard
      # Not in the retail catalogue, so give the output-token price explicitly.
      price_per_1k_tokens_usd: 0.015

Controlling spend: the cost ceiling and kill-switch

Spend is governed by a single number, cost_ceiling_usd — a monthly ceiling in USD for the whole account. Two percentages act on it:

alert_at_percent (default 80)

Emails the users tagged ai_alert in users.csv. A heads-up only, no enforcement.

kill_at_percent (default 90)

The hard stop. When month-to-date spend reaches this percentage the account’s API keys are regenerated, which immediately cuts off every agent still using the old key. Logs and data are left intact — it is a hard stop, not a teardown.

The 10 % gap between the alert and the kill, and the further gap up to the 100 % backstop below, is deliberate head-room so a lagging signal still stops spend before it runs away.

How the kill is enforced

Two independent mechanisms, by design:

Near-real-time (primary)

A per-account guardian Logic App runs every guardian_interval_minutes minutes (default 1). Using a shared read-only managed identity it reads each model deployment’s output tokens from the metrics API, multiplies by that model’s output-token price, sums the account total, and once it reaches kill_at_percent of the ceiling it triggers the pipeline that rotates the keys. Because it computes exact per-model cost it reacts within one interval of a real breach; any read error fails the run safely without firing, and the agent only spins up on an actual breach. Raise guardian_interval_minutes to cut Logic App run cost in exchange for a slower reaction.

Budget backstop (fallback)

An Azure cost budget on the same ceiling posts the same kill at 100 % of the ceiling, and emails the ai_alert users at the same time. It is price-independent — it reads real billed USD, not token metrics — so it catches anything the token math misses: non-text models, a price the matcher could not resolve, or a stalled Logic App. It lags by hours, which is why it sits above the near-real-time kill rather than replacing it.

Why it doesn’t re-fire every minute

Month-to-date spend stays above the threshold after a kill, so a naive check would re-trigger every minute and spin up an agent each time. Instead the guardian stamps a guardianResetAt timestamp tag on the account when it fires, and the Logic App only counts tokens since max(start-of-month, guardianResetAt). Rotating the keys stops new usage, so that window stays empty and the switch goes quiet on its own. The tag is excluded from Terraform’s state (lifecycle.ignore_changes) so a later deploy does not wipe it.

Recovering after a kill

Recovery is deliberate and manual: an operator regenerates (re-issues) the account keys and hands the new key to the consumers. The reset timestamp is already in place, so the counter simply resumes from the kill point — the Logic App needs no attention. A deliberate re-issue can therefore spend up to another ceiling’s worth in the same month; permanently raising the cap instead means editing cost_ceiling_usd and redeploying (or changing it in Azure by hand). That friction is intentional — nobody bumps the ceiling by reflex.

Output-token pricing

The kill-switch turns token counts into USD, so it needs each model’s output-token price. That price is pulled automatically from the Azure Retail Prices API using the model and version you configured, and it resolves for most current OpenAI, Meta Llama, Microsoft Phi and xAI Grok models.

Some models cannot be priced automatically: a few are billed under a name that differs from their deployment id (for example DeepSeek), and some are not published in the retail catalog at all (for example Anthropic Claude). In those cases the deploy stops with a clear error naming the model, and you set price_per_1k_tokens_usd on that model to continue. Read the output-token price from the pricing pages — Azure OpenAI and Microsoft Foundry models — which quote per 1,000,000 tokens; divide by 1000 for the per-1,000-token value this field expects.

Change-detection alerting

The role also adds an activity-log alert that emails the ai_alert users on administrative changes to the Foundry account and its model deployments — a new or changed deployment, a public-access or network-ACL change, or a delete. It is scoped to the account, not the whole resource group, so the routine networking, private-endpoint and budget writes a deploy makes do not generate mail. The guardian’s own key rotation is an account write, so a kill shows up here too — the intended audit trail of an enforcement action.