Prompt Engineering
Prompt engineering is the skill of writing instructions that get accurate, useful and consistent results from AI models. The same model can give a vague answer or an excellent one depending on how you ask. Good prompts are clear, give context, show what you want, and are refined through testing.
1. Be clear and specific
A model can't read your mind. Say exactly what you want: the task, the audience, the length and the format.
Weak: Write about M-Pesa.
Better: Write a 150-word explanation of how M-Pesa changed everyday payments
in Kenya, for a Form Four student who has never studied economics.
Use simple language and end with one discussion question.
Ask yourself: if I gave this instruction to a capable new colleague with no background, would they know what to do?
2. Give context
Explain the situation, the goal and any constraints. Context helps the model make the right choices.
- Who it's for: "The reader is a small-scale farmer in Kakamega."
- Why: "This will be read aloud on community radio, so avoid bullet points."
- Source material: paste the document you want summarised, and clearly mark where it starts and ends, for example with tags like <report> and </report>.
- A role can help set tone and expertise: "You are an experienced high school chemistry teacher."
3. Show examples (few-shot prompting)
Examples are one of the most powerful tools you have. Showing two or three input-output pairs often works better than a long description:
Classify each customer message as BILLING, NETWORK or OTHER. Message: "I was charged twice for my data bundle." Category: BILLING Message: "No signal in Kitengela since morning." Category: NETWORK Message: "My airtime balance is wrong after topping up." Category:
Use examples that cover different cases, including tricky ones, so the model doesn't just copy one pattern.
4. Ask for structured output
If a program will use the answer, ask for a fixed format such as JSON, and describe the exact fields:
Read the review below and reply with only a JSON object with these keys:
"sentiment" ("positive", "negative" or "mixed"), "topic" (one word),
"urgent" (true or false).
<review>Delivery took 9 days and nobody answered my calls.</review>
Then parse and check the reply in code rather than trusting it blindly:
import json
reply = '{"sentiment": "negative", "topic": "delivery", "urgent": true}'
data = json.loads(reply)
required = {"sentiment", "topic", "urgent"}
if required.issubset(data) and data["sentiment"] in {"positive", "negative", "mixed"}:
print("Valid:", data["sentiment"], data["topic"], data["urgent"])
else:
print("Invalid reply, ask the model again")
Note that JSON's true becomes Python's True. Many AI APIs also offer a structured-output or JSON mode that enforces a schema. See Working with CSV & JSON.
5. Break down complex tasks
- Ask the model to think step by step before answering maths, logic or planning questions, or to list its assumptions first.
- Split big jobs into a chain: first extract the key facts from a report, then, in a second prompt, write the summary from those facts.
- Say what to do when unsure: "If the document doesn't contain the answer, say so instead of guessing." This reduces hallucination.
- Tell the model what to do, not only what to avoid: "Write in plain paragraphs" works better than "Don't use markdown."
6. Iterate and test
Prompting is experimental. Write a first version, try it on several realistic inputs, look at where it fails, and adjust one thing at a time. For a prompt that will be used repeatedly in an app, keep a small set of test inputs with the answers you expect, and re-run them after every change, much like a test set in machine learning.
Wanjiru builds a WhatsApp bot that answers questions about her county's bursary process. Version 1 ("Answer questions about bursaries") invents deadlines. Version 2 includes the official guidelines and says "Use only the guidelines below. If the answer isn't there, tell the user to contact the ward office." Testing with 20 real questions shows it now declines when unsure. Version 3 adds two example replies to fix the tone, and asks for answers under 80 words for easy reading on a phone.
Prompt injection: if your app feeds untrusted text (emails, web pages, user uploads) into a prompt, that text may contain instructions such as "ignore previous instructions". Treat model output as untrusted, never give a model unchecked power to send money or delete data, and validate outputs in code.
Believing a good prompt guarantees correct facts. Prompting reduces errors but can't remove them. Check important outputs, and for questions about your own documents, provide them in the prompt or use retrieval-augmented generation.
Key points
- Be specific about the task, audience, length and format.
- Give context and clearly marked source material.
- Use a few varied examples to show the pattern you want.
- Request structured output and validate it in code; iterate with test cases.
Tip: use the ← and → keys to move between pages.
