Lesson 1 of 4 Securing Microsoft Foundry Workloads

Configuring AI Gateway for Microsoft Foundry

This lesson explains why a gateway sits in front of models, how to deploy it, and which policies matter most.

Learning outcomes

After completing this lesson, you will be able to:

  • Explain the benefit of a gateway.
  • Describe how to add AI Gateway to Foundry.
  • Distinguish the main llm- policies.
  • Explain the 429 and 403 responses.

Why place a gateway in front of models

When many teams call AI models directly, an organization loses control. A single runaway script can use up a shared quota. Costs cannot be attributed to a team. Each application must implement its own safety checks. Keys spread across projects. AI Gateway, a set of capabilities in Azure API Management, solves these by acting as a single entry point for model, agent and tool traffic, where policies apply consistently.

A policy in API Management is an XML statement that runs on a request or a response. If you know API Management, then AI Gateway simply adds policies designed for language models, which count tokens rather than calls. That matters because the cost of a request to a language model depends on its tokens: two calls may differ a thousandfold in expense.

The following diagram shows the gateway between applications and models.

AI Gateway in Azure API Management as the single entry point for model, agent and tool traffic. Applications call the gateway instead of the models. Policies count tokens rather than calls: llm-token-limit returns 429 or 403 when a team exceeds its rate or quota, llm-emit-token-metric attributes use, llm-content-safety screens prompts, and semantic caching reuses responses to similar prompts. The gateway then forwards to Foundry deployments.
Policies apply consistently to every team's traffic.

Deploying for Microsoft Foundry

The Foundry portal has a built-in experience for this. Under Manage > AI Gateway, choose Add AI Gateway, and then either Create new, which creates a Basic v2 instance, or Use existing, which needs an API Management instance in a v2 tier, in the same tenant and subscription.

Projects are then added with Add project to gateway, and new projects are enabled by default. The gateway can govern model deployments, registered custom agents, and MCP tools, which are in preview. Limits can be set per project, which supports a chargeback model and fair use.

True or false

Deploying AI Gateway

Exam skills 3.1.7

Check how AI Gateway is deployed for Microsoft Foundry.

Log in to try this activity Interactive activities are free with an account.

The main policies

The following table describes the policies that this exam is most likely to involve.

PolicyPurposeBehaviour
llm-token-limitLimits tokens per key429 when tokens per minute are exceeded, and 403 when a quota is exceeded
llm-emit-token-metricSends token use to Azure Monitor and Application InsightsDimensions allow reporting by user, application or subscription
llm-content-safetyScreens prompts and optionally completionsReturns 403 when blocked, and needs Azure AI Content Safety
llm-semantic-cache-lookup and -storeReuses a response for a similar promptNeeds Azure Managed Redis or another RediSearch-compatible cache, and an embeddings model

The following example shows how the token limit policy might appear in a policy definition, with a per-subscription limit of 5,000 tokens a minute and a monthly quota.

<inbound>
    <base />
    <llm-token-limit counter-key="@(context.Subscription.Id)"
        tokens-per-minute="5000"
        token-quota="1000000"
        token-quota-period="Monthly"
        estimate-prompt-tokens="false"
        remaining-tokens-header-name="x-remaining-tokens" />
</inbound>

The counter-key decides who the limit applies to, so a value based on the subscription, the client IP address or a claim in a token gives very different results. estimate-prompt-tokens is required: true estimates before calling the model and can reject a request early, and false uses the actual count from the model's response.

Match the pairs

AI Gateway policies

Exam skills 3.1.7

Match each AI Gateway policy with its purpose.

Log in to try this activity Interactive activities are free with an account.

Authentication and content safety

A gateway can hide model keys altogether. The client authenticates to API Management with a subscription key or a Microsoft Entra token, and API Management uses its managed identity to call the back-end model, so applications hold no model key. The identity needs an appropriate role on the Azure OpenAI resource, such as Cognitive Services OpenAI User.

For llm-content-safety, a backend points to Azure AI Content Safety, and API Management's managed identity needs the Cognitive Services User role on it. The policy can screen hate, sexual, self-harm and violence categories at a threshold from 0 to 7, can use shield-prompt for prompt attacks, which defaults to false, and can use blocklists. enforce-on-completions extends screening to model output.

Scenario questions

Keys and content safety

Exam skills 3.1.7

Apply the gateway's authentication and content safety settings.

Log in to try this activity Interactive activities are free with an account.

Resilience and load balancing

Availability is also a security concern. A backend pool can spread requests among several deployments using round-robin, weighted, priority-based or session-aware methods. A circuit breaker stops sending traffic to an unhealthy back end, and it can respect the Retry-After value from a throttled model. Priority routing is a common pattern, in which traffic goes first to a deployment with provisioned capacity, and overflows to a pay-as-you-go deployment.

Worked example

Limiting one team

Three teams share an Azure OpenAI deployment in Microsoft Foundry. One team's experiment recently exhausted the capacity for everyone. The company wants each team limited to a monthly token budget and a per-minute rate, with usage reported per team. Applications must no longer hold model keys. What should you implement?

Add AI Gateway, and apply llm-token-limit with a counter key per team, with tokens-per-minute and a monthly token-quota. Add llm-emit-token-metric with a team dimension for reporting. Use API Management's managed identity for the back end, so that applications need no model key.

A plain rate-limit policy counts calls, not tokens, so it would not control a large prompt. Separate deployments would be costly, and they would not provide unified reporting. Quota on the model deployment applies to the deployment as a whole, and not to each team.

Check yourself

Why is a tokens-per-minute limit better than a calls-per-minute limit for a language model?

Show answer

A single call can vary greatly in size, and cost follows tokens rather than calls.

Exam Tips

Remember for the exam

  • 429 means the per-minute rate; 403 means the quota.
  • AI Gateway counts tokens, not just calls.
  • Use the gateway's managed identity, so that applications hold no model keys.
  • llm-content-safety needs Cognitive Services User for the gateway's identity.