You've learned how LLMs work under the hood. Now comes the skill that matters most in practice: prompt engineering โ crafting instructions that get the results you actually want.
The same model can give you a brilliant answer or a useless one depending entirely on how you ask:
โ Bad prompt: "Tell me about climate"
โ Vague, unfocused 2,000-word essay
โ
Good prompt: "Explain three key causes of climate change
in bullet points, suitable for a 14-year-old.
Keep it under 100 words."
โ Focused, useful, right-sized answer
Think of an LLM like a brilliant expert who takes instructions very literally. If you say "write something about dogs", you'll get a random essay. If you say "write a 200-word comparison of Golden Retrievers and Labradors for a pet adoption website, focusing on family-friendliness", you'll get exactly what you need.
Give the model a task with no examples. Relies entirely on the model's training.
Prompt: "Classify this review as POSITIVE, NEGATIVE, or NEUTRAL:
'The battery life is amazing but the camera is disappointing.'"
Response: "NEUTRAL"
Works well for straightforward tasks where the model already understands the format.
Provide a few examples before your actual request. The model learns the pattern from your examples:
Prompt:
"Classify these reviews:
Review: 'Absolutely love this product!' โ POSITIVE
Review: 'Broke after two days.' โ NEGATIVE
Review: 'It's okay, works fine.' โ NEUTRAL
Review: 'The design is sleek but the software crashes often.'
Sentiment: "
Response: "NEGATIVE"
Few-shot prompting was a GPT-3 breakthrough: just 2โ3 examples dramatically improve accuracy on new tasks โ no fine-tuning required.
Ask the model to think step by step. This dramatically improves reasoning on complex problems:
โ Without CoT:
"If a train travels 120 km in 2 hours, then speeds up by 50%,
how far does it travel in the next 3 hours?"
โ Model might guess incorrectly
โ
With CoT:
"If a train travels 120 km in 2 hours, then speeds up by 50%,
how far does it travel in the next 3 hours?
Let's think step by step."
โ Step 1: Original speed = 120 km / 2 hours = 60 km/h
โ Step 2: Speed increase of 50% = 60 ร 1.5 = 90 km/h
โ Step 3: Distance in 3 hours = 90 ร 3 = 270 km
โ Answer: 270 km โ
A system prompt sets the model's behaviour, personality, and constraints before the user ever sends a message.
messages = [
{
"role": "system",
"content": """You are a senior Python code reviewer.
Rules:
- Only comment on bugs, security issues, and performance
- Ignore style and formatting
- Be direct and specific
- Rate severity: LOW / MEDIUM / HIGH / CRITICAL
- If the code is clean, say 'LGTM' and nothing more"""
},
{
"role": "user",
"content": "Review this code:\n\ndef login(user, pwd):\n query = f'SELECT * FROM users WHERE name=\"{user}\" AND pass=\"{pwd}\"'\n return db.execute(query)"
}
]
The model would flag the SQL injection vulnerability as CRITICAL โ because the system prompt told it exactly what to look for and how to respond.
1. ROLE: Who the model should act as
2. TASK: What it should do
3. CONSTRAINTS: Rules and boundaries
4. FORMAT: How to structure the output
5. EXAMPLES: Optional โ what good output looks like
System prompts are your most powerful tool for consistent, reliable AI behaviour. Invest time in crafting them โ a great system prompt can turn a general-purpose model into a specialised expert.
LLMs have a knowledge cutoff and can hallucinate. RAG fixes both by giving the model access to your own data at query time: Index documents โ Retrieve relevant chunks โ Augment the prompt โ Generate a grounded answer.
# Simplified RAG example
def answer_with_rag(question, document_store):
relevant_chunks = document_store.search(question, top_k=3)
context = "\n\n".join(chunk.text for chunk in relevant_chunks)
prompt = f"""Answer based ONLY on the provided context.
If the context doesn't contain the answer, say "I don't have that information."
Context:
{context}
Question: {question}
Answer:"""
return llm.generate(prompt)
RAG is like giving a student an open-book exam. The student (LLM) still does the reasoning, but can look up facts โ making answers much more accurate and trustworthy.
For production apps, you need structured data, not free-form text.
# JSON Mode โ force structured responses
response = client.chat.completions.create(
model="gpt-4o",
response_format={"type": "json_object"},
messages=[
{"role": "system", "content": "Extract product info as JSON: name, price, category, in_stock"},
{"role": "user", "content": "The Sony WH-1000XM5 costs $349, available in Electronics."}
]
)
# Returns: {"name": "Sony WH-1000XM5", "price": 349, "category": "Electronics", "in_stock": true}
Function calling goes further โ letting the model decide when to invoke external tools (weather APIs, databases, calculators) by describing available functions in a schema. The model returns a structured call rather than free text.
Ask the same question multiple times (higher temperature), then take the majority answer. Converging reasoning paths signal higher confidence.
Instead of one chain of reasoning, explore multiple branches and evaluate feasibility before continuing deeper โ prune weak branches, expand promising ones. This excels at planning and complex problem-solving tasks.
Create reusable prompt templates for common tasks:
PROMPT_LIBRARY = {
"summarise": """Summarise the following text in {length} bullet points.
Focus on key facts and actionable insights.
Text: {text}""",
"code_review": """Review this {language} code for:
1. Bugs and logic errors
2. Security vulnerabilities
3. Performance issues
Ignore style. Rate each issue as LOW/MEDIUM/HIGH/CRITICAL.
Code:
```{language}
{code}
```""",
"explain": """Explain {concept} to a {audience}.
Use analogies from everyday life.
Keep it under {word_limit} words.
End with a "Test yourself" question.""",
"extract_data": """Extract the following fields from the text as JSON:
Fields: {fields}
If a field is not found, use null.
Text: {text}""",
}
def build_prompt(template_name, **kwargs):
template = PROMPT_LIBRARY[template_name]
return template.format(**kwargs)
# Usage examples
prompt = build_prompt(
"explain",
concept="quantum computing",
audience="curious 15-year-old",
word_limit=150
)
print(prompt)
A prompt library is a living document โ iterate and improve your templates based on results. Version-control them like code, test them with different inputs, and share the best ones with your team.
You've now mastered the three pillars of modern AI practice: deep architectures, language models, and prompt engineering. In the upcoming lessons, we'll tackle AI system design โ how to architect production AI applications that are reliable, scalable, and responsible. ๐๏ธ