If you have ever shipped translation features in production, you already know the pain: everything works in staging, then one provider slowdown makes your app feel broken.

The mistake is designing around the happy path only.

Reliability comes from explicit fallback behavior, not from assuming the primary provider is always available.

Step 1: Validate requests before calling any model

Never spend model calls on impossible inputs.

const supportedPairs = new Set([
  'en->es',
  'en->fr',
  'fr->en',
]);

function validatePair(from: string, to: string): void {
  const key = from + '->' + to;
  if (!supportedPairs.has(key)) {
    throw new Error('unsupported language pair: ' + key);
  }
}

This one guard can remove a lot of fake "model failures".

Step 2: Put the primary model behind a timeout wall

If you wait forever, your API queue grows and user trust drops.

async function withTimeout<T>(task: Promise<T>, ms: number): Promise<T> {
  return Promise.race([
    task,
    new Promise<T>((_, reject) =>
      setTimeout(() => reject(new Error('timeout')), ms),
    ),
  ]);
}

Now your system decides quickly when to use fallback.

Preview: first 50% is visible. Unlock to read the full article.
To view this content, you must be a member of CodeWithWilliamJiamin's Patreon at $1 or more
Already a qualifying Patreon member? Refresh to access this content.