Prompt Optimization
Learn how to improve prompts systematically instead of changing words randomly. Define a target, test a prompt, measure the result, and keep the changes that actually improve quality, consistency, cost, or latency.
By the end of this lesson, you will be able to:
- Explain what prompt optimization means in an AI application.
- Define measurable criteria for prompt quality.
- Build a small evaluation dataset for prompt experiments.
- Improve prompts using controlled iterations with Python.
- Balance answer quality against tokens, latency, and complexity.
What Is Prompt Optimization?
Prompt optimization is the process of improving a prompt so that an AI application performs better on the task it is designed to solve.
βBetterβ needs to be defined. Depending on the application, you may care about correctness, consistency, format compliance, helpfulness, safety, cost, or response time.
Change words until it feels better
Try a longer instruction. Try a different adjective. Try another example. Looks better β ship it.
This does not tell you whether the change actually improved the application.
Change β test β measure
Baseline prompt
β
Test dataset
β
Measure results
β
Change one thing
β
Compare again
This creates evidence for deciding which prompt version to keep.
Start With a Clear Optimization Goal
Before changing a prompt, decide what you are trying to improve. A prompt cannot be βbetterβ in the abstract.
Create a Baseline Prompt
Your first tested version is the baseline. Every later version should be compared against it using the same evaluation conditions.
Classify the customer message.
Categories:
- billing
- shipping
- technical
- account
Return the category.
Message:
{message}
Save the prompt text, model settings, test inputs, outputs, and evaluation results. Without a baseline, you cannot confidently say that a change helped.
Build an Evaluation Dataset
A small evaluation dataset is a collection of inputs with expected outcomes. It lets you compare prompt versions fairly.
Measure More Than βLooks Goodβ
Choose metrics that match your task. For classification, accuracy and invalid-output rate may be useful. For extraction, field-level correctness matters. For generation, a human or model-based rubric may be needed.
Change One Important Thing at a Time
If you replace the instruction, add five examples, change the output format, and change the model at the same time, you will not know which change caused the result.
Controlled changes make prompt optimization easier to debug and explain to other developers.
Improve the Prompt in Layers
A useful optimization sequence is to remove ambiguity before adding complexity.
Before and After: Optimize a Classifier
Classify this message. Message: "I need to change my email."
Category boundaries and output format are not explicit.
TASK: Classify the message. CATEGORIES: billing, shipping, technical, account RULES: - Choose exactly one category. - Use only the supplied message. - Return the category in lowercase. - Do not add extra text. MESSAGE: "I need to change my email." OUTPUT:
The optimized version reduces ambiguity and makes the output contract explicit.
Python Example: Compare Prompt Versions
A simple evaluation loop can compare two prompt versions on the same test inputs.
from openai import OpenAI
client = OpenAI()
test_cases = [
("I was charged twice.", "billing"),
("My tracking has not changed.", "shipping"),
("The app crashes.", "technical"),
("I cannot reset my password.", "account"),
]
prompt_v1 = """
Classify the message as billing, shipping, technical, or account.
Return the category.
Message:
{message}
"""
prompt_v2 = """
TASK:
Classify the message.
CATEGORIES:
billing, shipping, technical, account
RULES:
- Choose exactly one category.
- Return only the lowercase category.
- Do not add an explanation.
MESSAGE:
{message}
OUTPUT:
"""
def classify(template, message):
response = client.responses.create(
model="YOUR_MODEL",
input=template.format(message=message),
)
return response.output_text.strip().lower()
for name, template in [("v1", prompt_v1), ("v2", prompt_v2)]:
correct = 0
for message, expected in test_cases:
prediction = classify(template, message)
if prediction == expected:
correct += 1
accuracy = correct / len(test_cases)
print(name, accuracy)
Optimize Few-shot Examples Too
Prompt optimization also applies to demonstrations. More examples can improve coverage, but they consume context and may add latency and cost.
5 examples
Accuracy: 92%
Prompt: 480 tokens
3 examples
Accuracy: 92%
Prompt: 310 tokens
1 example
Accuracy: 84%
Prompt: 180 tokens
Watch for Prompt Regressions
An optimization can fix one case while breaking another. This is a prompt regression.
The overall score improved, but an important subgroup became worse. Keep separate checks for critical categories and edge cases rather than relying only on one aggregate number.
Optimize for Quality, Cost, and Latency
Production prompt optimization is usually a trade-off. A longer prompt may improve quality but increase token usage. A shorter prompt may be cheaper but less reliable.
Prompt Optimization in Production
Once a prompt is used by real users, optimization becomes an ongoing engineering process.
Optimize a Support Prompt
Start with this prompt:
Classify the customer message as billing, shipping, technical, or account.
Return the category.
Message:
{message}
Test yourself
The goal and the metric you want to improve, such as correctness, consistency, format, cost, or latency.
It gives you a fixed version against which later prompt changes can be compared.
Changing the test set makes it difficult to tell whether the prompt change caused the difference.
Yes. For example, a longer prompt may improve accuracy while increasing token usage and latency.
30-Second Recap
- Prompt optimization is systematic improvement, not random rewriting.
- Define what βbetterβ means before changing the prompt.
- Use a baseline and an evaluation dataset.
- Change important variables in a controlled way.
- Watch for regressions in edge cases.
- Optimize the complete trade-off between quality, cost, latency, and reliability.
Key Takeaways
- A good prompt should solve a measurable application problem.
- Evaluation turns prompt engineering into an engineering workflow.
- Shorter is not always better, and longer is not always better.
- Examples, constraints, context, and output rules should each earn their place.
- Production prompts need versioning, regression tests, and ongoing review.