← The journal
System Architecture / freelm

LLM Rate Limits (429): Automatic Failover Across Providers

How to handle LLM rate limits (HTTP 429) with key rotation, circuit breakers and cross-provider failover, the pattern freelm implements for Python and JS.

You launch your AI wrapper. It hits the front page of Hacker News. Suddenly, your LLM provider sends back a wall of HTTP 429 Too Many Requests errors. Your app crashes, users leave, and your launch is ruined.

Rate limits are inevitable. But downtime isn't.

At Shahriar Labs, we realized that relying on a single LLM provider is a single point of failure. We needed a load balancer for LLMs. That is why we open-sourced freelm.

Building Resilient AI

freelm (available on npm and PyPI) isn't just an API wrapper. It is a robust gateway designed around Circuit Breaking and Automatic Failover.

1. Interleaved Failover Routing

When you provide freelm with multiple keys (e.g., OpenRouter, Gemini, and NIM), it doesn't just exhaust one provider before moving to the next.

It uses an interleaved routing strategy: it attempts to use the best model from your primary provider. If that fails, it tries the best model from your secondary provider. This ensures you always get high-quality responses, even during a failover, rather than degrading to a weak fallback model on the primary provider.

2. Intelligent Circuit Breakers

When an API key returns a 429 Rate Limited or a 5xx Server Error:

  • freelm immediately opens the circuit for that specific key.
  • The key is placed on a cooldown timer (e.g., 60 seconds).
  • Subsequent requests bypass this key entirely, avoiding wasted time and avoiding further penalization from the provider.
  • Once the cooldown expires, the circuit enters a "half-open" state to test if the provider is healthy again.

3. Slow and Hung Providers (since 0.5)

A 429 is the easy case: it comes back fast, so failing over costs one round trip. The expensive case is a provider that accepts the request and then says nothing. Before freelm 0.5, that attempt held the whole call until its 60 second deadline and the call failed without ever reaching the healthy provider behind it.

Now a slow attempt is raced. Once it has run for three times that provider's usual latency (clamped: 1.5 to 6 seconds for a stream's first token, 4 to 12 seconds for a whole answer), the next provider starts in parallel and the first answer wins. The slow request is cancelled and that provider is ranked lower, so the next call goes straight to the fast one.

Failure in front of a healthy providerfreelm 0.40.5, first call0.5, next call
429 or 5000.3 s0.3 s0.3 s
Provider hangsfailed after 60 s6.3 s0.3 s
Stream stalls before the first tokenfailed after 60 s3.3 s0.3 s
Slow provider (8 s)8.0 s every call6.3 s0.3 s

Simulation run on 2026-10-10 with fake providers on localhost over real HTTP; the healthy provider answers in 0.3 s. Reproduce it with benchmarks/failover.py.

For the exact error each provider returns when it rate-limits you (Gemini's 429 RESOURCE_EXHAUSTED, OpenRouter's free-models-per-day, Groq's TPM limit and others), see the free LLM API error reference.

4. Quota Awareness

Even better than failing over is predicting a failure. freelm implements a Token Bucket algorithm internally. It knows the default RPM (Requests Per Minute) limits of the free tiers it supports.

If freelm sees that your Groq key is about to hit its 30 RPM limit, it proactively routes the 31st request to Gemini—before Groq even has a chance to reject it.

The Developer Experience

The best part about this architecture? You don't have to write a single try/except or try/catch block.

// Node.js Example
import { OpenAI } from 'freelm/compat';

const client = new OpenAI(); // Automatically reads your environment variables

// If OpenRouter is down, this will transparently hit Gemini instead.
const completion = await client.chat.completions.create({
  model: "auto",
  messages: [{ role: "user", "content": "Why is failover important?" }],
});

console.log(completion.choices[0].message.content);

Conclusion

Stop letting rate limits dictate your uptime. By pooling multiple free providers using freelm, you can achieve enterprise-grade reliability on a zero-dollar budget.

Keep reading
Shihab portraying Goku in an anime-inspired costume, keeping his own facial identity
The other lives of Shihab

01/ 08

The pursuit of better.

Goku / Dragon Ball

A training arc never really ends.

Personal cosplay artwork. Same face. A different world.Enter the six-form training arc ↗
A little energy from the whole world.
1 of 8