Skip to main content
Handbook/Amplify/Chapter 68 · AI Reliability

Making AI Reliable

Share

Share this page

Pass it to someone who needs it.

Key takeaway: Assume the model will be wrong sometimes

A model sounds the same when it is wrong as when it is right. So an AI feature that works in your demo can quietly hand a real user a made-up answer in production. You cannot trust raw model output the way you trust code you wrote. This chapter gets your AI feature to fail safely instead of confidently.

7.5.1The model will be confidently wrong

A model has no sense of when it is wrong. It predicts fluent, plausible text, so a made-up answer reads exactly like a correct one.

Models hallucinate: they produce output that is fluent, plausible, and false. It is not a rare glitch you patch out, it is a property of how the model works.

So plan for wrong answers instead of hoping for right ones. Treat every response as an untrusted guess until something you control has checked it.

7.5.2Constrain the output and validate it

Free-form prose is hard to check by machine. So ask the model for structured output, a fixed shape like JSON, not a paragraph you have to interpret.

A shape you defined is a shape you can check. Validate every response against a schema with a validator like Zod, and reject anything that does not fit. It is the same discipline you apply to any external input.

// Ask the model to reply as JSON, then check // the shape before you trust it. const schema = z.object({ sentiment: z.enum(['positive', 'negative']), score: z.number().min(0).max(1), }) async function classify(text) { const raw = await model.json(text) const result = schema.safeParse(raw) if (!result.success) { throw new Error('Model returned a bad shape') } return result.data }

Malformed output never flows downstream: a response that fails the check is dropped or retried, not passed to the user.

Once real users can reach the feature, wrong answers are not the only risk: some will try to make the model say something harmful or break its own rules, a jailbreak. The defense is a guardrail, a checkpoint that inspects what goes into and comes out of the model and blocks or rewrites anything unsafe. The easy win is running text through a moderation service instead of hand-writing filters; the input side, users smuggling instructions into their text, is the prompt-injection problem the security part covers.

7.5.3Evaluate it, do not eyeball it

You tweak a prompt, try it once, and the answer looks better. But one hand-check says nothing about the cases you did not try, and one fix often breaks another.

An eval is a set of example inputs paired with the results you expect, run against your feature after every change. It is testing aimed at a fuzzy feature. When you swap a model or edit a prompt, it flags the answers that got worse.

// An eval: inputs paired with expected results, // run after every prompt or model change. const cases = [ { text: 'I love this', want: 'positive' }, { text: 'Worst thing ever', want: 'negative' }, ] for (const c of cases) { const got = await classify(c.text) if (got.sentiment !== c.want) { console.log('Regression on:', c.text) } }

Start with five cases that matter, including ones the feature has gotten wrong before. Grow the set each time you find a new failure, the way a test suite grows.

An AI feature can also degrade silently over time as real-world inputs shift or the model behind it changes. That slow slide is drift, and it is why you keep the eval set running instead of checking once.

7.5.4Give it a timeout and a fallback

A model call is a network call to a service that can be slow or down, so it needs the same guardrails as any external call. Put a timeout on it, a limit on how long the call may run, so a slow response cannot hang your feature. Add a fallback, a safe answer for when the call times out, errors, or fails validation.

The fallback is whatever keeps the feature usable without the model: a cached answer, or an honest message that the smart version is unavailable. A bad model call should degrade the feature, never break it.

A time-boxed, validated call has one safe exit and one fallback, so slow or wrong output never reaches the user.

This prompt wraps one AI feature in all three defenses:

Ready prompt
Act as a senior AI engineer making one AI feature fail safely, without changing what it does for the user. Add three layers of defense. 1. Structured output. Make the model return a fixed shape, then validate every response against a schema. Reject or retry anything malformed so it never flows downstream. 2. An eval. Build a small set of example inputs with their expected results, plus a script that runs the feature against them, so I catch regressions when I change a prompt or a model. Seed it with cases it has gotten wrong before. 3. A timeout and a fallback. Bound how long the call may take, and return a safe fallback when it times out, errors, or fails validation. Give me the failure points first as a short list, then the fix for each. Keep the happy-path behavior identical. The AI feature to make reliable:

Do this now: paste the prompt with your riskiest AI feature, the one whose wrong answer a user would act on, and let your agent wrap it in a schema check, an eval, and a fallback.

Mahmoud Zalt

Mahmoud Zalt

Software engineer, 16+ yrs · built Sistava.com in 3 months, idea to production, using these methods

Resources
Contribute
Donate

Support my work

A small tip keeps the free work coming.

© 2026 Mahmoud Zalt. Free to read, not to republish.
Copyright & license