Skip to solution
mediumAI Engineering

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

156 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.

Nudge consolestandby

Stuck? Beam a request up — the console returns a conceptual nudge that guides your logic without spoiling the implementation.

03

Study the solution

Guardrails are programmatic checks wrapped around model calls: input-side (block or sanitize prompt-injection attempts, off-topic or abusive requests, PII), and output-side (schema validation, content policy classification, grounding/citation checks, blocklists for competitors/claims/URLs). Implement as a pipeline: che

Solution ready — 2 min read

Classified // press E to declassify

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.

Transmission complete // awaiting log

KEEP THE
STREAK ALIVE.

Dossier 53 of 80 decoded in the AI Engineering track. One more won't hurt.

Back to track