GenAI Course · Module 8 · Session 26

Prompt Fundamentals

⏱ 11 min read🎯 Beginner friendly🖼 2 diagrams🧪 2 live demos🐍 Python code

The same model can give a useless answer or a brilliant one. Very often, the only difference is the prompt. Prompt engineering is the skill of telling an LLM exactly what you want, clearly enough that it gets it right the first time.

In this session

  • Why prompt engineering matters, with a before-and-after example
  • The six elements of an effective prompt
  • System, user and assistant roles
  • Zero-shot and few-shot prompting

01Why prompts matter

An LLM only knows what you put in the prompt (plus what it learned in training). It cannot read your mind about the audience, length, format or rules. Compare these two prompts:

🧵

Like giving instructions to a tailor. “Make me a shirt” gets you a shirt. Your measurements, fabric, colour, collar style and the date you need it get you your shirt.

02Six elements of an effective prompt

You don’t always need all six, but missing ones are the usual reason for bad answers.

A few practical tips:

03System, user and assistant roles

Chat models receive a list of messages, each with a role:

The model reads the whole list every time and writes the next assistant message.
roles.py
# pip install openai   (works with any OpenAI-compatible API)
from openai import OpenAI
client = OpenAI()   # reads OPENAI_API_KEY from the environment

messages = [
    {"role": "system", "content": "You are a polite support assistant for an Indian bank. Answer in at most 2 sentences."},
    {"role": "user", "content": "How long does a NEFT transfer take?"},
]
reply = client.chat.completions.create(model="gpt-4o-mini", messages=messages, temperature=0.2)
print(reply.choices[0].message.content)

04Zero-shot and few-shot prompting

Zero-shot means you describe the task with no examples. Modern LLMs are good at this for common tasks. Few-shot means you include a few input → output examples. This helps a lot when the task has your own labels, a special format, or tricky edge cases.

Zero-shotFew-shot
Examples in the promptNoneUsually 2–8
Best forCommon, well-defined tasksCustom labels, strict formats, edge cases
CostFewer tokensMore tokens per call
Watch outFormat may driftModel may copy the examples too closely; keep them varied and balanced

05Summary

06Quick quiz

1. Where should “always reply in Hindi, max 50 words” go?

2. Few-shot prompting means…

3. Which line helps reduce made-up answers?

4. Why wrap user data in tags like <sms>…</sms>?

5. “Reply as JSON with label and reason” is which element?

07References

Created by Ajeetkumar