Sub topic 4
Start with prompting
This chapter assumes you have called a chat-style LLM API, can read Python, and know what a context window is. Prompting is the first lever to pull because it changes only the input. It needs no new data, no index, and no training run, and you can undo any change in seconds.
What prompting changes
A prompt is everything you send to the model: the system message, earlier turns, and the user's question. The model's weights stay fixed. You steer behavior by changing what the model reads before it answers.
Prompting fixes behavior problems such as the wrong format, the wrong tone, or ignored constraints. It cannot supply facts the model never saw. If the model lacks the knowledge, you need retrieval, which a later chapter covers.
A working prompt structure
A reliable prompt has four parts:
- Role and task. One sentence saying what the model does.
- Constraints. The allowed outputs, length limits, and what to do when unsure.
- Examples. Optional input and output pairs that show the pattern.
- The input. The actual question, placed last.
The following function builds a ticket-classification prompt as a chat message list. Each example becomes a user turn followed by an assistant turn, so the model sees the exact output it should copy.
def build_messages(question, examples):
messages = [{
"role": "system",
"content": "Classify the support ticket as billing, bug, or other. Reply with one word.",
}]
for text, label in examples:
messages.append({"role": "user", "content": text})
messages.append({"role": "assistant", "content": label})
messages.append({"role": "user", "content": question})
return messages
examples = [
("I was charged twice this month.", "billing"),
("The export button crashes the app.", "bug"),
]
messages = build_messages("My invoice shows the wrong amount.", examples)The system message sets the task and the output limit. The two examples show the format. The real question comes last, so it is the freshest thing the model reads.
The iteration loop
Prompt work is an experiment loop, not guesswork. Change one thing at a time and score the result against your evaluation set.
Tactics that tend to work
- Be specific about the output. "Reply with one word" beats "be brief."
- State the fallback. Say what to do when the input fits no category, or the model will guess.
- Use delimiters. Wrap user-supplied text in markers such as triple quotes, so instructions and data stay separate.
- Show, don't only tell. Two or three varied examples often beat a paragraph of rules.
- Ask for structure. Request JSON with named fields, and validate it in code.
Common errors
- Changing several things at once. You cannot tell which edit helped.
- Judging by one or two outputs. Model output varies. Use the evaluation set, and lower the temperature if you need steadier results.
- Examples that are too similar. The model copies their surface features. Cover the real range of inputs, including edge cases.
- Unbalanced examples. If most examples have one label, the model leans toward that label.
- Piling on rules. Long prompts full of contradicting instructions get followed inconsistently. Remove rules before adding more.
- Expecting prompts to add knowledge. A confident wrong answer about your private data is a knowledge gap, not a wording problem.
When to stop prompting
Move on when failures share one cause that wording cannot fix. Facts the model cannot know point to RAG. A style or format the model drifts from, even with clear instructions and good examples, points to fine-tuning. The long prompt itself can also be the problem: many examples cost tokens on every call and add latency.
Exercise
Your prompt classifies tickets correctly 70% of the time on your evaluation set. You rewrite the instructions, add three examples, and lower the temperature in a single edit, and the score rises to 78%. What is wrong with this process, and what should you do instead?
Show answer
Three changes were made at once, so you do not know which one helped or whether one hurt. Revert to the baseline and apply each change separately, scoring on the evaluation set after each one. Keep the changes that improve the score.
Exercise
A prompt asks a model about your company's 2026 refund policy, which is not in its training data. Several rewrites still give wrong answers. Which lever should you consider next, and why?
Show answer
RAG. The failures come from missing knowledge, not wrong behavior, and no wording can supply facts the model has not seen. Retrieval can place the policy text in the prompt at query time.