What are the different types of prompt engineering?
#gen-ai#prompt-engineering
Answer
Types of Prompt Engineering
Prompt engineering encompasses many techniques for eliciting better responses from LLMs. Here's a comprehensive taxonomy.
1. Zero-Shot Prompting
No examples — just a direct instruction:
pythonprompt = "Classify the sentiment of this review: 'The product broke after one day.'" # → "Negative"
2. Few-Shot Prompting
Provide examples to guide the model:
pythonprompt = '''Classify sentiment: Review: "Amazing product, love it!" → Positive Review: "Terrible, broke on day one." → Negative Review: "It's okay, nothing special." → Neutral Review: "Best purchase I've made this year!" → ''' # → "Positive"
3. Chain-of-Thought (CoT)
Ask the model to reason step by step:
pythonprompt = ''' Q: A farmer has 15 sheep. All but 8 die. How many are left? A: Let me think step by step. - "All but 8" means 8 survive - 8 sheep are left Answer: 8 Q: A jar has 12 cookies. You eat 3 and give 4 to a friend. How many remain? A: '''
4. Zero-Shot CoT
Simply add "Let's think step by step":
pythonprompt = "What is 25% of 480? Let's think step by step." # Model reasons: 25% = 1/4, 480/4 = 120
5. Self-Consistency
Generate multiple reasoning paths, take the majority answer:
pythonanswers = [] for _ in range(5): answer = llm.invoke("Solve: " + problem, temperature=0.7) answers.append(extract_answer(answer)) final_answer = max(set(answers), key=answers.count) # Majority vote
6. Tree of Thoughts (ToT)
Explore multiple reasoning branches, prune dead ends:
textProblem ├── Approach A → evaluate → continue if promising ├── Approach B → evaluate → abandon if poor └── Approach C → evaluate → continue
7. Role Prompting
Assign a persona to shift behavior:
pythonsystem = "You are a Socratic tutor. Never give direct answers. Guide students with questions." user = "What is machine learning?" # → "What do you think makes a system 'learn'? What's different about..."
8. ReAct (Reason + Act)
Interleave reasoning with action:
textThought: I need to find the current stock price Action: search_web("AAPL stock price today") Observation: AAPL is trading at $189.42 Thought: Now I can answer Answer: Apple stock is currently $189.42
9. Prompt Chaining
Break complex tasks into sequential prompts:
pythonstep1 = llm.invoke(f"Extract the key facts from: {document}") step2 = llm.invoke(f"Identify contradictions in: {step1}") step3 = llm.invoke(f"Write a summary highlighting: {step2}")
10. Maieutic Prompting
Ask the model to explain its reasoning, then verify consistency:
pythonanswer = llm.invoke(question) explanation = llm.invoke(f"Explain why: {answer}") verification = llm.invoke(f"Is this explanation consistent with: {answer}? {explanation}")
Techniques Comparison
| Technique | Best For | Complexity |
|---|---|---|
| Zero-shot | Simple tasks, good models | Low |
| Few-shot | Consistent format needed | Low |
| CoT | Math, logic, multi-step | Medium |
| Self-consistency | High-stakes decisions | High |
| ToT | Complex reasoning | High |
| Role prompting | Behavior shaping | Low |
| ReAct | Agent tasks with tools | Medium |
| Prompt chaining | Complex multi-part tasks | Medium |