What is Prompt Engineering?
Prompt engineering is the practice of designing, testing, and improving instructions so an LLM can perform a task more clearly and reliably.
By the end of this lesson, you will be able to:
- Explain what prompt engineering means.
- Break a prompt into task, context, constraints, and output requirements.
- Turn vague requests into precise prompts.
- Test prompt changes with Python and an LLM API.
- Build a reusable prompt template and evaluate it on multiple inputs.
What Is a Prompt?
A prompt is the input you send to an LLM to communicate a task. It may be a question, instruction, conversation, document, examples, or a combination of these.
“Explain RAG.”
The topic is clear, but the audience, depth, format, and goal are missing.
“Explain RAG to a Python beginner in 5 bullet points. Define each technical term and include one customer-support example.”
The model now has clearer success criteria.
Prompt Engineering Is an Iteration Loop
Good prompts usually come from testing, not from guessing the perfect sentence on the first attempt.
Engineering mindset: test a prompt on several representative inputs. One impressive response does not prove a prompt is reliable.
Vague Prompt vs. Engineered Prompt
Imagine an application that summarizes customer incidents.
Summarize this.
- No audience or purpose
- No length
- No required facts
- No instruction about invented details
You are a customer-support assistant. Summarize the incident in 3 bullets. Include: problem, customer impact, and duration. Do not invent facts.
- Role is clear
- Output is constrained
- Important fields are explicit
- Unsupported details are discouraged
The Anatomy of a Useful Prompt
Not every task needs every component, but these building blocks are useful when a task requires predictable behavior.
Example: Improve a Python Error Prompt
Compare these two requests when you want an LLM to help debug code.
“Fix this Python error.”
The model cannot tell whether you want an explanation, corrected code, or both.
“Explain the error in simple terms. Identify the exact cause and provide corrected Python code. Keep the explanation under 120 words.”
The task, output, and length are explicit.
Another useful pattern is to specify the audience. “Explain embeddings.” becomes “Explain embeddings to a Python beginner using one shopping-search example and no equations.”
Run a Real Prompt Experiment
Use the same input with two different instructions. The goal is not to expect identical output every time, but to see whether the engineered prompt makes the desired requirements clearer.
import os
from openai import OpenAI
client = OpenAI(api_key=os.environ["OPENAI_API_KEY"])
text = "Our API returned 503 errors for 20 minutes. Checkout requests failed."
def run_prompt(instruction):
response = client.responses.create(
model="gpt-5-mini",
input=[
{"role": "system", "content": instruction},
{"role": "user", "content": text},
],
)
return response.output_text
vague = "Summarize this."
engineered = "Summarize in 3 bullets. Include problem, impact, and duration. Do not add facts."
print(run_prompt(vague))
print(run_prompt(engineered))Important: keep API keys in environment variables. Model outputs can vary, so evaluate whether requirements are met instead of comparing exact wording.
Context + Instructions
Context tells the model what information is available. Instructions tell it how to use that information.
Unused products can be returned within 30 days with proof of purchase.
If the policy is insufficient, say what information is missing.
The response has evidence and a defined task.
Context only: “Returns are accepted within 30 days.”
Useful evidence, but the desired operation is unclear.
Context + instruction: “Using only this policy, decide whether the purchase is eligible.”
Evidence and task are both explicit.
Output Format Is Part of the Design
If another program must consume the answer, define a predictable structure and validate it in your application.
The ticket looks like billing and seems urgent.
{
"category": "billing",
"priority": "high"
}Build a Reusable Prompt Template
Templates keep the instructions stable while application data changes.
def build_prompt(customer_message, policy):
return f"""You are a customer-support assistant.
Use only the policy below.
Do not invent information.
If the policy is insufficient, say so.
POLICY:
{policy}
CUSTOMER MESSAGE:
{customer_message}
OUTPUT:
Return exactly 3 bullet points:
1. Issue
2. Policy-based answer
3. Next step
"""
print(build_prompt(
"Can I return my order after 20 days?",
"Unused products can be returned within 30 days with proof of purchase.",
))Later, the policy variable can come from a database or RAG retrieval step. The prompt template remains the reusable instruction layer.
Test Prompts Like an Engineer
Change one meaningful variable at a time and compare several representative inputs.
Common Mistakes
Mini-Project: Support Ticket Classifier
Build a small workflow that classifies support messages into billing, technical, shipping, or account.
Prompt Improvement Challenge
Start with “Write about RAG.” Improve it in three rounds: add an audience and goal; then add format and length; finally add constraints and a real-world example.
Explain retrieval-augmented generation (RAG) to a Python beginner.
Use exactly 5 bullet points.
Include one real-world customer-support example.
Define each technical term before using it.
Do not assume the reader knows vector databases.Test yourself
What is the main purpose of prompt engineering?
30-Second Recap
- A prompt communicates a task to an LLM.
- Prompt engineering is the process of designing, testing, evaluating, and improving prompts.
- Useful prompts often clarify role, task, context, constraints, and output.
- Good prompting defines success criteria but does not guarantee perfect answers.
- Prompt engineering works with context, retrieval, tools, and application-level validation.
- Reliable prompts are tested on representative inputs rather than one lucky response.