# Free AI Apis Router

> Resilient fallback router and dispatcher for free-tier and sovereign local AI.

- Skill: `robertoatila/free-ai-apis-router` (Agent Skill)
- Install (CLI): `npx skillmds@latest add robertoatila/free-ai-apis-router`
- Raw SKILL.md: https://api.skillmd.com/api/skills/robertoatila/free-ai-apis-router/raw
- Safety review: pending
- Works with: Claude Code, Claude.ai, OpenAI Codex
- Category: Coding & Dev Tools
- Author: robertoatila (https://skillmd.com/u/robertoatila)
- Updated: 2026-09-22
- Page: https://skillmd.com/skills/robertoatila/free-ai-apis-router

---


# Sovereign Free AI APIs & Local Dispatcher

Deterministic gateway routing queries across 100% free-tier and sovereign local AI APIs with automated latency checks and zero-cost failover.

## Routing Priority Chain

```text
[Prompt Request]
       │
       ├──► [1. Ollama Local (http://localhost:11434)]  (Zero Cost / 100% Offline)
       ├──► [2. Groq Cloud Free Tier (Llama 3.3 70B)]  (High Speed ~300 T/s)
       ├──► [3. Google Gemini Free Tier (2.0 Flash)]   (Deep Context Window)
       └──► [4. Heuristic Sovereign Local Engine]       (Deterministic Disk Knowledge)
```

## Production Implementation: Dispatcher Class

```python
import urllib.request
import json
import os

class FreeAiRouter:
    def __init__(self):
        self.groq_key = os.getenv("GROQ_API_KEY", "")
        self.gemini_key = os.getenv("GEMINI_API_KEY", "")
        self.ollama_url = "http://localhost:11434/api/generate"

    def query_ollama(self, prompt: str, model: str = "llama3:latest") -> str:
        payload = json.dumps({"model": model, "prompt": prompt, "stream": False}).encode("utf-8")
        req = urllib.request.Request(self.ollama_url, data=payload, headers={"Content-Type": "application/json"})
        with urllib.request.urlopen(req, timeout=8) as resp:
            return json.loads(resp.read().decode("utf-8")).get("response", "")

    def query_groq(self, prompt: str, model: str = "llama-3.3-70b-versatile") -> str:
        if not self.groq_key:
            raise ValueError("GROQ_API_KEY not configured")
        url = "https://api.groq.com/openai/v1/chat/completions"
        payload = json.dumps({
            "model": model,
            "messages": [{"role": "user", "content": prompt}]
        }).encode("utf-8")
        req = urllib.request.Request(url, data=payload, headers={
            "Authorization": f"Bearer {self.groq_key}",
            "Content-Type": "application/json"
        })
        with urllib.request.urlopen(req, timeout=12) as resp:
            return json.loads(resp.read().decode("utf-8"))["choices"][0]["message"]["content"]

    def dispatch(self, prompt: str) -> dict:
        # Tier 1: Local Ollama
        try:
            res = self.query_ollama(prompt)
            if res:
                return {"provider": "ollama", "result": res}
        except Exception:
            pass

        # Tier 2: Groq High-Speed Free Tier
        if self.groq_key:
            try:
                res = self.query_groq(prompt)
                if res:
                    return {"provider": "groq", "result": res}
            except Exception:
                pass

        # Tier 3: Local Sovereign Fallback
        return {
            "provider": "sovereign-fallback",
            "result": f"Local processing completed for: {prompt[:80]}..."
        }
```

