Skip to content
7 min read·Lesson 3 of 8

Prompt Engineering Patterns

The seven core prompting patterns that move the needle — role, instruction clarity, examples, structure, constraints, output format, and iteration.

"Prompt engineering" is a misleading term — it's less engineering and more disciplined writing. The patterns in this lesson are the difference between hit-or-miss output and reliable production behaviour.

Pattern 1: Role + Task + Constraints

State three things up front: who the model is, what to do, and what the rules are.

You are a senior security engineer reviewing a pull request.
Review the following diff for security vulnerabilities.
Rules:
- Only flag issues you can name precisely (e.g., "SQL injection in line 12").
- If no issues, respond with the single word: "Clean".
- Do not comment on style or performance.

Compare to "Look at this code". The structured prompt eliminates 80% of common failure modes — irrelevant advice, vague feedback, missing structure.

Pattern 2: Few-Shot Examples

Show the model what good output looks like:

Classify the support ticket sentiment as positive, neutral, or negative.

Ticket: "App crashed when I uploaded a photo, please fix soon."
Sentiment: negative

Ticket: "The new dark mode looks great, thanks!"
Sentiment: positive

Ticket: "Where do I find my invoices?"
Sentiment: neutral

Ticket: "I've been waiting 4 days for a response. Unacceptable."
Sentiment:

2-5 examples is the sweet spot. Choose examples that span the expected distribution — including the edge cases you care about getting right.

Pattern 3: Use Delimiters

When your prompt has multiple sections (instructions, context, user input, examples), separate them clearly:

<instructions>
Summarise the article in three bullet points.
</instructions>

<article>
{{ article_text }}
</article>

Anthropic recommends XML tags; OpenAI works fine with triple backticks, JSON, or any consistent format. The benefit: prevents the user input from being interpreted as new instructions (a primitive form of prompt-injection defence), and helps the model "find" each section.

Pattern 4: Specify Output Format

If you want JSON, ask for JSON — and either use structured outputs (constrained generation) or describe the schema in the prompt:

Respond ONLY with JSON matching this schema:
{
  "sentiment": "positive" | "neutral" | "negative",
  "confidence": number between 0 and 1,
  "key_phrases": [string, ...] (up to 5)
}
Do not include any text outside the JSON object.

When parsing user-facing markdown, give a literal example:

Format your answer as:

## Summary
<one paragraph>

## Action Items
- <item 1>
- <item 2>

Pattern 5: Be Specific About "Don't"

The model isn't great at parsing negation in the abstract. Replace "Don't be too long" with "Limit your response to 200 words." Replace "Don't make things up" with "If you don't know, respond with: 'I don't know.'"

VaguePrecise
Don't use jargonWrite at a 9th-grade reading level
Be conciseMaximum 3 sentences
Don't speculateIf unsure, respond: "I cannot determine from the context."
Be friendlyUse first names and one casual phrase per message

Pattern 6: Persona and Audience

"Explain quantum entanglement" is unbounded. "Explain quantum entanglement to a curious 12-year-old" or "...to a physics PhD reviewing a paper" produces dramatically different (and more useful) outputs.

Pattern 7: Iterate Empirically

Prompt engineering is not theory — it's testing. Set up an evaluation harness:

  1. Collect 20-50 representative inputs
  2. Hand-label the desired outputs (or define a checker)
  3. Run each prompt variant; score outputs
  4. Keep the prompt that scores best
  5. Repeat on new failures

Without a harness, you're tuning by vibes — and the model's behaviour can be wildly different across edge cases.

Bonus: The "Think Step-by-Step" Pattern

For multi-step reasoning, adding "Think step by step before answering" or "Show your reasoning" reliably improves accuracy on math, logic, and multi-hop questions. This is the entry-level form of chain-of-thought prompting, which we explore deeply in the next lesson.

Bonus: Self-Critique

For high-stakes outputs, ask the model to critique its own response and revise:

Step 1: Draft an answer to the question.
Step 2: Review your draft. List any errors, missing details, or weak arguments.
Step 3: Provide a revised, final answer.

This often catches mistakes a single pass would miss.

Prompting Anti-Patterns

  • Polite filler: "Please could you possibly help me with..." wastes tokens and doesn't help
  • Overlong system prompts: 4-page system prompts dilute focus. Aim for ~500 words.
  • Contradictory instructions: "Be brief but cover all the topics in depth" — pick one
  • Examples that disagree: Inconsistent few-shot examples confuse the model
  • Trusting "rules" alone: "Never say X" can still produce X. Validate output programmatically.

A Production-Quality Prompt Template

## Role
You are {{ role_description }}.

## Task
{{ task_description }}

## Constraints
- {{ constraint 1 }}
- {{ constraint 2 }}

## Examples
{{ few_shot_examples }}

## Output format
{{ format_specification }}

## Input
{{ user_input_delimited }}

Most production prompts converge to something close to this shape. It's worth more than it looks — clarity of structure transfers directly to clarity of output.

In the next lesson we go beyond patterns into the techniques that unlock LLMs' reasoning capabilities: chain-of-thought, ReAct, and tool use.

Key Takeaways

  • Be explicit: tell the model role, task, constraints, and desired output format.
  • Few-shot examples (2-5) outperform zero-shot for most structured tasks.
  • Use delimiters (XML tags, triple quotes, JSON) to separate sections of a long prompt.
  • Specify the output format precisely — JSON schema, markdown structure, or example.
  • Iterate empirically: small changes to wording have outsized effects.

Test your knowledge

Try exam-style practice questions to reinforce what you've learned.

Practice Questions →