Back to Patterns

Instruction Hierarchy

Prompt Patterns

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

Build This Pattern

Copy this prompt and paste it into Claude Code, OpenCode, Codex, or Cursor to implement this pattern.

Build a prompt system with hierarchical instruction levels. ROLE: You are a prompt composition system that manages four priority levels of instructions, detecting conflicts and merging content with precedence rules. CONSTRAINTS: - Four levels: Level 1 (System) - immutable rules, Level 2 (Task) - specific instructions, Level 3 (Context) - background info, Level 4 (Output) - format requirements - Higher priority content takes precedence on conflict - Conflict detection must identify overlapping or contradictory instructions across levels - Visual level markers must be included in composed prompt for debugging - Levels can be disabled at runtime; empty levels are skipped TOOL CALLING: - Use function calling for: compose_prompt(levels[]), detect_conflicts(levels[]), validate_level(level_data), get_composition_stats(prompt_id?) - Each tool returns structured JSON with composition data and metadata STRUCTURED OUTPUT: - Composed prompt must return JSON: { prompt_id: string, levels: [{ level: number, name: string, content: string, priority: number, marker: string }], conflicts: [{ level_a: number, level_b: number, instruction_a: string, instruction_b: string, resolution: string }], full_prompt: string, token_count: number } - Conflict detection must return JSON: { conflicts: [{ level_a: number, level_b: number, instruction_a: string, instruction_b: string, overlap_type: 'contradiction' | 'redundancy' | 'ambiguity', resolution: string }], total_conflicts: number, auto_resolved: number } - Level validation must return JSON: { valid: boolean, level: number, issues: [{ type: string, message: string, severity: 'error' | 'warning' }], token_count: number } CHAIN OF THOUGHT: - Validation: check each level → verify format → count tokens → identify issues - Conflict detection: compare levels pairwise → identify overlaps → classify type → determine resolution - Composition: merge levels in priority order → apply resolution rules → add markers → format output - Statistics: analyze composition → report level contributions → identify optimization opportunities FEW-SHOT EXAMPLES: Levels: [ { level: 1, name: 'System', content: 'Always be helpful and accurate', priority: 100 }, { level: 2, name: 'Task', content: 'Summarize the document concisely', priority: 80 }, { level: 3, name: 'Context', content: 'Document is about AI agents', priority: 60 }, { level: 4, name: 'Output', content: 'Use bullet points, max 5 items', priority: 40 } ] Composed: { conflicts: [], full_prompt: '[SYSTEM] Always be helpful and accurate\n[TASK] Summarize the document concisely\n[CONTEXT] Document is about AI agents\n[OUTPUT] Use bullet points, max 5 items' } Conflict: { level_a: 1, level_b: 2, instruction_a: 'Always be helpful', instruction_b: 'Be brief', overlap_type: 'ambiguity', resolution: 'System level takes precedence: be helpful, but concise when possible' } EVALUATION CRITERIA: - Conflict detection accuracy: percentage of genuine conflicts correctly identified - Resolution appropriateness: percentage of conflicts resolved with sensible precedence - Composition completeness: percentage of levels that are correctly merged - Token efficiency: percentage of context window used effectively The system should: 1) Implement prompt composer with four priority levels as separate data structures with metadata, 2) Level 1 (System) for immutable rules and constraints, Level 2 (Task) for specific task instructions, Level 3 (Context) for background information, Level 4 (Output) for format and style requirements, 3) Each level has priority weight and optional override policy, 4) Composer merges levels into final prompt with higher-priority content taking precedence on conflict, 5) Render final prompt with visual level markers, 6) Include conflict detection module that scans for overlapping or contradictory instructions across levels, 7) On conflict detection, warn user with conflicting instructions and which level takes priority, 8) Handle malformed level definitions by skipping with warning, 9) Support levels with no content (empty level), 10) Handle nested priorities within a single level, 11) Support selectively disabling levels at runtime.