Module 19 — Prompt Engineering
Introduction
Prompt Engineering is the practice of designing effective instructions for Large Language Models (LLMs) to improve the quality, accuracy, and reliability of their responses.
A prompt is more than just a question—it defines:
- The task
- The context
- The expected output
- The response format
- The reasoning process
- The constraints
Modern AI systems such as chatbots, coding assistants, document analyzers, and AI agents rely heavily on well-designed prompts.
In this module, you'll learn:
- Zero-shot Prompting
- One-shot Prompting
- Few-shot Prompting
- Chain of Thought (CoT)
- Self Consistency
- Tree of Thoughts (ToT)
- ReAct
- Structured Prompting
- Prompt Templates
- System Prompts
- Build an AI Assistant
How Prompt Engineering Works
A prompt guides the model toward a desired behavior.
1 User Prompt 2 │ 3 Instructions + Context 4 │ 5 Large Language Model 6 │ 7 Reasoning & Generation 8 │ 9 Final Response
A well-structured prompt reduces ambiguity and improves response quality.
Anatomy of a Good Prompt
A high-quality prompt usually contains:
1Role 2↓ 3 4Task 5 6↓ 7 8Context 9 10↓ 11 12Constraints 13 14↓ 15 16Expected Output
Example
1Role: 2You are a Python expert. 3 4Task: 5Explain decorators. 6 7Constraints: 8Use beginner-friendly language. 9 10Output: 11Step-by-step explanation with examples.
1. Zero-shot Prompting
What is Zero-shot Prompting?
Zero-shot prompting asks the model to perform a task without providing any examples.
Example
1Translate the following sentence into French. 2 3I love machine learning.
The model relies entirely on its pretrained knowledge.
Advantages
- Simple
- Fast
- Minimal prompt size
Limitations
- May produce inconsistent formatting
- Less reliable for specialized tasks
Python Example
1from transformers import pipeline 2 3generator = pipeline( 4 task="text-generation", 5 model="gpt2" 6) 7 8prompt = "Explain what a Transformer model is." 9 10result = generator( 11 prompt, 12 max_new_tokens=80 13) 14 15print(result[0]["generated_text"])
2. One-shot Prompting
What is One-shot Prompting?
One-shot prompting provides one example before the actual task.
Example
1Example 2 3Input: 4Apple 5 6Output: 7Fruit 8 9Now classify: 10 11Carrot
The model learns the desired format from a single demonstration.
Advantages
- Better formatting
- Improved consistency
- Easy to design
3. Few-shot Prompting
What is Few-shot Prompting?
Few-shot prompting provides multiple examples.
Example
1Positive → Great product 2 3Negative → Poor quality 4 5Positive → Excellent service 6 7Now classify: 8 9Amazing experience
Advantages
- Better task adaptation
- Improved accuracy
- More consistent outputs
4. Chain of Thought (CoT)
What is Chain of Thought?
Chain of Thought encourages the model to reason step by step before producing a final answer.
Conceptually, this technique decomposes complex problems into intermediate reasoning steps.
Example prompt
1Solve the following math problem. 2 3Explain your reasoning step by step before giving the final answer.
Advantages
- Better reasoning
- Improved performance on complex tasks
- More interpretable intermediate logic
Note: When using hosted reasoning models, you generally don't need to ask for internal reasoning. It's often better to ask for a concise explanation of the solution rather than the model's hidden reasoning process.
5. Self Consistency
What is Self Consistency?
Self Consistency improves reasoning by generating multiple candidate solutions and selecting the most consistent final answer.
Workflow
1Question 2 3↓ 4 5Reasoning Path 1 6 7Reasoning Path 2 8 9Reasoning Path 3 10 11↓ 12 13Select Most Consistent Answer
Advantages
- Higher accuracy
- More robust reasoning
- Reduces single-path errors
6. Tree of Thoughts (ToT)
What is Tree of Thoughts?
Tree of Thoughts extends linear reasoning into a tree of alternative solution paths.
Architecture
1Problem 2 3├── Thought A 4 5│ ├── A1 6 7│ └── A2 8 9├── Thought B 10 11│ ├── B1 12 13│ └── B2 14 15└── Best Solution
Advantages
- Explores multiple strategies
- Better planning
- Strong performance on search and puzzle tasks
7. ReAct (Reason + Act)
What is ReAct?
ReAct combines reasoning with tool usage.
Instead of answering immediately, the model can decide to use external tools, observe the results, and continue.
Workflow
1Question 2 3↓ 4 5Reason 6 7↓ 8 9Use Tool 10 11↓ 12 13Observe 14 15↓ 16 17Reason Again 18 19↓ 20 21Answer
Applications
- AI agents
- Search systems
- Retrieval-Augmented Generation (RAG)
- Database queries
- API integrations
Example
1User: 2What is today's weather in Tokyo? 3 4Reason: 5I need current weather information. 6 7Action: 8Call weather API. 9 10Observation: 1122°C and cloudy. 12 13Final Answer: 14Today's weather in Tokyo is 22°C and cloudy.
8. Structured Prompting
What is Structured Prompting?
Structured prompting organizes instructions into clearly defined sections.
Template
1Role: 2... 3 4Objective: 5... 6 7Context: 8... 9 10Constraints: 11... 12 13Output Format: 14...
Advantages
- Better consistency
- Easier debugging
- More predictable outputs
9. Prompt Templates
Prompt templates make prompts reusable by replacing values with variables.
Example
1You are a {role}. 2 3Task: 4{task} 5 6Context: 7{context} 8 9Output: 10{format}
Python Example
1template = """ 2You are a {role}. 3 4Task: 5{task} 6 7Context: 8{context} 9 10Output Format: 11{format} 12""" 13 14prompt = template.format( 15 role="Python Expert", 16 task="Explain decorators", 17 context="Audience is beginners", 18 format="Markdown" 19) 20 21print(prompt)
Advantages
- Reusable
- Dynamic
- Easy to automate
10. System Prompts
What are System Prompts?
A system prompt defines the model's overall behavior, personality, and constraints before user messages are processed.
Example
1You are an experienced software engineer. 2 3Always provide Python examples. 4 5Use beginner-friendly explanations. 6 7Avoid unnecessary jargon.
System prompts help maintain consistent behavior across an entire conversation.
Prompting Strategy Comparison
| Technique | Uses Examples | Best For |
|---|---|---|
| Zero-shot | No | Simple tasks |
| One-shot | One | Formatting and style |
| Few-shot | Multiple | Classification, extraction |
| Chain of Thought | Optional | Complex reasoning |
| Self Consistency | Multiple reasoning paths | Higher reasoning reliability |
| Tree of Thoughts | Multiple branches | Planning and search |
| ReAct | Tool use | Agents and RAG |
| Structured Prompting | Structured sections | Production applications |
| Prompt Templates | Variables | Reusable workflows |
| System Prompts | Persistent instructions | Chat assistants |
Practice 1 — Zero-shot vs Few-shot
1from transformers import pipeline 2 3generator = pipeline( 4 "text-generation", 5 model="gpt2" 6) 7 8zero_prompt = """ 9Classify: 10 11The movie was fantastic. 12""" 13 14few_prompt = """ 15Positive → Great 16 17Negative → Bad 18 19Positive → Amazing 20 21Classify: 22 23The movie was fantastic. 24""" 25 26print(generator( 27 zero_prompt, 28 max_new_tokens=40 29)[0]["generated_text"]) 30 31print(generator( 32 few_prompt, 33 max_new_tokens=40 34)[0]["generated_text"])
What You'll Learn
- Compare zero-shot and few-shot prompting.
- Observe how examples influence the model's responses.
- Understand when demonstrations improve performance.
Practice 2 — Structured Prompt Template
1template = """ 2Role: 3{role} 4 5Task: 6{task} 7 8Context: 9{context} 10 11Constraints: 12{constraints} 13 14Output Format: 15{format} 16""" 17 18prompt = template.format( 19 role="AI Tutor", 20 task="Explain Transformers", 21 context="Audience: Beginners", 22 constraints="Use simple language", 23 format="Markdown" 24) 25 26print(prompt)
What You'll Learn
- Build reusable prompt templates.
- Separate context, task, and constraints.
- Create prompts suitable for production systems.
Mini Project — Build an AI Assistant
1from transformers import pipeline 2 3assistant = pipeline( 4 task="text-generation", 5 model="gpt2" 6) 7 8system_prompt = """ 9You are an AI programming assistant. 10 11Rules: 12- Answer clearly. 13- Provide Python code when appropriate. 14- Explain concepts step by step. 15- Keep responses concise. 16""" 17 18user_question = """ 19How do I reverse a list in Python? 20""" 21 22prompt = system_prompt + "\n\nUser:\n" + user_question + "\n\nAssistant:" 23 24response = assistant( 25 prompt, 26 max_new_tokens=120, 27 temperature=0.7 28) 29 30print(response[0]["generated_text"])
What You'll Learn
- Combine a system prompt with user input.
- Define consistent assistant behavior.
- Generate structured responses using a reusable prompt template.
Best Practices for Prompt Engineering
| Recommendation | Benefit |
|---|---|
| Be explicit about the task | Reduces ambiguity |
| Provide relevant context | Improves accuracy |
| Specify output format | Produces consistent responses |
| Add constraints | Prevents unwanted behavior |
| Use examples when needed | Improves task performance |
| Reuse prompt templates | Simplifies automation |
| Use system prompts for persistent behavior | Consistent assistant responses |
Module Summary
After completing this module, you will be able to:
- Explain the purpose and importance of prompt engineering.
- Distinguish between zero-shot, one-shot, and few-shot prompting.
- Understand the concepts behind Chain of Thought, Self Consistency, Tree of Thoughts, and ReAct.
- Design structured prompts with clear roles, tasks, context, constraints, and output formats.
- Create reusable prompt templates for different applications.
- Write effective system prompts that define assistant behavior.
- Build a simple AI assistant using prompt templates and Hugging Face Transformers.
- Choose the most appropriate prompting strategy based on the task and desired level of reasoning or tool use.
Next Module: Module 20 – Fine-Tuning & PEFT, where you'll learn transfer learning, Hugging Face Datasets, LoRA, QLoRA, PEFT, supervised fine-tuning (SFT), quantization, model evaluation, and deployment of custom LLMs.