---
title: Publish embedding models for search and RAG
summary: Publish a Foundry embedding model through the gateway behind its own alias, generate vectors with curl, the OpenAI SDK, and the portal playground, build a mini RAG app on one gateway URL and key, and cap ingestion with a token limit.
category: models
level: Intermediate
duration: 30
tracks: [portal, cli, iac, terraform, code]
order: 3.8
status: new
illustration: diagrams/embeddings.svg
tags: [embeddings, text-embedding-3-small, RAG, retrieval-augmented generation, vector search, cosine similarity, dimensions, model alias, tokenLimit, ingestion]
objectives:
  - Publish a Foundry embedding deployment through the gateway behind its own alias
  - Generate embeddings with curl, the OpenAI Python SDK, and the portal's embeddings playground
  - Build a mini RAG app that embeds, retrieves, and answers through one gateway URL and one key
  - Cap an ingestion job with a per-caller token limit on the embedding model
prereqs:
  - 02-first-model-call
---

# Publish embedding models for search and RAG

Your team is building a help desk assistant with retrieval-augmented generation (RAG): an ingestion job turns every
article into a vector, and the app finds the articles closest to each question before a chat model answers. The
chat model already goes through AI Gateway, but the embedding calls go straight to Foundry, with their own key and
no limits or usage records. In this lab you publish `text-embedding-3-small` through the same gateway, call it with
curl, the OpenAI SDK, and the portal, build a mini RAG app that uses one URL and one key for both models, and cap
the ingestion job with a token limit.

![Callers send embeddings and chat requests to one gateway URL with one key. The gateway resolves each alias, applies its policies, and forwards the request to the embedding or chat deployment in Foundry](../assets/diagrams/embeddings.svg "One gateway URL and one key for both models. Each alias accepts only its own API, and the token limit caps ingestion per caller.")

## Before you begin

<!-- setup: tools sign-in extension clone -->

> [!TIP]
> On the **Bicep + azd** and **Terraform** tracks, `azd up` or `terraform apply` creates its own gateway, Foundry
> account with both models, aliases, token limit, and key, so you can skip this section. Run that track in
> [Publish the embedding model](#publish-the-embedding-model) first: it sets the variables that the next sections
> and **Verify** use. You need only the one-time setup above, and Python 3.10 or later.

- Complete [Publish a Foundry model and make your first call](../02-first-model-call/README.md). You need the
  gateway, the `gpt-5-mini` alias, and a runtime API key. Lab 02 also gave the gateway the **Foundry User** role on
  your Foundry account, which covers the embedding deployment in this lab.
- Permission to change the gateway, for example **Contributor** on its resource group, and to create deployments
  in the Foundry account.
- Python 3.10 or later for the mini RAG app and **Verify**, and `jq` for the Bash steps.
- Run the scripts and samples from the lab's folder, `~/aigateway/labs/26-embeddings`.

Set these variables once. Every track and the **Verify** section reuse them.

```bash title="Bash"
# Name your gateway, the Foundry account from lab 02, the provider, and the two aliases that apps 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 EMBEDDINGS="text-embedding-3-small"       # the Foundry deployment and the backend model
export AI_GATEWAY_EMBEDDINGS_MODEL="text-embedding-3-small"   # the alias that apps send in "model"
export AI_GATEWAY_MODEL="gpt-5-mini"             # the chat alias from lab 02
echo "📝 Alias $AI_GATEWAY_EMBEDDINGS_MODEL will serve $EMBEDDINGS 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)"
```

```powershell title="PowerShell"
# Name your gateway, the Foundry account from lab 02, the provider, and the two aliases that apps 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
$EMBEDDINGS                      = "text-embedding-3-small"     # the Foundry deployment and the backend model
$env:AI_GATEWAY_EMBEDDINGS_MODEL = "text-embedding-3-small"     # the alias that apps send in "model"
$env:AI_GATEWAY_MODEL            = "gpt-5-mini"                 # the chat alias from lab 02
Write-Host "📝 Alias $env:AI_GATEWAY_EMBEDDINGS_MODEL will serve $EMBEDDINGS 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.

```bash title="Bash"
# 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
```

```powershell title="PowerShell"
# 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 table
```

Deploy `text-embedding-3-small` in the same Foundry account as `gpt-5-mini`. Embedding inference is billed per use,
at a small fraction of the price of chat tokens.

```bash title="Bash"
# Deploy text-embedding-3-small next to gpt-5-mini, with 20,000 tokens per minute of Global Standard capacity
az cognitiveservices account deployment create -g "$FOUNDRY_RG" -n "$FOUNDRY_ACCOUNT" \
  --deployment-name "$EMBEDDINGS" --model-name text-embedding-3-small --model-version 1 \
  --model-format OpenAI --sku-name GlobalStandard --sku-capacity 20 \
  --query "{name:name, state:properties.provisioningState}" &&
  echo "✨ Model deployment $EMBEDDINGS is ready in $FOUNDRY_ACCOUNT"
```

```powershell title="PowerShell"
# Deploy text-embedding-3-small next to gpt-5-mini, with 20,000 tokens per minute of Global Standard capacity
az cognitiveservices account deployment create -g $FOUNDRY_RG -n $FOUNDRY_ACCOUNT `
  --deployment-name $EMBEDDINGS --model-name text-embedding-3-small --model-version 1 `
  --model-format OpenAI --sku-name GlobalStandard --sku-capacity 20 `
  --query "{name:name, state:properties.provisioningState}"
if ($?) { Write-Host "✨ Model deployment $EMBEDDINGS is ready in $FOUNDRY_ACCOUNT" }
```

## How it works

An embedding model turns text into a vector: a list of numbers in which texts with similar meanings sit close
together. A RAG app embeds its documents once, when it ingests them, and embeds each question when a user asks it.
Since the [October 28, 2026 release](https://ai.gateway.azure.com/docs/releases#release-2026-10-28), the gateway publishes
embedding models like chat models: you register the deployment as a backend model, give it an alias, and apps send
the alias in `model`. Apps get one URL and one key or Entra token for both models, ingestion jobs get the same token
limits and usage records as chat, and you can change the backend behind the alias without touching the apps.

An embedding model supports only the embeddings endpoint. An alias exposes the endpoints that all its backend models
share, and an embedding model shares none with a chat model, so each one gets its own alias.

| What | Value |
|---|---|
| Endpoint | `POST $AI_GATEWAY_URL/default/models/openai/v1/embeddings` |
| OpenAI SDK `base_url` | `$AI_GATEWAY_URL/default/models/openai/v1`, the same as for chat |
| Backend model | Supported endpoint `/openai/v1/embeddings`. The portal shows the API format **Embeddings** |
| `input` | A string, an array of strings, or arrays of token IDs. One request embeds a whole batch |
| `dimensions` | Optional. `text-embedding-3-small` returns 1,536 numbers by default, and fewer when you ask, such as `256` |
| `encoding_format` | `float` (the default) or `base64` |
| `usage` | `prompt_tokens` and `total_tokens`. Token limits and Monitoring count them |

## Publish the embedding model

Register the deployment as a backend model under your Foundry provider, then publish it under an alias with the same
name, `text-embedding-3-small`, so that apps that called the model on Foundry keep the same model name.

<!-- track: portal | Portal -->

### Import the embedding model

1. Open the [AI Gateway portal](https://ai.gateway.azure.com) and select your gateway in the top bar.
2. Select **Models** › **Add** › **Models from Microsoft Foundry**.
3. In **Resource groups**, select the resource group of your Foundry account. The wizard otherwise selects every
   Foundry resource in the subscription. Keep **Managed identity** as the **Authentication method**.
4. Clear **Create an alias for each model**. When it's selected, the wizard names the alias
   `<foundry-account-name>/text-embedding-3-small`. You create a shorter alias in the next step.
5. Under **Models**, select your Foundry account. Its row says **This model provider already exists in your
   gateway. 1 new model is available.**
6. Select **Add models**. The portal confirms **Models imported** and `1 model(s) were created. No aliases were
   created.`

![The Add models from Microsoft Foundry page, filtered to one resource group, with Create an alias for each model cleared and the Foundry account selected](../assets/screens/embeddings-01-import.webp "Filter to your Foundry account's resource group, and clear Create an alias for each model.")

Select **Models** › **Backend models**. `text-embedding-3-small` shows **Embeddings** under **API formats** and
`--` under **Aliases**, and a banner says **Some models don't have an alias yet.**

![The Backend models tab, where text-embedding-3-small has the API format Embeddings and no alias](../assets/screens/embeddings-02-backend-models.webp "The imported embedding model has the API format Embeddings and no alias yet.")

### Create the alias

1. Select **Models** › **Add** › **Alias for existing models**.
2. On **Basics**, set **Alias name** to `text-embedding-3-small` and **Description** to
   `Embeddings for search and RAG`. Select **Next**.
3. On **Auto model selection (preview)**, keep it off. Select **Next**.
4. On **Select models**, select `text-embedding-3-small`, whose **API formats** column says **Embeddings**. The
   page notes that **Models must share at least one API format**, so don't add `gpt-5-mini`. Select **Next**.
5. Keep the defaults on **Session affinity**, **Priorities and weights**, and **Failure handling**. On **Review**,
   select **Create**.

![The Select models step of Create alias, with text-embedding-3-small selected and its API format Embeddings](../assets/screens/embeddings-03-alias-select.webp "Select only the embedding model. A chat model shares no API format with it.")

The alias's overview opens. Under **Endpoints**, **Embeddings** shows the **Base URL** for SDKs and the full
**Endpoint URL**.

![The alias overview, with the Embeddings endpoint's Base URL and Endpoint URL, and the Test in catalog button](../assets/screens/embeddings-04-alias-endpoints.webp "The alias's Embeddings endpoint. Test in catalog opens the embeddings playground.")

> **Checkpoint**
> **Models** › **Aliases** lists `text-embedding-3-small`, and its overview shows an **Embeddings** endpoint that
> ends with `/default/models/openai/v1/embeddings`.

<!-- track: cli | Azure CLI -->

### Register the embedding model

`az aigateway model create` registers the deployment under the provider. `--foundry-resource-id` and `--deployment`
name the Foundry deployment, and the CLI reads its model name and version. Set `--supported-endpoints` to the
embeddings endpoint, and leave out `--api-format`: its values, such as `OpenAIChatCompletions`, describe chat APIs.

```bash title="Bash"
# Register the deployment as a backend model that supports only the embeddings endpoint
az aigateway model create -g "$RG" --gateway-name "$GATEWAY" --provider-name "$PROVIDER" -n "$EMBEDDINGS" \
  --display-name "$EMBEDDINGS" --supported-endpoints /openai/v1/embeddings \
  --foundry-resource-id "$FOUNDRY_ID" --deployment "$EMBEDDINGS" \
  --query "{name:name, endpoints:properties.supportedEndpoints}" &&
  echo "✨ Model $EMBEDDINGS is published through $PROVIDER"
```

```powershell title="PowerShell"
# Register the deployment as a backend model that supports only the embeddings endpoint
az aigateway model create -g $RG --gateway-name $GATEWAY --provider-name $PROVIDER -n $EMBEDDINGS `
  --display-name $EMBEDDINGS --supported-endpoints /openai/v1/embeddings `
  --foundry-resource-id $FOUNDRY_ID --deployment $EMBEDDINGS `
  --query "{name:name, endpoints:properties.supportedEndpoints}"
if ($?) { Write-Host "✨ Model $EMBEDDINGS is published through $PROVIDER" }
```

To import every deployment of a Foundry account at once, `az aigateway model-provider import` (or
`az aigateway model-provider create --import-models`) registers embedding deployments with the embeddings endpoint
too, but it creates no aliases. Create them as in the next step.

### Create the alias

```bash title="Bash"
# Publish the backend model under an alias with the same name, which apps send in "model"
echo "🔖 Alias $AI_GATEWAY_EMBEDDINGS_MODEL:"
az aigateway model-alias set -g "$RG" --gateway-name "$GATEWAY" -n "$AI_GATEWAY_EMBEDDINGS_MODEL" \
  --description "Embeddings for search and RAG" \
  --backend-model resource-id="$GATEWAY_ID/workspaces/default/modelProviders/$PROVIDER/models/$EMBEDDINGS" \
  --only-show-errors --query "{endpoints:properties.supportedEndpoints, backend:properties.backendModels[0].resourceId}" -o json |
  jq -r '"Endpoints: \(.endpoints | join(", "))\nBackend:   \(.backend | split("/") | last)"'
```

```powershell title="PowerShell"
# Publish the backend model under an alias with the same name, which apps send in "model"
Write-Host "🔖 Alias $($env:AI_GATEWAY_EMBEDDINGS_MODEL):"
az aigateway model-alias set -g $RG --gateway-name $GATEWAY -n $env:AI_GATEWAY_EMBEDDINGS_MODEL `
  --description "Embeddings for search and RAG" `
  --backend-model resource-id="$GATEWAY_ID/workspaces/default/modelProviders/$PROVIDER/models/$EMBEDDINGS" `
  --only-show-errors --query "{endpoints:properties.supportedEndpoints, backend:properties.backendModels[0].resourceId}" -o json |
  ConvertFrom-Json | ForEach-Object { "Endpoints: $($_.endpoints -join ', ')"; "Backend:   $(($_.backend -split '/')[-1])" }
```

`az aigateway model-alias set` creates the alias, or updates it if it exists, so you can run the step again.

> **Checkpoint**
> The output shows `Endpoints: /openai/v1/embeddings` and `Backend:   text-embedding-3-small`.

<!-- track: iac | Bicep + azd -->

**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](../quickstart/infra/resources.bicep) for the gateway, `gpt-5-mini`, its alias, and the
`app-first-call` key. Then it deploys `text-embedding-3-small` in the same Foundry account with
[embeddings-deployment.bicep](infra/embeddings-deployment.bicep), and publishes it:

<!-- include: infra/main.bicep -->

`infra/publish-embeddings.bicep` registers the deployment with the embeddings endpoint and a token limit, then
creates the alias. You can also deploy it on its own to the gateway from lab 02:

<!-- include: infra/publish-embeddings.bicep -->

### Deploy with azd up

Move into the lab's folder, create an azd environment named `aigw-lab26` in your current subscription, then provision
everything. It takes about 27 minutes, most of it while the new gateway activates.

```bash title="Bash"
# Move into the lab's folder, which holds the azd project
cd ~/aigateway/labs/26-embeddings &&
  echo "📂 Lab folder: $(pwd)"

# Create an azd environment named aigw-lab26 in your current subscription, so that azd doesn't prompt you
azd env new aigw-lab26 --location eastus2 --subscription "$(az account show --query id --output tsv)" &&
  echo "📝 azd environment aigw-lab26 deploys to eastus2"

# Provision the resource group, the gateway, the Foundry account with both models, the aliases, and the key
azd up &&
  echo "🚀 Gateway deployed: $(azd env get-value AI_GATEWAY_URL)"
```

```powershell title="PowerShell"
# Move into the lab's folder, which holds the azd project
Set-Location ~/aigateway/labs/26-embeddings
if ($?) { Write-Host "📂 Lab folder: $(Get-Location)" }

# Create an azd environment named aigw-lab26 in your current subscription, so that azd doesn't prompt you
azd env new aigw-lab26 --location eastus2 --subscription (az account show --query id --output tsv)
if ($?) { Write-Host "📝 azd environment aigw-lab26 deploys to eastus2" }

# Provision the resource group, the gateway, the Foundry account with both models, the aliases, and the key
azd up
if ($?) { Write-Host "🚀 Gateway deployed: $(azd env get-value AI_GATEWAY_URL)" }
```

Expected output (your names differ):

```text
  (✓) Done: Resource group: rg-aigw-lab26
  (✓) Done: Foundry: aigw2bn3tl
  (✓) Done: Azure AI Services Model Deployment: aigw2bn3tl/gpt-5-mini
  (✓) Done: Azure AI Services Model Deployment: aigw2bn3tl/text-embedding-3-small

SUCCESS: Your application was provisioned and deployed to Azure in 26 minutes 37 seconds.
```

For another region, pick one where AI Gateway, `gpt-5-mini`, and `text-embedding-3-small` are available, as in
[Find the available regions](../SETUP.md#find-the-available-regions). The token limit comes from the azd
environment value `EMBEDDINGS_TOKENS_PER_MINUTE`, with the default `2000` in `infra/main.parameters.json`.

### Read the outputs and the key

Set the variables that the next sections and **Verify** use from azd's environment values, then read the key from
the gateway so that it never shows on screen.

```bash title="Bash"
# Read azd's outputs into the variables that the next sections use
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 AI_GATEWAY_MODEL="$(azd env get-value AI_GATEWAY_MODEL)"
export AI_GATEWAY_EMBEDDINGS_MODEL="$(azd env get-value AI_GATEWAY_EMBEDDINGS_MODEL)"
export EMBEDDINGS="$AI_GATEWAY_EMBEDDINGS_MODEL"
export FOUNDRY_RG="$RG"
export FOUNDRY_ACCOUNT="$(azd env get-value FOUNDRY_ACCOUNT_NAME)"
export AI_GATEWAY_URL="$(azd env get-value AI_GATEWAY_URL)"
echo "📝 Aliases $AI_GATEWAY_EMBEDDINGS_MODEL and $AI_GATEWAY_MODEL 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)"
```

```powershell title="PowerShell"
# Read azd's outputs into the variables that the next sections use
$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
$env:AI_GATEWAY_MODEL            = azd env get-value AI_GATEWAY_MODEL
$env:AI_GATEWAY_EMBEDDINGS_MODEL = azd env get-value AI_GATEWAY_EMBEDDINGS_MODEL
$EMBEDDINGS                      = $env:AI_GATEWAY_EMBEDDINGS_MODEL
$FOUNDRY_RG                      = $RG
$FOUNDRY_ACCOUNT                 = azd env get-value FOUNDRY_ACCOUNT_NAME
$env:AI_GATEWAY_URL              = azd env get-value AI_GATEWAY_URL
Write-Host "📝 Aliases $env:AI_GATEWAY_EMBEDDINGS_MODEL and $env:AI_GATEWAY_MODEL 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)"
```

> **Checkpoint**
> `azd up` ended with `SUCCESS`, the variables point to the new gateway, and the key has 32 characters.

<!-- track: terraform | Terraform -->

**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](../SETUP.md#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](../quickstart/terraform/resources) for the gateway, `gpt-5-mini`, its alias, and the
`app-first-call` key:

<!-- include: terraform/main.tf -->

`terraform/embeddings.tf` deploys `text-embedding-3-small` in the same Foundry account with the `azurerm` provider,
then registers it as a backend model with its token limit, and publishes its alias, with the `azapi` provider, which
sends the AI Gateway API version, `2026-11-01`, as is:

<!-- include: terraform/embeddings.tf -->

`terraform/variables.tf` declares the token limit with the same default as the Bicep template, and
`terraform/outputs.tf` returns the names that the next sections use:

<!-- include: terraform/variables.tf -->

### 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, most of it while the new gateway activates.

```bash title="Bash"
# Move into the lab's Terraform folder
cd ~/aigateway/labs/26-embeddings/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 aliases, the
# token limit, and the key
terraform init &&
  terraform plan -out tfplan &&
  terraform apply tfplan &&
  echo "🚀 Gateway deployed: $(terraform output -raw ai_gateway_url)"
```

```powershell title="PowerShell"
# Move into the lab's Terraform folder
Set-Location ~/aigateway/labs/26-embeddings/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 aliases, the
# token limit, 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, `gpt-5-mini`, and
`text-embedding-3-small` are available, as in [Find the available regions](../SETUP.md#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, and read the key from the
gateway, so that it never shows on screen or lands in the Terraform state.

```bash title="Bash"
# Read the configuration's outputs into the variables that the next sections use
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 AI_GATEWAY_MODEL="$(terraform output -raw ai_gateway_model)"
export AI_GATEWAY_EMBEDDINGS_MODEL="$(terraform output -raw ai_gateway_embeddings_model)"
export EMBEDDINGS="$AI_GATEWAY_EMBEDDINGS_MODEL"
export FOUNDRY_RG="$RG"
export FOUNDRY_ACCOUNT="$(terraform output -raw foundry_account_name)"
export AI_GATEWAY_URL="$(terraform output -raw ai_gateway_url)"
echo "📝 Aliases $AI_GATEWAY_EMBEDDINGS_MODEL and $AI_GATEWAY_MODEL 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)"
# Move back to the lab's folder, which holds the scripts
cd .. && echo "📂 Lab folder: $(pwd)"
```

```powershell title="PowerShell"
# Read the configuration's outputs into the variables that the next sections use
$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
$env:AI_GATEWAY_MODEL            = terraform output -raw ai_gateway_model
$env:AI_GATEWAY_EMBEDDINGS_MODEL = terraform output -raw ai_gateway_embeddings_model
$EMBEDDINGS                      = $env:AI_GATEWAY_EMBEDDINGS_MODEL
$FOUNDRY_RG                      = $RG
$FOUNDRY_ACCOUNT                 = terraform output -raw foundry_account_name
$env:AI_GATEWAY_URL              = terraform output -raw ai_gateway_url
Write-Host "📝 Aliases $env:AI_GATEWAY_EMBEDDINGS_MODEL and $env:AI_GATEWAY_MODEL 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)"
# Move back to the lab's folder, which holds the scripts
Set-Location ..
if ($?) { Write-Host "📂 Lab folder: $(Get-Location)" }
```

> **Checkpoint**
> `terraform apply` ended with `Apply complete! Resources: 12 added, 0 changed, 0 destroyed.`, the variables point
> to the new gateway, and the key has 32 characters.

The next sections run from the lab's folder, with `-chdir=terraform` for the `terraform` commands.

<!-- /tracks -->

## Generate embeddings

Send text to the alias and look at what comes back: one vector per input, and the tokens that the request used. A
new alias reaches the runtime within a few seconds. If the first call returns `model_not_found`, wait a few seconds
and try again.

<!-- track: portal | Portal -->

### Test in the playground

1. On the alias's overview, select **Test in catalog**. The **Generate embeddings** playground opens in
   **Catalog**.
2. Under **Authentication**, keep **API key** as the **Method**, and select your key from lab 02 in **API key**.
   The default, **Built-in all-access API key**, works too, but testing with your app's key tests its access.
3. In **Input text**, enter `How do I reset my password?`. Set **Dimensions** to `256`, and keep **Float** as the
   **Encoding format**.
4. Select **Generate embeddings**.

![The embeddings playground in Catalog, with Dimensions set to 256, the app-first-call key, and a response of one vector with 256 dimensions and 7 input tokens](../assets/screens/embeddings-05-playground.webp "The playground returns the full response, with a summary of the vectors, dimensions, input tokens, and time.")

To embed a batch or token IDs, set **Input format** to **JSON** and enter an array, such as
`["First document", "Second document"]`.

### Continue in code

Select **Continue in code** › **OpenAI SDK**. The Python snippet sets `base_url` to the alias's base URL, which ends
with `/default/models/openai/v1`, and calls `client.embeddings.create`. The **cURL** snippet calls the
**Embeddings API** at the full endpoint URL.

![The Continue in code dialog, with the OpenAI SDK snippet whose base_url ends with /default/models/openai/v1](../assets/screens/embeddings-06-code.webp "Continue in code gives you the base URL and an embeddings call for the OpenAI SDK.")

> **Checkpoint**
> The **Response** summary shows `1 vector · 256 dimensions · 7 input tokens`. The first call to a new alias can
> take several seconds; the next ones take a few hundred milliseconds.

<!-- track: cli | curl and PowerShell -->

### Embed a sentence

```bash title="Bash"
# Embed one sentence through the gateway: the embeddings alias in model, and the runtime key as a bearer token
curl -sS "${AI_GATEWAY_URL%/}/default/models/openai/v1/embeddings" \
  -H "Authorization: Bearer $AI_GATEWAY_API_KEY" -H "Content-Type: application/json" \
  -d "{\"model\":\"$AI_GATEWAY_EMBEDDINGS_MODEL\",\"input\":\"How do I reset my password?\"}" \
  | jq -r 'if .data then "📊 \(.data | length) vector of \(.data[0].embedding | length) numbers, starting \(.data[0].embedding[0:3]), \(.usage.total_tokens) tokens" else "🚨 \(.error.message // .message // .)" end'
```

```powershell title="PowerShell"
# Build the request: the embeddings alias in model, and one sentence as input
$body = @{ model = $env:AI_GATEWAY_EMBEDDINGS_MODEL; input = "How do I reset my password?" } | ConvertTo-Json

# Embed the sentence through the gateway, with the runtime key as a bearer token
$result = Invoke-RestMethod -Method Post `
  -Uri "$($env:AI_GATEWAY_URL.TrimEnd('/'))/default/models/openai/v1/embeddings" `
  -Headers @{ Authorization = "Bearer $env:AI_GATEWAY_API_KEY" } -ContentType "application/json" -Body $body
if ($?) { Write-Host "📊 $($result.data.Count) vector of $($result.data[0].embedding.Count) numbers, starting $($result.data[0].embedding[0..2] | ConvertTo-Json -Compress), $($result.usage.total_tokens) tokens" }
```

Expected output (your numbers differ slightly):

```text
📊 1 vector of 1536 numbers, starting [0.017578125,-0.045745849609375,0.029815673828125], 7 tokens
```

### Embed a batch with shorter vectors

One request can embed many inputs. `dimensions` asks `text-embedding-3-small` for shorter vectors, which take less
space in a vector index.

```bash title="Bash"
# Embed two sentences in one request, with 256 dimensions instead of the default 1,536
curl -sS "${AI_GATEWAY_URL%/}/default/models/openai/v1/embeddings" \
  -H "Authorization: Bearer $AI_GATEWAY_API_KEY" -H "Content-Type: application/json" \
  -d "{\"model\":\"$AI_GATEWAY_EMBEDDINGS_MODEL\",\"input\":[\"Reset my password\",\"Order a new laptop\"],\"dimensions\":256}" \
  | jq -r 'if .data then "📊 \(.data | length) vectors of \(.data[0].embedding | length) numbers, \(.usage.total_tokens) tokens" else "🚨 \(.error.message // .message // .)" end'
```

```powershell title="PowerShell"
# Build the request: two sentences as input, with 256 dimensions instead of the default 1,536
$body = @{ model = $env:AI_GATEWAY_EMBEDDINGS_MODEL; input = @("Reset my password", "Order a new laptop"); dimensions = 256 } | ConvertTo-Json

# Embed both sentences in one request
$result = Invoke-RestMethod -Method Post `
  -Uri "$($env:AI_GATEWAY_URL.TrimEnd('/'))/default/models/openai/v1/embeddings" `
  -Headers @{ Authorization = "Bearer $env:AI_GATEWAY_API_KEY" } -ContentType "application/json" -Body $body
if ($?) { Write-Host "📊 $($result.data.Count) vectors of $($result.data[0].embedding.Count) numbers, $($result.usage.total_tokens) tokens" }
```

> **Checkpoint**
> The first call printed `1 vector of 1536 numbers`, and the second `2 vectors of 256 numbers, 7 tokens`.

<!-- track: code | Code -->

### Install the SDK

The samples read `AI_GATEWAY_URL`, `AI_GATEWAY_EMBEDDINGS_MODEL`, `AI_GATEWAY_MODEL`, and `AI_GATEWAY_API_KEY` from
the environment. Run the commands from the lab folder.

```bash title="Python"
# Install the samples' requirements: the openai package
pip install -r infra/requirements.txt &&
  echo "📦 The OpenAI SDK is installed"
```

### Embed with the OpenAI SDK

Point the client's `base_url` at the gateway, the same base URL as for chat, and call `client.embeddings.create`
with the alias as the model:

```python title="Python"
client = OpenAI(
    base_url=f"{gateway_url}/default/models/openai/v1",
    api_key=os.environ["AI_GATEWAY_API_KEY"],
)

# One call embeds the whole batch: the response has one vector per input, in the same order.
result = client.embeddings.create(model=model, input=sentences)
vectors = [item.embedding for item in result.data]
```

The sample embeds three sentences, compares the first one with the other two by cosine similarity, and then asks
for 256 dimensions:

<!-- include: infra/embed.py -->

### Run the sample

```bash title="Python"
# Run the sample: it embeds three sentences and compares them
echo "📊 Embeddings through the gateway:"
python infra/embed.py
```

Expected output (your numbers differ slightly):

```text
📊 Embeddings through the gateway:
Model:       text-embedding-3-small
Vectors:     3 with 1536 dimensions each
Tokens used: 25
Similarity:  0.524  'How do I reset my password?' vs 'I forgot the credentials I use to sign in.'
Similarity:  0.137  'How do I reset my password?' vs "What's on the cafeteria menu today?"
Shorter:     256 dimensions with dimensions=256
```

> **Checkpoint**
> The two password questions score much higher than the password question and the cafeteria question, although
> they share almost no words.

<!-- /tracks -->

## Build a mini RAG app

[infra/rag.py](infra/rag.py) is a whole RAG app in about 80 lines. It embeds eight help desk articles in one batch,
embeds the question with the same alias, ranks the articles by cosine similarity, and asks `gpt-5-mini` to answer
from the two best matches. Both models use the same `OpenAI` client, so the same gateway URL and key. A real app
stores the vectors in a vector index, such as Azure AI Search, instead of a Python dictionary.

<!-- include: infra/rag.py -->

<!-- steps -->

### Ask a question

```bash title="Bash or PowerShell"
# Install the openai package that the app uses, if you haven't on the Code track
pip install -r infra/requirements.txt &&
  echo "📦 The OpenAI SDK is installed"

# Run the app with its default question: it embeds the articles and the question, then answers with gpt-5-mini
echo "🤖 Mini RAG through the gateway:"
python infra/rag.py
```

Expected output (your answer differs):

```text
🤖 Mini RAG through the gateway:
Indexed:  8 articles with text-embedding-3-small, 216 tokens
Question: My laptop screen cracked. How do I get it fixed, and how long does it take?
Match:    0.701  laptop-repair
Match:    0.298  new-hire
Answer:   Report a cracked screen in the IT portal under Hardware > Repair; Contoso ships a loaner within two business days and the repair takes about a week [laptop-repair].
Tokens:   373 with gpt-5-mini
```

### Ask your own question

```bash title="Bash or PowerShell"
# Ask a question that no article answers: the app should say so instead of making something up
echo "🤖 Mini RAG through the gateway:"
python infra/rag.py "Can I bring my dog to the office?"
```

<!-- /steps -->

> **Checkpoint**
> The best match for the laptop question is `laptop-repair`, the answer cites `[laptop-repair]`, and the question
> about dogs gets an answer that says the articles don't cover it.

## Cap ingestion with a token limit

An ingestion job can embed thousands of chunks in a few minutes and use up the deployment's quota that the RAG app
needs for its questions. Give the embedding model its own token limit: **2,000 tokens per minute for each caller
identity**, low enough to watch it work. In production, size it from the deployment's capacity, 20,000 tokens per
minute in this lab, and the number of callers.

<!-- track: portal | Portal -->

### Add the token limit

1. Select **Policies** › **Add policy**.
2. On **Type**, select **Token rate limit**. Select **Next**.
3. On **Scope**, select **Individual assets**, then select `text-embedding-3-small — foundry-models`, the row whose
   **Type** is **Model**. Select **Next**.

   ![The Scope step of Add policy, with Individual assets selected and the text-embedding-3-small model checked](../assets/screens/embeddings-07-policy-scope.webp "Scope the token limit to the embedding model, so that every alias that routes to it shares the limit.")

4. On **Details**, keep **Per minute** as the **Time period**, set **Allowed tokens** to `2000`, and keep
   **Identity** in **Counter dimensions**. Select **Create**.

   ![The Details step of Add policy, with Per minute, 2000 allowed tokens, and the Identity counter dimension](../assets/screens/embeddings-08-policy-details.webp "2,000 tokens per minute, with one counter for each caller identity.")

> **Checkpoint**
> **Policies** lists `2,000 tokens / minute per caller identity`, of type **Token rate limit**, on
> **Model: text-embedding-3-small**.

<!-- track: cli | Azure CLI -->

### Set the token limit

```bash title="Bash"
# Add a token limit to the embedding model: 2,000 tokens per minute, with its own counter for each caller identity
az aigateway model policy set-token-limit -g "$RG" --gateway-name "$GATEWAY" --provider-name "$PROVIDER" \
  -n "$EMBEDDINGS" --token-limit 2000 --token-period minute --token-counter-key Identity &&
  echo "🔐 Token limit of 2,000 tokens per minute is on $EMBEDDINGS"
```

```powershell title="PowerShell"
# Add a token limit to the embedding model: 2,000 tokens per minute, with its own counter for each caller identity
az aigateway model policy set-token-limit -g $RG --gateway-name $GATEWAY --provider-name $PROVIDER `
  -n $EMBEDDINGS --token-limit 2000 --token-period minute --token-counter-key Identity
if ($?) { Write-Host "🔐 Token limit of 2,000 tokens per minute is on $EMBEDDINGS" }
```

> **Checkpoint**
> The command prints the model's policies: one `tokenLimit` with `count` `2000`, `period` `minute`, and
> `counterKey` `["identity"]`.

<!-- track: iac | Bicep + azd -->

### Check the token limit

`publish-embeddings.bicep` already put the token limit in the model's `policies`. Print it:

```bash title="Bash"
# Print the policies of the embedding model that azd deployed
echo "🔐 Policies on $EMBEDDINGS:"
az aigateway model show -g "$RG" --gateway-name "$GATEWAY" --provider-name "$PROVIDER" -n "$EMBEDDINGS" \
  --query properties.policies -o json
```

```powershell title="PowerShell"
# Print the policies of the embedding model that azd deployed
Write-Host "🔐 Policies on ${EMBEDDINGS}:"
az aigateway model show -g $RG --gateway-name $GATEWAY --provider-name $PROVIDER -n $EMBEDDINGS `
  --query properties.policies -o json
```

> **Checkpoint**
> The output has one `tokenLimit` with `count` `2000`, `period` `minute`, and `counterKey` `["identity"]`.

<!-- track: terraform | Terraform -->

### Check the token limit

`terraform/embeddings.tf` already put the token limit in the model's `policies`. Print it:

```bash title="Bash"
# Print the policies of the embedding model that Terraform deployed
echo "🔐 Policies on $EMBEDDINGS:"
az aigateway model show -g "$RG" --gateway-name "$GATEWAY" --provider-name "$PROVIDER" -n "$EMBEDDINGS" \
  --query properties.policies -o json
```

```powershell title="PowerShell"
# Print the policies of the embedding model that Terraform deployed
Write-Host "🔐 Policies on ${EMBEDDINGS}:"
az aigateway model show -g $RG --gateway-name $GATEWAY --provider-name $PROVIDER -n $EMBEDDINGS `
  --query properties.policies -o json
```

> **Checkpoint**
> The output has one `tokenLimit` with `count` `2000`, `period` `minute`, and `counterKey` `["identity"]`.

To size the limit for production later, pass `embeddings_tokens_per_minute` to `terraform plan` and apply the plan,
for example `terraform -chdir=terraform plan -var embeddings_tokens_per_minute=20000 -out tfplan`. Only the
embedding model changes: the plan ends with `Plan: 0 to add, 1 to change, 0 to destroy.` Keep `2000` for **Verify**.

<!-- /tracks -->

## Verify

<!-- steps -->

### Call each alias with the wrong API

Each alias accepts only the endpoints of its backend models. A chat request to the embeddings alias, or a request
for a model that the gateway doesn't know, fails at the gateway:

```bash title="Bash"
# Send a chat request to the embeddings alias, which supports only the embeddings endpoint
curl -sS "${AI_GATEWAY_URL%/}/default/models/openai/v1/chat/completions" \
  -H "Authorization: Bearer $AI_GATEWAY_API_KEY" -H "Content-Type: application/json" \
  -d "{\"model\":\"$AI_GATEWAY_EMBEDDINGS_MODEL\",\"messages\":[{\"role\":\"user\",\"content\":\"ping\"}]}" \
  | jq -r '"🚦 \(.error.code // .statusCode): \(.error.message // .message)"'

# Ask for an embedding model that the gateway doesn't publish
curl -sS "${AI_GATEWAY_URL%/}/default/models/openai/v1/embeddings" \
  -H "Authorization: Bearer $AI_GATEWAY_API_KEY" -H "Content-Type: application/json" \
  -d "{\"model\":\"text-embedding-3-large\",\"input\":\"ping\"}" \
  | jq -r '"🚦 \(.error.code // .statusCode): \(.error.message // .message)"'
```

```powershell title="PowerShell"
# Send a chat request to the embeddings alias, which supports only the embeddings endpoint
$body = @{ model = $env:AI_GATEWAY_EMBEDDINGS_MODEL; messages = @(@{ role = "user"; content = "ping" }) } | ConvertTo-Json -Depth 5
$reply = Invoke-WebRequest -Method Post -Uri "$($env:AI_GATEWAY_URL.TrimEnd('/'))/default/models/openai/v1/chat/completions" `
  -Headers @{ Authorization = "Bearer $env:AI_GATEWAY_API_KEY" } -ContentType "application/json" -Body $body -SkipHttpErrorCheck
$error1 = ($reply.Content | ConvertFrom-Json).error
Write-Host "🚦 $($error1.code): $($error1.message)"

# Ask for an embedding model that the gateway doesn't publish
$body = @{ model = "text-embedding-3-large"; input = "ping" } | ConvertTo-Json
$reply = Invoke-WebRequest -Method Post -Uri "$($env:AI_GATEWAY_URL.TrimEnd('/'))/default/models/openai/v1/embeddings" `
  -Headers @{ Authorization = "Bearer $env:AI_GATEWAY_API_KEY" } -ContentType "application/json" -Body $body -SkipHttpErrorCheck
$error2 = ($reply.Content | ConvertFrom-Json).error
Write-Host "🚦 $($error2.code): $($error2.message)"
```

Expected output:

```text
🚦 unsupported_endpoint: Model 'text-embedding-3-small' does not support the requested endpoint '/default/models/openai/v1/chat/completions'.
🚦 model_not_found: 'text-embedding-3-large' is not a known model alias.
```

### Run an ingestion burst

[infra/ingest.py](infra/ingest.py) sends eight batches of 20 short chunks, about 800 tokens each, with the SDK's
retries turned off so that you see every response. The gateway applies a new token limit within about 30 seconds, so
if you added it a moment ago, wait half a minute first.

<!-- include: infra/ingest.py -->

```bash title="Bash or PowerShell"
# Send eight batches of 20 chunks to the embeddings alias, and print the status and tokens of each one
echo "🚦 Expect 429 Too Many Requests once the 2,000-token allowance runs out:"
python infra/ingest.py
```

Expected output:

```text
🚦 Expect 429 Too Many Requests once the 2,000-token allowance runs out:
Batch  Status  Tokens  Total   Retry-After
1      200     800     800     -
2      200     800     1600    -
3      200     800     2400    -
4      429     -       2400    11 s
5      429     -       2400    10 s
6      429     -       2400    10 s
7      429     -       2400    10 s
8      429     -       2400    10 s
```

The gateway checks the counter before it forwards a request. The third batch starts at 1,600 tokens, under the
limit, so it passes and takes the counter to 2,400. Every batch after it gets `429` with a `Retry-After` header until
the minute is over. Keep the SDK's retries on in a real ingestion job: the OpenAI SDKs wait for `Retry-After` and
send the batch again.

While the counter is over the limit, every request to the alias gets `429`, even the chat request from the previous
step, because the gateway applies the token limit before it checks the endpoint.

<!-- /steps -->

> **Checkpoint**
> The chat request to the embeddings alias got `unsupported_endpoint`, the unknown model got `model_not_found`, and
> the burst got `429` after about 2,000 tokens.

## Go further

- **Change the embedding model behind the alias.** Apps keep sending `text-embedding-3-small`, but vectors from
  another model, or with other dimensions, can't be compared with the ones in your index. Re-embed your documents
  with the new model first, then switch the alias. See [model aliases](https://ai.gateway.azure.com/docs/models/aliases).
- **Spread ingestion across deployments.** An alias can route among several deployments of the same embedding model
  by priority and weight, and each request or batch goes to one of them. See
  [Route an alias across models by priority and weight](../22-model-routing/README.md).
- **Watch ingestion in Monitoring.** Token usage per caller, model, and alias shows in **Monitoring** once the
  gateway sends telemetry to Application Insights. See [Monitor usage, cost, and traces](../13-monitoring/README.md).
- **Sign in without keys.** Apps can call both aliases with a Microsoft Entra token instead of a key. See
  [Go keyless with Microsoft Entra ID and RBAC](../11-entra-rbac/README.md).
- **Shrink the payload.** `"encoding_format": "base64"` returns each vector as a base64 string of 4-byte floats,
  8,192 characters for 1,536 numbers, instead of a long JSON array.

## Clean up

Delete the embeddings alias and its backend model. The token limit goes with the model, and `gpt-5-mini`, its alias,
and your key from lab 02 stay.

<!-- track: portal | Portal -->

### Delete the alias and the model

1. Select **Models** › **Aliases**. On the `text-embedding-3-small` row, select **More actions** (⋮) › **Delete**,
   then **Delete alias**. Requests that use the alias fail from now on, and the backend model stays.
2. Select **Backend models**, where `text-embedding-3-small` now shows `--` under **Aliases**. Select its checkbox,
   then **Delete selected items** (the trash can) in the bar that says **1 item selected**, then **Delete**.

> **Checkpoint**
> **Models** › **Backend models** lists only `gpt-5-mini`, with its alias `gpt-5-mini`.

<!-- track: cli | Azure CLI -->

### Delete the alias and the model

```bash title="Bash"
# Delete the alias first, then the backend model that it routes to, with its token limit
az aigateway model-alias delete -g "$RG" --gateway-name "$GATEWAY" -n "$AI_GATEWAY_EMBEDDINGS_MODEL" --yes &&
  echo "🚮 Alias $AI_GATEWAY_EMBEDDINGS_MODEL is deleted"
az aigateway model delete -g "$RG" --gateway-name "$GATEWAY" --provider-name "$PROVIDER" -n "$EMBEDDINGS" --yes &&
  echo "🚮 Model $EMBEDDINGS is deleted"
```

```powershell title="PowerShell"
# Delete the alias first, then the backend model that it routes to, with its token limit
az aigateway model-alias delete -g $RG --gateway-name $GATEWAY -n $env:AI_GATEWAY_EMBEDDINGS_MODEL --yes
if ($?) { Write-Host "🚮 Alias $env:AI_GATEWAY_EMBEDDINGS_MODEL is deleted" }
az aigateway model delete -g $RG --gateway-name $GATEWAY --provider-name $PROVIDER -n $EMBEDDINGS --yes
if ($?) { Write-Host "🚮 Model $EMBEDDINGS is deleted" }
```

<!-- track: iac | Bicep + azd -->

### Delete everything azd created

In the `aigateway/labs/26-embeddings` folder, delete everything that `azd up` created. It takes about two minutes.

```bash title="Bash or PowerShell"
# Delete the resource group with the gateway and the Foundry account, and purge the account so its name is free
azd down --purge --force &&
  echo "🚮 The lab's resources are deleted"
```

<!-- track: terraform | Terraform -->

### Delete everything Terraform created

In the `aigateway/labs/26-embeddings` folder, delete the resource group with the gateway, the Foundry
account with both models, and the aliases. The [`azurerm` provider](../SETUP.md#delete-what-terraform-created) also
purges the soft-deleted Foundry account. It takes about two minutes.

```bash title="Bash or PowerShell"
# 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"
```

<!-- /tracks -->

On the portal and CLI tracks, the Foundry deployment stays. Delete it when no other app uses it:

```bash title="Bash"
# Delete the embedding deployment from the Foundry account
az cognitiveservices account deployment delete -g "$FOUNDRY_RG" -n "$FOUNDRY_ACCOUNT" --deployment-name "$EMBEDDINGS" &&
  echo "🚮 Model deployment $EMBEDDINGS is deleted"
```

```powershell title="PowerShell"
# Delete the embedding deployment from the Foundry account
az cognitiveservices account deployment delete -g $FOUNDRY_RG -n $FOUNDRY_ACCOUNT --deployment-name $EMBEDDINGS
if ($?) { Write-Host "🚮 Model deployment $EMBEDDINGS is deleted" }
```

Keep the gateway, `gpt-5-mini`, and the key from lab 02: the next labs reuse them. `azd down` and `terraform destroy`
don't touch them, because the Bicep + azd and Terraform tracks created their own gateways.

## Next steps

- [Throttle tokens per caller with token limits](../06-token-limits/README.md): give every caller its own allowance.
- [Monitor usage, cost, and traces](../13-monitoring/README.md): see ingestion and chat tokens side by side.
- [Route an alias across models by priority and weight](../22-model-routing/README.md): spread one alias across
  deployments.
- Product docs: [supported APIs](https://ai.gateway.azure.com/docs/models/api-formats-and-providers#supported-apis),
  [import models](https://ai.gateway.azure.com/docs/models/import-models), and
  [create an alias](https://ai.gateway.azure.com/docs/models/create-alias).
- [October 28 release: embedding models](https://ai.gateway.azure.com/docs/releases#release-2026-10-28)
