Your team wants to try gpt-6.1-sol, but every app already sends "model": "chat", and nobody wants a big-bang
switch. In this lab you put the current model and the new one behind the same alias, send a fifth of the traffic
to the new model, count where the requests land, and then use priority to choose which model is preferred. The
apps never change the model name they send.
Before you begin
One-time setupThe Setup guide tasks that this lab uses. Mark each one done once, and the labs stop listing it.
- Choose where to run commandsEvery track except the portal
- Sign in to AzureOn your own machine. Cloud Shell is already signed in.
- Install the
aigatewayextensionThe Azure CLI tracks and the Verify steps - Clone the labs repoThe Bicep + azd and Terraform tracks, the notebooks, and the code samples
You've done the one-time setup that this lab uses.
On the Bicep + azd and Terraform tracks, azd up or terraform apply creates its own gateway, both
models, the alias, and a key, so you can skip this section. You need only the one-time setup above.
- Complete Publish a Foundry model and make your first call. You need the
gateway, a runtime API key, and the
gpt-5-minibackend model. Lab 02 also gave the gateway the Foundry User role on your Foundry account, which covers the new deployment in this lab. - Permission to change the gateway, for example Contributor on its resource group, and to create deployments in the Foundry account.
- Optional: Publish models from any provider behind stable aliases explains aliases in more depth.
- Run the scripts and samples from the lab's folder,
~/aigateway/labs/22-model-routing.
Set these variables once. Every track and the Verify section reuse them.
# Name your gateway, the Foundry account from lab 02, the two backend models, and the alias that clients call
export RG="<resource-group>"
export GATEWAY="<gateway>"
export FOUNDRY_RG="<foundry-resource-group>" # resource group of the Foundry account from lab 02
export FOUNDRY_ACCOUNT="<foundry-account-name>"
export PROVIDER="foundry-models" # the gateway provider that holds gpt-5-mini
export PRIMARY="gpt-5-mini" # the backend model that serves today's traffic
export CANDIDATE="gpt-6.1-sol" # the new model to try
export CANDIDATE_VERSION="2026-09-29"
export ALIAS="chat" # the name that clients send in "model"
echo "📝 Alias $ALIAS will route between $PRIMARY and $CANDIDATE on gateway $GATEWAY in $RG"
# Read the resource IDs of the gateway and the Foundry account, and the gateway's runtime URL
export GATEWAY_ID="$(az aigateway show -g "$RG" -n "$GATEWAY" --query id -o tsv)"
export FOUNDRY_ID="$(az cognitiveservices account show -g "$FOUNDRY_RG" -n "$FOUNDRY_ACCOUNT" --query id -o tsv)"
export AI_GATEWAY_URL="https://$(az aigateway show -g "$RG" -n "$GATEWAY" --query properties.frontend.defaultHostname -o tsv)"
echo "🆔 Gateway: $GATEWAY_ID"
echo "🆔 Foundry account: $FOUNDRY_ID"
echo "🌐 Runtime URL: $AI_GATEWAY_URL"
# Paste the gateway runtime key from lab 02 without showing it, and don't commit it or write it to a file
printf "AI Gateway API key: " && read -rs AI_GATEWAY_API_KEY && export AI_GATEWAY_API_KEY && echo
echo "🔑 Key: in AI_GATEWAY_API_KEY (${#AI_GATEWAY_API_KEY} characters, not shown)"# Name your gateway, the Foundry account from lab 02, the two backend models, and the alias that clients call
$RG = "<resource-group>"
$GATEWAY = "<gateway>"
$FOUNDRY_RG = "<foundry-resource-group>" # resource group of the Foundry account from lab 02
$FOUNDRY_ACCOUNT = "<foundry-account-name>"
$PROVIDER = "foundry-models" # the gateway provider that holds gpt-5-mini
$PRIMARY = "gpt-5-mini" # the backend model that serves today's traffic
$CANDIDATE = "gpt-6.1-sol" # the new model to try
$CANDIDATE_VERSION = "2026-09-29"
$ALIAS = "chat" # the name that clients send in "model"
Write-Host "📝 Alias $ALIAS will route between $PRIMARY and $CANDIDATE on gateway $GATEWAY in $RG"
# Read the resource IDs of the gateway and the Foundry account, and the gateway's runtime URL
$GATEWAY_ID = az aigateway show -g $RG -n $GATEWAY --query id -o tsv
$FOUNDRY_ID = az cognitiveservices account show -g $FOUNDRY_RG -n $FOUNDRY_ACCOUNT --query id -o tsv
$env:AI_GATEWAY_URL = "https://$(az aigateway show -g $RG -n $GATEWAY --query properties.frontend.defaultHostname -o tsv)"
Write-Host "🆔 Gateway: $GATEWAY_ID"
Write-Host "🆔 Foundry account: $FOUNDRY_ID"
Write-Host "🌐 Runtime URL: $env:AI_GATEWAY_URL"
# Paste the gateway runtime key from lab 02 without showing it, and don't commit it or write it to a file
$env:AI_GATEWAY_API_KEY = Read-Host "AI Gateway API key" -MaskInput
Write-Host "🔑 Key: in AI_GATEWAY_API_KEY ($($env:AI_GATEWAY_API_KEY.Length) characters, not shown)"The CLI and Bicep tracks of lab 02 name the provider foundry-models. A portal import names it after your
Foundry account. To check, list the backend models. The Provider column shows the provider of each one.
# List the backend models on the gateway, with the provider that holds each one
echo "✨ Backend models on $GATEWAY:"
az aigateway model list -g "$RG" --gateway-name "$GATEWAY" -o table# List the backend models on the gateway, with the provider that holds each one
Write-Host "✨ Backend models on ${GATEWAY}:"
az aigateway model list -g $RG --gateway-name $GATEWAY -o tableDeploy the candidate model in the same Foundry account as gpt-5-mini. Pick a region where the model is
available. Model inference is billed per use.
# Deploy the candidate model next to gpt-5-mini, with 10,000 tokens per minute of Global Standard capacity
az cognitiveservices account deployment create -g "$FOUNDRY_RG" -n "$FOUNDRY_ACCOUNT" \
--deployment-name "$CANDIDATE" --model-name "$CANDIDATE" --model-version "$CANDIDATE_VERSION" \
--model-format OpenAI --sku-name GlobalStandard --sku-capacity 10 \
--query "{name:name, state:properties.provisioningState}" &&
echo "✨ Model deployment $CANDIDATE is ready in $FOUNDRY_ACCOUNT"# Deploy the candidate model next to gpt-5-mini, with 10,000 tokens per minute of Global Standard capacity
az cognitiveservices account deployment create -g $FOUNDRY_RG -n $FOUNDRY_ACCOUNT `
--deployment-name $CANDIDATE --model-name $CANDIDATE --model-version $CANDIDATE_VERSION `
--model-format OpenAI --sku-name GlobalStandard --sku-capacity 10 `
--query "{name:name, state:properties.provisioningState}"
if ($?) { Write-Host "✨ Model deployment $CANDIDATE is ready in $FOUNDRY_ACCOUNT" }How it works
An alias holds a list of backend models. Each one has a priority and a weight:
| Setting | Values | Default | What it does |
|---|---|---|---|
| Priority | Whole number from 0 to 100 | 0 | Lower numbers are preferred. Backend models with the same priority form a group, and requests go to the available group with the lowest number. |
| Weight | Whole number from 1 to 100 | 1 | Splits the requests within a group in proportion to the weights. Weights count requests, not tokens or cost. |
A backend model's Share within priority is its weight divided by the sum of the weights at its priority.
Weights 80 and 20 give 80% and 20%; weights 3 and 1 give 75% and 25%. Weights are relative, so 5 and
1 split the requests like 50 and 10. With one backend model, the alias behaves like the single-target aliases
of labs 02 and 03.
Priority and weight are two steps of the alias's routing order. The gateway first drops the backend models whose backend condition doesn't match the request. Then it applies priority, auto model selection, and session affinity in the order that the alias sets. Weights choose among the backend models that remain. This lab uses only priority and weight, so the gateway picks the lowest priority group, then a backend model in it by weight.
Pick the values from the job you need to do:
| Scenario | Priority | Weight |
|---|---|---|
| Canary a new model version | Same for both | Small share for the new version, for example 95 and 5, then raise it |
| A/B test two models | Same for both | Equal, for example 50 and 50 |
| Balance deployments across regions or providers | Same for all | In proportion to each deployment's capacity, for example 3 and 1 |
| Prefer purchased capacity | Provisioned deployment 0, pay-as-you-go 1 | Any |
A few rules keep every request answerable:
- Each backend model can appear only once in an alias.
- All backend models must support at least one interface in common, for example OpenAI Chat Completions.
Mixing an Anthropic Messages model with an OpenAI model in one alias fails with
Active-priority models must share at least one supported endpoint. - The response tells you which backend served the request: the
modelfield holds the backend's model name and version, for examplegpt-6.1-sol-2026-09-29, and Foundry models also return anx-ms-served-modelheader.
Priority sets a preference, not a failover. Without failure rules on the alias, when the preferred backend
returns an error such as 429 or 404, the gateway passes that error to the client and doesn't try a
lower-priority backend for that request. To retry the backend or switch to the next one, add failure rules to the
alias, as in Fail over to a backup model when a backend fails.
Split traffic 80/20 between two models
Register the candidate model in the gateway, then create the chat alias with both models at priority 0 and
weights 80 and 20.
Import the candidate model
- Open the AI Gateway portal and select your gateway in the top bar.
- Select ModelsAddModels from Microsoft Foundry.
- In Subscriptions and Resource groups, select those of your Foundry account. Keep Managed identity as the Authentication method.
- Clear Create an alias for each model: this lab publishes the model through the
chatalias. - In Models, select only the
gpt-6.1-soldeployment of your Foundry account, then select Add models.
Create the chat alias
- Select ModelsAddAlias for existing models.
- On Basics, set Alias name to
chatand Description toCanary: 80/20 between the current and the new model. Select Next. - On Auto model selection (preview), leave Choose a model based on the prompt off. Select Next.
- On Select models, select
gpt-5-miniandgpt-6.1-solin the grid. Select Next. - On Session affinity, clear Enable session affinity, which is on by default, so that the weights alone split the requests. Select Next.
- On Priorities and weights, keep Priority at
0for both models. Set Weight to80forgpt-5-miniand20forgpt-6.1-sol. Share within priority shows80%and20%. Select Next. - On Failure handling, keep No custom failure rules configured. Select Next.
- On Review, check the shares, then select Create.
The alias's Overview shows Session affinity Disabled and Routing order Priorities only. Its
Backend models tab shows gpt-5-mini and gpt-6.1-sol, both at priority 0, with weights 80 and 20.
Register the candidate model
A backend model registers one deployment under the provider. This is the same command as in lab 02, with the model version.
# Register the candidate deployment as a backend model under the provider, with its model version
az aigateway model create -g "$RG" --gateway-name "$GATEWAY" --provider-name "$PROVIDER" -n "$CANDIDATE" \
--display-name "$CANDIDATE" --api-format OpenAIChatCompletions \
--supported-endpoints /openai/v1/chat/completions /openai/v1/responses \
--deployment-config "{resourceId:$FOUNDRY_ID/deployments/$CANDIDATE,modelName:$CANDIDATE,modelVersion:$CANDIDATE_VERSION}" \
--query "{name:name, version:properties.deployment.modelVersion}" &&
echo "✨ Model $CANDIDATE is published through $PROVIDER"# Register the candidate deployment as a backend model under the provider, with its model version
az aigateway model create -g $RG --gateway-name $GATEWAY --provider-name $PROVIDER -n $CANDIDATE `
--display-name $CANDIDATE --api-format OpenAIChatCompletions `
--supported-endpoints /openai/v1/chat/completions /openai/v1/responses `
--deployment-config "{resourceId:$FOUNDRY_ID/deployments/$CANDIDATE,modelName:$CANDIDATE,modelVersion:$CANDIDATE_VERSION}" `
--query "{name:name, version:properties.deployment.modelVersion}"
if ($?) { Write-Host "✨ Model $CANDIDATE is published through $PROVIDER" }Create the chat alias
Each --backend-model adds an entry to the alias's backendModels list, with a resource-id, a priority, and
a weight. With more than one backend model, each entry needs both a priority and a weight.
# Build the resource ID prefix of the provider's backend models, which each --backend-model entry uses
export MODELS="$GATEWAY_ID/workspaces/default/modelProviders/$PROVIDER/models"
# Create or update the alias with both backend models at priority 0 and weights 80 and 20, then print the routing
echo "🔖 Alias $ALIAS routes to:"
az aigateway model-alias set -g "$RG" --gateway-name "$GATEWAY" -n "$ALIAS" \
--description "Canary: 80/20 between the current and the new model" \
--backend-model resource-id="$MODELS/$PRIMARY" priority=0 weight=80 \
--backend-model resource-id="$MODELS/$CANDIDATE" priority=0 weight=20 \
--only-show-errors --query properties.backendModels -o json |
jq -r '.[] | "\(.resourceId | split("/") | last) priority \(.priority) weight \(.weight)"'# Build the resource ID prefix of the provider's backend models, which each --backend-model entry uses
$MODELS = "$GATEWAY_ID/workspaces/default/modelProviders/$PROVIDER/models"
# Create or update the alias with both backend models at priority 0 and weights 80 and 20, then print the routing
Write-Host "🔖 Alias $ALIAS routes to:"
az aigateway model-alias set -g $RG --gateway-name $GATEWAY -n $ALIAS `
--description "Canary: 80/20 between the current and the new model" `
--backend-model resource-id="$MODELS/$PRIMARY" priority=0 weight=80 `
--backend-model resource-id="$MODELS/$CANDIDATE" priority=0 weight=20 `
--only-show-errors --query properties.backendModels -o json |
ConvertFrom-Json | ForEach-Object { '{0} priority {1} weight {2}' -f ($_.resourceId -split '/')[-1], $_.priority, $_.weight }az aigateway model-alias set creates the alias, or updates it if it exists, so you can run the step again.
The output shows gpt-5-mini priority 0 weight 80 and gpt-6.1-sol priority 0 weight 20.
What you need: only the one-time setup from Before you begin. This track doesn't use your gateway from lab 02:
azd up creates a new one, so it works on its own.
Review the templates
infra/main.bicep creates the resource group rg-<environment name> and calls the quick start's
resources.bicep for the gateway, gpt-5-mini, its default alias, and the
app-first-call key. Then it deploys the candidate model in the same Foundry account and publishes the chat
alias:
// Lab 22 with azd: "azd up" creates the resource group rg-<environment name>, the quick start's gateway, Foundry
// model, default alias, and runtime API key, then deploys a candidate model in the same Foundry account and publishes
// the chat alias, which routes across both models by weight or by priority. The runtime API key is never an output.
targetScope = 'subscription'
@description('azd environment name. The resource group is rg-<environmentName>.')
@minLength(1)
@maxLength(60)
param environmentName string
@description('Region for every resource. Pick one where AI Gateway and the lab\'s Foundry models are available. List them with az provider show --namespace Microsoft.ApiManagement (Setup guide, Find the available regions).')
@metadata({ azd: { type: 'location' } })
param location string
@description('Candidate model to deploy in Foundry and route to. azd reads it from CANDIDATE_MODEL.')
param candidateModelName string = 'gpt-6.1-sol'
@description('Version of the candidate model. azd reads it from CANDIDATE_MODEL_VERSION.')
param candidateModelVersion string = '2026-09-29'
@description('weighted: both models share priority 0 and split traffic by weight. priority: the primary is preferred. azd reads it from ROUTING_STRATEGY.')
@allowed([
'weighted'
'priority'
])
param strategy string = 'weighted'
@description('Weighted strategy only: the candidate weight. The primary gets 100 minus this value. azd reads it from ROUTING_CANDIDATE_WEIGHT.')
@minValue(1)
@maxValue(99)
param candidateWeight int = 20
var suffix = take(uniqueString(subscription().id, environmentName, location), 6)
var tags = { 'azd-env-name': environmentName }
resource rg 'Microsoft.Resources/resourceGroups@2024-03-01' = {
name: 'rg-${environmentName}'
location: location
tags: tags
}
// The quick start's template: a gateway, gpt-5-mini behind the default alias, and the app-first-call key.
module base '../../quickstart/infra/resources.bicep' = {
name: 'aigw-base'
scope: rg
params: {
location: location
tags: tags
gatewayName: 'aigw-${suffix}'
foundryName: 'aigw${suffix}'
}
}
// The candidate deployment, in the same Foundry account. The gateway's Foundry User role already covers it.
module candidate 'candidate-deployment.bicep' = {
name: 'aigw-candidate-deployment'
scope: rg
params: {
foundryName: base.outputs.foundryName
modelName: candidateModelName
modelVersion: candidateModelVersion
}
}
module routing 'alias-routing.bicep' = {
name: 'aigw-alias-routing'
scope: rg
params: {
gatewayName: base.outputs.gatewayName
providerName: base.outputs.providerName
primaryModelName: base.outputs.modelName
foundryAccountId: candidate.outputs.foundryId
candidateDeploymentName: candidate.outputs.deploymentName
candidateModelVersion: candidateModelVersion
aliasName: 'chat'
strategy: strategy
candidateWeight: candidateWeight
}
}
output AZURE_RESOURCE_GROUP string = rg.name
output AI_GATEWAY_NAME string = base.outputs.gatewayName
output AI_GATEWAY_ID string = base.outputs.gatewayId
output AI_GATEWAY_URL string = base.outputs.gatewayUrl
output AI_GATEWAY_PROVIDER string = base.outputs.providerName
output AI_GATEWAY_BACKEND_MODEL string = base.outputs.modelName
output AI_GATEWAY_CANDIDATE_MODEL string = routing.outputs.candidateModelName
output AI_GATEWAY_MODEL string = routing.outputs.aliasName
output FOUNDRY_ACCOUNT_NAME string = base.outputs.foundryNameinfra/candidate-deployment.bicep deploys gpt-6.1-sol next to gpt-5-mini. The gateway's Foundry User role
on the account already covers it:
// Deploys the candidate model in an existing Foundry account, next to the primary model. main.bicep calls it after the
// quick start's template has created the account and its gpt-5-mini deployment.
targetScope = 'resourceGroup'
@description('Name of the existing Foundry (Azure AI Services) account.')
param foundryName string
@description('Model to deploy. The deployment uses the same name.')
param modelName string = 'gpt-6.1-sol'
@description('Version of the model to deploy.')
param modelVersion string = '2026-09-29'
@description('Deployment capacity in thousands of tokens per minute.')
@minValue(1)
param capacity int = 10
resource foundry 'Microsoft.CognitiveServices/accounts@2025-04-01-preview' existing = {
name: foundryName
}
resource deployment 'Microsoft.CognitiveServices/accounts/deployments@2025-04-01-preview' = {
parent: foundry
name: modelName
sku: {
name: 'GlobalStandard'
capacity: capacity
}
properties: {
model: {
format: 'OpenAI'
name: modelName
version: modelVersion
}
}
}
output deploymentName string = deployment.name
output foundryId string = foundry.idinfra/alias-routing.bicep registers the candidate deployment under the existing provider and publishes the alias.
It only references gpt-5-mini, so that model's settings stay as they are. Its strategy parameter switches
between a weighted split and a priority preference. You can also deploy it on its own to the gateway from lab 02:
// Routes one callable alias across two Foundry backend models. main.bicep calls it after the quick start's template,
// and you can also deploy it on its own to a gateway you already have, such as the one from lab 02.
// Registers a candidate deployment under the existing Foundry provider, then publishes an alias that either splits
// traffic between the primary and the candidate by weight, or prefers the primary by priority.
// The primary backend model is only referenced, so its policies stay untouched.
targetScope = 'resourceGroup'
@description('Name of the existing AI Gateway (Microsoft.ApiManagement/aigateways) in this resource group.')
param gatewayName string
@description('Name of the existing Foundry model provider in the gateway, for example foundry-models from lab 02.')
param providerName string = 'foundry-models'
@description('Name of the existing backend model that takes most of the traffic, for example gpt-5-mini.')
param primaryModelName string = 'gpt-5-mini'
@description('Resource ID of the Foundry account that hosts the candidate deployment.')
param foundryAccountId string
@description('Name of the candidate deployment in that account, for example gpt-6.1-sol.')
param candidateDeploymentName string = 'gpt-6.1-sol'
@description('Version of the candidate model, for example 2026-09-29. Optional.')
param candidateModelVersion string = ''
@description('Callable alias that clients send in the request "model" field.')
param aliasName string = 'chat'
@description('weighted: both backends share priority 0 and split traffic by weight. priority: the primary is preferred, and the candidate gets priority 1.')
@allowed([
'weighted'
'priority'
])
param strategy string = 'weighted'
@description('Weighted strategy only: the candidate weight. The primary gets 100 minus this value.')
@minValue(1)
@maxValue(99)
param candidateWeight int = 20
type route = {
@description('Resource ID of the backend model.')
resourceId: string
@description('Lower numbers are preferred. 0 to 100.')
@minValue(0)
@maxValue(100)
priority: int
@description('Share of traffic among backends with the same priority. 1 to 100.')
@minValue(1)
@maxValue(100)
weight: int
}
#disable-next-line BCP081
resource aiGateway 'Microsoft.ApiManagement/aigateways@2026-11-01' existing = {
name: gatewayName
}
#disable-next-line BCP081
resource defaultWorkspace 'Microsoft.ApiManagement/aigateways/workspaces@2026-11-01' existing = {
parent: aiGateway
name: 'default'
}
#disable-next-line BCP081
resource provider 'Microsoft.ApiManagement/aigateways/workspaces/modelProviders@2026-11-01' existing = {
parent: defaultWorkspace
name: providerName
}
#disable-next-line BCP081
resource primary 'Microsoft.ApiManagement/aigateways/workspaces/modelProviders/models@2026-11-01' existing = {
parent: provider
name: primaryModelName
}
#disable-next-line BCP081
resource candidate 'Microsoft.ApiManagement/aigateways/workspaces/modelProviders/models@2026-11-01' = {
parent: provider
name: candidateDeploymentName
properties: {
displayName: candidateDeploymentName
description: 'Candidate deployment that the ${aliasName} alias routes to.'
supportedEndpoints: [
'/openai/v1/chat/completions'
'/openai/v1/responses'
]
deployment: {
resourceId: '${foundryAccountId}/deployments/${candidateDeploymentName}'
modelName: candidateDeploymentName
modelVersion: empty(candidateModelVersion) ? null : candidateModelVersion
}
}
}
var routes route[] = strategy == 'weighted'
? [
{ resourceId: primary.id, priority: 0, weight: 100 - candidateWeight }
{ resourceId: candidate.id, priority: 0, weight: candidateWeight }
]
: [
{ resourceId: primary.id, priority: 0, weight: 1 }
{ resourceId: candidate.id, priority: 1, weight: 1 }
]
#disable-next-line BCP081
resource alias 'Microsoft.ApiManagement/aigateways/workspaces/aliases@2026-11-01' = {
parent: defaultWorkspace
name: aliasName
properties: {
description: strategy == 'weighted'
? 'Splits traffic ${100 - candidateWeight}/${candidateWeight} between ${primaryModelName} and ${candidateDeploymentName}.'
: 'Prefers ${primaryModelName}. ${candidateDeploymentName} has a lower preference.'
backendModels: [
for r in routes: {
resourceId: r.resourceId
priority: r.priority
weight: r.weight
}
]
}
}
output aliasName string = alias.name
output candidateModelName string = candidate.name
output routes array = [
for r in routes: {
model: last(split(r.resourceId, '/'))
priority: r.priority
weight: r.weight
}
]
output chatCompletionsUrl string = 'https://${aiGateway.properties.frontend.defaultHostname}/default/models/openai/v1/chat/completions'Deploy with azd up
Move into the lab's folder, create an azd environment named aigw-lab22 in your current subscription, then provision
everything. It takes about three minutes.
# Move into the lab's folder, which holds the azd project
cd ~/aigateway/labs/22-model-routing &&
echo "📂 Lab folder: $(pwd)"
# Create an azd environment named aigw-lab22 in your current subscription, so that azd doesn't prompt you
azd env new aigw-lab22 --location eastus2 --subscription "$(az account show --query id --output tsv)" &&
echo "📝 azd environment aigw-lab22 deploys to eastus2"
# Provision the resource group, the gateway, the Foundry account with both models, the chat alias, and the key
azd up &&
echo "🚀 Gateway deployed: $(azd env get-value AI_GATEWAY_URL)"# Move into the lab's folder, which holds the azd project
Set-Location ~/aigateway/labs/22-model-routing
if ($?) { Write-Host "📂 Lab folder: $(Get-Location)" }
# Create an azd environment named aigw-lab22 in your current subscription, so that azd doesn't prompt you
azd env new aigw-lab22 --location eastus2 --subscription (az account show --query id --output tsv)
if ($?) { Write-Host "📝 azd environment aigw-lab22 deploys to eastus2" }
# Provision the resource group, the gateway, the Foundry account with both models, the chat alias, and the key
azd up
if ($?) { Write-Host "🚀 Gateway deployed: $(azd env get-value AI_GATEWAY_URL)" }For another region, pick one where AI Gateway and the gpt-5-mini and gpt-6.1-sol models are available, as in Find
the available regions. The parameters come from azd
environment values, with the defaults in infra/main.parameters.json: ROUTING_STRATEGY (weighted),
ROUTING_CANDIDATE_WEIGHT (20), CANDIDATE_MODEL (gpt-6.1-sol), and CANDIDATE_MODEL_VERSION (2026-09-29).
Read the outputs and the key
Set the variables that the next sections and Verify use from azd's environment values, read the key from the gateway so that it never shows on screen, then print the alias's routes.
# Read azd's outputs into the variables that the next sections use, and build the backend models' ID prefix
export RG="$(azd env get-value AZURE_RESOURCE_GROUP)"
export GATEWAY="$(azd env get-value AI_GATEWAY_NAME)"
export GATEWAY_ID="$(azd env get-value AI_GATEWAY_ID)"
export PROVIDER="$(azd env get-value AI_GATEWAY_PROVIDER)"
export PRIMARY="$(azd env get-value AI_GATEWAY_BACKEND_MODEL)"
export CANDIDATE="$(azd env get-value AI_GATEWAY_CANDIDATE_MODEL)"
export ALIAS="$(azd env get-value AI_GATEWAY_MODEL)"
export MODELS="$GATEWAY_ID/workspaces/default/modelProviders/$PROVIDER/models"
export AI_GATEWAY_URL="$(azd env get-value AI_GATEWAY_URL)"
echo "📝 Alias $ALIAS routes between $PRIMARY and $CANDIDATE on gateway $GATEWAY in $RG"
echo "🌐 Runtime URL: $AI_GATEWAY_URL"
# Read the app-first-call key from the gateway, so that it never shows on screen or lands in a file
export AI_GATEWAY_API_KEY="$(az aigateway api-key list-secrets --ids "$GATEWAY_ID/apiKeys/app-first-call" \
--query primaryKey --output tsv)"
echo "🔑 Key: in AI_GATEWAY_API_KEY (${#AI_GATEWAY_API_KEY} characters, not shown)"
# Print the alias's backend models, with the priority and the weight of each one
echo "🔖 Alias $ALIAS routes to:"
az aigateway model-alias show -g "$RG" --gateway-name "$GATEWAY" -n "$ALIAS" --query properties.backendModels -o json |
jq -r '.[] | "\(.resourceId | split("/") | last) priority \(.priority) weight \(.weight)"'# Read azd's outputs into the variables that the next sections use, and build the backend models' ID prefix
$RG = azd env get-value AZURE_RESOURCE_GROUP
$GATEWAY = azd env get-value AI_GATEWAY_NAME
$GATEWAY_ID = azd env get-value AI_GATEWAY_ID
$PROVIDER = azd env get-value AI_GATEWAY_PROVIDER
$PRIMARY = azd env get-value AI_GATEWAY_BACKEND_MODEL
$CANDIDATE = azd env get-value AI_GATEWAY_CANDIDATE_MODEL
$ALIAS = azd env get-value AI_GATEWAY_MODEL
$MODELS = "$GATEWAY_ID/workspaces/default/modelProviders/$PROVIDER/models"
$env:AI_GATEWAY_URL = azd env get-value AI_GATEWAY_URL
Write-Host "📝 Alias $ALIAS routes between $PRIMARY and $CANDIDATE on gateway $GATEWAY in $RG"
Write-Host "🌐 Runtime URL: $env:AI_GATEWAY_URL"
# Read the app-first-call key from the gateway, so that it never shows on screen or lands in a file
$env:AI_GATEWAY_API_KEY = az aigateway api-key list-secrets --ids "$GATEWAY_ID/apiKeys/app-first-call" `
--query primaryKey --output tsv
Write-Host "🔑 Key: in AI_GATEWAY_API_KEY ($($env:AI_GATEWAY_API_KEY.Length) characters, not shown)"
# Print the alias's backend models, with the priority and the weight of each one
Write-Host "🔖 Alias $ALIAS routes to:"
az aigateway model-alias show -g $RG --gateway-name $GATEWAY -n $ALIAS --query properties.backendModels -o json |
ConvertFrom-Json | ForEach-Object { '{0} priority {1} weight {2}' -f ($_.resourceId -split '/')[-1], $_.priority, $_.weight }azd up ended with SUCCESS, and the last command printed gpt-5-mini priority 0 weight 80 and
gpt-6.1-sol priority 0 weight 20.
What you need: only the one-time setup from Before you begin, plus Terraform 1.9 or later, as in
Deploy a lab with Terraform. This track doesn't use your gateway from
lab 02: terraform apply creates a new one, so it works on its own.
Review the configuration
terraform/main.tf creates the resource group rg-<environment_name> and calls the quick start's
resources module for the gateway, gpt-5-mini, its default alias, and the
app-first-call key:
# Lab 22 with Terraform: creates the resource group rg-<environment_name>, the quick start's gateway, Foundry model,
# default alias, and runtime API key, then deploys a candidate model in the same Foundry account
# (candidate-deployment.tf) and publishes the chat alias, which routes across both models by weight or by priority
# (alias-routing.tf). The runtime API key is never an output.
data "azapi_client_config" "current" {}
locals {
suffix = substr(sha1("${data.azapi_client_config.current.subscription_id}/${var.environment_name}/${var.location}"), 0, 6)
tags = { "environment-name" = var.environment_name }
}
resource "azurerm_resource_group" "rg" {
name = "rg-${var.environment_name}"
location = var.location
tags = local.tags
}
# The quick start's module: a gateway, gpt-5-mini behind the default alias, and the app-first-call key.
module "base" {
source = "../../quickstart/terraform/resources"
subscription_id = data.azapi_client_config.current.subscription_id
resource_group_name = azurerm_resource_group.rg.name
resource_group_id = azurerm_resource_group.rg.id
location = var.location
tags = local.tags
gateway_name = "aigw-${local.suffix}"
foundry_name = "aigw${local.suffix}"
}terraform/candidate-deployment.tf deploys the candidate model in the same Foundry account with the azurerm
provider:
# The candidate deployment, in the same Foundry account as gpt-5-mini. The gateway's Foundry User role on the
# account already covers it.
resource "azurerm_cognitive_deployment" "candidate" {
name = var.candidate_model_name
cognitive_account_id = module.base.foundry_id
sku {
name = "GlobalStandard"
capacity = 10
}
model {
format = "OpenAI"
name = var.candidate_model_name
version = var.candidate_model_version
}
}terraform/alias-routing.tf registers the candidate as a backend model and publishes the chat alias with the
azapi provider, which sends the AI Gateway API version, 2026-11-01, as is. It keeps both strategies in a map,
and the strategy variable picks one, so switching strategies changes only the alias. terraform/variables.tf
declares the strategy, the candidate weight, and the candidate model, with the same defaults as the Bicep template:
# Registers the candidate deployment under the quick start's Foundry provider, then publishes the chat alias, which
# either splits traffic between gpt-5-mini and the candidate by weight, or prefers gpt-5-mini by priority.
# gpt-5-mini is only referenced, so its settings stay untouched.
locals {
primary = module.base.model_name
candidate = azapi_resource.candidate.name
# The chat alias's backend models for each strategy. Lower priority numbers are preferred (0 to 100), and the
# weights split the traffic among the models that share a priority (1 to 100).
routes = {
weighted = [
{ resourceId = module.base.model_id, priority = 0, weight = 100 - var.candidate_weight },
{ resourceId = azapi_resource.candidate.id, priority = 0, weight = var.candidate_weight },
]
priority = [
{ resourceId = module.base.model_id, priority = 0, weight = 1 },
{ resourceId = azapi_resource.candidate.id, priority = 1, weight = 1 },
]
}
descriptions = {
weighted = "Splits traffic ${100 - var.candidate_weight}/${var.candidate_weight} between ${local.primary} and ${local.candidate}."
priority = "Prefers ${local.primary}. ${local.candidate} has a lower preference."
}
}
resource "azapi_resource" "candidate" {
type = "Microsoft.ApiManagement/aigateways/workspaces/modelProviders/models@2026-11-01"
name = azurerm_cognitive_deployment.candidate.name
parent_id = module.base.provider_id
body = {
properties = {
displayName = azurerm_cognitive_deployment.candidate.name
description = "Candidate deployment that the chat alias routes to."
supportedEndpoints = [
"/openai/v1/chat/completions",
"/openai/v1/responses",
]
deployment = {
resourceId = azurerm_cognitive_deployment.candidate.id
modelName = var.candidate_model_name
modelVersion = var.candidate_model_version
}
}
}
schema_validation_enabled = false
}
resource "azapi_resource" "chat_alias" {
type = "Microsoft.ApiManagement/aigateways/workspaces/aliases@2026-11-01"
name = "chat"
parent_id = module.base.workspace_id
body = {
properties = {
description = local.descriptions[var.strategy]
backendModels = local.routes[var.strategy]
}
}
schema_validation_enabled = false
}Deploy with terraform apply
Move into the lab's Terraform folder, select your current subscription, then plan and apply the configuration. It takes about three minutes.
# Move into the lab's Terraform folder
cd ~/aigateway/labs/22-model-routing/terraform &&
echo "📂 Terraform folder: $(pwd)"
# Deploy to the Azure CLI's current subscription
export ARM_SUBSCRIPTION_ID="$(az account show --query id --output tsv)" &&
echo "📍 Subscription: $ARM_SUBSCRIPTION_ID"
# Download the providers, then plan and apply the gateway, the Foundry account with both models, the chat alias,
# and the key
terraform init &&
terraform plan -out tfplan &&
terraform apply tfplan &&
echo "🚀 Gateway deployed: $(terraform output -raw ai_gateway_url)"# Move into the lab's Terraform folder
Set-Location ~/aigateway/labs/22-model-routing/terraform
if ($?) { Write-Host "📂 Terraform folder: $(Get-Location)" }
# Deploy to the Azure CLI's current subscription
$env:ARM_SUBSCRIPTION_ID = az account show --query id --output tsv
if ($?) { Write-Host "📍 Subscription: $env:ARM_SUBSCRIPTION_ID" }
# Download the providers, then plan and apply the gateway, the Foundry account with both models, the chat alias,
# and the key
terraform init
if ($?) { terraform plan -out tfplan }
if ($?) { terraform apply tfplan }
if ($?) { Write-Host "🚀 Gateway deployed: $(terraform output -raw ai_gateway_url)" }The configuration deploys to eastus2. For another region where AI Gateway and the gpt-5-mini and gpt-6.1-sol
models are available, as in Find the available regions, run
export TF_VAR_location=<region> (PowerShell: $env:TF_VAR_location = "<region>") before terraform plan.
Read the outputs and the key
Set the variables that the next sections and Verify use from the Terraform outputs, read the key from the gateway so that it never shows on screen or lands in the Terraform state, then print the alias's routes.
# Read the configuration's outputs into the variables that the next sections use, and build the backend models'
# ID prefix
export RG="$(terraform output -raw azure_resource_group)"
export GATEWAY="$(terraform output -raw ai_gateway_name)"
export GATEWAY_ID="$(terraform output -raw ai_gateway_id)"
export PROVIDER="$(terraform output -raw ai_gateway_provider)"
export PRIMARY="$(terraform output -raw ai_gateway_backend_model)"
export CANDIDATE="$(terraform output -raw ai_gateway_candidate_model)"
export ALIAS="$(terraform output -raw ai_gateway_model)"
export MODELS="$GATEWAY_ID/workspaces/default/modelProviders/$PROVIDER/models"
export AI_GATEWAY_URL="$(terraform output -raw ai_gateway_url)"
echo "📝 Alias $ALIAS routes between $PRIMARY and $CANDIDATE on gateway $GATEWAY in $RG"
echo "🌐 Runtime URL: $AI_GATEWAY_URL"
# Read the app-first-call key from the gateway, so that it never lands in a file or the Terraform state
export AI_GATEWAY_API_KEY="$(az aigateway api-key list-secrets --ids "$GATEWAY_ID/apiKeys/app-first-call" \
--query primaryKey --output tsv)"
echo "🔑 Key: in AI_GATEWAY_API_KEY (${#AI_GATEWAY_API_KEY} characters, not shown)"
# Print the alias's backend models, with the priority and the weight of each one
echo "🔖 Alias $ALIAS routes to:"
az aigateway model-alias show -g "$RG" --gateway-name "$GATEWAY" -n "$ALIAS" --query properties.backendModels -o json |
jq -r '.[] | "\(.resourceId | split("/") | last) priority \(.priority) weight \(.weight)"'
# Move back to the lab folder, where the scripts of the next sections are
cd .. && echo "📂 Lab folder: $(pwd)"# Read the configuration's outputs into the variables that the next sections use, and build the backend models'
# ID prefix
$RG = terraform output -raw azure_resource_group
$GATEWAY = terraform output -raw ai_gateway_name
$GATEWAY_ID = terraform output -raw ai_gateway_id
$PROVIDER = terraform output -raw ai_gateway_provider
$PRIMARY = terraform output -raw ai_gateway_backend_model
$CANDIDATE = terraform output -raw ai_gateway_candidate_model
$ALIAS = terraform output -raw ai_gateway_model
$MODELS = "$GATEWAY_ID/workspaces/default/modelProviders/$PROVIDER/models"
$env:AI_GATEWAY_URL = terraform output -raw ai_gateway_url
Write-Host "📝 Alias $ALIAS routes between $PRIMARY and $CANDIDATE on gateway $GATEWAY in $RG"
Write-Host "🌐 Runtime URL: $env:AI_GATEWAY_URL"
# Read the app-first-call key from the gateway, so that it never lands in a file or the Terraform state
$env:AI_GATEWAY_API_KEY = az aigateway api-key list-secrets --ids "$GATEWAY_ID/apiKeys/app-first-call" `
--query primaryKey --output tsv
Write-Host "🔑 Key: in AI_GATEWAY_API_KEY ($($env:AI_GATEWAY_API_KEY.Length) characters, not shown)"
# Print the alias's backend models, with the priority and the weight of each one
Write-Host "🔖 Alias $ALIAS routes to:"
az aigateway model-alias show -g $RG --gateway-name $GATEWAY -n $ALIAS --query properties.backendModels -o json |
ConvertFrom-Json | ForEach-Object { '{0} priority {1} weight {2}' -f ($_.resourceId -split '/')[-1], $_.priority, $_.weight }
# Move back to the lab folder, where the scripts of the next sections are
Set-Location ..
if ($?) { Write-Host "📂 Lab folder: $(Get-Location)" }terraform apply ended with Apply complete! Resources: 12 added, 0 changed, 0 destroyed., and the alias
command printed gpt-5-mini priority 0 weight 80 and gpt-6.1-sol priority 0 weight 20.
Watch the split
Send 20 requests to chat and count which backend model answered each one. The script sends one request every
half second and reads the model field of each response.
# Send 20 requests to the alias, one every half second, and count which backend model answered each one
echo "📊 Counting the backend models that serve $ALIAS, one request every half second"
./infra/count-models.sh "$ALIAS" 20# Send 20 requests to the alias, one every half second, and count which backend model answered each one
Write-Host "📊 Counting the backend models that serve $ALIAS, one request every half second"
./infra/count-models.ps1 -Alias $ALIAS -Count 20Requests per backend model:
16 gpt-5-mini-2025-08-07
4 gpt-6.1-sol-2026-09-29In our tests the gateway interleaved the two models, four requests to gpt-5-mini and then one to
gpt-6.1-sol, so 20 requests split exactly 16 and 4. Plan around the shares, not the exact order.
To see the header for a single call:
# Send one request to the alias, and print only the x-ms-served-model header of the response
echo "✨ Backend model that served one request to $ALIAS:"
curl -sS -D - -o /dev/null "$AI_GATEWAY_URL/default/models/openai/v1/chat/completions" \
-H "Authorization: Bearer $AI_GATEWAY_API_KEY" -H "Content-Type: application/json" \
-d "{\"model\": \"$ALIAS\", \"max_completion_tokens\": 16, \"messages\": [{\"role\": \"user\", \"content\": \"Hi\"}]}" |
grep -i "^x-ms-served-model"# Build a short request to the alias, capped at 16 completion tokens
$body = @{ model = $ALIAS; max_completion_tokens = 16; messages = @(@{ role = "user"; content = "Hi" }) } | ConvertTo-Json -Compress
# Send it, and print only the x-ms-served-model header of the response
Write-Host "✨ Backend model that served one request to ${ALIAS}:"
(Invoke-WebRequest -Method Post -Uri "$env:AI_GATEWAY_URL/default/models/openai/v1/chat/completions" `
-Headers @{ Authorization = "Bearer $env:AI_GATEWAY_API_KEY" } -ContentType "application/json" -Body $body).Headers["x-ms-served-model"]About four of every five requests show gpt-5-mini-…, and the rest show gpt-6.1-sol-…. The client sent
"model": "chat" every time. Right after you create the alias, the first requests can return
HTTP 404 model_not_found for a few seconds; run the count again.
Prefer one model by priority
The canary looks good, but finance wants gpt-5-mini to handle the traffic until the new model is approved.
Give gpt-6.1-sol priority 1. It stays in the alias, ready to promote, but requests go to gpt-5-mini.
Edit the alias
- In ModelsAliases, select
chat, then select Edit. - In Priorities and weights, set Priority to
1forgpt-6.1-soland set both weights to1. Share within priority shows100%for each model:gpt-5-miniat priority0, andgpt-6.1-solat priority1. - Select Save. New requests use the new settings within a few seconds.
The alias Backend models tab shows gpt-5-mini at priority 0 and gpt-6.1-sol at priority 1.
Update the priorities
model-alias update replaces the whole backendModels list, so pass both backend models.
# Prefer gpt-5-mini: it keeps priority 0, and the candidate moves to priority 1, both with weight 1
echo "🔖 Alias $ALIAS now routes to:"
az aigateway model-alias update -g "$RG" --gateway-name "$GATEWAY" -n "$ALIAS" \
--description "Prefer the current model" \
--backend-model resource-id="$MODELS/$PRIMARY" priority=0 weight=1 \
--backend-model resource-id="$MODELS/$CANDIDATE" priority=1 weight=1 \
--only-show-errors --query properties.backendModels -o json |
jq -r '.[] | "\(.resourceId | split("/") | last) priority \(.priority) weight \(.weight)"'# Prefer gpt-5-mini: it keeps priority 0, and the candidate moves to priority 1, both with weight 1
Write-Host "🔖 Alias $ALIAS now routes to:"
az aigateway model-alias update -g $RG --gateway-name $GATEWAY -n $ALIAS `
--description "Prefer the current model" `
--backend-model resource-id="$MODELS/$PRIMARY" priority=0 weight=1 `
--backend-model resource-id="$MODELS/$CANDIDATE" priority=1 weight=1 `
--only-show-errors --query properties.backendModels -o json |
ConvertFrom-Json | ForEach-Object { '{0} priority {1} weight {2}' -f ($_.resourceId -split '/')[-1], $_.priority, $_.weight }The output shows gpt-5-mini priority 0 weight 1 and gpt-6.1-sol priority 1 weight 1.
Provision the priority strategy
Set ROUTING_STRATEGY to priority and provision again. Only the alias changes: gpt-5-mini keeps priority 0,
and gpt-6.1-sol gets priority 1.
# Switch the template's routing strategy from the weighted split to priority
azd env set ROUTING_STRATEGY priority &&
echo "📝 ROUTING_STRATEGY is priority"
# Provision again: only the alias changes
azd provision &&
echo "🚀 The $ALIAS alias now prefers $PRIMARY"# Switch the template's routing strategy from the weighted split to priority
azd env set ROUTING_STRATEGY priority
if ($?) { Write-Host "📝 ROUTING_STRATEGY is priority" }
# Provision again: only the alias changes
azd provision
if ($?) { Write-Host "🚀 The $ALIAS alias now prefers $PRIMARY" }To go back to the weighted split, run azd env set ROUTING_STRATEGY weighted and azd provision. Change
ROUTING_CANDIDATE_WEIGHT the same way to canary in steps.
azd provision ended with SUCCESS. Read the alias in Verify shows gpt-5-mini priority 0 weight 1
and gpt-6.1-sol priority 1 weight 1.
Apply the priority strategy
Pass strategy=priority to terraform plan and apply the plan. Only the alias changes: gpt-5-mini keeps
priority 0, and gpt-6.1-sol gets priority 1.
# Move into the lab's Terraform folder
cd ~/aigateway/labs/22-model-routing/terraform &&
echo "📂 Terraform folder: $(pwd)"
# Plan the change from the weighted split to priority: only the alias changes
terraform plan -var strategy=priority -out tfplan &&
echo "📝 The plan updates only the $ALIAS alias"
# Apply the plan, then move back to the lab folder for Verify
terraform apply tfplan &&
echo "🚀 The $ALIAS alias now prefers $PRIMARY" &&
cd ..# Move into the lab's Terraform folder
Set-Location ~/aigateway/labs/22-model-routing/terraform
if ($?) { Write-Host "📂 Terraform folder: $(Get-Location)" }
# Plan the change from the weighted split to priority: only the alias changes
terraform plan -var strategy=priority -out tfplan
if ($?) { Write-Host "📝 The plan updates only the $ALIAS alias" }
# Apply the plan, then move back to the lab folder for Verify
terraform apply tfplan
if ($?) { Write-Host "🚀 The $ALIAS alias now prefers $PRIMARY"; Set-Location .. }To go back to the weighted split, plan and apply again without -var. Pass candidate_weight the same way, for
example -var candidate_weight=50, to canary in steps.
The plan ended with Plan: 0 to add, 1 to change, 0 to destroy., for azapi_resource.chat_alias. Read the
alias in Verify shows gpt-5-mini priority 0 weight 1 and gpt-6.1-sol priority 1 weight 1.
Verify
Read the alias
# Print the alias's backend models, with the priority and the weight of each one
echo "🔖 Alias $ALIAS routes to:"
az aigateway model-alias show -g "$RG" --gateway-name "$GATEWAY" -n "$ALIAS" --query properties.backendModels -o json |
jq -r '.[] | "\(.resourceId | split("/") | last) priority \(.priority) weight \(.weight)"'# Print the alias's backend models, with the priority and the weight of each one
Write-Host "🔖 Alias $ALIAS routes to:"
az aigateway model-alias show -g $RG --gateway-name $GATEWAY -n $ALIAS --query properties.backendModels -o json |
ConvertFrom-Json | ForEach-Object { '{0} priority {1} weight {2}' -f ($_.resourceId -split '/')[-1], $_.priority, $_.weight }Count the requests again
# Send 10 requests to the alias and count which backend model answered each one
echo "📊 Counting the backend models that serve $ALIAS, one request every half second"
./infra/count-models.sh "$ALIAS" 10# Send 10 requests to the alias and count which backend model answered each one
Write-Host "📊 Counting the backend models that serve $ALIAS, one request every half second"
./infra/count-models.ps1 -Alias $ALIAS -Count 10All 10 requests show gpt-5-mini-…. gpt-6.1-sol stays in the alias at priority 1. If one of the first
requests still shows gpt-6.1-sol-…, the change was still reaching the gateway; run the count again. If some
show HTTP 429 rate_limit_exceeded, the gpt-5-mini deployment used up its 10 requests per minute; wait a
minute and run the count again.
Promote the new model
To finish the rollout, swap the priorities: gpt-6.1-sol at 0, gpt-5-mini at 1. Use the portal, or run
the az aigateway model-alias update command from the previous section with $PRIMARY and $CANDIDATE
swapped. Run the count again: every request now shows gpt-6.1-sol-…. To roll back, swap them again. Clients
never change.
Go further
- Canary in steps. Move the weights from
95/5to80/20to50/50while you watch errors, latency, and cost per model in Monitor usage, cost, and traces. Weights must be at least1, so to stop sending traffic to a model, give it a higher priority number or remove it from the alias. - Prefer provisioned throughput. Put a provisioned deployment at priority
0and a pay-as-you-go deployment of the same model at priority1, so purchased capacity is used first. Add a failure rule for429so that requests switch to the pay-as-you-go deployment when the provisioned one is saturated, as in Fail over to a backup model when a backend fails. - Balance regions or providers. Register the same model from two Foundry accounts, or from Foundry and a custom provider, and give them weights in proportion to their quota.
- Limit each model and the alias. A token limit on a backend model, as in
Throttle tokens per caller with token limits, applies to that model. A limit
on the alias applies to every request that calls
chat.
Clean up
Keep the gateway, the runtime key, and gpt-5-mini from lab 02. Later labs use them. Remove what you added in
this lab: the alias first, then the backend model, then the Foundry deployment.
Remove the alias and the candidate model
In ModelsAliases, select More actions on the chat row, then Delete and Delete alias.
Requests that use the alias fail immediately; the backend models stay in the gateway. Then delete the
gpt-6.1-sol model, and any alias that the import created for it, from Models. Finally, delete the
gpt-6.1-sol deployment from your Foundry account, for example with the last command of the CLI track.
Remove the alias and the candidate model
# Delete the alias first, so that nothing points to the candidate model: requests to the alias now fail
az aigateway model-alias delete -g "$RG" --gateway-name "$GATEWAY" -n "$ALIAS" --yes &&
echo "🚮 Alias $ALIAS is deleted"
# Delete the candidate backend model from the gateway's provider
az aigateway model delete -g "$RG" --gateway-name "$GATEWAY" --provider-name "$PROVIDER" -n "$CANDIDATE" --yes &&
echo "🚮 Model $CANDIDATE is deleted from $PROVIDER"
# Delete the candidate deployment from the Foundry account, and keep gpt-5-mini for later labs
az cognitiveservices account deployment delete -g "$FOUNDRY_RG" -n "$FOUNDRY_ACCOUNT" --deployment-name "$CANDIDATE" &&
echo "🚮 Model deployment $CANDIDATE is deleted from $FOUNDRY_ACCOUNT"# Delete the alias first, so that nothing points to the candidate model: requests to the alias now fail
az aigateway model-alias delete -g $RG --gateway-name $GATEWAY -n $ALIAS --yes
if ($?) { Write-Host "🚮 Alias $ALIAS is deleted" }
# Delete the candidate backend model from the gateway's provider
az aigateway model delete -g $RG --gateway-name $GATEWAY --provider-name $PROVIDER -n $CANDIDATE --yes
if ($?) { Write-Host "🚮 Model $CANDIDATE is deleted from $PROVIDER" }
# Delete the candidate deployment from the Foundry account, and keep gpt-5-mini for later labs
az cognitiveservices account deployment delete -g $FOUNDRY_RG -n $FOUNDRY_ACCOUNT --deployment-name $CANDIDATE
if ($?) { Write-Host "🚮 Model deployment $CANDIDATE is deleted from $FOUNDRY_ACCOUNT" }If an alias that the portal import created still points to gpt-6.1-sol, delete that alias before the model.
Delete everything azd created
In the aigateway/labs/22-model-routing folder, delete the resource group with the gateway, the Foundry account and
its two deployments, and the chat alias. --purge also purges the
soft-deleted Foundry account. It takes about three minutes.
# Delete everything that azd up created in this lab, purge the soft-deleted Foundry account, and skip the prompt
azd down --purge --force &&
echo "🚮 Everything that azd up created is deleted"Delete everything Terraform created
In the aigateway/labs/22-model-routing folder, delete the resource group with the gateway, the Foundry
account and its two deployments, and the chat alias. The
azurerm provider also purges the soft-deleted Foundry account. It
takes one to two minutes.
# Delete everything that terraform apply created, and purge the soft-deleted Foundry account
terraform -chdir=terraform destroy -auto-approve &&
echo "🚮 Everything that terraform apply created is deleted"azd down and terraform destroy don't touch the gateway, key, and gpt-5-mini from lab 02, because the Bicep +
azd and Terraform tracks created their own gateways.