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.
I explain reliability with a driving analogy. Your app needs a main route, but also a known detour. If the main bridge is closed, users should still arrive.
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.
Step 3: Implement fallback as a first-class path
Fallback should not be an afterthought. It should be a normal, tested path.
type TranslationResult = {
text: string;
provider: 'primary' | 'fallback';
};
async function translate(input: { text: string; from: string; to: string }): Promise<TranslationResult> {
validatePair(input.from, input.to);
try {
const primary = await withTimeout(primaryProvider.translate(input), 2500);
return { text: primary.text, provider: 'primary' };
} catch {
const backup = await fallbackProvider.translate(input);
return { text: backup.text, provider: 'fallback' };
}
}
Step 4: Normalize output shape
Different providers return different payload formats. Normalize immediately so your frontend never has to care which provider answered.
Step 5: Treat quality monitoring as part of the feature
Track these metrics daily:
- fallback rate
- timeout count
- user correction rate
If fallback rate spikes, you can react before users start reporting failures.
Final teaching note
Reliable AI features are product design plus systems design, not just model choice.
- Validate early.
- Time out fast.
- Fallback cleanly.
- Normalize outputs.
- Measure quality.
That combination is what makes translation features feel dependable in real life.