Stream AI responses through an AI gateway
Connect a Python chat app on App Service Builder to a model through a dedicated Azure AI Gateway. The app calls the gateway with managed identity, and the gateway calls the model with its own identity. Visitors open the chat without signing in.
This sample keeps its dedicated gateway setup. The current foundryInference
Connections profile selects an Azure OpenAI account, not an AI Gateway resource,
and cannot replace this two-identity gateway chain without changing its purpose.
Keep the gateway's documented app settings, scoped model alias and least-privilege
identities; do not route around it merely to use a different connection profile.
Anyone who can reach the app can send prompts and incur model charges. The sample's request limits are not authentication or a spending cap. Use synthetic prompts and a new, isolated resource group for at most two hours, then delete it.
Prerequisites
- An Azure subscription with permission to create resources and assign roles.
- Azure CLI with Bicep and the Builder CLI, signed in to the intended subscription.
- A GitHub fork of the sample and Builder GitHub App access to your fork. Keep the root
builder.yaml. - Builder in West US 2, dedicated AI Gateway availability in East US 2, and at least 20,000 tokens per minute of GlobalStandard
gpt-5.4-miniquota in East US 2.
The gateway Preview's no-charge offer doesn't cover model inference or other Azure charges.
1. Prepare the sample and Azure resources
- Browser to app
The app accepts a bounded development/test chat prompt. Its server owns the gateway settings; visitors do not receive Azure credentials.
- App identity to gateway
AI Gateway Models User permits the app identity to call only the configured
appservice-chatmodel alias. - Gateway identity to model
The gateway's different identity uses account-scoped Cognitive Services OpenAI User to invoke the model and stream the result back through the app.
Fork the AI Gateway chat sample, then clone your fork and open its root:
git clone "<your-fork-https-url>" ai-gateway-chat
cd ai-gateway-chat
Set your subscription, a new resource group, and your fork's owner/name in Bash:
SUBSCRIPTION_ID="<your-subscription-id>"
RESOURCE_GROUP="rg-builder-ai-gateway-demo"
REPOSITORY="<your-github-owner>/<your-fork>"
az group create --subscription "$SUBSCRIPTION_ID" \
--name "$RESOURCE_GROUP" --location westus2
az deployment group create --subscription "$SUBSCRIPTION_ID" \
--resource-group "$RESOURCE_GROUP" --name gateway-sample \
--template-file infra/main.bicep
APP_NAME=$(az deployment group show --subscription "$SUBSCRIPTION_ID" \
--resource-group "$RESOURCE_GROUP" --name gateway-sample \
--query properties.outputs.builderAppName.value --output tsv)
APP_URL=$(az deployment group show --subscription "$SUBSCRIPTION_ID" \
--resource-group "$RESOURCE_GROUP" --name gateway-sample \
--query properties.outputs.builderAppUrl.value --output tsv)
The template creates the Builder app, an identity, the gateway, and a model deployment, granting AI Gateway Models User to the app identity (only on the appservice-chat alias) and Cognitive Services OpenAI User to the gateway identity (only on the model account), with local model keys and visitor sign-in disabled (see managed identities). It sets AZURE_AI_GATEWAY_HOST, AZURE_CLIENT_ID, APP_PUBLIC_HOST, and SAMPLE_EXPIRES_AT (two hours out; don't rerun provisioning to extend it).
2. Connect your fork
builder app source connect github --name "$APP_NAME" \
--subscription "$SUBSCRIPTION_ID" --resource-group "$RESOURCE_GROUP" \
--repository "$REPOSITORY" --branch main
Complete any requested GitHub access steps.
3. Deploy the application
builder app deploy --name "$APP_NAME" \
--subscription "$SUBSCRIPTION_ID" --resource-group "$RESOURCE_GROUP"
printf '%s\n' "$APP_URL"
Wait for success, then open the URL before the sample expires.
4. Verify a streamed response
Select Explain AI Gateway and send the message; text should stream in, followed by Response complete. A healthy page alone doesn't verify model access. For HTTP 429, wait at least five seconds. The sample allows two concurrent chats per process and stops model calls at its deadline or after 12 model requests, including retries; counters reset on restart and multiply with scaling. See the sample README for diagnostics.
Clean up resources
Delete only the isolated resource group created for this tutorial, and confirm it holds only this exercise's resources:
az resource list --subscription "$SUBSCRIPTION_ID" --resource-group "$RESOURCE_GROUP" \
--query '[].{name:name,type:type}'
az group delete --subscription "$SUBSCRIPTION_ID" \
--name "$RESOURCE_GROUP" --yes
az group exists --subscription "$SUBSCRIPTION_ID" \
--name "$RESOURCE_GROUP"
Wait until the existence check returns false.