Symptoms
- Every app behind the AI gateway gets
429 Too Many Requests, often a few seconds after the first calls of each minute. - Azure OpenAI metrics show low traffic and almost no 429s from the backend.
- It started right after a policy deployment (APIOps pipeline, portal edit, Bicep or Terraform change).
- Rolling back the app doesn't help.
Step 1: prove the 429 comes from APIM, not the model (3 minutes)
With APIM diagnostic logs in Log Analytics (resource-specific mode):
ApiManagementGatewayLogs
| where TimeGenerated > ago(30m)
| where ResponseCode == 429
| summarize count() by ApiId, BackendResponseCode, bin(TimeGenerated, 1m)
| order by TimeGenerated desc
BackendResponseCode | Meaning |
|---|---|
empty or 0 | APIM rejected the call before forwarding it. A gateway policy is throttling |
429 | The backend throttled. Look at quota and capacity, not this runbook |
If you can't query logs quickly, call the API directly with a test subscription key and look at the response headers. Policy-generated 429s don't include the Azure OpenAI headers such as x-ratelimit-remaining-tokens from the backend.
Step 2: read the current policy
Policies can be set at global, product, API and operation scope, and <base /> pulls in the parent scope. Read every level:
SUB=$(az account show --query id -o tsv)
RG=rg-apim-prod
APIM=apim-ai-prod
API=azure-openai
BASE="https://management.azure.com/subscriptions/$SUB/resourceGroups/$RG/providers/Microsoft.ApiManagement/service/$APIM"
# Global (All APIs)
az rest --method get --url "$BASE/policies/policy?api-version=2022-08-01&format=rawxml" --query properties.value -o tsv
# API scope
az rest --method get --url "$BASE/apis/$API/policies/policy?api-version=2022-08-01&format=rawxml" --query properties.value -o tsv
# Products the API belongs to
az apim product list -g $RG -n $APIM --query "[].name" -o tsv
For each product, read $BASE/products/<product>/policies/policy the same way.
Step 3: match the policy to the failure
A correct per-subscription token limit looks like this:
<inbound>
<base />
<azure-openai-token-limit
counter-key="@(context.Subscription.Id)"
tokens-per-minute="100000"
estimate-prompt-tokens="false"
remaining-tokens-header-name="x-apim-remaining-tokens"
tokens-consumed-header-name="x-apim-tokens-consumed" />
</inbound>
APIM keeps one counter per distinct counter-key value. Most incidents come from that key:
| Policy mistake | Effect | Fix |
|---|---|---|
counter-key="@(context.Request.IpAddress)" behind Front Door, Application Gateway or a NAT | All callers share one IP, so all share one counter | Key on subscription, app ID or a validated JWT claim |
counter-key="ai-gateway" (a constant) | One counter for the whole gateway | Use an expression per caller |
Key from a header the apps don't send, e.g. @(context.Request.Headers.GetValueOrDefault("x-team-id","")) | Every caller lands on the empty-string counter | Default to the subscription ID, or reject requests without the header with a clear 400 |
tokens-per-minute off by a factor of 10–1,000 (1000 instead of 100000) | Limit hit after one or two prompts | Restore the reviewed value |
| Policy moved from API scope to global or a product | Applies to APIs that should not be limited, shared counters across APIs | Move it back; prefix the key per API, e.g. @("chat-" + context.Subscription.Id) |
estimate-prompt-tokens="true" with large RAG prompts | Estimated prompt size is counted before the call and can be over-counted | Size the limit for real prompt sizes, or set false and accept post-hoc counting |
| All apps share one APIM subscription key | Per-subscription counter is effectively global | Issue a subscription per app |
llm-token-limit is the provider-neutral version of the same policy and has the same attributes and counter-key pitfalls. Newer APIM versions also support quota attributes (token-quota, token-quota-period) for longer periods. A wrong value there can cause a 403 or 429 that lasts hours, not minutes. Check the policy reference for your tier.
Note
The token counters are approximate under concurrency. Actual usage is only known when the response returns, so parallel requests can briefly go over the limit. Don't tighten the limit to compensate. Size it with headroom.
Step 4: restore service
The fastest safe fix is to redeploy the last known-good policy from source control through your normal pipeline. If the pipeline is broken or slow, put the corrected XML at the right scope directly:
cat > policy.json <<'EOF'
{
"properties": {
"format": "rawxml",
"value": "<policies><inbound><base /><azure-openai-token-limit counter-key=\"@(context.Subscription.Id)\" tokens-per-minute=\"100000\" estimate-prompt-tokens=\"false\" /></inbound><backend><base /></backend><outbound><base /></outbound><on-error><base /></on-error></policies>"
}
}
EOF
az rest --method put \
--url "$BASE/apis/$API/policies/policy?api-version=2022-08-01" \
--body @policy.json
Caution
A PUT replaces the whole policy document at that scope, including backend routing, authentication (
authentication-managed-identity), logging and safety policies. Start from the current XML from Step 2 and change only the token limit element. Removing the backend or auth section by accident creates a second outage.
If you can't identify the bad value in a few minutes, raise tokens-per-minute well above normal per-app usage as a temporary measure. The backend's own deployment quota still protects capacity, so this is safer than leaving every app down.
Then commit the same change to the repo straight away, or the next pipeline run will put the broken policy back.
Step 5: confirm
- The Step 1 query shows no new 429s with an empty
BackendResponseCode. - Response headers show sensible values in
x-apim-remaining-tokensfor two different apps. They should differ, which proves the counters are separate. - Backend token usage climbs back to its normal level.
Prevent it next time
- Keep APIM policies in source control (APIOps or IaC) and deploy only from the pipeline. Block portal edits in production with RBAC.
- Add a policy test in a non-production APIM instance: two test subscriptions send traffic in parallel, and the test fails if one exhausts the other's limit.
- Review counter-key expressions as security-relevant code. Key on identity you trust (subscription or validated token claim), never on client-supplied headers alone or on IP behind a proxy.
- Alert on APIM-generated 429s separately from backend 429s, using the
BackendResponseCodesplit above. - Document the intended limits per app in one place, and size them from measured p95 tokens per minute plus headroom.
Get the weekly HubDatAI email
One email a week with everything new: posts, learning sessions and videos. Free, no spam, unsubscribe in one click. What you get