Lesson 2: Prompt Engineering Mastery
Learn advanced techniques like few-shot prompting and chain-of-thought.
By Bishwambhar Sen • Last updated • 60 min
Prompting is the art of directing an LLM's attention. Instead of retraining massive models, we can change their behavior completely just by structuring our text prompts.
System Prompts
**System Prompts** (also called system instructions) define the character, boundaries, guidelines, and output format of the AI before any user message is sent. They establish high-level behavioral constraints (e.g., *"You are a helpful chemistry tutor who never gives the answer directly but guides the student step-by-step"*).
Few-Shot Prompting
LLMs are excellent at learning patterns on the fly. In a standard "Zero-Shot" prompt, you ask a question directly. In **Few-Shot Prompting**, you provide 2 to 5 examples of input-output pairs inside the prompt. This teaches the model the exact style, tone, and format you expect.
Chain of Thought (CoT)
If you ask an LLM a complex math or logic question directly, it may output a wrong answer because it attempts to predict the final answer immediately. **Chain of Thought** prompting instructs the model to *"think step-by-step"* before outputting the final result. This allows the model to compute intermediate reasoning steps, drastically increasing accuracy.
Anatomy of a Good Prompt
Most weak prompts fail for the same reason: the model has to guess something you never said. A reliable prompt usually states five things explicitly: the role the model should take, the task, the context it needs (the text to work on, the audience, any constraints), the output format, and what to do when the input is unclear. Compare a vague request with a specified one:
# Vague
Summarize this support ticket.
# Specified
You are a support triage assistant.
Read the ticket between the <ticket> tags and return JSON with exactly these keys:
"summary" : one sentence, max 25 words
"category" : one of "billing", "bug", "feature_request", "other"
"urgent" : true only if the customer reports data loss or being locked out
If the ticket is empty or not a support request, return {"category": "other", "summary": "", "urgent": false}.
<ticket>
{ticket_text}
</ticket>Notice the delimiter tags around the untrusted text. They make it clear where your instructions end and the data begins, which makes the prompt easier to maintain and harder to derail with text that happens to look like an instruction.
Building a Few-Shot Prompt in Code
In an application you rarely write a prompt by hand; you assemble it from a template and a list of examples. Keeping the examples as data makes them easy to test, swap and extend. This is the same pattern the coding challenge below asks you to build:
EXAMPLES = [
("The delivery was fast and the packaging was perfect.", "POSITIVE"),
("It stopped working after two days.", "NEGATIVE"),
("Arrived on time, but the box was crushed.", "NEGATIVE"),
]
def build_prompt(text):
lines = ["Classify the sentiment of each review as POSITIVE or NEGATIVE.", ""]
for review, label in EXAMPLES:
lines.append(f'Review: "{review}"')
lines.append(f"Sentiment: {label}")
lines.append("")
lines.append(f'Review: "{text}"')
lines.append("Sentiment:")
return "\n".join(lines)
print(build_prompt("The battery died in 10 minutes."))The prompt ends with Sentiment: so the most natural continuation is the label itself. Examples should cover the cases you care about (including a tricky one, like the mixed review above), keep an identical format, and be balanced across labels. If every example is POSITIVE, the model leans toward POSITIVE.
Chain of Thought in Practice
Chain of thought helps most on multi-step arithmetic, logic and planning questions. Consider: "A shop sells pens at 3 for $2. How much do 12 pens cost?" A step-by-step prompt makes the model work through it: 12 pens is 4 groups of 3, 4 groups at $2 each is $8. Two practical points:
- Ask for the reasoning and then a clearly marked final answer, for example "Think step by step, then give the final answer on a line starting with
ANSWER:". Your code can then parse just that line. - Many recent models can reason internally before answering (often called thinking or reasoning modes). With those, an explicit "think step by step" instruction adds less than it does with plain chat models, so test both versions on your own examples.
Common Pitfalls
- Telling the model what not to do, only."Don't be verbose" is weaker than "answer in at most two sentences". State the target behavior positively and measurably.
- Contradictory instructions.A system prompt that says "be concise" in one place and "explain every step in detail" in another produces inconsistent output. Reread long prompts for conflicts.
- Unrepresentative examples. The model copies surface features of your examples, including length, wording and label order. Vary them.
- Assuming a prompt works because it worked once. Outputs vary between runs. Judge a prompt on a small set of 10 to 20 realistic inputs, including awkward ones, not on a single success.
- Trusting free-form output in code. If a program consumes the answer, request a strict format such as JSON, validate it, and handle the case where it does not parse.
- Treating the system prompt as a security boundary. It guides behavior but a determined user can often talk around it. Never put secrets in it.
Recap
Specify role, task, context, format and edge cases. Use a system prompt for standing rules, few-shot examples to pin down format and tone, and step-by-step reasoning for multi-step problems. Then treat prompts like code: keep them in templates, test them against a fixed set of inputs, and change one thing at a time.
Coding Challenge: Zero-Shot to Few-Shot
Let's simulate a few-shot sentiment analyzer using standard string formatting in Python!
- Create a multi-line string variable named
few_shot_prompt. - Write 2 examples of input-output mapping positive/negative sentiment (e.g.
Input: "Great service!" -> Output: POSITIVE). - Append a new input
Input: "The battery died in 10 minutes." -> Output:and print the prompt.
Verify your structured template in the execution console on the right!