|
| 1 | +--- |
| 2 | +title: GenAI gateway capabilities in Azure API Management |
| 3 | +description: Learn about policies and features in Azure API Management that support generative AI (GenAI) gateway capabilities, such as token limiting, load balancing, semantic caching, and more. |
| 4 | +services: api-management |
| 5 | +author: dlepow |
| 6 | + |
| 7 | +ms.service: api-management |
| 8 | +ms.topic: concept-a |
| 9 | +ms.date: 07/16/2024 |
| 10 | +ms.author: danlep |
| 11 | +--- |
| 12 | + |
| 13 | +# Overview of generative AI gateway capabilities in Azure API Management |
| 14 | + |
| 15 | +[!INCLUDE [api-management-availability-all-tiers](../../includes/api-management-availability-all-tiers.md)] |
| 16 | + |
| 17 | +While generative AI services and their APIs provide powerful capabilities for understanding, interpreting, and generating human-like text and images, they can also impose significant management and security challenges. This article provides an introduction to how Azure API Management can help you manage generative AI APIs, such as those provided by [Azure OpenAI Service](../ai-services/openai/overview.md). Azure API Management provides a range of capabilities including policies, metrics, and other features to enhance security, performance, and reliability of APIs for your intelligent apps. Collectively, this set of features enables API Management to be a *generative AI (GenAI) gateway* for your applications. |
| 18 | + |
| 19 | +## Challenges in managing generative AI APIs |
| 20 | + |
| 21 | +One of the main resources you have in Azure OpenAI Service is tokens. Azure OpenAI assigns quota for your model deployments expressed in tokens-per-minute (TPM) which is then distributed across your model consumers - for example, different applications, developer teams, departments within the company, etc. |
| 22 | + |
| 23 | +Azure makes it easy to connect a single app to Azure OpenAI Service. Your intelligent application connects to Azure OpenAI Service directly using an API key with a TPM limit configured directly on the model deployment level. However, when you start growing your application portfolio, you are presented with multiple apps calling single or even multiple Azure OpenAI Service endpoints deployed as pay-as-you-go or [Provisioned Throughput Units](../ai-services/openai/concepts/provisioned-throughput.md) (PTU) instances. That comes with certain challenges: |
| 24 | + |
| 25 | +* How is token usage tracked across multiple applications? Can cross charges be calculated for multiple applications/teams that use Azure OpenAI Service models? |
| 26 | +* How do you ensure that a single app doesn't consume the whole TPM quota, leaving other apps with no option to use Azure OpenAI Service models? |
| 27 | +* How is the API key securely distributed across multiple applications? |
| 28 | +* How is load distributed across multiple Azure OpenAI endpoints? Can you ensure that the committed capacity in PTUs is used first before falling back to pay-as-you-go instances? |
| 29 | + |
| 30 | +The rest of this article describes how Azure API Management can help you address these challenges. |
| 31 | + |
| 32 | +## Import Azure OpenAI Service resource as an API |
| 33 | + |
| 34 | +[Import an API from an Azure OpenAI Service endpoint](azure-openai-api-from-specification.md) to Azure API management using a single-click experience. API Management streamlines the onboarding process by automatically importing the OpenAPI schema for the Azure OpenAI API and sets up authentication to the Azure OpenAI endpoint using managed identity, removing the need for manual configuration. Within the same user-friendly experience, you can preconfigure policies for [token limits](#token-limit-policy) and [emitting token metrics](#emit-token-metric-policy). |
| 35 | + |
| 36 | +:::image type="content" source="media/azure-openai-api-from-specification/azure-openai-api.png" alt-text="Screenshot of Azure OpenAI API tile in the portal."::: |
| 37 | + |
| 38 | +## Token limit policy |
| 39 | + |
| 40 | +Configure the [Azure OpenAI token limit policy](azure-openai-token-limit-policy.md) to manage and enforce limits per API consumer based on the usage of Azure OpenAI Service tokens. With this policy you can set limits, expressed in tokens-per-minute (TPM). |
| 41 | + |
| 42 | +:::image type="content" source="media/genai-gateway-capabilities/token-rate-limiting.png" alt-text="Diagram of limiting Azure OpenAI Service tokens in API Management."::: |
| 43 | + |
| 44 | +This policy provides flexibility to assign token-based limits on any counter key, such as subscription key, IP address, or an arbitrary key defined through a policy expression. The policy also enables precalculation of prompt tokens on the Azure API Management side, minimizing unnecessary requests to the Azure OpenAI Service backend if the prompt already exceeds the limit. |
| 45 | + |
| 46 | +The following basic example demonstrates how to set a TPM limit of 500 per subscription key: |
| 47 | + |
| 48 | +```xml |
| 49 | +<azure-openai-token-limit counter-key="@(context.Subscription.Id)" |
| 50 | + tokens-per-minute="500" estimate-prompt-tokens="false" remaining-tokens-variable-name="remainingTokens"> |
| 51 | +</azure-openai-token-limit> |
| 52 | +``` |
| 53 | + |
| 54 | +## Emit token metric policy |
| 55 | + |
| 56 | +The [Azure OpenAI emit token metric](azure-openai-emit-token-metric-policy.md) policy sends metrics to Application Insights about consumption of large language model tokens through Azure OpenAI Service APIs. The policy helps provide an overview of the utilization of Azure OpenAI Service models across multiple applications or API consumers. This policy could be useful for chargeback scenarios, monitoring, and capacity planning. |
| 57 | + |
| 58 | +:::image type="content" source="media/genai-gateway-capabilities/emit-token-metrics.png" alt-text="Diagram of emitting Azure OpenAI Service token metrics using API Management."::: |
| 59 | + |
| 60 | +This policy captures prompt, completions, and total token usage metrics and sends them to an Application Insights namespace of your choice. Moreover, you can configure or select from predefined dimensions to split token usage metrics, so you can analyze metrics by subscription ID, IP address, or a custom dimension of your choice. |
| 61 | + |
| 62 | +For example, the following policy sends metrics to Application Insights split by client IP address, API, and user: |
| 63 | + |
| 64 | +```xml |
| 65 | +<azure-openai-emit-token-metric namespace="openai"> |
| 66 | + <dimension name="Client IP" value="@(context.Request.IpAddress)" /> |
| 67 | + <dimension name="API ID" value="@(context.Api.Id)" /> |
| 68 | + <dimension name="User ID" value="@(context.Request.Headers.GetValueOrDefault("x-user-id", "N/A"))" /> |
| 69 | +</azure-openai-emit-token-metric> |
| 70 | +``` |
| 71 | + |
| 72 | +## Load balancer and circuit breaker |
| 73 | + |
| 74 | +One of the challenges when building intelligent applications is to ensure that the applications' backends are resilient to backend failures and can handle high loads. By configuring your Azure OpenAI Service endpoints using [backends](backends.md) in Azure API Management, you can balance the load across them. You can also define circuit breaker rules to stop forwarding requests to the Azure OpenAI Service backends if they're not responsive. |
| 75 | + |
| 76 | +The backend [load balancer](backends.md#backends-in-api-management) supports round-robin, weighted, and priority-based load balancing, giving you flexibility to define a load distribution strategy that meets your specific requirements. For example, define priorities within the load balancer configuration to ensure optimal utilization of specific Azure OpenAI endpoints, particularly those purchased as PTUs. |
| 77 | + |
| 78 | +:::image type="content" source="media/genai-gateway-capabilities/backend-load-balancing.png" alt-text="Diagram of using backend load balancing in API Management."::: |
| 79 | + |
| 80 | +The backend [circuit breaker](backends.md#circuit-breaker) features dynamic trip duration, applying values from the Retry-After header provided by the backend. This ensures precise and timely recovery of the backends, maximizing the utilization of your priority backends to their fullest. |
| 81 | + |
| 82 | +## Semantic caching policy |
| 83 | + |
| 84 | +Configure [Azure OpenAI semantic caching](azure-openai-enable-semantic-caching.md) policies to optimize token consumption by using semantic caching, which stores completions for prompts with similar meaning. |
| 85 | + |
| 86 | +:::image type="content" source="media/genai-gateway-capabilities/semantic-caching.png" alt-text="Diagram of semantic caching in API Management."::: |
| 87 | + |
| 88 | +In API Management, enable semantic caching by using Azure Redis Enterprise or another external cache compatible with RediSearch and onboarded to Azure API Management. By leveraging the Azure OpenAI Service Embeddings API, this policy identifies semantically similar prompts and stores their respective completions in the cache. This approach ensures completions reuse, resulting in reduced token consumption and improved response performance. |
| 89 | + |
| 90 | + |
| 91 | +## Labs and samples |
| 92 | + |
| 93 | +* [Labs for the GenAI gateway capabilities of Azure API Management](https://github.com/Azure-Samples/AI-Gateway) |
| 94 | +* [Azure API Management (APIM) - Azure Open AI Sample (Node.js)](https://github.com/Azure-Samples/genai-gateway-apim) |
| 95 | +* [Python sample code for using Azure OpenAI with API Management](https://github.com/Azure-Samples/openai-apim-lb/blob/main/docs/sample-code.md) |
| 96 | + |
| 97 | +## Related content |
| 98 | + |
| 99 | +* [Blog: Introducing GenAI capabilities in Azure API Management](https://techcommunity.microsoft.com/t5/azure-integration-services-blog/introducing-genai-gateway-capabilities-in-azure-api-management/ba-p/4146525) |
| 100 | +* [Designing and implementing a gateway solution with Azure OpenAI resources](/ai/playbook/technology-guidance/generative-ai/dev-starters/genai-gateway/) |
| 101 | +* [Smart load balancing for OpenAI endpoints and Azure API Management](https://techcommunity.microsoft.com/t5/fasttrack-for-azure/smart-load-balancing-for-openai-endpoints-and-azure-api/ba-p/3991616) |
| 102 | +* [Authenticate and authorize access to Azure OpenAI APIs using Azure API Management](api-management-authenticate-authorize-azure-openai.md) |
0 commit comments