0Pricing
AI Prompt Engineering · Lesson

What Are Guardrails

Safety and quality gates.

What Are Guardrails is a free AI Prompt Engineering lesson on CoddyKit — lesson 1 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the AI Prompt Engineering learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

Guardrails Defined

Guardrails are programmatic checks that sit around an LLM to enforce safety, policy, and quality. They run on the way in (user input) and on the way out (model output), gating what reaches the model and what reaches the user.

The model is probabilistic; guardrails are deterministic policy enforcement layered on top.

Why Prompting Alone Is Insufficient

System-prompt instructions like 'never reveal secrets' are soft: they can be overridden by jailbreaks, eroded over long contexts, or ignored under distribution shift. Guardrails are hard because they are code outside the model that cannot be argued with.

Defense in depth: prompt the model and wrap it in independent checks.

Input vs Output Guardrails

Two placements with different jobs:

  • Input guardrails block prompt injection, disallowed requests, and PII before tokens cost money.
  • Output guardrails catch unsafe content, leaked data, hallucinations, and schema violations before delivery.

Input checks save cost; output checks save users.

Block, Redact, Regenerate, Escalate

A guardrail must decide an action when it trips:

  • Block — refuse and return a safe message.
  • Redact — strip the offending span and continue.
  • Regenerate — re-prompt with the violation noted.
  • Escalate — route to a human or a stricter model.

The right action depends on severity and user trust.

def on_violation(severity):
    if severity == 'critical': return 'block'
    if severity == 'pii': return 'redact'
    if severity == 'quality': return 'regenerate'
    return 'escalate'

The Guardrail Pipeline

Compose guardrails as an ordered pipeline; fail fast on the first hard violation.

def guarded_generate(user_input):
    for g in INPUT_GUARDS:
        verdict = g.check(user_input)
        if verdict.block:
            return safe_refusal(verdict)
    output = call_model(user_input)
    for g in OUTPUT_GUARDS:
        verdict = g.check(output)
        if verdict.block:
            return verdict.handle(output)
    return output

Deterministic vs Model-Based Guards

Two implementation styles:

  • Deterministic — regex, schema, allow/deny lists, classifiers with fixed thresholds. Fast, cheap, auditable.
  • Model-based — a moderation model or LLM judge evaluates nuanced policy. Flexible, but slower and itself fallible.

Use deterministic guards for hard rules and model guards for judgment calls.

Latency and Cost Budgets

Every guardrail adds latency. Strategies to stay fast:

  • Run independent output guards in parallel.
  • Order cheap deterministic checks before expensive model checks.
  • Short-circuit on the first hard block.
  • Stream output but hold delivery until critical guards pass.
verdicts = await asyncio.gather(*[g.check(out) for g in OUTPUT_GUARDS])
if any(v.block for v in verdicts):
    return handle(verdicts)

False Positives Are a Real Cost

Over-aggressive guardrails block legitimate requests and frustrate users (the 'I cannot help with that' for benign queries). Tune thresholds against a labeled set and track the false-positive rate as a first-class metric, not just recall on attacks.

Streaming Complicates Output Guards

If you stream tokens to the user, a late-arriving violation may already be on screen. Options:

  • Buffer fully, guard, then release (safest, higher latency).
  • Guard on sentence boundaries during streaming.
  • Stream optimistically but retract/replace on violation.

Choose based on how harmful a leaked partial output would be.

Auditability and Logging

Guardrails are compliance artifacts. Log every verdict with input hash, guard id, severity, and action taken, while being careful not to log the sensitive content itself. This record proves enforcement during audits and powers tuning.

audit.log({'guard': g.id, 'verdict': verdict.label,
           'action': verdict.action, 'input_hash': sha256(inp)})

Guardrails Are Not a Substitute for Design

Guardrails reduce risk; they do not eliminate it. A model with no access to a secret cannot leak it. Prefer architectural controls (least privilege, scoped tools, no sensitive data in context) and use guardrails as the last layer, not the only one.

Quick Check

Which placement of a guardrail primarily reduces token spend on disallowed requests?

Recap

Guardrails fundamentals:

  • Deterministic policy layered around a probabilistic model.
  • Input guards save cost; output guards protect users.
  • Actions: block, redact, regenerate, escalate.
  • Mix deterministic and model-based checks; run in parallel.
  • Track false positives; log verdicts; prefer architecture over patching.

Next: input and output filtering in depth.

Frequently asked questions

Is the “What Are Guardrails” lesson free?

Yes — the full text of “What Are Guardrails” is free to read here on the web, and the AI Prompt Engineering course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the AI Prompt Engineering course, upgrade to CoddyKit PRO.

What will I learn in “What Are Guardrails”?

Safety and quality gates. You practise AI Prompt Engineering with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start AI Prompt Engineering?

No prior experience is required. AI Prompt Engineering on CoddyKit is structured for beginners through advanced learners; this is — lesson 1 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “What Are Guardrails” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this AI Prompt Engineering lesson?

Yes. Every AI Prompt Engineering lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. What Are Guardrails
  2. Input and Output Filtering
  3. Schema and Rule Validators
  4. Self-Critique Validation
← Back to AI Prompt Engineering