Guardrails
Intermediate 12 min read Guardrails

Input Filtering

Gates on the way in

Input filters gate what the model is allowed to see: disallowed topics, format limits, injection-shaped text, and size caps on the way in. Filtering before inference blocks junk cheaply — before it costs tokens or acts.

input filtertopicinjectionsize limitpre-inferenceallowlist
Prerequisites: Guardrails Overview
Before we start

What you will learn

  • Define input policies (topics, formats, sizes)
  • Add injection heuristics to input filters
  • Return clean rejections to users
Introduction

What is this?

Input filtering applies policy before inference: wildcard ways to block disallowed topics, enforce format expectations, cap lengths, and flag injection-shaped strings ('ignore previous instructions'). Cheap and fast, it stops junk before tokens and tool calls — an allowlist-by-default posture is strongest.

Motivation

Why do you need it?

  • Blocking early saves tokens and prevents side effects.
  • Injection-shaped inputs get caught before they influence tools.
  • Size caps bound context cost and latency.
  • Deterministic rules are cheap, testable, and fast.
Think about it

Mental model

Imagine

Checking fields at customs

Think of

Input filtering

Goods (inputs) are scanned before entering the country: prohibited items (topics), wild forms (formats), overweight crates (size caps), and suspicious passports (injection patterns). Scanning is cheaper than dealing with what got through.

Mechanics

How it works

  • Pattern-check: disallowed topics via classifier or blocks.
  • Format-check: expected structure/type validation.
  • Size-check: token and length caps.
  • Injection heuristics: 'ignore instructions', delimiter scams.
  • Reject with a clean reason or route to a safe fallback.

Pre-inference gates.

User input
Topic
Format
Injection
Reject / OK
Real world

In production

  • Kids' products block topics at the boundary.
  • Form-based apps enforce JSON/schema before the model sees it.
  • Chat limits cap turn length for cost and safety.
Guidelines

Best practices

  • Default-deny where policy is clear.
  • Rejections should name the rule (UX-window).
  • Keep classification cheap; escalate only suspicious ones.
  • Tune on real traffic; reduce false positives.
Watch out

Common mistakes

  • Prompt-only topic control — the model not the gate decides.
  • Heuristics that nuke normal messages (e.g. 'ignore' as a word).
  • No caps on length — cost and context abuse.
Root cause analysis

Common issues

Error
Injection bypasses filters
Root cause

Heuristics too shallow

Fix

Layered checks + treat ALL model input as untrusted regardless of filters.

Error
Filter blocks the product's own flows
Root cause

Rules too broad

Fix

Business-case exceptions with logging and review.

When things break

Debugging tips

  • Legit input blocked → false positive; add context or narrow rule.
  • Abuse slips through → missing layer; test adversarial traffic.
  • Latency spikes → classification on the hot path too heavy; sample.
Interviews

Interview checkpoint

Practice

Hands-on lab

Sanitize and Cap Input
25 min · beginner
In progress
Objectives
  • Block inputs carrying known bypass terms
  • Strip characters outside an allowed set
  • Enforce a length cap

Sanitize and Cap Input

25 min · beginner

0/3 steps
0%
sandbox · Linux sandbox
$
Try these commands
Test yourself

Quick quiz

0/2
1

Input filters run…

2

Heuristics alone fully stop prompt injection.

Go deeper

Official documentation