mediumAI Engineering

What are guardrails and how do you implement them around a model?

155 views
01

Understand the problem

Input filters, output validators and policy checks — the sandwich around every model call.

guardrailsvalidationsafetymoderation
02

Attempt it yourself

Sketch your approach before reading the solution — that's what interviews test.

Stuck? AI Nudge Available

Get a conceptual hint to guide your logic without spoiling the final implementation.

03

Study the solution

The solution is waiting

Give it an honest attempt first — then compare your thinking with the full walkthrough.

04

Read the code

Guard pipeline with repair-once
async function guardedAnswer(input: string, ctx: Ctx) {
  await cheapInputGuards(input);                        // length, regex, rate — throws fast
  const [scopeOk, injectionRisk] = await Promise.all([  // model guards in parallel
    scopeClassifier(input), injectionClassifier(input),
  ]);
  if (!scopeOk) return CANNED.offTopic;                 // degrade
  if (injectionRisk > 0.9) return escalate(input, ctx); // human queue

  let out = await model.answer(input, ctx);
  const violations = await outputGuards(out, ctx);      // schema, citations, policy
  if (violations.length) {
    out = await model.answer(input, ctx, { repairNote: violations });  // repair once
    if ((await outputGuards(out, ctx)).length) return CANNED.safeFallback;
  }
  metrics.guardReport({ scopeOk, injectionRisk, violations });
  return out;
}
05

Join the discussion

Discussion (0)

Sign in to join the discussion.

No responses yet. Be the first to share what you think.