By the end of this lesson you will be able to:
- Explain what a prompt is and why the same request can give very different results.
- Build prompts from five parts: role, task, context, examples and format.
- Use zero-shot and few-shot prompting, delimiters and templates.
- Structure system and user messages the way chat APIs expect.
What is a prompt?
A prompt is everything you send to the model: your instructions, any text you want it to work on, examples, and the conversation so far. Prompt engineering is the skill of designing that input so the model reliably gives you what you need.
Remember Lesson 1: the model predicts the most likely continuation of your text. A vague prompt has many likely continuations, so you get generic, unpredictable answers. A precise prompt narrows the possibilities to the answer you want.
Vague prompt
Write about Diwali sale.
Generic copy, random length, wrong tone, no shop details.
Engineered prompt
You are a copywriter for small Indian shops.
Write a WhatsApp message for Sri Lakshmi
Electronics' Diwali sale (20% off LED TVs,
25 Oct–5 Nov). Under 60 words, 2 emojis,
end with the shop address.
Specific, usable, on-brand, the right length.
The five parts of a strong prompt
- Role: who the model should act as. "You are a patient maths teacher."
- Task: the instruction, with a clear action verb. "Explain", "Summarise", "List", "Classify".
- Context: the facts the model needs and can't guess: audience, details, constraints.
- Examples: a sample of the output you want, so the model copies the style.
- Format: length, structure, tone, language, or an exact output format like JSON.
Switch the parts on and off below. Watch how the prompt grows and how the kind of reply you would get changes.
You don't always need all five. For a quick question, the task alone is fine. Add parts when the answer isn't what you need. The more the output matters (a customer message, data your code will read), the more parts you add.
Zero-shot, one-shot and few-shot
How many examples you include has its own name:
- Zero-shot: no examples, just the instruction. Works well for common tasks.
- One-shot: one example of input and output.
- Few-shot: several examples. Best when you need a specific format, style or labelling scheme.
Few-shot classification of student doubts:
Classify each student message as STUDY, ADMIN or OFF_TOPIC.
Message: "Why does ice float on water?"
Label: STUDY
Message: "When is the fee payment deadline?"
Label: ADMIN
Message: "Who will win the IPL this year?"
Label: OFF_TOPIC
Message: "What is the difference between speed and velocity?"
Label:
The model sees the pattern and continues it with STUDY. It's still next-token prediction, steered by examples.
Practical techniques
Use delimiters for the content
When the prompt contains text to work on (an essay or a chapter), separate it from your instructions with clear markers like triple quotes, ### or XML-style tags. This stops the model confusing your content with your instructions, and makes some prompt-injection attacks harder.
Summarise the student's essay in 3 bullet points for the teacher.
<essay>
...student's essay here...
</essay>
Say what to do, not only what to avoid
"Don't use difficult words" is weaker than "Use words a Class 6 student knows. Explain any technical term in brackets."
Specify the output format
If your code will read the answer, ask for an exact structure, for example "Reply only with JSON: {"topic": string, "difficulty": "easy"|"medium"|"hard"}". Lesson 5 goes deeper.
Use cues to start the answer
Ending a prompt with the beginning of the answer, such as Key points:\n1., nudges the model straight into the format you want.
Iterate
Prompt engineering is experimental. Write, test on several inputs, look at what goes wrong, adjust, and test again.
- v1: "Explain gravity." → a long, college-level answer. Too hard.
- v2: + "for a Class 6 student" → simpler, but 400 words. Too long.
- v3: + "in under 80 words" → good length, but abstract.
- v4: + "use one everyday Indian example" → mentions a mango falling from a tree. Clear and memorable.
- Result: four small edits turned a generic answer into one a 12-year-old actually understands.
System messages and chat structure
Chat APIs (OpenAI, Azure OpenAI, Anthropic and others) take a list of messages, each with a role:
- system: the app's standing instructions, the role and rules. The user usually doesn't see it.
- user: what the person typed.
- assistant: the model's earlier replies. Sending them back gives the model memory of the conversation.
messages = [
{"role": "system", "content": "You are Vidya, a friendly tutor for Class 6–12 students..."},
{"role": "user", "content": "What is a prime number?"},
{"role": "assistant", "content": "A prime number has exactly two factors: 1 and itself..."},
{"role": "user", "content": "Is 1 a prime number?"}
]
Put stable rules in the system message and the changing request in the user message. You will use exactly this structure when we build chat apps in Lesson 7.
Hands-on: prompt templates in Python
Real apps don't hand-write each prompt. They use a template with blanks filled in from the user's request and the app's data.
Your task: complete the build_prompt() template so it includes the role, the task, the context and a Format line. Call it to explain "photosynthesis" to a Class 7 student in exactly 3 bullet points. The output must contain Format: Exactly 3 bullet points.
def build_prompt(topic, grade, bullets):
role = "You are a patient science teacher for Indian school students."
task = f"Explain {topic} to a Class {grade} student."
context = "Use simple words and one everyday Indian example."
# TODO: add a format line like "Format: Exactly 3 bullet points, each under 20 words."
fmt = ""
return "\n".join(part for part in [role, task, context, fmt] if part)
prompt = build_prompt("photosynthesis", 7, 3)
print(prompt)
print("---")
print("Characters:", len(prompt), "| about", len(prompt) // 4, "tokens")
Show solution
fmt = f"Format: Exactly {bullets} bullet points, each under 20 words."
Your task: build the messages list for a chat API: one system message with the tutor's rules and one user message with the student's question wrapped in <question> tags. Then print it as JSON. This is the exact payload shape you'll send in Lesson 7.
import json
system_rules = "You are Vidya, a friendly tutor for Class 6-12 students. Explain step by step. If unsure, say so."
question = "Why is the sky blue?"
# TODO: create a list with two dicts: a "system" message and a "user" message
messages = []
print(json.dumps({"model": "your-model-name", "temperature": 0.3, "messages": messages}, indent=2))
Show solution
messages = [
{"role": "system", "content": system_rules},
{"role": "user", "content": f"<question>{question}</question>"},
]
Check your understanding
Which part of a prompt tells the model "under 60 words, as a numbered list"?
- Role
- Context
- Format
- Examples
Length, structure and output shape belong in the format part.
You show the model three labelled examples before the real input. This is…
- Zero-shot prompting
- Few-shot prompting
- Fine-tuning
- Temperature scaling
Including several examples in the prompt is few-shot prompting. The model's weights don't change, unlike in fine-tuning.
Why wrap a student's essay in tags like <essay>…</essay>?
- So the model separates content from instructions
- It makes the answer shorter
- Models can only read tagged text
- It lowers the cost
Delimiters mark exactly what is data and what are instructions. That improves accuracy and makes injection harder.
Where should your app's permanent rules (scope, tone, safety) go?
- In every user message
- In the assistant message
- Nowhere, the model knows them
- In the system message
The system message holds standing instructions. User messages carry each new request.
Key takeaways
- A prompt is everything the model sees, and precision narrows the answers to what you want.
- Build with role, task, context, examples and format, adding parts as the stakes rise.
- Use few-shot examples for formats and labels, and delimiters for content.
- Chat APIs use system, user and assistant messages. Keep rules in the system message.
- Iterate: test, look at the failures, refine.
Next: advanced prompting, including chain-of-thought, self-checking and reliable structured output.
Your progress is saved in this browser.
Adapted from Lesson 4 of Microsoft's "Generative AI for Beginners" (MIT License), rewritten and extended by Aiprojects4U. View source