LESSON 19 · PROMPT ENGINEERING

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.

25 min readBeginnerGenerative AI

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.
1

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.

EXAMPLE 1 · VAGUE

“Explain RAG.”

The topic is clear, but the audience, depth, format, and goal are missing.

EXAMPLE 2 · SPECIFIC

“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.

Core idea:A prompt is communication with the model. Prompt engineering makes that communication explicit enough for the task you want to accomplish.
2

Prompt Engineering Is an Iteration Loop

Good prompts usually come from testing, not from guessing the perfect sentence on the first attempt.

1. DefineWhat should happen?
2. WriteState the task and rules.
3. RunGenerate an output.
4. EvaluateCheck requirements.
5. ImproveChange and test again.

Engineering mindset: test a prompt on several representative inputs. One impressive response does not prove a prompt is reliable.

3

Vague Prompt vs. Engineered Prompt

Imagine an application that summarizes customer incidents.

VAGUE
Summarize this.
  • No audience or purpose
  • No length
  • No required facts
  • No instruction about invented details
ENGINEERED
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
What changed?The model did not become smarter. The request became easier to interpret because the desired output was defined.
4

The Anatomy of a Useful Prompt

Not every task needs every component, but these building blocks are useful when a task requires predictable behavior.

RoleWho is the assistant?“You are a support analyst.”
TaskWhat should it do?“Classify this ticket.”
ContextWhat evidence matters?“Use the policy below.”
ConstraintsWhat rules apply?“Do not invent facts.”
OutputWhat should it return?“Return JSON.”
ROLE+TASK+CONTEXT+CONSTRAINTS+OUTPUTClearer request
5

Example: Improve a Python Error Prompt

Compare these two requests when you want an LLM to help debug code.

BEFORE

“Fix this Python error.”

The model cannot tell whether you want an explanation, corrected code, or both.

AFTER

“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.”

6

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.

</> Python
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.

7

Context + Instructions

Context tells the model what information is available. Instructions tell it how to use that information.

CONTEXTReturn policy

Unused products can be returned within 30 days with proof of purchase.

+
INSTRUCTIONUse only the policy

If the policy is insufficient, say what information is missing.

OUTPUTGrounded answer

The response has evidence and a defined task.

EXAMPLE 1

Context only: “Returns are accepted within 30 days.”

Useful evidence, but the desired operation is unclear.

EXAMPLE 2

Context + instruction: “Using only this policy, decide whether the purchase is eligible.”

Evidence and task are both explicit.

8

Output Format Is Part of the Design

If another program must consume the answer, define a predictable structure and validate it in your application.

FREE-FORM
The ticket looks like billing and seems urgent.
STRUCTURED
{
  "category": "billing",
  "priority": "high"
}
Remember:A prompt can request a format, but important production workflows should still validate model output before using it in business logic.
9

Build a Reusable Prompt Template

Templates keep the instructions stable while application data changes.

</> Python
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.

10

Test Prompts Like an Engineer

Change one meaningful variable at a time and compare several representative inputs.

Experiment AAdd an audience.“Explain to a beginner.”
Experiment BAdd a constraint.“Use 5 bullets.”
Experiment CAdd examples.“Follow these examples.”
Experiment DChange format.“Return JSON fields.”
INPUT SET5–20 examples
PROMPT ABaseline
PROMPT BOne change
COMPARERequirements + consistency
Good evaluation:Measure what matters for your application: correct classification, required fields, groundedness, length, tone, or another explicit success criterion.
11

Common Mistakes

Too vague“Make this better.”Define what “better” means.
Conflicting rules“Be detailed” + “one short sentence.”Resolve priorities.
Too much contextSend every document.Select relevant evidence.
No evaluationTrust the first response.Test representative inputs.
No validationTrust output as truth.Validate important fields in code.
Change everythingRewrite the whole prompt each time.Run controlled experiments.
12

Mini-Project: Support Ticket Classifier

Build a small workflow that classifies support messages into billing, technical, shipping, or account.

1. Define labelsKeep the allowed categories explicit.
2. Write a baselineAsk for one category.
3. Add constraintsDo not return categories outside the list.
4. Define outputReturn a predictable structure.
5. Test examplesUse unseen messages.
6. ImproveChange one prompt component and compare.
Success criteria:Your prompt should satisfy the category and format requirements across a test set, and your application should handle invalid model output safely.
PRACTICE

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.

LEVEL 1 · CLEAR TASKLEVEL 2 · REQUIREMENTSLEVEL 3 · TEST + ITERATE
QUICK QUIZ

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.