A free-tier AI gateway on Cloudflare Workers
Every free tier has a ceiling, and you find it at the worst possible time. Groq is fast until the rate limiter says no. OpenRouter's :free models queue behind everyone else's traffic. Gemini's quota resets on a schedule
Every free tier has a ceiling, and you find it at the worst possible time. Groq is fast until the rate limiter says no. OpenRouter's :free models queue behind everyone else's traffic. Gemini's quota resets on a schedule you don't control. Wire an app directly to one of them and it breaks the first time that provider has a bad hour.
I got tired of patching around that, so I put all of them behind a single Cloudflare Worker.
What it is
One Worker that speaks the OpenAI chat-completions API and the Anthropic Messages API. Your client points at the Worker instead of a provider, and the Worker decides where the request goes.
It runs on the Workers free plan, stores its state in KV, and doesn't need a paid API key to work. Requests are grouped by what you're doing rather than by model name:
-
assistantfor general chat -
agentfor multi-step planning and tool calls -
codingfor code generation and agents like Claude Code -
textfor fast extraction and summaries - plus image and video endpoints
Each group is an ordered list of providers. coding, for example, starts with a free Qwen coder, falls back to Codestral, then Gemini, then Groq. You can pin a group with "model": "coding" and stop thinking about which vendor is up today.
Routing, and what happens when a provider fails
The interesting part isn't the happy path. It's the failure handling:
- 429s put that specific key on a cooldown, not the whole provider. Cooldown state lives in KV, so a Worker restart doesn't reset it.
- 402s are treated as a stop sign. The router never escalates to a paid tier, because a surprise bill is worse than a failed request.
- Key rotation spreads load across multiple keys per provider when you have them.
- Predictive exhaustion estimates burn rate from recent usage and moves traffic off a key before it hits the wall, instead of after.
- Timeouts default to 60 seconds. Reasoning models routinely need more than the 20 seconds I started with, and short per-provider hints were quietly shrinking the budget.
There's also a /v1/status endpoint that reports provider health, key counts and active cooldowns. When something looks broken, that's the first place I look.
Using it with existing tools
Because it's OpenAI-compatible, most SDKs work by changing the base URL. For Claude Code and other Anthropic clients:
export ANTHROPIC_BASE_URL="https://your-worker.workers.dev"
export ANTHROPIC_AUTH_TOKEN="your-gateway-token"
No shim, no proxy process on your machine.
Observability that doesn't cost anything
The Worker keeps per-key RPM limits, budget caps, and usage analytics in KV. There's a semantic cache in front of the models, A/B testing for prompt variants, and a simple anomaly detector that flags request spikes and error-rate jumps per IP.
None of it is fancy. All of it has saved me from debugging blind at 2am.
The honest limits
- Free tiers change without warning. A provider order that works today may need editing next month.
- Latency is higher than calling a provider directly, because every request goes through one more hop and sometimes one more retry.
- There's no SLA. It's a free Worker in front of free APIs.
- Semantic caching only helps when your prompts actually repeat.
I use it for side projects and local tooling. I wouldn't put a paid product behind it without a paid provider in the mix.
Deploying it
It's MIT licensed. There's a Deploy to Cloudflare button in the README, or you can run the deploy script with a Workers API token:
CF_API_TOKEN="your_token" node scripts/deploy-cf.mjs
Repo: https://github.com/kashyap-digital/sentient-gateway
If you try it, I'd like to know which provider order held up best for your workload. That's the part that changes most often.
Originally published by Dev.to AI. Aggregated on AIWithGhost for educational purposes — full credit and traffic to the original publisher.