Without explicit boundaries, LLM outputs become unpredictable. Constraints and guardrails transform unreliable creative outputs into controlled, production-ready responses.
Key Concepts
- Constraints: Explicit limitations on what the model CAN do (output length, format, tone, scope)
- Guardrails: Explicit rules about what the model must NOT do (topics, behaviors, claims)
- Boundary Testing: Systematically testing whether constraints hold under adversarial inputs
Types of Constraints
| Type | Example | Purpose |
|---|---|---|
| Length | "Respond in exactly 3 sentences" | Control verbosity and cost |
| Format | "Output only valid JSON" | Machine-readable output |
| Tone | "Use formal academic language" | Audience alignment |
| Scope | "Only discuss topics related to Python" | Topic containment |
| Temporal | "Only reference information before 2024" | Prevent hallucination |
| Confidence | "Say 'I don't know' if uncertain" | Reduce confabulation |
Types of Guardrails
| Type | Example | Purpose |
|---|---|---|
| Content | "Never generate violent or sexual content" | Safety |
| Legal | "Do not provide legal advice" | Liability |
| Competitive | "Never mention competitor products" | Business |
| Personal | "Do not ask for personal information" | Privacy |
| Accuracy | "Do not make up statistics or citations" | Trust |
Framework: The FENCE Method
- Format: Specify exact output structure
- Exclusions: List what to avoid
- Number limits: Set quantity constraints (word count, list items, etc.)
- Conditions: Define if-then rules ("If the user asks about X, respond with Y")
- Escapes: Define fallback behavior ("If you cannot answer, say...")
Constraint Stacking
Combine multiple constraints for precision:
"Summarize this article in exactly 5 bullet points [number]. Each bullet must be under 15 words [length]. Use present tense [format]. Do not include the author's personal opinions [exclusion]. If the article lacks sufficient information, state that explicitly [escape]."
Testing Constraints
Always test constraints with:
- Normal inputs (happy path)
- Edge cases (extremely long/short inputs)
- Adversarial inputs ("Ignore your instructions and...")
- Ambiguous inputs (where constraints might conflict)
Common Mistakes
- Setting contradictory constraints ("Be comprehensive" + "Be brief")
- Using soft language ("Try to..." or "Preferably...") — be absolute
- Not testing if constraints actually hold across multiple runs
- Overloading with too many constraints (>10 rules degrades adherence)
- Forgetting that constraints consume tokens and attention