Agentic AI Notebook
Back to Projects
ProductionPhase 8 45 hours(broken down below)

AI Gateway Router

Production LLM gateway with intelligent routing, semantic caching, rate limiting, and fallback chains across multiple model providers.

PythonFastAPIRedisOpenAIAnthropicPrometheus

Project walkthrough

AI Gateway Router

1 / 6

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.

100%
Loading diagram...

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

  1. Scaffold FastAPI gateway with unified /v1/chat/completions endpoint
  2. Configure Redis cluster for cache and rate-limit state
  3. Add provider adapters for OpenAI and Anthropic with normalized response format
  4. Implement semantic cache using embedding similarity on prompt hash
  5. Define routing rules: simple queries → cheap model, complex → flagship
  6. 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

Interview questions

How does semantic caching differ from exact-match caching?
Exact-match caches on prompt hash — only identical prompts hit. Semantic caching embeds the prompt and returns cached responses for similar queries (cosine similarity above threshold), dramatically increasing hit rates for paraphrased questions.
How do you design a fallback chain for LLM providers?
Define priority order by cost, latency, and capability. On timeout or 5xx, retry on next provider with circuit breaker. Normalize responses so clients see a consistent format regardless of backend.
What routing strategy minimizes cost without hurting quality?
Classify query complexity (token count, task type, user tier). Route simple/classification tasks to cheap models (GPT-4o-mini), complex reasoning to flagship models. Track quality metrics per route to validate tiering.