Why this matters
Large language models follow instructions, but not all instructions carry the same weight. A clear hierarchy (goal → rules → steps → format) makes outputs reliable, safe, and consistent.
- Customer support: ensure replies are empathetic, compliant, and never promise refunds outside policy.
- Data tasks: enforce schema, delimiter choices, and validation so outputs parse cleanly.
- Code generation: limit libraries, define function signatures, and require tests.
- Summaries: cap length, enforce audience level, and include required sections.
Concept explained simply
Think of a good prompt like a well-structured contract. The top sets the purpose, the next layer sets non‑negotiable rules, then procedures, then the exact output format.
Mental model
- Constitution (highest): Role + Goal. What is the assistant and what outcome matters most?
- Policies: Hard constraints and guardrails. What must never happen? What must always happen?
- Procedures: Steps or method. How should the model work?
- Output Spec: Exact format, fields, limits, and examples.
When conflicts happen, higher layers win. Place the most important rules higher and earlier.
Hierarchy template you can reuse
You are: [role] Primary goal: [single clear objective] Non-negotiable constraints: - [safety/compliance] - [content/knowledge bounds] - [style/tone limits] Process (do, then show): 1) [steps] 2) [checks] Output format (required): - [structure, fields, order] - [length limits] - [examples] If uncertain: - [ask clarifying question OR return "NEEDS-INFO" with missing fields]
Worked examples
Example 1: Summary for non-experts with legal safety guardrails
Naive prompt
Summarize this contract for a layperson.
Hierarchical prompt
You are: Policy-aware explainer for non-experts. Primary goal: Produce an accessible summary of the contract. Non-negotiable constraints: - Do not provide legal advice; include a clear disclaimer. - No speculation about unenclosed sections. If uncertain, state "Unknown". Process: 1) Identify purpose, key obligations, termination, and risks. 2) Simplify language to ~8th-grade reading level. 3) Sanity-check: no advice, no speculation. Output format (required): - Title: [doc title] - Sections: Purpose; Parties; Key Duties; Termination; Major Risks; Disclaimer - Each section 1–2 bullet points, max 80 words total.
Example 2: Code generation with strict constraints
Naive prompt
Write Python to compute Fibonacci numbers.
Hierarchical prompt
You are: Senior Python engineer. Primary goal: Implement fib(n) efficiently for n ≤ 10^6. Non-negotiable constraints: - Python 3.10; standard library only. - Time complexity O(log n) using fast doubling; iterative or memoized recursion allowed. - Return int; handle n=0. Process: 1) Implement fast doubling. 2) Add simple doctest examples. 3) Verify: no prints, no I/O. Output format (required): python # code only
Example 3: Customer support reply with policy compliance
Naive prompt
Reply to this customer who wants a refund.
Hierarchical prompt
You are: Customer support agent for ACME. Primary goal: Provide a helpful, empathetic reply that follows policy. Non-negotiable constraints: - Do not promise refunds. Offer steps to request one. - No personal data collection beyond order number and email. Process: 1) Acknowledge and empathize. 2) Ask for order number if missing. 3) Explain refund steps. 4) Offer next-step channel. Output format (required): - 120–160 words. - Tone: warm, professional. - Include "We’ll review your request within 2 business days."
How to design constraints step-by-step
- State one primary goal. Remove extra goals or make them secondary.
- List 3–7 non-negotiable constraints. If there are more, combine or move some to procedures.
- Write 2–5 process steps (how to work, not what to output).
- Define a strict output spec: field order, length, style, and examples.
- Conflict check: If two rules conflict, keep the higher-level one and rephrase the lower.
- Run a dry test: Ask the model to restate the constraints before producing output.
Checklist: Is your hierarchy solid?
- Primary goal appears once, at the top.
- Constraints are concrete (numbers, lists, allowed/forbidden).
- Process separated from output format.
- There is a plan for uncertainty (ask or return NEEDS-INFO).
- Length limits and structure are unambiguous.
Exercises
Do these in your own editor or an LLM playground. Then compare with the solutions provided in collapsible sections.
Exercise 1 — Rewrite a messy prompt into a clear hierarchy
Messy prompt to fix:
Summarize this product review and talk about pros/cons and ratings, but keep it short. Also be kind, don’t be salesy, and include a warning if there is safety stuff. Give me bullets and maybe a table if you want.
- Task: Rewrite using the hierarchy: role, primary goal, non-negotiable constraints, process, output format.
- Keep it under 120 words in the final output.
- Include a safety warning only if present in the text.
Show guidance
- Define the audience (shopper) and tone (neutral, kind).
- Limit bullets and forbid tables for consistency.
- Specify when to include warnings.
Exercise 2 — Add guardrails to a SQL generation task
Draft a hierarchy that generates a safe SELECT-only SQL query from a natural language request.
- Forbid INSERT/UPDATE/DELETE.
- Require a schema echo before the query.
- If tables/columns are missing, return NEEDS-INFO with exact missing items.
- Output only two blocks: SCHEMA and QUERY.
Self-check after exercises
- Did you define exactly what to output and in what order?
- Do constraints cover safety, format, and scope?
- Is there a clear plan for uncertainty?
Common mistakes and how to self-check
- Conflict between instructions: Fix by moving the winning rule higher and deleting the conflicting one.
- Vague format: Replace "keep it short" with numeric limits or field counts.
- Over-constraint: If the model cannot satisfy all rules, mark priorities and allow graceful fallback (e.g., return NEEDS-INFO).
- Multiple goals crammed together: Split tasks or make one primary with the others secondary.
- Hidden safety needs: Explicitly forbid risky actions (e.g., no legal/medical advice).
Quick self-test ritual
- Ask: "What is the single primary goal?" If you can’t answer in one sentence, rewrite.
- Ask the model to restate your constraints before doing the task. If it misses any, your prompt is unclear.
Practical projects
- Documentation summarizer: Build a prompt that outputs standardized release notes (sections, limits, unknowns policy).
- Data normalizer: Design a hierarchy that converts messy CSV descriptions into a strict JSON schema with validation.
- Support macro library: Create 5 reusable support reply hierarchies (refunds, shipping delay, damaged item, missing order, account help).
Who this is for, prerequisites, and learning path
Who this is for
- Prompt engineers building reliable workflows.
- Data/ML practitioners turning LLM output into pipelines.
- Product and ops folks designing safe AI assistants.
Prerequisites
- Basic prompt writing (roles, examples, output formatting).
- Comfort reading JSON/CSV and simple code snippets.
Learning path
- Start: Role and goal clarity.
- Then: Constraint design and hierarchy (this lesson).
- Next: Evaluation prompts and self-check steps.
- Finally: Tool-use boundaries and error handling.
Mini challenge
Create a hierarchy for an LLM that converts messy meeting notes into a project brief with sections: Objectives, Risks, Decisions, Action Items (owners, due dates). Include a rule to return NEEDS-INFO if owners or due dates are ambiguous.
Quick Test
The test below is available to everyone for free. If you log in, your progress is saved.
Open the Quick Test to check your understanding and get targeted feedback.