01 — Overview & Problem
Thrifty Router is an open-source, cost-optimizing LLM proxy for Google Cloud Vertex AI (Gemini 3.1 and 3). Running on Google Cloud Run with FastAPI, it intercepts prompt requests and dynamically determines the most cost-effective model tier — lite (Gemini 3.1 Flash-Lite at $0.25/$1.50 per 1M tokens), standard (Gemini 3 Flash at $0.50/$3.00), or pro (Gemini 3.1 Pro at $2.00/$12.00) — capable of fulfilling the request with high fidelity. The gateway implements four distinct routing strategies: static fixed mapping, semantic anchor classification using 768-dim embeddings via gemini-embedding-001, zero-shot LLM classification with automated fallback, and opportunistic cascade routing that attempts generation on cheaper tiers before validating and selectively escalating. Integrated with a two-tier semantic caching engine (SHA-256 exact match + Firestore vector search with cosine similarity >= 0.95) and a thread-safe micro-dollar cost ledger with a daily budget circuit breaker, On the 300-prompt golden set, semantic routing matched the all-Pro baseline's 4.90 judge score at 39% of its cost, and Flash-Lite alone kept 98% of it at 3%, as scored by a Gemini 3.1 Pro LLM-as-a-judge.
Defaulting all LLM workloads to flagship reasoning models (like Gemini 3.1 Pro) inflates inference bills by 10x to 20x for simple tasks like factual extraction, sentiment analysis, text formatting, and conversational greetings. Conversely, locking an application into a smaller, cheaper model causes hallucinations or silent failures when users present complex reasoning, code synthesis, or nuanced logic. Furthermore, unmetered public API endpoints risk unbounded cloud charges without real-time spend caps, and repetitive queries incur full model inference latency and expense without semantic deduplication.
02 — The Approach
Thrifty Router implements a layered defense-in-depth architecture. First, incoming requests pass through Slowapi per-IP rate limiting and an in-memory CostLedger that fails closed with HTTP 402 if daily spend exceeds DAILY_BUDGET_USD. Second, the semantic cache layer normalizes prompts, computes SHA-256 hashes for sub-millisecond hits, and queries Firestore vector indexes using gemini-embedding-001 with a 0.95 cosine similarity threshold — serving near-duplicate queries instantly at $0.00 cost with fail-safe error recovery. Third, cache misses enter the strategy router: the flagship Cascade Strategy invokes Gemini 3.1 Flash-Lite (lite) with an optional self-rated confidence tag, evaluating the response through a multi-factor verifier checking finish reason, non-empty text, JSON schema compliance, and confidence >= 70%. If any check fails, it cleanly escalates to standard and pro tiers. Fourth, an automated evaluation harness featuring 300 hand-written, self-contained golden prompts across 8 task categories runs blinded evaluations scored by a Gemini 3.1 Pro judge, measuring what each strategy actually costs and scores. The run also exposed a design limit: Flash-Lite self-reports 95 to 100 confidence on every prompt, so the cascade's confidence gate never escalates and cascade behaves as fixed lite.
System Architecture Diagrams
End-to-end request flow, spend guardrails, and tier routing
300 hand-written golden prompts and LLM-as-a-judge evaluation
03 — Tech Stack Spec
FastAPI
Python 3.12+ Async REST Gateway
Vertex AI
Gemini 3.1 Flash-Lite, 3 Flash, 3.1 Pro
Google Gen AI SDK
google-genai 1.x with Vertex AI Backend
Firestore Vector Search
find_nearest() 768-dim Cosine Similarity
Cascade Router
Multi-Factor Verification & Escalation
Slowapi Limiter
Per-IP Token Bucket Rate Limiting
Cost Ledger
Micro-Dollar Tracking & 402 Stop
Cloud Run
Scale-to-Zero Container Deployment