Skip to content
mentoor

Topic 2 of 5 · 5 min read

Output checks before users see a response

Goal: Write the output checks for a feature and decide for each failure whether to block, retry or flag the response.

An output check treats the model's reply as untrusted input to your own system, and each failure maps to one of three actions: block, retry or flag. The model can return malformed JSON, a leaked key, an invented citation or text that breaks your policy, and it sounds sure each time.

Check yourself · 1 of 3

A model reply should be JSON with an amount field, but the amount is missing. What is the usual first action?

Choose one answer

Why it matters

Whatever the model writes goes somewhere: into a UI, a database, a shell, another service. OWASP lists this as improper output handling. A reply that contains <script> is a web problem. A reply with a customer's email address in the wrong account is a privacy incident. Checking on the way out is the last point where you still control what happens.

The checks

  1. Schema. Parse the reply and validate it against the type your code expects. Prefer a provider's structured-output mode, and still validate.
  2. Safety and policy. Run moderation or a policy classifier on the text.
  3. Personal data and secrets. Scan for key formats (sk_live_, AKIA, private-key headers), emails, phone numbers and IDs that should not appear.
  4. Grounding and citations. If the answer must come from retrieved documents, check that every cited source id exists in the retrieved set and that quoted text appears in it.
  5. Business rules. Refund amounts under the limit, only allowed URLs, no discount codes that do not exist.

Block, retry or flag

  • Retry when the failure is mechanical (bad JSON, missing field). Send the error back once, with a hard cap on attempts.
  • Block when the content is harmful or leaks data. Show a specific message and log the event.
  • Flag when it is uncertain: deliver, mark for review, and sample the flags. Use this for low-stakes checks where over-blocking costs more than a miss.

Streaming

Tokens that have reached the screen cannot be taken back. Either buffer and check before display, check at sentence boundaries and hold back the last chunk, or stream only for low-risk features and run a full check at the end that can retract the message.

Before and after

// Before: trusts the model
const reply = await llm(prompt);
return res.json(JSON.parse(reply));

// After: parse, validate, scan, then decide
const out = ReplySchema.safeParse(tryJson(reply));
if (!out.success) return retryOnce(prompt, out.error);
if (hasSecret(out.data.text)) return block("secret_leak");
return res.json(out.data);

What goes wrong

  • Retrying without a limit. A bad prompt becomes a cost loop.
  • Checking the final text only while streaming the draft.
  • Trusting a citation because it looks right.

Examples

A grounding check

function grounded(answer: Answer, retrieved: Doc[]): boolean {
  const ids = new Set(retrieved.map((d) => d.id));
  return answer.citations.every((c) => ids.has(c.docId) &&
    retrieved.find((d) => d.id === c.docId)!.text.includes(c.quote));
}

If it returns false, the answer is not shown. The user sees "I could not confirm this from the documents" and the case is logged.

Choosing the action

Failure Action
JSON missing a field Retry once with the error
API key pattern in text Block and log
Tone slightly off-brand Flag and sample

Key points

  • Treat the model's reply as untrusted input to your own code and validate it against a schema.
  • Scan outputs for secrets and personal data, because models can repeat what was in their context.
  • Verify cited sources against the retrieved set; a plausible-looking citation proves nothing.
  • Retry mechanical failures once, block harmful or leaking output, and flag uncertain cases for review.
  • Streamed tokens cannot be recalled, so buffer or check in chunks when the risk is high.

Common mistakes

  • Retrying on failure with no attempt limit.
  • Streaming the draft to the user and checking only after it has been shown.
  • Validating the schema but not the values, such as an amount above the refund limit.
  • Blocking every flag, so users see refusals for harmless text.

Check yourself · 2 of 3

Why scan model output for key patterns such as sk_live_ when your prompt never contains a key?

Choose one answer

Check yourself · 3 of 3

A summary streams to the screen token by token. A check then finds a leaked email address. What is the problem?

Choose one answer

Practice items, not counted towards your mastery.

Start here

More for this topic