Summary
The Instruction Hierarchy pattern structures prompts with explicit priority levels, ensuring model responses respect higher-priority instructions over lower-priority ones. This approach improves reliability, reduces prompt injection vulnerabilities, and creates more predictable model behavior by establishing clear instruction precedence.
Priority levels
- System: Developer instructions (highest priority)
- Task: User request (medium priority)
- Context: Retrieved content (lower priority)
- Output: Model response (lowest priority)
Key principles
- System prompts override all other instructions
- Task-level instructions take precedence over context
- Explicitly mark instruction boundaries
- Use separators to distinguish instruction layers
- Validate that outputs respect hierarchy
Benefits
- Improved security against prompt injection
- More predictable model behavior
- Clearer debugging of instruction conflicts
- Better separation of concerns