Deploy an intelligent routing gateway backed by a Redis semantic vector cache that intercepts repetitive queries, serves cached responses in sub-15ms at $0 cost, and routes non-cached queries between small language models and frontier models.
~90 minutes hands-on · FDE Track, Core Production difficulty.
A corporate client’s OpenAI API bill reached $25,000 in two weeks. An audit reveals that 65% of employee queries are repetitive FAQ inquiries. You will build a proxy gateway using Redis vector search that intercepts queries with $\ge 0.95$ cosine similarity and dispatches residual queries by task complexity.
cache_gateway.py: Redis semantic cache with vector index creation.router.py: Intent and complexity classifier dispatching to SLM vs LLM.telemetry.py: Token savings and latency metric counter.Install Dependencies:
pip install redis==5.0.7 numpy==1.26.4
Implement Vector Distance Caching: Embed incoming queries and run a vector similarity search against cached questions in Redis. If similarity exceeds 0.95, return the cached answer immediately.
Deploy Tiered Routing:
For cache misses, inspect query length and intent classification. Route simple single-clause queries to lightweight models (gpt-4o-mini or local Llama-3-8B), reserving frontier models for multi-document reasoning.
Verify It Works: Simulate 1,000 queries with 60% repetition. Confirm that the semantic cache achieves a $\ge 55%$ hit rate, cutting simulated API spend by over 70%.