AI Gateway Router
Production LLM gateway with intelligent routing, semantic caching, rate limiting, and fallback chains across multiple model providers.
Project walkthrough
AI Gateway Router
Project Goal
Central LLM gateway with caching, routing, rate limits, and fallbacks.
- →Single API for multiple LLM providers
- →Semantic cache to cut costs on repeated queries
- →Per-tenant rate limits and quotas
- →Automatic fallback when providers fail
Use ← → arrow keys or buttons to navigate the walkthrough
Time breakdown (45h)
Each phase maps to the estimated hours — follow in order for a realistic build schedule.
Gateway scaffold & routing
10h- •Unified OpenAI-compatible API surface
- •Provider adapters with normalized request/response
- •Complexity classifier for model tier selection
Semantic caching layer
8h- •Embed incoming prompts, lookup similar cached responses
- •TTL and invalidation policies per endpoint
- •Cache hit rate metric and bypass for streaming
Rate limiting & quotas
8h- •Token bucket per API key and tenant
- •Daily/monthly quota enforcement with 429 responses
- •Admin API for quota management
Observability & fallback
10h- •Fallback chain: primary → secondary → tertiary provider
- •Prometheus: latency, error rate, cost, cache hits
- •Structured logging with request_id propagation
Load testing & deployment
9h- •k6 load test at 100 RPS with p95 < 2s
- •Docker Compose for local stack
- •Runbook for provider outage scenarios
Architecture
All LLM traffic flows through a single gateway that checks a semantic cache, enforces per-tenant rate limits, and routes to the optimal provider and model tier based on query complexity. Failed requests cascade through a fallback chain while Prometheus tracks latency, cache hit rate, and cost per route.
Scroll inside the frame to explore · use + / − to zoom up to 200%
Prerequisites
- FastAPI production patterns (middleware, dependency injection)
- Redis for caching and rate-limit counters
- Multiple LLM provider API keys (OpenAI, Anthropic)
- Understanding of token-based billing and model tiers
- Prometheus metrics and basic load testing (locust or k6)
Setup steps
- Scaffold FastAPI gateway with unified /v1/chat/completions endpoint
- Configure Redis cluster for cache and rate-limit state
- Add provider adapters for OpenAI and Anthropic with normalized response format
- Implement semantic cache using embedding similarity on prompt hash
- Define routing rules: simple queries → cheap model, complex → flagship
- Deploy behind nginx and run k6 load test at 100 RPS
Features to build
- Multi-provider routing
- Semantic response caching
- Per-tenant rate limits
- Fallback chains
- Cost-aware model selection
Expected result
Route 100 RPS through the gateway, demonstrate 40%+ cache hit rate on repeated queries, show automatic fallback when a provider is down, and display a Grafana panel with per-model cost and latency breakdown.
Resume bullet points
- →Built production LLM gateway routing 500K+ requests/month across providers
- →Reduced inference costs by 50% via semantic caching and model tiering
