Prompt Optimization and Practical Use Cases

Chapter 8: Prompt Optimization and Practical Use Cases

← All chapters

Chapter 8 of 17
Prompt Optimization and Practical Use Cases

Turns prompting into a measurable process. It covers optimisation techniques such as grounding and templates, how to evaluate and A/B test prompts, and practical prompts for coding, writing and customer service, with the Klarna and NBER call-centre evidence.

8.1 What prompt optimisation means

Prompt optimisation is the systematic improvement of a prompt until it produces output that is correct, relevant, consistent and in the required format, at acceptable cost. In personal use this is trial and error. In a business system, where one prompt may run thousands of times a day, it becomes a disciplined test-and-measure process.

8.2 Optimisation techniques

Technique How to apply it Example
Iterative refinement Change one thing at a time and compare results Add audience, then tone, then length
Specificity Replace vague words with measurable ones "Short" becomes "under 80 words"
Delimiters Separate instructions from input text with clear markers Put the source text between triple quotes or under a heading "Text:"
Output templates Give the exact structure to fill "Return JSON with keys: name, issue, priority"
Positive instructions Say what to do, not only what to avoid "Use simple words a Class 6 student knows" instead of "don't be complex"
Grounding Tell the model to use only supplied material "Answer only from the policy below. If the answer is not there, say 'Not covered in policy'."
Allowing uncertainty Give the model a way out instead of guessing "If you are not sure, say so."
Decomposition Break a complex task into steps or a prompt chain See Chapter 7
Examples Add few-shot examples for custom formats See Chapter 7
System instructions Put fixed rules in the system or custom instructions field Brand tone, language, safety rules for a chatbot
Temperature and length Lower temperature for factual tasks; set maximum length Temperature 0.2 for data extraction, 0.8 for slogans

Grounding is the single most effective way to reduce hallucination in business use. When the model must answer from supplied documents, the system that fetches the right documents first is called Retrieval-Augmented Generation (RAG).

8.3 Evaluating prompt output

Criterion Question How to measure
Accuracy Are the facts and numbers correct? Check against a source or answer key
Relevance Does it answer what was asked? Reviewer rating 1–5
Completeness Are all required parts present? Checklist
Format compliance Does it follow the required structure? Automatic check, for example valid JSON
Consistency Does the same input give similar output each time? Run the same case several times
Tone and safety Is it polite, on-brand, free of harmful content? Reviewer check
Cost and speed How many tokens and seconds does it take? Platform usage reports

Building a test set. Collect 20–100 real examples with known good answers, covering normal and tricky cases. Run each prompt version on the whole set and compare scores. This is how companies decide which prompt goes live.

8.4 Use cases in coding

Generating code

You are a senior Python developer.
Write a function calculate_emi(principal, annual_rate, years) that returns the monthly EMI.
Use the standard reducing-balance formula. Handle a zero interest rate.
Include a docstring and three example calls with expected outputs.

Explaining code: "Explain this SQL query line by line for a beginner, then say what it returns."

Refactoring: "Refactor this function to remove duplicated code and improve variable names. Do not change behaviour. List the changes you made."

Debugging: "This code raises KeyError: 'price' on line 14. Here is the code and a sample input. Find the cause and suggest a fix."

Good practice: always run and test generated code, check for security issues, and never paste secret keys or proprietary code into a public tool.

8.5 Use cases in writing

Task Example prompt
Article drafting "Write a 600-word blog post for small shop owners on accepting UPI payments safely. Use three subheadings and end with a checklist."
Editing "Correct grammar and spelling in the text below. Do not change the meaning. Show changes in bold."
Tone change "Rewrite this complaint letter to be firm but polite."
Simplification "Rewrite this legal clause in plain English for a tenant."
Translation "Translate this notice into Hindi and Marathi, keeping a formal tone."
Outlining "Create an outline for a 2,000-word report on EV charging infrastructure in Indian cities."

AI-drafted writing must still be checked for facts, edited for the writer's own voice, and used within the rules of the institution or publisher.

8.6 Use cases in customer interaction

FAQ answering with grounding

You are the support assistant for ABC Broadband.
Answer the customer's question using ONLY the FAQ below.
If the FAQ does not cover it, reply: "Let me connect you to our team" and stop.
Keep answers under 60 words, friendly and clear.
FAQ: """..."""
Customer: "How do I change my plan?"

Complaint handling: "Read this complaint. Identify the issue, the customer's emotion and what they want. Draft an apology that acknowledges the issue, explains the next step and gives a timeline. Do not promise refunds."

Scripted replies: generate a set of approved responses for common situations (delivery delay, refund status, technical fault) that human agents can personalise.

8.7 Worked numericals

Numerical 8.1: A/B testing two prompt versions.

A company tests two prompts for extracting invoice data on the same 50 invoices.

Measure Prompt V1 (zero-shot, no template) Prompt V2 (template + 2 examples)
All fields correct 38 46
Valid JSON output 41 50
Average tokens per run 420 610

V2 costs about 45% more per run but cuts errors from 12 to 4 per 50 invoices. If each error takes a clerk 10 minutes to fix, V2 saves 80 minutes per 50 invoices, which usually outweighs the token cost.

Numerical 8.2: Error rate and human review load.

A bank's chatbot answers 12,000 queries a month. With the original prompt 8% of answers need human correction; with a grounded prompt, 3%.

At 6 minutes per correction, that is 600 × 6 = 3,600 minutes, or 60 staff-hours a month.

Numerical 8.3: Time saved from a reported case (with stated assumptions).

Klarna reported its AI assistant handled 2.3 million conversations in its first month, with average resolution time falling from 11 minutes to under 2 minutes. If each conversation would otherwise have taken 11 minutes and now takes 2:

This is an upper-bound estimate; it assumes every conversation would have taken the full 11 minutes.

8.8 Industry use cases

  • Klarna customer service (2024). Klarna, a Swedish payments company, reported that its OpenAI-powered assistant handled 2.3 million conversations in its first month, about two-thirds of its service chats. It said this equalled the work of 700 full-time agents, cut resolution time from 11 minutes to under 2, reduced repeat inquiries by 25%, worked in over 35 languages, and was expected to improve 2024 profit by US$40 million (OpenAI customer story). These are company-reported figures; Klarna later said it would keep human agents available for customers who want them.
  • AI assistance for support agents (independent research). Brynjolfsson, Li and Raymond studied 5,179 customer support agents at a software firm. Access to a generative AI assistant raised issues resolved per hour by 14% on average, and by 34% for novice and low-skilled workers (NBER Working Paper 31161).
  • Coding. GitHub Copilot users write prompts as code comments and function names; the quality of those "prompts" directly affects the suggestions they get. Chapter 15 covers this.

8.9 Review questions

  1. List eight prompt optimisation techniques and give an example of each.
  2. What is grounding? Why does it reduce hallucination? What is RAG?
  3. Name seven criteria for evaluating prompt output. How would you measure format compliance automatically?
  4. Write an optimised prompt for a college helpdesk chatbot that answers only from the admission brochure.
  5. Prompt A gets 30 of 40 test cases right; Prompt B gets 36. Compute each accuracy and the percentage-point improvement. (Answer: 75%, 90%, 15 points.)
  6. Summarise the findings of Brynjolfsson, Li and Raymond. Why might less experienced workers gain more?

Knowledge sources

Independent study notes for learning purposes. Not affiliated with any university or course provider. Facts and case-study figures are linked to their original sources. ChatGPT, Gemini, Copilot, Jasper, Synthesia, Runway, Lumen5 and other product names are trademarks of their respective owners.