Area 06 · AI Security · Concept 05 of 20

Input Guardrails

Check and filter what goes into the model.

01–10 · The basePhase 7 · Weeks 24–26≈ 1 hour

The idea

Input guardrails run before the main model call. They can block or flag injection attempts, off-topic requests, abusive content, personal data that should not be sent, oversized inputs and known-bad patterns.

Use layers: cheap deterministic checks (length, file type, regex for secrets or card numbers), then classifiers (a small LLM prompt or a dedicated model such as Llama Guard or a provider moderation API). Run them in parallel with the main call where possible to save latency, and cancel the main call if a check fails.

Guardrails reduce risk; they do not eliminate it. Measure false positives as carefully as misses.

Why it matters

They stop the cheapest attacks and most accidental misuse before you spend tokens on them.

Words to know

  • GuardrailA check that can block, modify or flag a request or response.

Try it without code

List your inputs

  1. List every way text enters your model (chat, uploads, fetched pages, tool results).
  2. Write which guardrail applies to each.

Use it in code

Layered input checks, deterministic first.

python
import re

MAX_CHARS = 8000
SECRET_PATTERNS = [r"sk-[A-Za-z0-9]{20,}", r"AKIA[0-9A-Z]{16}", r"\b(?:\d[ -]?){13,16}\b"]

def check_input(text, classify_injection):
    if len(text) > MAX_CHARS:
        return False, "Message too long. Please shorten it."
    for p in SECRET_PATTERNS:
        if re.search(p, text):
            return False, "It looks like you pasted a key or card number. Please remove it."
    if classify_injection(text):           # small LLM or dedicated model
        return False, "I can only help with questions about your account."
    return True, None
PRACTICE · TICK IT AFTER THIS

Add input guardrails to your agent and run your 20-attack set plus 30 normal messages; report block rate and false positives.

Common mistakes

  • Only checking the chat box, not uploaded files and tool results.
  • Error messages that teach attackers what was detected.

Go deeper