Prompt Engineering Patterns That Actually Work
Reusable patterns: role priming, few-shot, structured output, and self-critique.
What You'll Learn
- Apply role priming to set the model's persona and expertise
- Use few-shot examples to constrain output format
- Request structured output (JSON, tables, lists) reliably
- Implement self-critique loops for higher quality outputs
- Chain prompts for complex multi-step tasks
Prerequisites
- Access to an LLM API (OpenAI, Anthropic, Google, or similar)
- Basic understanding of what a language model is
Step-by-Step Guide
Step 1
1. Role Priming
Start your system prompt with a clear role: 'You are a senior backend engineer with 10 years of Python experience.' This primes the model's vocabulary, assumptions, and level of detail. Be specific — 'expert' is weaker than 'a staff engineer who has shipped Python microservices to millions of users'. Role priming is especially powerful for domain-specific tasks like medical, legal, or engineering queries.
Step 2
2. Few-Shot Examples
Provide 2–5 input/output pairs before the actual task. The model pattern-matches and applies your examples' format, tone, and level of detail. For classification: Input: 'My order arrived broken.' → Category: 'Damaged Goods'. Use at least 2 examples per class. For generation, examples constrain length, structure, and vocabulary better than instructions alone.
Step 3
3. Structured Output
Ask for JSON, Markdown tables, or numbered lists explicitly and show the schema. 'Respond ONLY with valid JSON matching this schema: { name: string, score: number, reason: string }'. For APIs, use OpenAI's response_format: { type: 'json_object' } or Anthropic's structured output feature to guarantee parseable responses. Never rely on post-processing to fix malformed output.
Step 4
4. Self-Critique Loops
After generating a first draft, prompt the model: 'Review the above output. List any factual errors, logical gaps, or missing edge cases. Then provide an improved version.' This two-step process consistently produces higher quality results than a single prompt. Automate it in code: generate → critique → regenerate. Three iterations is usually sufficient.
Step 5
5. Prompt Chaining for Complex Tasks
Break multi-step tasks into a pipeline of focused prompts. Step 1: Extract key facts from a document. Step 2: Generate a summary using the extracted facts. Step 3: Translate the summary. Each prompt is simpler, easier to test, and failures are isolatable. Use LangChain's SequentialChain or hand-roll the pipeline with a loop and structured data passed between steps.
Further Reading & Resources
Ready to put this into practice?
Apply for a real AI & Automation internship and ship production projects with mentorship.
