KatInterview

Design an Enterprise AI Gateway for Heterogeneous Models

System DesignModel answer
MediumModel answerSystem Designsystem-designai-gatewayllm-infrastructure

Last updated

Design a high-throughput, multi-tenant AI proxy layer featuring unified OpenAI-compatible routing, token-aware rate limiting, low latency overhead, and PII masking.

Scenario

Modern enterprises utilize a mixture of commercial foundation models (such as OpenAI, Anthropic, or AWS Bedrock) alongside self-hosted open-source deployments (such as vLLM or TGI on Kubernetes). Exposing these diverse downstream providers directly to internal product teams creates strong coupling, makes budget control difficult, increases risk during provider outages, and makes security auditing challenging.

You are tasked with designing an enterprise-grade AI Gateway that functions as an intelligent proxy layer positioned between consumer applications and model execution backends.

Requirements

Functional Requirements

  • Unified Interface: Expose a standardized OpenAI-compatible REST and SSE API (/v1/chat/completions, /v1/embeddings) across all underlying backends.
  • Dynamic Routing & Resiliency: Support configuration-driven routing (by latency, unit price, or policy) with automated fallbacks (e.g., fallback to Claude 3.5 Sonnet if GPT-4o encounters a 429 rate limit or 5xx server error).
  • Multi-Tenant Rate Limiting: Enforce API key authentication along with dual-dimension throttling: Requests Per Minute (RPM) and Tokens Per Minute (TPM) per client/team.
  • Cost & Usage Telemetry: Accurately track input tokens, output tokens, cached tokens, and calculated monetary cost per tenant with minimal ingestion delay.
  • Data Protection (PII Scrubbing): Sanitize sensitive or personally identifiable information (PII) from prompts prior to sending requests to external public providers.

Non-Functional Requirements

  • Low Overhead: Added latency overhead introduced by the proxy must remain strictly under 15ms at the P99 level (excluding the model's actual inference time).
  • High Availability: Maintain 99.99% overall service availability.
  • Throughput & Concurrency: Support a peak load of 10,000 requests per second (RPS), where approximately 70% of active streams utilize Server-Sent Events (SSE).

What to Cover

  1. System Architecture & Data Flow: Diagram and detail the core data plane and control plane services.
  2. Streaming Protocol & Memory Management: Explain how incremental SSE chunk proxying works while maintaining token telemetry without buffering full responses in memory.
  3. Dual-Dimension Quota Strategy (RPM & TPM): Detail the data structures and algorithms used to manage dynamic token reservations across distributed nodes.
  4. Resiliency & Fallback Mechanics: Cover circuit breaker state management and how mid-stream connection breaks or upstream failures are mitigated.
  5. Data Storage & Schema Design: Outline schemas for tenant authentication, model pricing snapshots, and analytics aggregation pipelines.

Hints

Hint 1

Calculate concurrent open connections rather than focusing solely on RPS, as SSE streams are long-lived.

Hint 2

Token usage (TPM) is unknown when a request arrives; consider a 'reserve-then-reconcile' mechanism during request admission.

Hint 3

Decouple the synchronous hot data path from analytical usage logging by placing telemetry dispatch onto an asynchronous message bus.