Contents
Introduction
I wanted to try AI Gateway cost limits in practice after my previous walkthrough. Setting a budget policy is straightforward enough in AI Gateway, but what does it actually do, and what are the options.
For this blog, I’m using the same Foundry model with two different Entra accounts. One as a default cost budget, whilst the other is to allow higher allowance through the policy override.
How does it work?
From reading the docs, AI Gateway uses the reported token usage and public model prices to estimate how much each call costs. It adds that to the caller’s spend counter, then checks the counter before letting another request through. I did notice however, that budgets seem to be in US dollars, even if your Azure subscription is billed in GBP or another currency, from what I can tell.
You can set renewal periods to hourly, daily, weekly, monthly or yearly. These follow the calendar, so an hourly budget resets on the hour (UTC). See more: Microsoft’s cost-budget walkthrough.
Additionally, this is estimated model spend only here; gateway and monitoring charges aren’t included.
Note
A request can take you over budget before the next one gets blocked, and requests running at the same time can push it further over.
Setting AI Gateway cost limits
Go to Policies → Add policy → Cost limit in the AI Gateway portal:

Here, I am choosing individual assets and select the backend model. I’m using gpt-4.1-mini (of course it’s the model of choice for a demo 😊). You can also choose an alias, or Gateway for a budget covering the workspace’s models:

Here I am setting my Budget amount to $1, Renewal period to Hourly and Counter dimensions to Identity:

With Identity selected, each caller gets their own allowance based on the Entra token’s tenant ID and object ID. If everyone uses your app through the same managed identity, they’ll share that budget. The gateway will only see the identity making the call rather than your users in that scenario.
Overrides
Next, you can add a Microsoft Entra identity and give it a different budget. My second account gets $3 per hour; my main account keeps the $1 default for my example policy.

You can match a specific identity or a combination of validated token claims. API keys have overrides too. Where several rules match, the first one wins as priority in my testing. Although API keys is an option here, MI is massively preferred as best practice.
After saving, my policies look like this:

I’ve also got a $50 gateway budget per caller and the token limit from the previous blog walkthrough. These still apply alongside the model budget. Giving my second account $3 doesn’t override those other limits.
RBAC gotchas
I hit a permissions issue when trying to save the policy via the Portal. My portal account could view the model but couldn’t save the policy, with the error:
Microsoft.ApiManagement/aigateways/workspaces/modelProviders/models/write
So I went and looked at the roles available and decided to plot them in the table. Here are the roles for each bit you may want to consider:
| Task | Role and scope |
|---|---|
| Edit the model policy | AI Gateway Contributor on the gateway |
| Edit the gateway-wide policy | Contributor on the gateway |
| Call the model alias | AI Gateway Models User on the alias |
| Gateway calls Foundry | Foundry User for the gateway identity on the backend |
AI Gateway Contributor currently doesn’t include Microsoft.ApiManagement/aigateways/workspaces/write, which you need for the gateway-wide policy. Also Owner on its own won’t let you call the model either. My second account got a 403 until I added AI Gateway Models User on the alias.
In action
Here are the response headers from one of my calls. Notice the x-cost-* headers showing the policy in action:

The gateway reports $0.0001484 for this request and $0.9951888 remaining in the caller’s hourly budget. The consumed name is a bit unhelpful here: it’s the individual request’s estimated cost, rather than the running hourly total. You can also find the per-request estimate in the Log Analytics workspace under azure.ai_gateway.client.token.cost.
Go to Monitoring → Models to see estimated spend alongside token usage. You can also check the policy results under Policies, or open a request under Traces. These use the gateway’s Application Insights telemetry.

For the earlier two account test, I first checked both accounts could call the model, then lowered the default budget to $0.0001 and the override to $0.0003 for my demo of the policy just so it would trigger the policy quickly.
My main account hit its budget and was blocked, whilst the account with the override kept working (as expected). That account was then blocked when it reached its own higher limit.
Below is the same cost budget block I got from a later call when sending requests to test this via the REST API in PowerShell:

Bicep
You can configure and deploy this with Bicep too. The cost policy sits in the model’s properties.policies array, with type: 'costLimit'. The Bicep resource is Microsoft.ApiManagement/aigateways/workspaces/modelProviders/models@2026-11-01.
Here’s an example policy object for my demo $1 default and $3 override:
targetScope = 'resourceGroup'
@description('Existing AI Gateway name.')
param gatewayName string
@description('Existing model provider name.')
param providerName string = 'foundry'
@description('Existing registered model name.')
param modelName string = 'gpt-4.1-mini'
@description('Writable model properties, including its endpoints, deployment and any other policies. GET the model before adopting a portal-created resource.')
param currentModelProperties object
@description('Keep the existing policy ID when adopting a portal-created policy, so its counter identity is preserved.')
param costPolicyId string
@description('Estimated USD per caller. A string permits decimal values in Bicep.')
param budgetAmount string = '1'
@description('Estimated USD for the selected caller.')
param overrideAmount string = '3'
@description('Tenant-local object ID of the user or service principal receiving the override.')
param overrideObjectId string
@allowed(['hour', 'day', 'week', 'month', 'year'])
param renewalPeriod string = 'hour'
resource gateway 'Microsoft.ApiManagement/aigateways@2026-11-01' existing = {
name: gatewayName
}
resource workspace 'Microsoft.ApiManagement/aigateways/workspaces@2026-11-01' existing = {
parent: gateway
name: 'default'
}
resource provider 'Microsoft.ApiManagement/aigateways/workspaces/modelProviders@2026-11-01' existing = {
parent: workspace
name: providerName
}
var costPolicy = {
type: 'costLimit'
id: costPolicyId
amount: json(budgetAmount)
period: renewalPeriod
counterKey: ['identity']
scope: 'resource'
overrides: [{
amount: json(overrideAmount)
claims: [
{ name: 'tid', value: tenant().tenantId }
{ name: 'oid', value: overrideObjectId }
]
}]
}
// Preserve all unrelated policies. A PUT replaces the model, so currentModelProperties must include the complete writable configuration.
var otherPolicies = filter(currentModelProperties.?policies ?? [], policy => policy.id != costPolicyId)
resource model 'Microsoft.ApiManagement/aigateways/workspaces/modelProviders/models@2026-11-01' = {
parent: provider
name: modelName
properties: union(currentModelProperties, {
policies: concat(otherPolicies, [costPolicy])
})
}
output modelResourceId string = model.id
Give currentModelProperties the model’s existing writable settings, including its deployment, endpoints and other policies. Bicep replaces the model configuration, so don’t leave those out.
Keep the same policy ID when moving from the portal to Bicep. Changing the budget amount won’t reset what’s already been spent. For fractional dollar amounts, use json('0.001') in Bicep (a bit like the ACA CPU workaround if you’re familiar with that).
There are also Azure CLI commands for setting budgets and caller overrides.
And that’s it! Hopefully this gives you some useful insight into one of the key AI Gateway policy features that is quite nice and very easy to set up now.




reply