Automatic prompt engineer

Also known as: APE

On this page 7

Automatic prompt engineer (APE) is a method that treats the writing of an instruction as a search problem solved by a language model: one model proposes many candidate prompts from a few input-output examples, the candidates are scored on held-out data, and the best-performing instruction is selected. In prompting, APE marks the shift from handcrafted wording toward automated prompt optimization, where the model itself discovers phrasing that people might not think to try.

The approach was introduced in 2022 research that framed instruction design as natural-language program synthesis. Its central finding was that model-generated instructions often matched or exceeded prompts written by people across instruction induction and reasoning benchmarks, which made APE a reference point for later techniques in artificial intelligence such as OPRO and gradient-free prompt search.

How the automatic prompt engineer search works

APE runs a propose, score and select loop. The user supplies a small set of demonstrations, for example ten word pairs that show antonyms, and a scoring function. The proposal model then infers what instruction could have produced those outputs, and an evaluation model tests each candidate on separate examples.

  1. Proposal: an LLM receives demonstrations and generates dozens of candidate instructions, often 50 or more.
  2. Scoring: each candidate is run on a held-out subset and scored by execution accuracy or by the log probability of the desired outputs.
  3. Filtering: low-scoring candidates are discarded, and the top fraction is kept.
  4. Resampling: optionally, the model writes semantic variations of the best candidates, and the cycle repeats.
  5. Selection: the highest-scoring instruction is returned for production use.

Proposal comes in two styles. Forward generation asks the model to continue a template such as “The instruction was” after the demonstrations. Reverse generation uses a model that can fill a blank in the middle of a text, which places the instruction slot before the examples and often yields more natural phrasing.

Key parameters and design choices

The quality of an APE run depends on a handful of settings that trade cost against coverage. The table below lists the decisions that most affect results.

ParameterTypical rangeEffect on results
Number of candidates20 to 200 per roundMore candidates raise the chance of a strong prompt but cost more calls
Demonstrations per proposal3 to 10 examplesToo few hide the task; too many crowd the context
Scoring set size20 to 100 examplesSmall sets reward lucky prompts and overfit
Scoring functionExecution accuracy or log probabilityMust match the real success criterion
Search rounds1 to 3 iterationsGains usually shrink after the first resampling round
Main APE settings and their effects

Original experiments reported that iterative Monte Carlo search improved prompts only modestly over a single well-sized proposal round. The decisive factor was usually the size and quality of the candidate pool, together with a scoring set that reflects real inputs.

APE compared with other prompt optimization methods

APE is one member of a broader family of automated prompt search methods. They differ in how they generate new prompts and in whether they use feedback about specific errors.

MethodHow new prompts are producedFeedback used
APELLM infers instructions from demonstrationsScores on held-out examples
OPROLLM sees a history of prompts with their scoresScore trajectory across iterations
Textual gradient methodsLLM criticizes failures and edits the promptNatural-language error analysis
Evolutionary prompt searchMutation and crossover of prompt textFitness scores per generation
Soft prompt tuningGradient descent on continuous embeddingsTraining loss; needs model weights
Manual prompt engineeringA person writes and revises wordingHuman judgment and spot checks
APE and related approaches

Unlike soft prompt tuning, APE produces readable hard prompts and requires only black-box access to the model. Unlike error-driven methods, it does not explain why a prompt fails, so it is weaker at repairing specific recurring mistakes.

When to use an automatic prompt engineer

APE pays off when a task is well defined, repeated at scale and measurable. It is less useful for open-ended writing where quality cannot be scored automatically.

SituationRecommendationReason
Classification or extraction with labelsUse APEAccuracy is cheap to measure
High-volume production promptUse APE, then review by handSmall accuracy gains multiply across many calls
Creative or stylistic writingPrefer manual iterationNo reliable automatic score exists
Very few labeled examplesCollect more data firstScoring becomes noisy and overfits
Model changes oftenRerun APE after each changeOptimized wording transfers imperfectly between models
Decision guide for applying APE

A sound workflow keeps a separate test set that the search never sees. The selected instruction is evaluated there once, which gives an honest estimate of how it will behave on new production inputs.

Limitations and common mistakes

The main risk is overfitting the scoring set. When hundreds of candidates are compared on a few dozen examples, the winner may simply be the luckiest phrasing. Optimized prompts can also be brittle across models, because wording that suits one model may confuse another.

  • Metric mismatch: optimizing exact-match accuracy can reward terse outputs that fail real users.
  • High compute cost: hundreds of candidates times dozens of examples quickly add up to thousands of calls.
  • Opaque wording: winning prompts may be odd phrases that teams hesitate to maintain.
  • Narrow scope: APE optimizes the instruction only, not examples, output format or system context.

These limits explain why practitioners treat APE output as a strong starting draft rather than a final answer, and why it is often combined with human review and broader context engineering.

Stripped to the essentials

An automatic prompt engineer lets an AI write instructions for another AI. You show it a few examples of the job. It guesses many possible instructions that could explain them. Then each guess is tested on more examples.

The best scorer wins. Think of it as holding auditions. Many candidates read the same lines, and the one with the best results gets the part. People still check the winner before it goes live.

Where it is applied

A customer support team routes about 20,000 incoming emails per day into twelve categories with a language model. Their handwritten instruction reaches roughly 84 percent accuracy on a labeled sample, and misrouted emails slow down replies. An engineer collects 300 labeled emails and splits them into proposal, scoring and test sets.

An APE run generates 80 candidate instructions from small groups of examples, scores each on 100 emails and resamples variations of the top ten. The winning instruction explicitly separates billing disputes from refund requests, a distinction the original prompt blurred. On the untouched test set it reaches about 89 percent accuracy. The team edits the wording slightly for readability, confirms the score holds and deploys it, which noticeably reduces manual rerouting work each day.