Architecture Decisions · 7 min read
Self-hosted LLM with cloud fallback: what timeout should you set?
Background: what "local model, cloud backup" is for
AllyOS is the business system I built with Claude Code and a small team of AI agents. A lot of its AI work is reading: scanned documents, email attachments, call notes that need a summary. Until now all of that went to cloud models.
Some of that material should not leave the company. So we put an isolated virtual machine on the office workstation, ran an open-source inference server with an OpenAI-compatible API, and routed the most sensitive reading jobs there first. If the office box can't answer, the call falls back to the cloud automatically.
Think of it as calling an internal extension before calling an outside contractor. Everyone gets the idea. The hard part is the details: how long do you let the extension ring? If someone picks up and says "who are you?", did the call connect? And does ringing time plus the outside call still fit inside the time a customer will wait at the counter?
Those are the three questions this post answers. The code and names below have been generalized.
Why does the local hop need its own timeout?
Short answer: without one, the fallback never gets its turn.
Our AI client already had a route chain: if the first route fails (connection error, rate limit, 5xx), try the next one in order. That was fine for cloud vendors, because they rarely hang. They answer fast or fail fast.
Your own machine is different. A saturated GPU, a frozen VM or a half-dead tunnel can leave a request neither succeeding nor failing, just hanging. Before changing anything I searched the client for abort signals: zero. No timeout anywhere. A hung local server would hold the request until the platform killed the whole function, and by then there would be no time left to ask the cloud. The fallback existed on paper only.
So step one was a deadline on every local attempt. When it fires, the attempt counts as "unreachable" and the chain moves on. AbortSignal.timeout() does this in one line in browsers and Deno. Make sure the deadline covers reading the response body, not just the headers, or a server that trickles bytes will still hang you.
One deliberate trade-off: the timeout applies to the local route only. A cloud vendor that is occasionally slow is normal, and cutting it off would kill long jobs that would have succeeded. The two routes behave differently and should not share one number.
How long? Ask who is waiting
Short answer: the timeout is two numbers, chosen by the caller.
I listed every caller that can hit the local route and sorted them by who waits for the result:
| Who is waiting | Example (generalized) | Local timeout |
|---|---|---|
| A person watching a page | a visitor uploads an image on the website, staff press "Scan" | 25 s |
| Nobody | queue jobs, async parsing after an email arrives | 60 s (default) |
The hard limit comes from the platform. Our backend functions run on Supabase, whose docs state a 150-second request idle timeout. The worst case is "wait out the local timeout, then run one full cloud call", so:
local timeout + one cloud call < 150 s
page-facing: 25 + cloud <= 150 -> about 125 s left for the cloud
background: 60 + cloud <= 150 -> about 90 s left, only for short-text jobs
One detail is worth calling out. The same shared "read an image" module serves both page-facing and background callers. My first version let the module pick 25 seconds itself, which forced background jobs onto the short deadline and gave up on a local box that was still working. The fix: the caller declares "a person is waiting" and the module just reads that flag. Only the caller knows who is waiting.
There is also an operator override: one environment variable can lower the local timeout for everyone. When both the caller and the operator set a value, the smaller wins. An operator can shorten all waits when the box is struggling, but can never accidentally stretch a page-facing 25 seconds to 60.
Which errors should fall back to the cloud?
Short answer: a local 401 should fall back; a cloud 401 should not.
The existing rule: connection errors, timeouts, 429s and 5xx responses advance the chain; any other 4xx, including 401 Unauthorized, stops it. That rule has a good reason. A cloud 401 usually means an expired API key. If the chain quietly moved on, everything would look fine and the dead key could go unnoticed for weeks.
The local route, however, sits behind an access gateway. A half-finished key rotation or an expired gateway token produces 401/403, and sometimes the gateway answers with an HTML login page instead of JSON. To a user that is the same as "the office box is offline", and the cloud should finish the job.
So I left the shared rule alone and made the local route translate three cases into "unreachable" itself:
- HTTP 401 or 403;
- any response body that is not JSON, whatever the status code;
- a timeout or connection failure.
Two cases still stop the chain: another 4xx with a JSON body (the request itself is wrong, another vendor will reject it too), and a JSON answer that doesn't match the expected schema. The second may feel odd. The reason: a wrong answer should be seen and counted by the caller's validation, not hidden by silently asking someone else.
The cloud's "401 stops" protection stays intact, and the local route's quirks live only in the local route's code. Each attempt writes its own trace row, so a fallback leaves two rows: the failed one and the successful one.
Who tells me the office box is down?
Short answer: the better the fallback, the more you need a separate alert.
Fallback has a side effect. When the local box dies, users notice nothing because the cloud covers for it. But the goal, keeping sensitive material in-house, silently stops being met.
So a daily probe checks the local server whenever its credentials are configured, whether or not any route currently puts it first. Each probe request is capped at 30 seconds. Connection failures, timeouts and 401/403 all send an alert. Cloud probes work differently: an occasional cloud timeout only goes to the error log, because it is noise. A local timeout almost always means the office machine is offline, which is exactly what I want to hear about.
Last, the rollout order. Merging the code changed no live call. Traffic moves only when an admin puts the local route first for one capability on a settings page. Code ships first; switching happens one capability at a time and can be reverted at any moment.
Measured numbers
| Metric | Value | How we measured |
|---|---|---|
| Timeouts in the AI client before the change | 0 | 2026-12-02, abort-signal search on the commit before the change |
| Page-facing callers | 7, local timeout 25 s | 2026-12-02, counted references to the short-timeout flag on the main dev branch |
| Default local timeout for background jobs | 60 s | 2026-12-02, read the constant in code |
| Platform request idle limit | 150 s | Supabase docs, checked 2026-12-02 |
| Cap per probe request | 30 s | 2026-12-02, read the constant in code |
| New code for the local route | 161 lines (client) + 190 lines (probe) | 2026-12-02, wc -l |
| Size of the change | 30 files, 998 lines added | 2026-12-02, git show --stat |
| Test cases covering the local route | 14, across 5 test files | 2026-12-02, static count (not run in this container) |
A checklist you can copy
- Search your AI client for any timeout. If there is none, your fallback chain does nothing against a hung server.
- Add a per-attempt timeout to the self-hosted route only, covering the response body; leave cloud routes alone.
- List every caller and split them by "is a person watching a page?". Pick a value for each group.
- Work backward from the platform's request limit: local timeout + one full cloud call must fit under it.
- Let the caller declare "a person is waiting". Shared modules should not guess.
- Give operators one override that can only lower the timeout: the smaller value wins.
- Inside the self-hosted route, translate 401/403 and non-JSON responses to "unreachable". Keep the shared fallback rule unchanged.
- Write one trace row per attempt so every fallback is visible in the logs.
- Add a daily probe independent of routing that alerts as soon as the self-hosted route times out.
- Ship code and switch-over separately, one capability at a time, with an instant way back.
FAQ
Why not set the timeout to 10 seconds and fail over faster?
A local model can easily need more than ten seconds to read one image. Too short a timeout gives up on a box that would have answered, sends all traffic to the cloud, defeats the point of keeping material in-house, and pays for a cloud call you didn't need. Twenty-five seconds is the compromise between what a person will wait and what the cloud needs; background jobs have nobody waiting, so the local box gets a looser 60.
What if the local model has streamed half an answer when the deadline fires?
That only happens with streaming. Text already shown cannot be taken back, and the cloud retry streams its own answer from the start, so the reader would see half a local answer followed by a full cloud one. None of the capabilities we route locally stream yet, so it cannot happen today, but the limit is documented and must be solved before a streaming capability switches. If you want AI wired into your business processes with fallback and audit trails like this, see services. An earlier post, why incremental sync silently misses records, is another story about an unstated assumption.
Won't the fallback hide an expired cloud key?
No. We deliberately kept "a cloud 401 stops the chain". Only the local route treats 401/403 as unreachable. When a cloud key expires, calls fail loudly and someone sees it right away.