Prompt engineering

Prompt engineering

Article

Prompt engineering is the creative process of writing an instruction (prompt) that makes the model generate the output you want. The model weights are not changed, as they are with fine-tuning (post-training).

Prompting is the most accessible way to make full use of a model's potential. That's why it pays to get everything out of it before you move on to another technique like fine-tuning, which takes far more resources.

Precisely because writing prompts seems so accessible, it's tempting to think there isn't much to it. That's a misconception. Many people think it's nothing more than a bit of writing and searching for the right words and phrasing.

Writing a prompt is relatively easy; writing effective prompts is a much bigger challenge. See it as a development process: there is a real engineering side to it, namely experimenting and evaluating systematically. Among other things, that takes:

  • Statistics
  • Engineering
  • Knowledge of machine learning
  • Keeping good track of the results of your experiments with different prompts
  • Dataset management and curation

A prompt generally consists of the following parts:

  • Task description: what do you want the model to do? What role should the model take on?
  • Examples of how to do the task.
  • The task: the concrete task you want the model to carry out.
  • Context: the information the model needs to carry out the task.

The instructions and criteria have to be clear to the model, or it will struggle to follow them.

How much engineering you need depends on how robust the model is. If a small change in the prompt leads to big changes in the output, the model isn't very robust and it pays to put a lot of attention into this process.

In-context learning: zero-shot and few-shot

A model learning through the prompt is called in-context learning. AI models can learn from examples given in the prompt, even if the model never saw that data during training. That's a very valuable property.

Each example you give is called a shot. Learning from a few examples is called few-shot learning; if you give no examples, it's zero-shot learning.

By experimenting with the number of examples, you get a sense of how many shots you need for a useful result.

In general, few-shot learning gives better results than zero-shot learning. That's all the more true when the application calls for more domain-specific knowledge, as in medicine or law. With the latest models the difference is getting smaller, though: they often follow instructions well without examples too.

Prompt and context

What's the difference? Opinions differ. In the book AI Engineering, Chip Huyen uses the following definition:

  • Prompt is the entire text used as input for the model: system prompt + user prompt.
  • Context refers to the information given to the model to carry out a task.

System prompt and user prompt

Roughly speaking, the system prompt is the task description and the user prompt is the task. Something you often see in a system prompt is a role description, for example “Take on the role of a tax advisor.” A user prompt might then be: “Can you help me with this year's income tax?”

The system prompt is usually defined by the makers of the application, and the user prompt by the users of that application.

Before these prompts go into the model, everything is combined into one big prompt:

system prompt + user prompt = prompt

For the question “What is 2 + 2?” that could look like this:

prompt = “Take on the role of a calculator.” + “What is 2 + 2?”

The instructions in the system prompt are the same for every question, while the context differs per question. The arithmetic example needs no context, but a customer service assistant does:

TextWhereWhat is it?
“You are a helpful customer service employee of an online store.”System promptTask description
“Can I still return these shoes?”User promptThe concrete task
The return policy and the customer's order detailsUser promptContext

Prompt engineering best practices

Anthropic and OpenAI have both published a guide with best practices (Anthropic, OpenAI). The most important advice:

  1. Write clear and explicit instructions, without ambiguity.
  2. Ask the model to take on a role, or persona.
  3. Give examples (few-shot).
  4. Specify the format you want the answer in.
  5. Give enough context.
  6. Break complex tasks down into smaller tasks.
  7. Give the model time to think:
    1. Chain of Thought (CoT): let the model reason step by step before it answers.
    2. Self-critique: let the model critically review and improve its own answer.
  8. Improve your prompts regularly and adjust them as your insight grows.

Prompt engineering tools

There are also tools that help you build prompts or improve them automatically:

  1. OpenPrompt: an open-source framework for prompt learning from Tsinghua University.
  2. DSPy: a framework from Stanford in which you write prompts as code and have them optimized automatically.
  3. Promptbreeder: an AI-driven prompt optimizer from Google DeepMind that lets prompts evolve.
  4. TextGrad: an AI-driven prompt optimizer from Stanford that uses feedback from a language model as a kind of gradient.

Finally

Writing a prompt is easy, writing an effective prompt is not. Treat it as engineering: experiment systematically, record your results and evaluate every change. Only once you've really made use of what prompting can do is it time to look at heavier techniques like fine-tuning.


I wrote this text 100% myself, in Dutch. I did use AI to check it and make corrections. The translation into English was also done by AI; according to Pangram, 36% of it is AI-written.

More articles