Skip to main content
On this page
Product

Production-Grade Semantic Routing: A CTO’s Guide to AI Gateways

Izzy A
Izzy A
CTO @PromptMetrics

Cut LLM costs 40–60% with semantic routing. A technical guide to multi-tier AI gateways, cascading logic, and policy-as-code governance for production.

Production-Grade Semantic Routing: A CTO’s Guide to AI Gateways

You have seen the pattern. Your team builds a compelling AI feature—an agent, a copilot, or a complex analyzer. In the demo, running on a frontier reasoning model, it appears to work perfectly. Stakeholders are thrilled.

Then you move to production.

Suddenly, that performance incurs a massive OpEx load. Users hit the endpoint thousands of times a day for simple queries like "reset my password" or "summarize this short note." You do the math and realize your AI infrastructure bill is scaling linearly with user growth—often faster than your revenue.

This is the "Demo Trap."

Most AI-native CTOs are currently stuck in a binary choice: route everything to the "smartest" model and burn cash, or route everything to a "cheaper" model and watch quality tank.

But mature engineering organizations are moving to a third state: Semantic Model Routing. When implemented correctly—with cascading, fallback, and strict governance—this approach transforms your AI gateway from a dumb proxy into a probabilistic control plane, cutting costs by 40–60% (assuming 60–80% of traffic is Tier 1-eligible and cascades are bounded) while strictly enforcing quality and latency SLOs.

The Shift: From Deterministic to Probabilistic Engineering

For the last 30 years, the CTO's job has been to manage deterministic systems. If X, then Y. Failure was a bug.

In the GenAI era, you are managing probabilistic systems. A model might answer correctly 99 times and fail the 100th, not because of a bug, but because of the inherent variance in the model's distribution. Your infrastructure needs to match this reality. You cannot simply hardcode model calls in your application layer.

You need a control plane that sits between your product and the models, making intelligent, dynamic decisions. This requires a fundamental shift in architecture:

The Control Loop: Signal → Decision → Action

To build a router that survives production, you must abstract the logic into three distinct phases:

  • Signals (Context):

    • Technical: Embedding vectors, classifier probabilities, token count estimates, and provider health status.

    • Business: PII detection flags, user budget state, session "latch" state (history).

  • Decision (Logic):

    • Policy: Compliance constraints (Data Residency, Provider Allow-lists).

    • Routing: Tier selection, Cascade strategy, Hysteresis checks.

  • Action (Enforcement):

    • Execution: Invoke model, Retry/Failover, Escalate, Block, Log & Meter.

1. The Operational Contract: Tiers and SLOs

To break the linear cost curve, you must stop treating all queries as equal. A user requesting a password reset does not require the reasoning capabilities (or the latency penalty) of a frontier model.

We recommend architecting your gateway around three distinct performance tiers, plus a compliance override.

The Tier Contract

Tier

Target Workload

Illustrative Cost Multiplier*

Latency Target (P95)

Escalation & Entry Triggers

Tier 1 (Fast)

Reflexive tasks: Classification, Extraction, FAQs, "Chitchat."

1x (Base)

< 400ms

Escalate on: Low confidence (entropy), Schema failure.

Tier 2 (Smart)

Generalist generation: Summarization, Code drafts, Contextual Q&A.

~10–20x

1.5s – 3s

Trigger: Heuristic markers ("step-by-step"), classifier "reasoning" flag.

Tier 3 (Reasoning)

Cognitive load: Complex math, Legal analysis, Root cause debugging.

~50–100x

5s – 15s+

Trigger: High-stakes domain (Medical/Legal), complex toolchain needs, or final cascade fallback.

Policy Override (Private)

Compliance: PII detected, Data Residency requirements.

Variable

Variable

Trigger: Policy overrides all performance routing.

*Note: Cost multipliers are relative to Tier 1 on the same provider class (e.g., Llama 8B vs 405B) and vary significantly by input/output token ratio.

Sample SLO Contract

A production gateway must enforce specific Service Level Objectives (SLOs) per route to ensure consistency:

  • Latency: Tier 1 P95 < 400ms; Tier 2 P95 < 3s; Tier 3 P95 < 15s.

  • Availability: Provider-agnostic uptime > 99.95% (via failover).

  • Cost Efficiency: Blended cost per request < $0.005 (Target dependent on workload/token length).

  • Escalation Budget: No more than 15% of Tier 1 queries should cascade to Tier 2.

2. The Mechanics: Routing vs. Cascading

A common misconception is that routing is just a classification problem. In production, it is a flow problem. There are two distinct patterns you must implement: Direct Routing and Cascade Routing.

Direct Routing (The Shortcut)

This routes traffic immediately based on intent.

  • Signal: Embedding similarity to a "Router Index" (a vector store of canonical prompts).

  • Latency: Tens of milliseconds (end-to-end, including network overhead).

  • Use Case: User asks, "How do I update my billing?" → 99% similarity to Tier 1 FAQ cluster → Route to Tier 1 immediately.

Cascade Routing (The Safety Net)

This is the "optimistic" pattern. It attempts the cheapest route first but maintains a fallback path if quality criteria aren't met.

  • Flow: Try Tier 1 → Measure Confidence/Entropy → If Low, Escalate to Tier 2 → If Quality is still low, Escalate to Tier 3.

  • Risk: If not bounded, this significantly increases tail latency (P99) because you are paying the latency penalty of the failed call and the retry.

  • Mitigation: Set aggressive timeouts on lower tiers (e.g., Tier 1 times out at 600ms to allow room for Tier 2).

Production Pseudocode: The Routing Logic

Here is what the logic looks like inside a mature AI Gateway. It combines policy checks, hysteresis (to prevent oscillation), and a full three-tier cascade with bounded timeouts.

Python

def semantic_route(request, session_state):
    # 1. Governance & Compliance Gates (Policy-as-Code)
    # endpoint_pool contains objects with {region, provider, endpoint_class}
    endpoint_pool = ALL_ENDPOINTS 
    
    # Region Constraints: Filter for EU-only if required
    if request.geo == "EU":
        # Filter: keep endpoints where region is in EU_REGIONS
        endpoint_pool = [e for e in endpoint_pool if e.region in EU_REGIONS]

    # Endpoint Class Constraints: Force private/VPC for high risk
    if detect_pii(request) or request.data_classification == "high_risk":
        # Filter: keep endpoints where class is VPC or PRIVATE
        endpoint_pool = [e for e in endpoint_pool if e.class in [VPC_MANAGED, PRIVATE_HOSTED]]
    
    # Budget Logic
    if is_over_budget(request.user_id):
        raise DenialOfWalletError("Daily limit exceeded")

    # 2. Signal Extraction
    intent_vector = get_embeddings(request.prompt)
    complexity_score = classify_complexity(request.prompt)
    
    # 3. Hysteresis / Session Latching
    # Prevent "flip-flopping": If user is in a reasoning session, stay there.
    if session_state.last_tier == "Tier 3" and complexity_score > 0.4:
        return run_tier_3(request, endpoint_pool)

    # 4. Direct Routing (High Confidence Shortcut)
    if intent_vector.similarity("simple_tasks_cluster") > 0.95:
        return run_tier_1(request, endpoint_pool)
        
    # 5. Domain Gating (High Stakes)
    # Domain tags are assigned upstream or by a gateway classifier 
    # and mapped to tiers via policy (versioned).
    if request.domain_tag in ["legal_contract", "medical_claims"]:
        return run_tier_3(request, endpoint_pool)

    # 6. Cascade Routing (Optimistic with Bounded Tail Latency)
    
    # Attempt Tier 1 (Fast)
    try:
        # Aggressive timeout to protect P99
        response = run_tier_1(request, endpoint_pool, timeout_ms=600)
        
        # Validation: Entropy, Perplexity, or Classifier
        # calibrated_threshold is loaded from versioned config (learned on golden set)
        if validate_confidence(response) > calibrated_threshold:
            return response
    except (LowConfidenceError, Timeout):
        log_escalation("Tier 1 -> Tier 2")

    # Attempt Tier 2 (Smart)
    try:
        # Check for quality signals (e.g., citation presence, formatting)
        response = run_tier_2(request, endpoint_pool, timeout_ms=1800)
        if validate_quality(response):
             return response
    except (QualityCheckFail, Timeout):
        log_escalation("Tier 2 -> Tier 3")

    # Final Fallback to Tier 3 (Reasoning)
    return run_tier_3(request, endpoint_pool, timeout_ms=8000)

3. Opening the "Black Box": Quality Checks

In the pseudocode above, validate_confidence and validate_quality serve different purposes in the latency hierarchy.

  1. validate_confidence (Tier 1 Gate): Fast, cheap, early-exit checks.

    • Entropy: High randomness in the first 5 tokens signals confusion.

    • Schema: Does the JSON parse? If not, fail immediately.

    • Classifier: A lightweight model scoring the response probability.

  2. validate_quality (Tier 2 Gate): Richer, semantic checks.

    • Heuristics: Response length, refusal pattern detection ("I cannot answer..."), citation format validation.

    • Sampled Judges: Asynchronously route 1% of traffic to a Tier 3 "Judge" model to score the response. Use this data to recalibrate your thresholds offline.

4. Governance: Policy-as-Code

In a multi-team enterprise, you cannot rely on developers to implement these checks in their application code. Governance must be enforced at the gateway level using Policy-as-Code (e.g., OPA/Rego).

  • Versioning: Policies are not config files; they are code. They should be versioned in Git, tested against a regression suite, and deployed via CI/CD.

  • Rollout: Updates to routing logic should use Canary Deployments (e.g., roll out new routing logic to 5% of traffic) to detect regression before full saturation.

  • Recovery: Rollback is reverting the policy version configuration, not redeploying application services. This ensures instant recovery during incidents.

The Observability Contract

To operate this safely, your gateway must emit a specific log schema for every request. "Logging" is not enough; you need a structured contract:

  • trace_id / tenant_id

  • routing_decision (Selected Tier + Selected Endpoint + Endpoint Class)

  • escalation_path (e.g., tier1_entropy_fail -> tier2_success)

  • metrics (Input Tokens, Output Tokens, Latency ms, Calculated Cost)

  • quality_signals (Schema Pass/Fail, Judge Score [if sampled])

5. Failure Modes and Guardrails

Routing introduces new failure modes. A robust gateway must protect against them to maintain system stability.

A. Denial-of-Wallet Attacks

If a user (or a loop) spams your endpoint with complex Tier 3 queries, your bill can explode in minutes.

  • Guardrail: Smart Cost Anomaly Detection (MAD). Using the Median Absolute Deviation statistic, the system detects when a user's spending velocity deviates by 3x from the norm.

  • Action: Hard block or forced downgrade to Tier 1.

B. Provider Outages & Latency Spikes

  • Guardrail: Circuit Breakers & Failover.

  • Action: If a provider returns 429/5xx errors or latency exceeds the P99 threshold for > 5% of requests over a 60-second window, trip the circuit breaker. Failover triggers must be tied to explicit timeouts/status codes and breaker state to avoid provider thrash.

  • Nuance: Sequential failover increases latency. For critical paths, consider parallel hedging: issue requests to the primary and backup simultaneously when the primary is degraded, accepting a 2x cost to preserve latency.

C. Routing Oscillation

  • Guardrail: Hysteresis Bands.

  • Action: To prevent a prompt from flip-flopping between tiers due to minor probability shifts, use hysteresis: Escalate to Tier 2 if probability > 0.80, but only de-escalate if it drops below 0.60. Cache routing decisions by prompt hash to ensure consistency.

D. Router Drift

  • Guardrail: Distribution Monitoring.

  • Action: User query patterns change over time. You must define quantitative triggers: Alert if Tier 1 → Tier 2 escalation rate increases > 10% week-over-week, or if Regret Rate exceeds 5% for 3 consecutive days. When triggered, retrain the classifiers and reindex the embeddings.

6. Evaluation: How to Validate Before You Ship

You cannot deploy a router based on "vibes." You need a rigorous evaluation pipeline to prove that routing won't degrade user experience.

  1. Build a Golden Set (Offline): Curate 500+ real production logs. Have human experts or a "Judge" model (Tier 3) label the ideal tier for each.

  2. Shadow Routing (Online): Deploy your router to production in "Shadow Mode." It processes live traffic and logs the decision it would have made, without affecting the user.

  3. Measure Routing Regret:

    • Over-Routing Regret: % of simple queries sent to expensive models (Waste).

    • Under-Routing Regret: % of complex queries sent to weak models (Quality Loss).

  4. Canary Rollout: Enable the router for 1–5% of traffic, monitor P99 latency, and collect user feedback.

The Strategic Takeaway

The AI-Native CTO is no longer just managing code; you are managing behavior and economics.

You can build this infrastructure yourself. It requires maintaining vector indices, training classifiers, managing policy engines, and creating extensive evaluation harnesses. For many teams, this turns their best product engineers into infrastructure maintainers.

PromptMetrics provides the primitives for this probabilistic control plane. We provide audit logs for compliance, a policy engine for governance, and anomaly-detection hooks to safeguard your budget, so you can build a routing strategy that scales without overhead.

Implementation Checklist

  • [ ] Define Tiers: Map your internal use cases to Tier 1, 2, and 3.

  • [ ] Establish Baselines: Record current cost/query and P95 latency.

  • [ ] Create Golden Set: Tag 500 historical prompts with "ideal tier."

  • [ ] Configure Policy: Set up Region/Endpoint class allow-lists in your gateway.

  • [ ] Deploy Shadow Router: Run purely for logging Regret Rate.

  • [ ] Activate Circuit Breakers: Set thresholds (e.g., 5% error rate).

  • [ ] Canary Launch: Roll out to 5% of traffic.

Self-hosted prompt registry + agent telemetry. Zero vendor lock-in. Runs on a $5 VPS.

Up next

Explore more from the blog

Engineering notes, release updates, and honest takes.

Get the best of the prompt engineering blog delivered to your inbox

Join thousands of AI enthusiasts receiving weekly insights, tips, and tutorials.