<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Muhammad hamthan</title>
    <description>The latest articles on DEV Community by Muhammad hamthan (@muhammad_hamthan_0600f8f2).</description>
    <link>https://dev.to/muhammad_hamthan_0600f8f2</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F3963308%2Fcd744675-3484-4e39-b6f0-7567157b9099.png</url>
      <title>DEV Community: Muhammad hamthan</title>
      <link>https://dev.to/muhammad_hamthan_0600f8f2</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/muhammad_hamthan_0600f8f2"/>
    <language>en</language>
    <item>
      <title>Designing a 3-Tier LLM Fallback Router with Cooldown Locking</title>
      <dc:creator>Muhammad hamthan</dc:creator>
      <pubDate>Mon, 01 Jun 2026 19:50:32 +0000</pubDate>
      <link>https://dev.to/muhammad_hamthan_0600f8f2/designing-a-3-tier-llm-fallback-router-with-cooldown-locking-1eec</link>
      <guid>https://dev.to/muhammad_hamthan_0600f8f2/designing-a-3-tier-llm-fallback-router-with-cooldown-locking-1eec</guid>
      <description>&lt;p&gt;&lt;em&gt;How I built a production-grade LLM router for a chatbot running on Groq's free tier — surviving rate limits without dropping users.&lt;/em&gt;&lt;/p&gt;




&lt;p&gt;I was building a chatbot for Smatal Academy — an institutional admissions assistant — and I had a constraint most LLM tutorials don't talk about:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;The free tier of my LLM provider.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Groq's free tier is generous, but it's rate-limited. When you hit the limit, your request fails with a &lt;code&gt;RateLimitError&lt;/code&gt; and you're locked out for a window. For a personal demo, that's fine. For a chatbot that real users were typing into, it was a problem.&lt;/p&gt;

&lt;p&gt;The naive answer is "upgrade to paid." Sometimes that's right. But this was a project deployed to Zoho Catalyst Cloud with modest traffic, and paying for an LLM subscription felt premature. So I asked a different question: &lt;strong&gt;can I survive rate limits architecturally instead of financially?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;The answer turned out to be yes. Here's what I built.&lt;/p&gt;




&lt;h2&gt;
  
  
  What "naive fallback" looks like (and why it breaks)
&lt;/h2&gt;

&lt;p&gt;The first instinct everyone has is this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;try&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="n"&gt;response&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;llm&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;invoke&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;question&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;except&lt;/span&gt; &lt;span class="n"&gt;RateLimitError&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="n"&gt;time&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;sleep&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;60&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;response&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;llm&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;invoke&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;question&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Two problems with it:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;The user waits 60 seconds.&lt;/strong&gt; That's a dead chat session. They'll close the tab.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;You're retrying the same model.&lt;/strong&gt; If you're rate-limited now, you're probably still rate-limited 60 seconds from now.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;The better instinct is to switch to a &lt;em&gt;different&lt;/em&gt; model. Groq hosts many models — LLaMA-3.3-70B, LLaMA-4-Scout-17B, Kimi-K2, and others. Each has its own rate-limit bucket. If one is exhausted, another probably isn't.&lt;/p&gt;

&lt;p&gt;But you don't want to fall back to a smaller model every time — only when you have to. And you don't want to keep hammering a rate-limited model when you know it's still cooling down.&lt;/p&gt;

&lt;p&gt;That's the 3-tier router.&lt;/p&gt;




&lt;h2&gt;
  
  
  The 3-tier router
&lt;/h2&gt;

&lt;p&gt;The core idea:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;MODEL_CONFIG&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;
    &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;fast&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;        &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;llama-3.3-70b-versatile&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
    &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;backup&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;      &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;meta-llama/llama-4-scout-17b-16e-instruct&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
    &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;last_resort&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;moonshotai/kimi-k2-instruct-0905&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
&lt;span class="p"&gt;]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Three models in descending order of preference. The &lt;code&gt;fast&lt;/code&gt; model is what you want most of the time. The &lt;code&gt;backup&lt;/code&gt; model kicks in when the fast one is rate-limited. The &lt;code&gt;last_resort&lt;/code&gt; exists so the chatbot never goes fully dark.&lt;/p&gt;

&lt;p&gt;When a request comes in:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Try the fast model.&lt;/li&gt;
&lt;li&gt;If it's currently cooling down, skip it and try the backup.&lt;/li&gt;
&lt;li&gt;If the backup is also cooling down, try the last resort.&lt;/li&gt;
&lt;li&gt;If all three are cooling down — only then return an error to the user.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;This converts a hard outage into a soft degradation. Responses might be slightly less rich temporarily, but the chatbot doesn't go down.&lt;/p&gt;




&lt;h2&gt;
  
  
  The cooldown mechanism
&lt;/h2&gt;

&lt;p&gt;This is the part most "fallback" tutorials skip.&lt;/p&gt;

&lt;p&gt;When you catch a &lt;code&gt;RateLimitError&lt;/code&gt;, the obvious thing to do is &lt;strong&gt;try the next model immediately&lt;/strong&gt;. That works for the current request. But what about the next request that comes in 5 seconds later? You'll try the rate-limited model first, get the same error, &lt;em&gt;then&lt;/em&gt; fall back.&lt;/p&gt;

&lt;p&gt;That's wasted latency on every request until the rate limit clears. Bad UX, bad cost.&lt;/p&gt;

&lt;p&gt;The fix: when a model gets rate-limited, &lt;strong&gt;mark it as unavailable for a cooldown window&lt;/strong&gt; (I used 1 hour — close to Groq's typical reset behaviour). The router skips any model whose cooldown hasn't expired:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;_llm_cooldowns&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mf"&gt;0.0&lt;/span&gt; &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;_&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="n"&gt;MODEL_CONFIG&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;_get_available_models&lt;/span&gt;&lt;span class="p"&gt;():&lt;/span&gt;
    &lt;span class="n"&gt;now&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;time&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;time&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
    &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;_&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="n"&gt;MODEL_CONFIG&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;now&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;=&lt;/span&gt; &lt;span class="n"&gt;_llm_cooldowns&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="p"&gt;]:&lt;/span&gt;
            &lt;span class="k"&gt;yield&lt;/span&gt; &lt;span class="n"&gt;name&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Subsequent requests immediately go to the backup model. Zero wasted calls on a model we know is dead.&lt;/p&gt;




&lt;h2&gt;
  
  
  The concurrency problem
&lt;/h2&gt;

&lt;p&gt;This is where it got interesting.&lt;/p&gt;

&lt;p&gt;I deployed the chatbot with Gunicorn running multiple workers. Each worker handles requests in parallel. The &lt;code&gt;_llm_cooldowns&lt;/code&gt; dictionary is shared state.&lt;/p&gt;

&lt;p&gt;Imagine two requests arrive at the same instant. Both try the fast model. Both get rate-limited. Both want to write to &lt;code&gt;_llm_cooldowns["fast"]&lt;/code&gt; simultaneously.&lt;/p&gt;

&lt;p&gt;In Python, dictionary writes are mostly atomic thanks to the GIL — so you won't see a corrupted dict — but you can absolutely see a race: one worker reads the cooldown value while another is mid-update, and the read returns a stale value. Result: the second worker thinks the model is still available and tries it again, wasting a call.&lt;/p&gt;

&lt;p&gt;The fix is a simple &lt;code&gt;threading.Lock&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;_cooldown_lock&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;Lock&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;

&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;run_with_fallback&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;chain_builder&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;retriever&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;memory&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;question&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;model_name&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="nf"&gt;_get_available_models&lt;/span&gt;&lt;span class="p"&gt;():&lt;/span&gt;
        &lt;span class="n"&gt;llm&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;LLMS&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;model_name&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
        &lt;span class="k"&gt;try&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
            &lt;span class="n"&gt;chain&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;chain_builder&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;retriever&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;memory&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;llm&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
            &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;chain&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;invoke&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;question&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;question&lt;/span&gt;&lt;span class="p"&gt;})&lt;/span&gt;
        &lt;span class="k"&gt;except&lt;/span&gt; &lt;span class="n"&gt;RateLimitError&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
            &lt;span class="k"&gt;with&lt;/span&gt; &lt;span class="n"&gt;_cooldown_lock&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
                &lt;span class="n"&gt;_llm_cooldowns&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;model_name&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;time&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;time&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;COOLDOWN_SECONDS&lt;/span&gt;
            &lt;span class="k"&gt;continue&lt;/span&gt;
    &lt;span class="k"&gt;raise&lt;/span&gt; &lt;span class="nc"&gt;RuntimeError&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;All LLMs are currently rate-limited.&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Worth noting: I only lock the &lt;strong&gt;write&lt;/strong&gt;, not the read. The read can tolerate a stale value — worst case, a worker tries a rate-limited model and gets the error, which we already handle. The lock just prevents a lost update.&lt;/p&gt;




&lt;h2&gt;
  
  
  What I learned
&lt;/h2&gt;

&lt;p&gt;Three things I didn't expect to learn going in:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Fallback isn't retry.&lt;/strong&gt; Retrying the same model on rate-limit is almost always wrong. Switching to a &lt;em&gt;different&lt;/em&gt; model is almost always right.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Cooldowns matter as much as fallbacks.&lt;/strong&gt; Without cooldowns, you waste latency probing dead models on every request. With cooldowns, you skip them instantly.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Shared state in multi-worker apps needs synchronization, even in Python.&lt;/strong&gt; The GIL gives you a lot, but not everything. A lock is one line. Use it.&lt;/li&gt;
&lt;/ol&gt;




&lt;h2&gt;
  
  
  What I'd do differently next time
&lt;/h2&gt;

&lt;p&gt;A few things I'd add if I rebuilt this:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Observability.&lt;/strong&gt; Right now I &lt;code&gt;print()&lt;/code&gt; when a fallback fires. I should be emitting metrics — fallback count per model, cooldown duration, total failure rate. Without metrics, you don't know when your "graceful degradation" has actually started degrading badly.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Circuit breaker pattern.&lt;/strong&gt; Right now any &lt;code&gt;RateLimitError&lt;/code&gt; triggers the cooldown. But other errors (network blip, 5xx) should be handled differently. A proper circuit breaker with separate counters for transient vs persistent failures would be more robust.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Half-open probes.&lt;/strong&gt; When the cooldown expires, I just allow traffic through. A safer design would send one "probe" request first, and only fully reopen if that probe succeeds.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Per-user routing.&lt;/strong&gt; Right now all users share the same cooldown state. In a paid product, premium users could be routed to the fast model preferentially while free users fall back sooner.&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  Closing thought
&lt;/h2&gt;

&lt;p&gt;A lot of LLM apps in 2026 are still built like demos — one model, one provider, no fallback. That's fine for prototypes. But the moment real users show up, &lt;em&gt;"what happens when the model is unavailable"&lt;/em&gt; stops being a hypothetical question.&lt;/p&gt;

&lt;p&gt;The 3-tier router with cooldown locking turned a chatbot that would die under rate limits into one that quietly degrades. Total code: about 60 lines. Total time: an afternoon. Total payoff: real users never saw an outage.&lt;/p&gt;

&lt;p&gt;If you're building on top of an LLM API and you don't have a fallback strategy yet, this is the cheapest reliability win you'll find.&lt;/p&gt;




&lt;p&gt;&lt;strong&gt;Code:&lt;/strong&gt; &lt;a href="https://github.com/muhammadhamthan/Smatal-Institude/tree/Samtal-Chatbot/Backend" rel="noopener noreferrer"&gt;github.com/muhammadhamthan/Smatal-Institude&lt;/a&gt; — see &lt;code&gt;llm_router.py&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Tags:&lt;/strong&gt; &lt;code&gt;python&lt;/code&gt; &lt;code&gt;llm&lt;/code&gt; &lt;code&gt;langchain&lt;/code&gt; &lt;code&gt;groq&lt;/code&gt; &lt;code&gt;reliability&lt;/code&gt; &lt;code&gt;systemdesign&lt;/code&gt;&lt;/p&gt;




&lt;p&gt;&lt;em&gt;Muhammad Hamthan is a backend engineer leading the backend of an AI-powered operations platform. He writes about backend architecture, AI integration, and the production reliability lessons most tutorials skip. — &lt;a href="https://github.com/muhammadhamthan" rel="noopener noreferrer"&gt;GitHub&lt;/a&gt; • &lt;a href="https://linkedin.com/in/muhammadhamthan" rel="noopener noreferrer"&gt;LinkedIn&lt;/a&gt;&lt;/em&gt;&lt;/p&gt;

</description>
      <category>ai</category>
      <category>llm</category>
      <category>showdev</category>
      <category>systemdesign</category>
    </item>
  </channel>
</rss>
