Prompt anatomy
A reliable prompt has a task, context, an example and a format
Role, task, context, examples, fenced input, output format.
A prompt is the only thing the model knows about your situation. Build it in parts. A role, so it knows its stance. One clear task. The context it cannot guess, like what urgent means here. An example of a good answer. The input itself, fenced in tags so the model cannot mistake data for instructions. And the exact output format your code expects.
Prompt anatomy · Going deeper
Prompting techniques and when to use each
Add examples, steps or chaining only where the plain prompt fails.
System prompt and messages
The system prompt sets the rules; messages carry the chat
System for standing rules; user and assistant turns resent every time.
Chat models take a list of messages, each with a role. The system prompt is written by the developer. It sets rules that apply to every turn: who the assistant is, what it may and may not do, how long to answer. Then come the user’s messages and the assistant’s earlier replies. The model remembers nothing between calls, so the app sends the whole list each time.
API differences · Going deeper
Same three parts in every chat API, under different field names
System prompt, turns and an output cap: check each API's field names when porting.
Few-shot prompting
Examples teach the format faster than any description
Zero-shot: instruction only. Few-shot: a few worked examples first.
On the left, a zero-shot prompt: just the instruction. The model answers in its own way, a paragraph about mixed feelings. On the right, three short examples come first, each with its label. Now the model copies the pattern exactly, and answers in one word. Examples carry the format, the allowed labels, and the judgement on borderline cases, all at once.
Step-by-step reasoning
Asking for the steps first improves multi-step answers
Write the reasoning first, then the answer. It costs tokens; use it where steps matter.
Some questions need several steps. Asked for the answer at once, the model jumps, and here gets it wrong. Asked to work step by step, it writes each step down, and each written step becomes context for the next. It reaches the right total. Reasoning models do this on their own, inside a thinking phase, at the cost of more tokens and more time.
Structured output
When code reads the answer, ask for JSON against a schema
A schema fixes fields and types. Validate before acting on the values.
When a program, not a person, reads the answer, free text is fragile. Send a schema: the fields, their types, the allowed values. Most model APIs can enforce it, so the reply always parses. Then validate it anyway: is the topic one of the three allowed, are both fields there? If not, retry once with the error message, and flag it for a person if it fails again.
Validate and retry · Going deeper
Five ways a JSON reply fails, and the loop that catches them
Parse, validate, retry once with the error, then flag for review.
Prompt failures
Most bad answers trace back to the prompt
Fix the prompt first: clear task, needed context, one priority, a format.
When an answer is bad, look at the prompt before blaming the model. A vague task gets a generic answer. Missing facts get filled from general knowledge, or invented. Conflicting rules get one of them ignored. And text inside the data that looks like an instruction may be obeyed, which is a security problem we come back to in lesson twelve.
Testing prompts
Version prompts like code and test every change
A fixed test set, a score per version, keep what improves it.
A prompt that looks good in three tries can fail on the fourth. So keep fifty real inputs with known right answers, and score every version of the prompt against all of them. The first draft gets sixty four percent. A schema lifts it to seventy eight. Three examples, one of them hard, to ninety. Letting the model say unsure for unclear cases, to ninety four. Run the same set when the model version changes, too.
Test set size · Going deeper
Fifty test cases cannot tell 90% from 94%
Put a confidence interval on every score; close versions need hundreds of cases.
Lesson complete
You finished “Prompting”
Nicely done.