AI & LLMs

Prompt Engineering: The New Interface for AI

With instruction-tuned models like InstructGPT now the default in OpenAI's API, and research showing that 'let's think step by step' can transform reasoning results, how you phrase a request has become a real engineering skill. This guide covers the prompt patterns that work in 2022, and how to test prompts like code.

By EthicalHat Research · · 3 min read
Infographic: Anatomy of a good prompt

When GPT-3 launched in 2020, getting useful results often felt like a dark art. You arranged text so that the model's most likely continuation happened to be what you wanted: fake documents, carefully chosen examples, odd phrasings that happened to work.

Two developments this year have turned that art into something closer to engineering:

  • In January, OpenAI made its InstructGPT models, trained with human feedback to follow instructions, the default in its API.
  • Research from Google (chain-of-thought prompting, January) and the University of Tokyo with Google ("Let's think step by step", May) showed that prompting large models to reason in steps dramatically improves their performance on multi-step problems.

Prompts are now the main interface to large language models. Writing them well is a skill worth developing.

From completion to instruction

Base language models continue text. To get a summary from them, you had to set up a pattern:

text
Article: <text>

TL;DR:

Instruction-tuned models understand direct requests:

text
Summarise the following article in three bullet points for a non-technical
executive audience. Focus on business impact.

Article:
"""
<text>
"""

The second prompt is clearer for humans and models alike.

Patterns that work

1. Be specific about the task and audience

"Write about our product" produces generic text. "Write a 120-word product description of our password manager for small-business owners, emphasising ease of setup, in a friendly but professional tone" produces something usable.

2. Delimit the input

Separate instructions from data with clear markers such as triple quotes, XML-style tags or headings. This reduces confusion, and it also makes it harder for text inside the data to be mistaken for instructions.

3. Specify the output format

If the output feeds another program, say exactly what shape you need:

text
Extract the following fields from the support email and return JSON only,
with no extra text:

{"customer_name": string, "product": string, "issue_category":
 one of ["billing", "login", "bug", "other"], "urgency": 1-5}

4. Show examples when format or style is subtle

A few input and output pairs (few-shot prompting) remain one of the most reliable ways to communicate a pattern the model should follow.

5. Ask for step-by-step reasoning

For arithmetic, logic and multi-step questions, asking the model to reason before answering helps large models significantly:

text
Q: A shop sells notebooks at $3 each or 4 for $10. How much do 10 notebooks cost at the lowest price?
A: Let's think step by step.

The chain-of-thought paper showed that including worked examples with reasoning steps markedly improved results on maths word problems for the largest models tested. The effect largely disappears in small models.

6. Give the model a way out

"If the answer is not in the text, say 'Not found'" reduces fabricated answers, though it does not eliminate them.

7. Control randomness

Use low temperature for extraction, classification and factual tasks; higher temperature for brainstorming and creative writing.

Treat prompts like code

Prompts that power real features deserve engineering discipline:

flowchart LR
  P[Prompt v1] --> E[Run against<br/>test dataset]
  E --> S[Score outputs<br/>accuracy, format, safety]
  S --> R{Better than<br/>current version?}
  R -->|yes| D[Review & deploy]
  R -->|no| P2[Revise prompt] --> E
  • Version control prompts alongside the code that uses them.
  • Build a test set of representative inputs with expected outputs or grading criteria.
  • Measure every change against that set before deploying. A tweak that fixes one example often breaks another.
  • Log production inputs and outputs (with appropriate privacy controls) to find new failure cases.
  • Pin model versions where possible, and re-test when the underlying model changes.

Limits of prompt engineering

  • It cannot add knowledge the model lacks. Prompts can surface what the model knows, but not facts it never saw.
  • Results are brittle. Wording, ordering and even punctuation can matter.
  • It does not guarantee safety. Users can craft inputs that override your instructions. This is an emerging security concern for applications that mix instructions with untrusted text.
  • Long prompts cost money and eat into the model's limited context window.

When prompting hits these limits, fine-tuning or retrieving relevant documents into the prompt are the next tools to reach for.

The bottom line

Prompt engineering is not a gimmick. It is the practical skill of communicating intent to a new kind of software component. The best prompts read like clear briefs to a capable colleague: specific, well-structured and explicit about what good output looks like. And like any engineering artefact, they should be tested before they ship.

Our agentic AI security and DevSecOps teams design, test and maintain prompts for production applications.

EthicalHat Research

The EthicalHat research team: penetration testers, SOC analysts and engineers writing about security, software engineering and AI.

Want an expert opinion on your own systems?

Email info@ethicalhat.com or call +1-844-838-4422. Our security engineers are happy to talk it through.