Automatic prompt optimisation
Automatic prompt optimisation is the use of an algorithm, often another language model, to search for the prompt wording and examples that score best on a chosen task metric, instead of tuning prompts by hand.
Writing prompts by hand is trial and error: change a sentence, try a few examples, see whether the output improves. Automatic prompt optimisation turns that into a search. A developer supplies a task, a set of example inputs with known good outputs and a way to score results. The optimiser proposes candidate prompts, scores each one on the examples, keeps the best and uses them to propose more. The model's weights never change; only the text sent to it does.
An early example was the Automatic Prompt Engineer, or APE, described by Yongchao Zhou and colleagues in November 2022. It had a language model generate a pool of candidate instructions and kept the one that scored best. Across 24 language tasks, its instructions matched or beat those written by human annotators on 19. In September 2023, Chengrun Yang and colleagues described OPRO, in which a language model acts as the optimiser: its prompt lists earlier attempts with their scores and asks for a better one. The best prompts it found beat human-designed prompts by up to 8% on the GSM8K maths benchmark and by up to 50% on Big-Bench Hard tasks.
The best-known developer tool in this area is DSPy, described by Omar Khattab and colleagues in October 2023. Instead of writing one long prompt, a developer builds a program from declarative modules, and a compiler optimises the whole pipeline against a chosen metric, for example by choosing the worked examples each module is shown. The paper reported that its self-built pipelines beat standard few-shot prompting by more than 25% with GPT-3.5 and 65% with Llama 2 13B chat.
The result is only as good as the metric and the examples. With a small evaluation set, the optimiser can find a prompt that fits those examples and fails on new ones. With a loose metric, it can find wording that scores well without doing the job. A prompt tuned against one model has only been scored on that model, so switching models means running the search again.
In practice, the approach pays off for repeated, well-defined tasks such as classification, extraction or routing, where a labelled set of examples exists or can be built. It is of little use for one-off requests. Its main effect is to move the effort from wordsmithing to building good test sets and deciding what a good answer looks like.
It is a method for improving prompts by search rather than by hand. An algorithm, often a language model, proposes prompt variations, tests them against a set of examples with a scoring rule, and keeps the ones that perform best. The model itself is not retrained. DSPy is an open framework for building language model pipelines from modules instead of hand-written prompts. Its optimisers, which the original paper called a compiler, tune each module's instructions and examples against a metric the developer chooses. On tasks with clear scoring it often matches or beats human prompts: APE's instructions did so on 19 of 24 tasks. It depends on having good examples and a sound metric, and a person still has to define what success looks like. Prompt optimisation alone does not; it only changes the text sent to the model. Some frameworks, including DSPy, can also fine-tune model weights as part of the same pipeline, which is a separate step.Common questions
What is automatic prompt optimisation?
What is DSPy?
Is automatic prompt optimisation better than writing prompts by hand?
Does prompt optimisation change the model?
Search for "automatic prompt optimization", "DSPy optimizers", "OPRO large language models as optimizers" or "automatic prompt engineer" to see the main approaches. The research papers on arXiv describe the methods and their measured gains, and the documentation of open-source frameworks shows how they are used in practice. Treat vendor claims of large gains with care unless the evaluation set and metric are described.Finding more
Working out what this means for your business?
Aileron Shift builds the systems behind this site: AI newsrooms that research, write and publish on their own, marketing automation and WordPress hosting for companies that need to move faster than their competitors.