Introduction
A language model continues the text it is given, so the way you write that text decides what you get back. Prompt engineering is the craft of writing prompts that make a model do the job reliably. It needs no special tools: you describe the task clearly, supply what the model needs to know, and say what the answer should look like. Because a prompt is just text, you can build it in code, test it, and improve it step by step, the same way you improve a program.
How it works
A strong prompt usually has five parts, though not every task needs all of them:
- Role: who the model should act as, for example "You label customer reviews".
- Task: the one thing to do, written as a clear instruction.
- Context: the facts the model needs, such as a document, a customer record or the rules to follow.
- Format: how the answer must look: "Answer with one word", "Reply as JSON", "Use three bullet points".
- Examples: sample inputs with the correct outputs.
Asking with instructions alone is zero-shot prompting. Adding a few worked examples is few-shot prompting. Examples show the exact format and style, so a few of them often work better than a long explanation. Their cost is length: every example uses part of the context window (the tokens lesson) and makes each request slower and dearer. So choose a few short, varied examples that match the real inputs.
In code, a prompt is built from a template: a fixed text with placeholders such as {review} that are filled in for each request. This keeps the wording the same every time and lets you change it in one place. Chat models take a list of messages, each with a role: the system message sets the rules, the user message holds the request, and the assistant messages hold earlier replies.
Real chat code looks like the lines below (shown, not run, because it needs an account and a network):
reply = client.chat(model="some-model", messages=messages)
print(reply.text)
Finally, a prompt is a guess about what works. The only way to know is to run it on a fixed set of test cases whose right answers you know, count the correct ones, and change one thing at a time.
Example
template = (
"You label customer reviews as positive or negative.\n"
"Answer with one word.\n\n"
"{shots}"
"Review: {review}\n"
"Label:"
)
shots = [
("Fast delivery and great quality.", "positive"),
("It broke after two days.", "negative"),
]
def build(review, examples):
text = ""
for example, label in examples:
text += f"Review: {example}\nLabel: {label}\n\n"
return template.format(shots=text, review=review)
zero_shot = build("The battery lasts all day.", [])
few_shot = build("The battery lasts all day.", shots)
print(zero_shot)
print("=" * 20)
print(few_shot)
print("=" * 20)
print("words:", len(zero_shot.split()), "zero-shot,", len(few_shot.split()), "few-shot")
# Chat models take a list of messages; the system message sets the rules
messages = [
{"role": "system", "content": "You label customer reviews. Answer with one word."},
{"role": "user", "content": "Review: The battery lasts all day."},
]
for m in messages:
print(f"{m['role']:>6}: {m['content']}")Line by line
templateholds the role, the task, the format rule and two placeholders:{shots}for the examples and{review}for the new input.shotslists two worked examples, each a review and its label.buildwrites each example in the same layout as the final question, then fills the template. Using one layout everywhere lets the model see the pattern.- The zero-shot prompt passes no examples, so
{shots}is empty. The few-shot prompt passes both. - The last lines count words: the examples nearly double the prompt, which is the price of few-shot prompting.
messagesshows the same task as a chat: a system message with the rules and a user message with the input.
Expected result
You label customer reviews as positive or negative.
Answer with one word.
Review: The battery lasts all day.
Label:
====================
You label customer reviews as positive or negative.
Answer with one word.
Review: Fast delivery and great quality.
Label: positive
Review: It broke after two days.
Label: negative
Review: The battery lasts all day.
Label:
====================
words: 19 zero-shot, 35 few-shot
system: You label customer reviews. Answer with one word.
user: Review: The battery lasts all day.
In the real world
Companies keep their prompts in version control and fill them from templates, so a support bot, a summarizer or a code assistant behaves the same for every customer. Teams keep a small test set of real questions and rerun it after every prompt change, much as they rerun unit tests after a code change. The system message is also where products place their rules, such as the tone to use and the topics to avoid.
Common mistakes
- Vague tasks: "Make this better" leaves the model to guess what better means.
- No format rule, so the answer comes back as a paragraph when the code needed one word.
- Examples that all look alike, which teaches the model a narrow pattern.
- Changing several things at once, so you cannot tell which change helped.
- Trusting one lucky answer instead of testing many cases.
Best practices
- State the task, the format and the limits in plain, short sentences.
- Keep the layout of the examples identical to the layout of the real question.
- Build prompts from a template, and keep the template in one place.
- Test on a fixed set of cases, and change one thing per test.
Summary
A prompt can carry a role, a task, context, a format rule and examples. Zero-shot prompts use instructions alone; few-shot prompts add worked examples that show the format at the cost of context space. Code fills templates with placeholders for each request, chat models separate system, user and assistant messages, and a prompt is only trusted after it passes a fixed set of tests.
Quick check
Key takeaways
- A good prompt states the task, gives the needed context, and says exactly what format the answer must have.
- Examples in the prompt (few-shot) often fix the format and style better than more explanation, but they cost context-window space.
- Treat prompts like code: build them from templates and test them on a fixed set of cases.
Practice problems for this lesson
My notes
Write down what you want to remember from this lesson. Notes are saved on this device as you type, and appear on your dashboard.