input sanitization
Input sanitization cleans and validates user inputs before LLM processing to prevent attacks. **Purposes**: Block prompt injection attempts, filter harmful content, normalize inputs, validate format. **Techniques**: **Keyword filtering**: Block known attack patterns ("ignore previous", "system prompt"). **Encoding detection**: Flag base64, hex, or obfuscated text that may hide payloads. **Length limits**: Prevent prompt stuffing attacks. **Character filtering**: Remove or escape special characters, control codes. **Format validation**: Ensure expected input structure (JSON, specific fields). **Content scanning**: Check for toxic content, PII, code injection. **Limitations**: Adversarial inputs constantly evolve, over-filtering harms usability, semantic attacks bypass keyword filters. **Layered approach**: Input sanitization + system prompt design + output filtering + monitoring. **Implementation**: Pre-processing pipeline before LLM call, can use regex, classifiers, or another LLM as detector. **Best practices**: Allowlist over blocklist, defense in depth, log flagged inputs, regular pattern updates. Essential first layer of defense but not sufficient alone.