input validation
**Input validation** for AI systems is the practice of **checking, sanitizing, and constraining** user inputs before they reach a language model or AI pipeline. It is the first line of defense against **prompt injection**, **jailbreaking**, **resource abuse**, and other attacks.
**What to Validate**
- **Length Limits**: Enforce maximum input length to prevent resource exhaustion and context window abuse. Reject or truncate inputs exceeding reasonable bounds.
- **Character Filtering**: Remove or escape special characters, control characters, invisible Unicode, and known adversarial sequences.
- **Content Screening**: Run input through a **toxicity classifier** or **moderation API** to detect and reject harmful content before it reaches the model.
- **Format Validation**: For structured inputs (JSON, API parameters), validate against expected schemas before processing.
- **Rate Limiting**: Track input frequency per user to prevent automated probing and abuse.
**Prompt Injection Defense**
- **Delimiter Validation**: Check that user input doesn't contain delimiters or formatting tokens used to separate system instructions from user content.
- **Instruction Detection**: Use a classifier to detect inputs that appear to contain **meta-instructions** (e.g., "ignore previous instructions," "you are now...").
- **Semantic Filtering**: Detect inputs that semantically attempt to override system behavior, even if they don't use obvious keywords.
**Implementation Best Practices**
- **Validate Before Processing**: All validation should happen **before** the input reaches the model — never rely only on output filtering.
- **Defense in Depth**: Input validation is one layer — combine with output filtering, rate limiting, and monitoring.
- **Allowlist Over Denylist**: When possible, define what **is** allowed rather than trying to enumerate everything that's forbidden.
- **Log and Monitor**: Record validation rejections for security analysis and pattern detection.
**Challenges**
- **False Positives**: Overly aggressive filtering can block legitimate inputs.
- **Adversarial Evasion**: Sophisticated attackers craft inputs that bypass filters through encoding tricks, paraphrasing, or multi-step approaches.
- **Multilingual Content**: Filters designed for English may miss attacks in other languages.
Input validation is a **non-negotiable security requirement** for any production AI application — it is the most effective and lowest-cost defense against the majority of LLM attacks.