title: Slashing AI API Costs by 70%: Building a Production Hybrid Router with Local Gemma 2B and Cloud Gemini

published: true

tags: ai, typescript, webdev, architecture, edge

canonical_url: https://github.com/sidsri14/hybrid-ai-router


Slashing AI API Costs by 70%: Building a Production Hybrid Router with Local Gemma 2B and Cloud Gemini

Most AI apps today make a financially catastrophic mistake: they send 100% of user queries to remote frontier cloud APIs.

Whether a user asks for a simple 10-word text summary, parses a local timestamp, or submits confidential credentials, the app triggers an external HTTP POST to OpenAI or Gemini. This results in:

  1. Unbounded Monthly Cloud Bills: At scale, simple queries eat millions of tokens at $0.35–$2.50+ per million.
  2. Artificial Latency: Adding 300ms–1500ms network round-trip overhead to routine text formatting.
  3. Severe Vulnerability to Network Drops: Mobile users go through a tunnel or airplane mode and the app completely crashes.
  4. PII Leakage: Sending sensitive internal keys, emails, and financial identifiers over public networks.

The solution is not abandoning cloud models. The solution is Hybrid Inference Routing.

In this article, we’ll construct hybrid-ai-router, a lightweight TypeScript engine that runs routine tasks locally on-device for $0.00 and <30ms latency, while seamlessly escalating complex reasoning, coding, and tool-calling to Google Gemini in the cloud.


🏗️ Architecture: How Hybrid AI Routing Works

flowchart TD
    Prompt[User Prompt / Request] --> Classifier{ComplexityClassifier}
    
    Classifier -->|PII Detected: SSN / Keys| Local[On-Device Model: Gemma 2B]
    Classifier -->|Offline Mode Active| Local
    Classifier -->|Score < 0.65: Routine Query| Local
    Classifier -->|Score >= 0.65: Complex Reasoning| Cloud[Cloud API: Gemini 1.5]
    Classifier -->|Requires Tools / Function Calling| Cloud
    
    Local -->|Success: <30ms, $0.00| Output[Response + Savings Metrics]
    Local -.->|Local OOM / Crash| CloudFallback[Escalate to Cloud Fallback]
    CloudFallback --> Output
    
    Cloud -->|Success: Deep Reasoning| Output
    Cloud -.->|Network Drop / Offline| LocalFallback[Recover via On-Device Fallback]
    LocalFallback --> Output

⚡ Step 1: Sub-Millisecond Complexity Classification

Before any model runs, our router evaluates the prompt in sub-millisecond time. We measure three dimensions:

  1. Estimated token length (normalized to the local model's context window).
  2. Cognitive reasoning keywords (e.g. architect, calculate proof, synthesize code, evaluate).
  3. PII and privacy markers (forcing sensitive data to stay local).
export class ComplexityClassifier {
  public classify(request: InferenceRequest): RoutingDecision {
    const fullText = `${request.systemInstruction ?? ""} ${request.prompt}`.trim();
    const tokenEstimate = Math.ceil(fullText.length / 4);

    // 1. Zero Cloud Leakage: PII guardrail
    if (request.privacySensitive || this.hasPII(fullText)) {
      return {
        targetTier: "on-device",
        reason: "Privacy-sensitive data detected: forced on-device execution",
        complexityScore: 0.0,
        tokenEstimate,
      };
    }

    // 2. High cognitive complexity -> Cloud
    const complexityScore = this.computeComplexityScore(fullText, tokenEstimate);
    if (complexityScore >= 0.65 || request.requiresTools) {
      return {
        targetTier: "cloud",
        reason: `Complex reasoning or tool calling required (${complexityScore.toFixed(2)})`,
        complexityScore,
        tokenEstimate,
      };
    }

    // 3. Routine task -> On-Device
    return {
      targetTier: "on-device",
      reason: "Routine query: executed on-device at $0.00 cost",
      complexityScore,
      tokenEstimate,
    };
  }
}

🛡️ Step 2: Bidirectional Circuit-Breaker Fallback

What happens if an on-device model runs out of memory on low-end hardware? Or what if a mobile device loses cellular signal mid-request?

hybrid-ai-router implements a bidirectional circuit breaker:

if (decision.targetTier === "on-device") {
  try {
    const result = await this.localEngine.infer(request);
    this.metrics.recordRequest("on-device", result.tokenCount, latency, false);
    return { ...result, tierUsed: "on-device", isFallback: false };
  } catch (localError) {
    // Escalate to Cloud fallback
    const cloudResult = await this.cloudEngine.infer(request);
    this.metrics.recordRequest("cloud", cloudResult.tokenCount, latency, true);
    return { ...cloudResult, tierUsed: "cloud", isFallback: true };
  }
}

💰 The Financial & Performance Impact

In benchmark simulations of 100,000 conversational user queries:


🚀 Get Started

The complete project is open source and tested (11/11 tests passing):

👉 GitHub: https://github.com/sidsri14/hybrid-ai-router

👉 Commercial Starter Kit ($49): Instant Checkout on Polar

Built for the Google #BuildHybridAI 2026 challenge on Devpost.


Need production LLM or Solana infrastructure built cleanly? Hire on Upwork ($60/hr) or get started immediately with our commercial kit: