Tripbae is a travel app for group trips. Its AI, Lumi, plans itineraries, answers "what should we do tonight?", logs expenses ("add ₹400 for dinner, split it") and suggests weekend getaways. Almost every screen calls an LLM somewhere.

That makes one thing scary: if the model provider has a bad day, the whole app has a bad day. A 429 from one provider, a model ID that suddenly 404s, prepaid credits running out at 2 a.m., and Lumi just stops talking.

So we stopped calling one model. Every AI task in Tripbae runs through a small cascade: three models spread over two independent paths (OpenRouter and Google's API directly). This post walks through how it works. It's one plain Node.js file with no framework, and the patterns carry over to any app.

1. Every task gets its own cascade

Different jobs need different trade-offs. Destination autocomplete needs speed, itinerary building needs a smarter model, and small JSON helpers need cheap and reliable. So each task has its own ordered list:

const or = (model, o = {}) => ({ via: 'or', model, ...o }); // via OpenRouter const g = (model, o = {}) => ({ via: 'g', model, ...o }); // via Google direct const CASCADES = { chat: [FAST_A(), FAST_B(), GOOGLE_LITE()], structured: [FAST_B(), GOOGLE_LITE(), CHEAP_C()], // titles, follow-ups, insights hints: [FAST_B(), GOOGLE_LITE(), FAST_A()], // autocomplete, latency-critical itinerary: [GOOGLE_SMART(), FAST_B(LOW), FAST_A(LOW)], }; 

The first entry is the primary. The key rule: each cascade mixes both paths. If OpenRouter has a problem, there's always a Google model in the list, and vice versa. To swap models, you edit this table and nothing else.

2. Not all errors are equal

The naive version is "on error, try the next model." That works until a dead model gets retried on every single request, or a billing problem on one path keeps burning a second per call.

So every failure is classified before we move on:

function classify(via, status, body = '') { if (status === 404) return { kind: 'disable' }; // model ID gone → skip until restart if (status === 401 || status === 402) return { kind: 'path', ms: 10 * 60_000 }; // key/credits → pause the whole provider if (status === 429) return { kind: 'model', ms: 65_000 }; // rate limited → cool this model down if (status >= 500) return { kind: 'model', ms: 30_000 }; // provider hiccup if (status === 400) return { kind: 'strike' }; // bad request → 3 strikes, then pause return { kind: 'none' }; } 

Each kind has a different blast radius:

  • disable: a 404 means the model ID is wrong or retired. Skip it until the next deploy, and send an alert.
  • path: a bad key or no credits affects every model on that provider. Pause the whole path for 10 minutes, so traffic flows to the other one.
  • model: rate limits and 5xx are usually temporary. Cool down just that model.
  • strike: a 400 might be our fault, one weird prompt. Pause only after 3 in a row.

Alerts go to a webhook, deduplicated to one per issue every 30 minutes, so an outage doesn't spam the team.

3. Cooldowns should never be the reason a request fails

Cooldowns create a new failure mode. Say you top up credits, but the path is still "paused" for 8 more minutes, so every request fails even though everything is fine.

So the runner makes two passes:

for (const ignoreCooldowns of [false, true]) { for (const entry of cascade) { if (!available(entry, ignoreCooldowns) || attempted.has(keyOf(entry))) continue; attempted.add(keyOf(entry)); try { return await call(entry); } catch (e) { penalize(entry, classify(e)); } } } 

Pass 1 respects cooldowns. Pass 2 only happens if everything healthy has already failed, and it tries the models that were cooling down too. Models disabled with a 404 stay skipped. A stale pause can slow a request down, but it never fails it.

4. Streaming: detect stalls in seconds, not minutes

Itinerary generation produces a lot of JSON. With one big request and a 90-second timeout, a provider that stalls halfway wastes 90 seconds before you fail over.

Instead, long outputs are always streamed internally, with a sliding idle timeout:

let timer = setTimeout(() => ctl.abort(), connectMs); // no first byte in time → abort const bump = () => { clearTimeout(timer); timer = setTimeout(() => ctl.abort(), idleMs); // any later gap too long → abort }; // call bump() on every chunk received 

A healthy stream is never cut off however long it runs. A stalled one dies in about 45 seconds and the cascade moves on.

5. User-facing chat: you can't switch models mid-sentence

For Lumi's live chat, tokens stream straight to the phone. If model A has already sent "Here are three great cafés near" and then dies, we can't fail over, because model B would start a different answer and the user would see two replies glued together.

So the rule is simple: fall back only if nothing has been sent yet.

let yielded = false; for await (const chunk of streamOnce(entry, opts)) { yielded = true; yield chunk; } // on error: if (yielded) throw err; // partial answer already shown → surface the error // otherwise → try the next model 

In practice almost every failure happens before the first token (auth, rate limit, cold start), so this catches the vast majority of failures without ever mixing two answers.

6. Tool calling gets its own isolated cooldowns

In-trip Lumi uses tool calling ("add this expense", "find a petrol pump nearby"). Some models are great at chat but flaky with tools. If a model fails at tool calls, we don't want that to pause it for normal chat.

So tool calling has its own cascade and its own cooldown map. Only provider-wide problems (bad key, no credits) are shared with the rest of the system.

What we got out of it

  • No single provider, model or billing account can take Lumi down.
  • Swapping in a new model is a one-line change to the cascade table.
  • We hear about real problems (a retired model, exhausted credits) through deduplicated alerts, not from users.
  • It's about 700 lines of plain Node.js with fetch, and no SDKs.

If you're building on LLMs, the pattern is: ordered fallbacks across independent providers, classify errors by blast radius, and never let your own safety mechanism be the reason a request fails.

Tripbae is a free AI travel app for group trips: plan with Lumi, split expenses in any currency, and share one trip album. tripbae.in · Android