Foundations 3 min

Prompt Engineering Basics

Craft prompts that get consistent, high-quality results from any language model.

You now understand how the model works: it predicts the next token based on the probability distributions learned during training. That means the quality of its output is almost entirely determined by the quality of its input - the prompt. Prompt engineering is the discipline of crafting that input deliberately. It is not magic, and it is not vague. It is applied knowledge of how the model processes context.

Briefing a brilliant contractor who just joined the project

Imagine hiring the world's best contractor for a renovation. They are extraordinarily skilled but they just walked in the door. They know nothing about your house, your timeline, your taste, or your constraints. If you say 'do the kitchen,' you might get a sleek modern look when you wanted farmhouse style. You might get marble counters when you have a tight budget. You might get it done in two weeks when you needed it in one. A good brief changes everything. 'I want a farmhouse-style kitchen renovation. Budget: $15K. Must be complete in 10 days. Prioritise counters and cabinets. Do not touch the plumbing.' Prompt engineering is writing that brief - every time, for every task.

The 5-part prompt structure that works
Role: Who the model should behave as. 'You are a senior Python engineer reviewing a pull request.'

Task: What it needs to do, precisely. 'Identify any security vulnerabilities in this code.'

Context: Background information it needs. Paste the relevant code, document, or data here.

Constraints: What it must or must not do. 'Focus only on authentication logic. Do not suggest refactors. Use OWASP terminology.'

Output format: Exactly what you want back. 'Return a numbered list. Each item: vulnerability name, line number, severity (High/Medium/Low), one-sentence explanation.'

Few-shot examples: the most underused lever. The fastest way to get consistent, high-quality output is to show the model an example of exactly what you want. This is called few-shot prompting. Instead of describing the output format in words, you demonstrate it. Models pick up on the pattern and replicate it with remarkable fidelity.

If you are building a system that classifies customer emails, do not just describe the categories - show two or three correctly classified examples before the actual email. Output quality will jump significantly.

python
# Few-shot prompting: show examples before the real task
import google.generativeai as genai

genai.configure(api_key="YOUR_API_KEY")
model = genai.GenerativeModel("gemini-1.5-flash")

prompt = """
Classify the customer email into one of: BILLING, TECHNICAL, GENERAL.
Reply with only the category label.

Email: "I was charged twice this month."
Category: BILLING

Email: "The app crashes when I open settings."
Category: TECHNICAL

Email: "How do I export my data?"
Category: GENERAL

Email: "My invoice shows the wrong plan, I'm on the Pro plan but it says Basic."
Category:"""

response = model.generate_content(prompt)
print(response.text.strip())  # → BILLING

Chain-of-thought: making the model reason out loud. For complex tasks, add the instruction 'think step by step' or 'reason through this before giving your final answer.' This forces the model to externalise its reasoning, which consistently produces more accurate results on logic, math, and multi-step problems. The model is not inherently better at math - but when it writes out intermediate steps, it can catch errors in its own chain the same way a person can catch a mistake when they write it on paper instead of just thinking it.

Interactive: Prompt Engineering Sandbox
Brief Builder

Build a prompt by toggling elements on the left. See how adding structure, constraints, and few-shot examples improves output format reliability.

Prompt Elements
System RoleSets the persona and expertise level.
Primary TaskSpecifies what action the model must take.
ConstraintsPlaces guardrails and limitations on the output.
Few-Shot ExampleShows the model a demonstration of input/output.
Compiled Context Input
[PRIMARY TASK]
Analyze the following Python script for vulnerability: exec(input())
Predictability Rating: Poor (Highly Unpredictable)25%
Model Raw JSON Output:

Click Run Test to evaluate output...

The three most common prompt mistakes
1. Describing the output in vague terms. 'Give me a good summary' is almost always worse than 'Summarise in exactly 3 bullet points, each under 20 words, starting with the most important finding.'

2. Overloading with instructions. If your prompt has 20 rules, the model will follow maybe 14 of them. Prioritise ruthlessly. Put your most important constraint first.

3. Not testing edge cases. A prompt that works on your first five examples will fail on the sixth. Test with ambiguous inputs, empty inputs, and inputs that could legitimately belong to multiple categories.
What's next
Even a perfectly engineered prompt cannot fix a model that is generating false information with total confidence. The next lesson explains exactly why models hallucinate, what mechanism causes it, and what architectural patterns actually reduce it - not just better wording.