Skip to main content
This pattern assembles three Azure-native components into a hardened AI gateway. Azure API Management owns identity validation, rate limiting, and request routing. Cognisafe owns AI-specific runtime security — OWASP LLM scoring, jailbreak detection, PII scanning, cost attribution, and the immutable audit trail. Azure OpenAI sits behind a private endpoint and is unreachable from the public internet. No component duplicates another’s responsibility. Use this pattern when your organisation needs to: satisfy Azure Security Benchmark controls on AI workloads, enforce per-team request quotas at the gateway layer, demonstrate an audit trail of every LLM exchange to a compliance team, or integrate AI API access with an existing Azure AD enterprise application model.

Architecture overview

Response flows back through the same path. APIM passes the LLM response payload through unchanged. Cognisafe scoring runs entirely asynchronously — the client receives the LLM response with no scoring latency.

Component responsibilities

Implementation

1

Deploy Azure OpenAI behind a private endpoint

Disable public network access on the Azure OpenAI resource. Create a private endpoint in the same VNet as your AKS cluster.
Verify the private endpoint resolves from inside the AKS cluster:
2

Deploy Cognisafe to AKS

Deploy the Cognisafe stack into AKS. The proxy service should be a ClusterIP (not LoadBalancer) — APIM reaches it over the VNet, not the public internet.
Set UPSTREAM_URL to the Azure OpenAI private endpoint hostname. The proxy resolves this over the private DNS zone configured in Step 1.
3

Create an APIM instance with VNet integration

APIM must be deployed in the same VNet (or a peered VNet) to reach the Cognisafe ClusterIP. Use Internal mode to keep APIM itself off the public internet, or External mode if you need the developer portal publicly accessible.
Add the Cognisafe proxy ClusterIP as a named backend. Use the AKS internal load balancer IP or the internal DNS name if APIM is in the same cluster VNet:
4

Configure the APIM inbound policy

The inbound policy does five things in sequence: validate the AAD JWT, enforce a quota, extract the project ID from the token claims, inject the Cognisafe API key, and set the backend.Create an API in APIM (e.g. ai-gateway) and apply this policy at the API level:
Store cognisafe-proxy-key as an APIM named value (type: secret). Retrieve it from Azure Key Vault rather than embedding it directly.
5

Register an Azure AD application for API consumers

Clients authenticate to APIM using an AAD access token scoped to your API registration.
Client applications acquire a token with:
6

Configure APIM products and subscriptions for quota tiers

Map your internal team/business unit structure to APIM products. Each product gets its own quota, and each team gets a subscription key tied to that product.
Apply per-product quota policy on the product scope (not API scope) to enforce monthly limits independently of the per-request rate limit applied at the API level.

Security properties

This pattern satisfies the following controls by construction:

OWASP LLM Top 10 controls addressed

Operational guidance

Log Analytics integration. Enable APIM diagnostics and stream to a Log Analytics workspace. Correlate APIM request IDs with Cognisafe’s llm_requests.id for end-to-end tracing:
Sentinel alerting. Forward Cognisafe webhook events (threat_detected, pii_detected) to a Sentinel custom log table via an Azure Logic App or Event Hub. This puts AI-layer threats into the same SIEM as your infrastructure alerts. Cost attribution. Cognisafe attributes token cost by project_id and agent_name. Use the /api/v1/costs?group_by=project_id endpoint to generate monthly chargeback reports per APIM product/subscription. Scaling. The Cognisafe proxy is stateless — scale the AKS deployment horizontally. The safety worker is independently scalable via a separate Deployment; each replica pulls from the same Redis queue. APIM scales within its SKU tier.

Failure modes

APIM’s validate-jwt policy caches the OpenID Connect metadata document. If you rotate your AAD app registration signing keys, allow up to 5 minutes for APIM to refresh the cache before forcing a pod restart.
Do not use the APIM Developer tier for production. It does not support VNet integration or zone redundancy, and has no SLA.