Inference Economics: Model Routing, Prompt Caching & Tenant Semantic Cache
Cutting AI cloud bills by 60-85% via difficulty routing, KV prefix pinning, semantic cache isolation, and batch queues.
Core Conceptual Brief
Staff AI Engineers are judged heavily on unit economics: structuring prompts to hit 90% prefix cache discounts, routing 70% of easy traffic to Flash/mini models, and safely scoping semantic caches across tenants.
Theoretical & Empirical Architecture
AI features fail commercially when token costs exceed subscription ARPU. Because frontier output tokens typically cost 3x-6x more than input tokens—and reasoning models emit hidden thinking tokens—unoptimized pipelines burn capital rapidly.
Four production levers compress costs 5x-10x without degrading quality: Prompt/Prefix Caching, Confidence-Based Model Routing, Tenant-Isolated Semantic Caching, and Async Batch APIs (50% off for non-realtime workflows).
Under the Hood: Key Engineering Mechanics
1Prefix Caching Ordering Rule
KV prefix caches match strictly from token 0 forward. Always order messages as: [Static System Prompt -> Tool Schemas -> Static Few-Shot Examples -> Cached Reference Corpus -> Dynamic User ID / Timestamp / Turn]. Putting Current Date: 2026-11-14T20:00Z at line 1 destroys your 50%-90% cache hit rate.
2Cascade & Difficulty Model Routing
Use a lightweight classifier or router to send 70-80% of deterministic formatting, extraction, and FAQ queries to $0.10-$0.15/1M Flash/mini models, escalating only low-confidence or complex multi-file synthesis turns to frontier Sonnet/Opus/GPT reasoning tiers.
3Tenant-Safe Semantic Caching
When caching query embeddings in Redis/Qdrant (e.g. cosine >= 0.96), ALWAYS shard cache lookups by tenant_id + role_acl + prompt_version. A global cross-tenant semantic cache is a critical data-leak vulnerability.
4Prompt Optimization, 50%-Off Batch APIs & Constant Accuracy Monitoring
Cut token waste further via three operational levers: (1) Prompt Optimization — trim verbose boilerplate instructions, prune excessive few-shot examples, and cap max_tokens because output tokens cost 3x–6x more; (2) Batch Processing — push non-realtime jobs (nightly summaries, classification backfills, CI eval runs) to 50%-off async Batch APIs; (3) Constant Monitoring + Accuracy Rule — track Cost per Request, cache hit rate, AND Golden Dataset accuracy (OpenTelemetry / Langfuse). Never ship a cheaper pipeline that breaks answer accuracy!
Cache-Friendly Prompt Builder & Tenant-Scoped Cache Key
import hashlib
def build_cache_aligned_messages(static_system: str, tools_json: str, tenant_id: str, user_query: str):
# Static prefix stays byte-identical across millions of calls -> maximizes KV prefix cache hits!
prefix_block = f"{static_system}\n<tools>{tools_json}</tools>"
# Dynamic per-request metadata placed STRICTLY at the end
user_turn = f"<tenant>{tenant_id}</tenant>\n<query>{user_query}</query>"
return [
{"role": "system", "content": prefix_block},
{"role": "user", "content": user_turn},
]
def make_semantic_cache_namespace(tenant_id: str, role: str, prompt_ver: str) -> str:
raw = f"{tenant_id}:{role}:{prompt_ver}"
return hashlib.sha256(raw.encode()).hexdigest()[:16]Production Guardrails & Takeaways
- Never place timestamps, UUIDs, or dynamic user names above static instructions/tools.
- Measure Total Cost per Resolved Task—including retries and verbose reasoning tokens—not just 1M token list price.
- Namespace every semantic cache lookup by tenant_id, RBAC role, and prompt_version.
Real Staff AI Systems Interview Question
Q: Your team is paying $45k/month on frontier LLM calls for a customer support copilot. How do you cut costs by 70% in two weeks without hurting CSAT?
First, reorder prompt blocks so system rules, policy docs, and tool schemas form an immutable prefix for Prompt Caching (50-90% input savings). Second, route classification and standard FAQ turns to a Flash/mini tier while escalating edge cases. Third, add tenant-scoped semantic caching for recurring support questions and tighten max_tokens / schema verbosity.