FullStack AI Hub
Studio
AI & LLM Systems · 03. Agents & Production Ops
9 min
6 steps
Intermediate

LLM Routing, Eval & Prompt Caching

Cut inference spend by 70% with semantic caching, cascading model routers, multi-provider fallbacks, and automated eval gates.

AI & LLM Systems03. Agents & Production OpsLesson 2 of 2
9 minIntermediate6 steps

LLM Routing, Eval & Prompt Caching

Cut inference spend by 70% with semantic caching, cascading model routers, multi-provider fallbacks, and automated eval gates.

Cost / 1k Req
$18.40
P95 Latency
2,400 ms
429 Errors
6.5%
Cache Hit
0%
All PromptsAI BackendFrontier API
Incoming Prompt
Fast / Cached Answer
Frontier Escalation
Eval Judge Sample
Provider 429 / Outage
In This LessonStep 1 of 6
What You Will Master
  • ✓Layer exact and semantic caches to serve recurring questions in under 30 ms
  • ✓Route easy requests to small fast models and escalate complex tasks to frontier models
  • ✓Survive upstream LLM rate limits (429s) with multi-region circuit-broken fallbacks
  • ✓Catch prompt regressions before shipping using golden datasets and LLM-as-a-Judge

Architectural Trade-Off Comparison Matrix

PatternBest ForKey Trade-Off
Semantic CacheHigh-volume FAQ, support, and template queries with slight phrasing shiftsRequires a tight cosine threshold (≥0.95) to avoid returning wrong answers
Model Router / CascadeMixed complexity workloads (classification + deep code reasoning)Classifier misrouting can hurt quality if escalation thresholds are too loose
Multi-Provider GatewayMission-critical AI apps exposed to single-vendor quota caps or outagesPrompts must be benchmarked across fallback models to preserve format fidelity
CI Eval Gate + Shadow JudgeContinuous prompt/model upgrades without silent quality regressionsJudge models need human-calibrated rubrics to prevent self-preference bias

Step-by-Step Architectural Breakdown (6 Steps)

Full architectural walkthrough transcript and state transitions for LLM Routing, Eval & Prompt Caching.

STEP 01 · PROBLEMJump to step →

Sending Every Request to One Frontier Model

Most AI features start by routing 100% of traffic—from simple intent classification to multi-file refactoring—to a single expensive frontier endpoint. Monthly token bills explode and a single upstream `429 Rate Limit` breaks the entire product.

watch:Typically 65–80% of production prompts are routine lookups or summaries that a small model handles at 1/15th the cost and 4× the speed.

Frontier Over-ProvisioningToken BurnProvider 429Latency Tail
STEP 02 · SOLUTIONJump to step →

Exact + Semantic Prompt Caching Layer

Place an **AI Gateway** in front of model calls with a two-tier cache: an exact hash lookup for identical system+user prompts, and a **semantic vector cache** (cosine ≥ 0.96) that returns verified answers in 25 ms with zero token cost.

good:Always namespace cache keys by tenant ID, user role, and prompt template version so private answers never cross tenants.

Semantic CacheCosine ThresholdPrompt Prefix CacheTenant Scoping
STEP 03 · TECHNIQUEJump to step →

Cascading Model Router (Fast vs Frontier)

A lightweight **Complexity Router** inspects query intent and token length: 75% of tasks route to a low-cost **Flash/Small Model**, while deep multi-step synthesis escalates to the **Frontier Model**.

idea:Combining a 35% semantic cache with a 75/25 Flash-to-Frontier router cuts total LLM bills by over 75% while halving median latency.

Model CascadeIntent RouterLatency-Cost FrontierConfidence Escalation
STEP 04 · TECHNIQUEJump to step →

Multi-Region Fallbacks & Circuit Breakers

LLM APIs experience regional brownouts and sudden TPM quota exhaustion. Configure your AI Gateway with **per-provider token buckets and circuit breakers** that shift traffic automatically to secondary regions or standby models within milliseconds.

watch:Never retry a 429 infinitely on the same endpoint—trip the circuit breaker and fail over to a secondary deployment with independent TPM quota.

TPM QuotaProvider FailoverCircuit BreakerHedged Requests
STEP 05 · TRADE-OFFJump to step →

Automated Evals & Online Shadow Judges

Changing two words in a system prompt can quietly break 8% of edge cases. Sample 5% of production traces asynchronously into an **Eval Queue** scored by calibrated **LLM-as-a-Judge** rubrics, and promote failures straight into your CI golden test suite.

idea:Run online evals asynchronously off a queue so scoring latency never touches the user-facing response path.

Golden DatasetLLM-as-a-JudgeTrace SamplingRegression Gate
STEP 06 · RECAPJump to step →

Production AI Gateway Recap

Treat AI model calls like any critical external dependency: front them with a **Semantic Cache**, **Cascading Model Router**, **Circuit-Broken Multi-Region Failover**, and an **Async Eval Flywheel**.

good:Great AI systems win on economics and reliability as much as model IQ—route smartly, cache aggressively, and measure continuously.

AI GatewayCost RoutingEval FlywheelMulti-Region SLO

Related Production Deep-Dives & Simulators

Included In Free Course
Free · Verifiable Credential

FS-102LLM Systems Foundations

Build a connected mental model from tokens through retrieval evaluation security and operations.

24 concepts~18h self-paced
Start Free Course
Weekly Engineering Dispatch

Get the weekly systems teardown

One technical note per week covering inference bottlenecks, retrieval harnesses, and commit-pinned teardowns.