Sub topic 4
Writing Prompts That Give Reliable Results
This chapter assumes you can write basic Python and know that a model call takes text in and returns text out. Earlier chapters covered how a model generates text and where it fails. Here you will learn to write prompts that fail less often.
Parts of a reliable prompt
A prompt is the full text you send to the model. Reliable prompts usually contain these parts, in this order:
- Task: one sentence saying what to do.
- Context: only the facts the model needs.
- Constraints: allowed values, length limits, and what to do when unsure.
- Output format: the exact shape of the reply.
- Examples: two or three input and output pairs (few-shot prompting).
The model samples its reply from patterns in the prompt. Vague wording gives wide variation. Specific wording narrows it.
A template you can reuse
Keep the prompt in a function, not scattered strings. Then you can version it and test it.
LABELS = ["billing", "bug", "feature_request", "other"]
EXAMPLES = [
("I was charged twice this month.", "billing"),
("The export button crashes the app.", "bug"),
]
def build_prompt(ticket: str) -> str:
lines = [
"Classify the support ticket into exactly one label from: "
+ ", ".join(LABELS) + ".",
"If none fits, answer other.",
"Reply with the label only.",
"",
]
for text, label in EXAMPLES:
lines.append(f"Ticket: {text}\nLabel: {label}\n")
lines.append(f"Ticket: {ticket}\nLabel:")
return "\n".join(lines)
def classify(ticket: str, complete) -> str:
answer = complete(build_prompt(ticket)).strip().lower()
return answer if answer in LABELS else "other"
# A stand-in for a real model call, so the code runs as shown.
def fake_complete(prompt: str) -> str:
return " Bug \n"
print(classify("The app freezes on login.", fake_complete))The last line of the prompt ends with Label:. This cues the model to continue with just a label. The code checks the answer against LABELS, because the prompt only makes a good reply likely. It does not guarantee one.
Common errors
| Symptom | Likely cause | Fix |
|---|---|---|
| Extra explanation around the answer | No format instruction | Say "Reply with the label only" |
| Labels you did not define | Open-ended task | List the allowed values and add a fallback |
| Output changes between runs | High sampling temperature, vague wording | Lower the temperature, tighten the wording |
| Examples copied into answers | Examples too similar to each other | Use varied examples |
| Ignored instruction | Buried in a long prompt | Move it near the end or repeat it briefly |
Check prompts against test cases
A prompt that works on one input proves little. Keep a small list of inputs with the expected labels. Run it after every edit.
CASES = [("I need a refund", "billing"), ("Please add dark mode", "feature_request")]
def accuracy(classify_fn) -> float:
hits = sum(classify_fn(text) == expected for text, expected in CASES)
return hits / len(CASES)
print(accuracy(lambda text: "billing"))This gives you a number to compare between prompt versions. A later chapter covers evaluation in depth.
Exercise
Your classifier sometimes replies "Label: bug" instead of "bug", and your code maps it to "other". Name two fixes, one in the prompt and one in the code.
Show answer
In the prompt, end with "Label:" and add examples that show the bare label. In the code, normalize the reply before checking it, for example by removing a "label:" prefix and stripping whitespace. Keep the allow-list check as the final guard.