Azure aigateway

From manu14357/zskills · View interactive version

Azure aigateway is an AI agent skill from the GitHub repository manu14357/zskills. Design an Azure AI gateway layer for centralized model routing, policy enforcement, and operational control. Use this skill when users ask about centralized AI traffic management, model governance, rate limiting, or gateway patterns.

How to use this skill

Skill file

Azure AI Gateway

Centralize LLM traffic through a controlled gateway for security, observability, policy enforcement, and cost control.

Use This Skill When

Context: Gateway Maturity

Immature: Clients call models directly, no centralization

Developing: Basic gateway, simple routing, minimal logging

Managed: Multi-model routing, policy enforcement, token budgets, audit trails → Target

Optimized: Real-time cost optimization, feedback-driven routing, self-healing, ML-based model selection

Required Inputs

Decision Tree

What's the primary goal of this gateway?
├─ Cost control → Route to cheaper model if accuracy acceptable
├─ Reliability → Route to alternative model on failure/timeout
├─ Governance → Enforce policies, audit all requests
├─ Performance → Route based on latency SLA
└─ Multi-tenancy → Tenant isolation, quota per tenant

Which models will the gateway route to?
├─ Single model (single Azure OpenAI deployment) → No routing needed
├─ Multiple Azure OpenAI models → Route by task/cost/latency
├─ Multiple providers (Azure OpenAI + third-party) → Handle auth differences
└─ Canary/blue-green (new model test) → Weighted routing

What policy enforcement is needed?
├─ None (simple routing only)
├─ Token budgets (cap usage per user/org)
├─ Rate limits (req/sec, concurrent, bursts)
├─ Approval workflows (route restricted requests for review)
└─ Compliance (PII redaction, output filtering, audit)

How much data volume?
├─ < 100 req/sec → Simple load balancer
├─ 100-1000 req/sec → Async processing, queue buffering
└─ > 1000 req/sec → Distributed gateway, database-backed quotas

Workflow

Phase 1: Design Gateway Architecture

┌─ Client Application ─┐
│  (web, mobile, API)  │
└──────────┬───────────┘
           │ (HTTPS)
           │
    ┌──────▼─────────────────────────────────────┐
    │ AI Gateway (entry point)                   │
    │                                            │
    │  1. Authenticate client (API key, JWT)    │
    │  2. Check token budget (quota system)     │
    │  3. Log request (audit trail)             │
    │  4. Validate payload (safety checks)      │
    └──────┬─────────────────────────────────────┘
           │
    ┌──────▼──────────────────┐
    │ Routing & Policy Engine │
    │                         │
    │  ├─ Route to model 1    │
    │  ├─ Route to model 2    │
    │  └─ Fallback to model 3 │
    └──────┬──────────────────┘
           │
    ┌──────▼──────────────────────────────────────────┐
    │ Model Pools                                      │
    │                                                  │
    │  [Azure OpenAI GPT-4]  → $0.03 per 1K in      │
    │  [Azure OpenAI GPT-3.5]→ $0.0015 per 1K in   │
    │  [Third-party LLaMA]   → $0.01 per 1K in     │
    │  [Custom Model (AKS)]  → On-premise            │
    └──────────────────────────────────────────────────┘
           │
    ┌──────▼──────────────────────────────────┐
    │ Observability                           │
    │                                         │
    │  ├─ Application Insights (traces)      │
    │  ├─ Log Analytics (audit logs)         │
    │  ├─ Cost Management (spend tracking)   │
    │  └─ Alerts (budget overrun, failures)  │
    └─────────────────────────────────────────┘

Phase 2: Implement Gateway (Choose Stack)

Option 1: Azure API Management + Logic Apps (low-code)

Client → [API Management] → [Logic App] → [Model endpoints]

Benefits: 
- Built-in rate limiting, throttling
- Policy enforcement (transformations, auth)
- Analytics and metrics

Limitations:
- Limited model-specific logic
- Async processing harder

Option 2: Custom Service (Node.js, Python, Go)

# App Service or Container Apps
az containerapp create \
  --resource-group $RG \
  --name "ai-gateway" \
  --image "myacr.azurecr.io/ai-gateway:latest" \
  --target-port 8000 \
  --environment-variables \
    OPENAI_ENDPOINT="https://myai.openai.azure.com/" \
    OPENAI_KEY="@keyvault(openai-key)" \
    FALLBACK_MODEL="gpt-35-turbo"

Option 3: Azure Service Fabric / Kubernetes (scale-out)

Each node = independent gateway instance
Database = shared quota store (Redis, SQL)
Enables high-throughput, multi-region

Phase 3: Configure Routing Rules

  1. Cost-optimized routing:

```python

# Python pseudo-code

def route_to_model(prompt, required_accuracy=0.9):

# Try cheap model first

try:

response = gpt35_turbo.call(prompt, timeout=2)

if quality_score(response) >= required_accuracy:

return response

except Timeout:

pass

# Fallback to expensive but reliable model

response = gpt4.call(prompt, timeout=5)

return response

# Cost breakdown

# 1000 requests: 700 via GPT-3.5 ($1.05) + 300 via GPT-4 ($9.00) = $10.05

# vs. Direct to GPT-4: 1000 × $0.03 = $30

```

  1. Latency-optimized routing:

```

SLA: Response < 500ms

Route 1: Model A (avg 100ms) → Use for 80% of traffic

Route 2: Model B (avg 300ms) → Use for 20% of traffic

Route 3: Model C (avg 1500ms) → Failover only if others fail

```

  1. Canary / Blue-Green:

```

New Model (v2):

└─ 5% of traffic for 1 hour

├─ Monitor error rate, latency, cost

└─ If healthy, shift to 50%, then 100%

Old Model (v1):

└─ 95% of traffic (safe)

```

Phase 4: Enforce Policies

  1. Rate limiting (prevent abuse):

```

Per-client:

├─ 10 requests/sec (burst)

├─ 100,000 requests/day

└─ 500 concurrent connections

Global:

├─ 100,000 requests/sec (max capacity)

└─ Backpressure: Queue & retry with exponential backoff

```

  1. Token budgeting (control cost):

```python

class TokenQuota:

def init(self, daily_limit=1_000_000):

self.limit = daily_limit

self.used = 0 # Track in Redis/database

def check_and_deduct(self, tokens_needed):

current = redis.get(f"quota:{user_id}")

if current + tokens_needed > self.limit:

raise QuotaExceededError(f"Budget exceeded")

redis.incr(f"quota:{user_id}", tokens_needed)

# Cost cap: $100/day per customer

# If usage exceeds budget, reject or queue for review

```

  1. Approval workflows (for sensitive operations):

```

Request: "Summarize confidential document X"

├─ Payload > 10K tokens? → Route to human review

├─ Contains PII pattern? → Route to compliance team

└─ User has restricted tag? → Require manager approval

Approved → Route to model

Rejected → Return error with reason

```

  1. Input/output filtering:

```

Input validation:

├─ Reject prompts with SQL injection patterns

├─ Reject attempts to override system prompt

└─ Redact email/SSN before sending to model

Output filtering:

├─ Block generation of malware/illegal content

├─ Redact PII before returning to user

└─ Flag hallucinated data if confidence low

```

Phase 5: Implement Observability

  1. Log every request (for audit):

```csharp

// C# - Log to Application Insights

var properties = new Dictionary<string, string>

{

{ "ClientId", clientId },

{ "Model", selectedModel },

{ "TokensIn", tokensIn.ToString() },

{ "TokensOut", tokensOut.ToString() },

{ "Cost", cost.ToString() },

{ "LatencyMs", latency.ToString() },

{ "Status", response.StatusCode.ToString() }

};

telemetryClient.TrackEvent("ai_gateway_request", properties);

```

  1. Cost tracking:

```bash

# Daily cost by model

customMetrics

| where name == "ai_gateway_cost"

| extend Model = tostring(customDimensions.model)

| summarize TotalCost = sum(value) by Model, bin(timestamp, 1d)

```

  1. Alerts:

Phase 6: Deploy & Manage

  1. Deploy gateway:

```bash

# Container Apps

az containerapp up \

--source . \

--build-env-vars "GATEWAY_VERSION=1.0" \

--environment-variables "OPENAI_ENDPOINT=..." \

--secrets "openai-key=$(az keyvault secret show --vault-name $KV --name openai-key --query value)"

```

  1. Monitor SLA:

```

Target SLA: 99.9% uptime

├─ Availability: Measure successful responses / total requests

├─ Latency p95: < 500ms

├─ Cost per request: < $0.002

└─ Error rate: < 0.1%

```

  1. Update routes (zero-downtime):

```

Current: 100% → Model A

Step 1: 90% → Model A, 10% → Model B (canary)

Step 2: Monitor for 1 hour

Step 3: 50% → Model A, 50% → Model B (if healthy)

Step 4: 0% → Model A, 100% → Model B (complete)

```

Output Contract

  1. Gateway Architecture
  1. Policy & Control Definitions
  1. Security & Compliance
  1. Monitoring & Cost
  1. Operational Runbook

Guardrails

Use this skill with your AI agent: Open on Skill Issue to copy, download, or add to your library. Works with Claude, ChatGPT, Gemini, Cursor, Windsurf and more.