Skip to content
tezvyn:
Menu

Designing input and output guardrails for a chatbot

Source: interviewMediumHow cards are made

Summary

layered safety architecture.

Key points

input guardrails filter or classify user prompts (injection, off-topic, PII) before the model; output guardrails validate responses for toxicity, leakage, and policy before sending.

What's really being asked

The interviewer wants a defense-in-depth design, not reliance on a single instruction line. Guardrails wrap the model so that even a compromised or misbehaving model cannot send unsafe content to a customer.

The full answer

Treat the LLM as an untrusted component and place independent checks on both sides. Input-side mechanism: before the prompt reaches the model, run validators. A moderation or toxicity classifier flags abusive input, a prompt-injection detector catches attempts to override instructions, a topic classifier rejects out-of-scope requests, and a PII detector redacts sensitive data the user pasted. Depending on the verdict, block, sanitize, or route to a canned response. Output-side mechanism: after generation but before the user sees it, validate the response. Run a toxicity and safety classifier, check for leakage of secrets, system prompts, or other users' data, enforce format and policy constraints, and for RAG verify groundedness against retrieved context to catch hallucinations. On failure, regenerate, return a safe fallback, or escalate to a human. Add logging and rate limiting for observability and abuse control. The principle is that guardrails are deterministic, auditable code paths separate from the model's own reasoning.

The mistakes people make

Relying solely on system-prompt instructions, which jailbreaks bypass. Putting checks only on output and ignoring malicious input. Using the same model with no independent validator, so one failure compromises everything.

What usually comes next

What is the latency cost, classifiers add milliseconds; parallelize where possible. How do you handle false positives, tune thresholds and offer appeal paths. How do you keep guardrails updated, retrain classifiers on new attacks.

A concrete example

A user pastes a prompt-injection payload telling the bot to reveal its system prompt. The input detector flags the injection and strips it; even if it slipped through, the output guardrail detecting system-prompt leakage blocks the response before it reaches the customer.

Interview question

Why is a system prompt alone insufficient as a guardrail for a customer-facing chatbot?

  • a.System prompts increase latency more than any classifier would
  • b.Output validation makes input validation unnecessary
  • c.Prompt instructions can be overridden by jailbreaks, so independent input and output validators are still neededCorrect
  • d.System prompts cannot be written for customer-facing models
Why?

Instructions in the prompt are not enforced code and can be subverted, so external validators provide defense in depth. The model is untrusted, so input checks remain necessary even with output checks.

Just read this? Test yourself on what you have been reading.

Read the original → developers.openai.com

Put your scrolling time to good use

Learn one idea, try a quiz and save useful cards for revision. Tezvyn makes it easy to learn and stay current in your tech field, a few minutes at a time.

The iPhone app is on the way

We are building it. Until it lands, nothing here is held back from you: every interview card, your saved cards, streaks and the job board all work in Safari, plus hundreds of free practice quizzes of thirty questions each. Sign in and it all carries over to the app the day it arrives.

Want it as an icon? Tap Share at the bottom of Safari, then Add to Home Screen. It opens full screen and the cards you have read stay available offline.

Get it on Google PlayiPhone app coming soon

We are hiring for this. Every open role lists the topics its interview covers, so you can prepare for the real thing rather than guessing.

See open roles