Automatic Prompt Engineering: Automate Candidate Generation and Evaluation
Automatic Prompt Engineering (APE) uses language models or search procedures to generate candidate task instructions, measure their performance on examples, and select stronger candidates. Instead of having a person repeatedly tweak wording, the process cycles through candidate generation, execution, and selection using a human-defined objective and evaluation data. Automation can help explore alternatives, but it cannot define the objective, provide sound data, or replace final validation.
Automatic Prompt Engineering (APE) uses language models or search procedures to generate candidate task instructions, measure their performance on examples, and select stronger candidates. Instead of having a person repeatedly tweak wording, the process cycles through candidate generation, execution, and selection using a human-defined objective and evaluation data. Automation can help explore alternatives, but it cannot define the objective, provide sound data, or replace final validation.
This article focuses on methods for automatically generating and searching natural-language instructions. It explains APE as a research approach and the mechanics of representative algorithms.
What changes when the process is automated?
In manual prompt work, a person writes an instruction, examines model outputs, and decides what to change next. In automated APE, software performs part of this loop. It runs the current prompt on evaluation data, compares outputs with labels or other evaluation signals, and identifies errors. A language model then proposes new instructions, an evaluator scores them, and a search procedure chooses which candidates to carry forward.
The central idea is therefore not simply “AI writes better wording.” Candidate generation, an objective score, data for comparing candidates, and search or selection rules are combined to explore the space of natural-language instructions. A candidate that sounds fluent is not useful if it scores poorly on the actual task.
Three representative approaches
APE: Generate instructions from examples and select by execution score
Zhou and colleagues framed Automatic Prompt Engineer (APE) as natural-language program synthesis and black-box search. A language model proposes multiple instructions from input-output examples; the target model executes each candidate and the resulting performance is scored. More iterative variants generate semantically similar instructions around promising candidates and search again. In their experiments across 24 language tasks, the authors reported that APE instructions matched or exceeded human-written instructions on 19 tasks in a zero-shot setting. This is a result for those data, models, and evaluation procedures, not evidence that APE replaces people across all tasks.
ProTeGi: Use error explanations as “textual gradients”
ProTeGi asks a language model to describe errors made by the current prompt on small batches of data, then uses that description to propose revisions addressing the prompt’s weaknesses. The paper calls this natural-language critique a textual gradient. It does not compute a numeric derivative or access model-internal gradients. It expands candidates in multiple directions and uses beam search and bandit selection to choose which candidates to continue. The original paper reported up to a 31% improvement over its starting prompt in preliminary experiments across four tasks. That result is specific to the studied tasks and settings; it should not be treated as a general performance expectation.
OPRO: Provide prior candidates and scores as context for the next generation
Optimization by PROmpting (OPRO) describes the objective and constraints in natural language and places a history of earlier candidates and their scores in a meta-prompt. The optimizer model uses that history to propose new candidates; their evaluation results become context for the next step. Balancing further exploration of promising candidates with attempts in new directions matters. The OPRO paper reports improvements for particular mathematical-reasoning benchmarks and model combinations, but those numbers do not automatically transfer to other tasks, models, or evaluation conditions.
All three approaches generate candidates and select them based on execution results, but they differ in how they produce and search candidates. APE starts from instruction induction using examples and candidate selection. ProTeGi uses language-based descriptions of errors to guide revisions. OPRO uses the trajectory of prior candidates and scores as context for subsequent generation.
A small workflow example
Hypothetical example: Suppose the task is to classify customer inquiries as refund, shipping, or account.
A person defines the categories and output format, then prepares inquiries and correct labels representative of intended use.
A candidate generator proposes instructions, perhaps clarifying category definitions or adding a tie-break rule when two categories are mentioned.
Each candidate runs on the same evaluation inputs. The system computes accuracy or per-category recall and checks category-level outcomes as well as the average so that one important error type is not hidden.
The search procedure carries high-scoring candidates into the next round and proposes revisions based on error cases.
The selected candidate is checked against a separate validation set not used during development. If validation is worse or a required constraint is violated, discard the candidate and return to the baseline prompt.
Parts of this workflow can be automated, but the quality of labels and the scoring function still matter. If errors in one category cause more harm than errors in another, optimizing plain accuracy can select a prompt with a high overall score that is still unsuitable for production.
A practical design sequence
1. Choose what to automate. Decide whether the search changes only the instruction or also searches over example selection and order, or output constraints. Searching many components at once increases the number of candidates and cost and makes it harder to identify causes.
2. Separate objectives from constraints. Distinguish scores such as accuracy, omission rate, and format validity from mandatory requirements such as safety, privacy, or an allowed vocabulary. Do not combine a hard requirement into an average score where a failure can be offset by success elsewhere.
3. Prevent data leakage. Separate development data used to generate candidates from validation data used for a final decision. If possible, keep the final test set untouched until the search ends. Repeatedly showing the same cases can cause the search procedure to select instructions that memorize answers rather than general rules.
4. Set search costs and stopping rules. Choose the number of candidates, iterations, model-call budget, maximum latency, and stopping criteria. More candidates do not guarantee better performance, and they increase evaluation costs and review effort.
5. Keep reproducible records. Save candidate prompts, the generation and evaluation models, dataset version, sampling settings, scores, and failure examples. Random generation and external models may mean the same candidates and rankings cannot be reproduced every time.
6. Have a person approve operational use. Do not deploy the highest-scoring candidate without review. Check whether its meaning is clear, safety restrictions remain intact, and edge cases are acceptable. Use a limited rollout and monitoring, with a way to restore the prior version if problems arise.
Limitations and risks
**Overfitting to evaluation scores:** Repeated search may favor instructions that fit only development examples. Check generalization on held-out data and operational samples.
**Proxy metrics:** A metric that is easy to measure may differ from actual user value. Consider multiple metrics, hard constraints, and the cost of different errors.
**Search costs:** Candidate generation and evaluation each require model calls. Start with a small evaluation batch and call budget, then use larger validation only for promising candidates.
**Model and data dependence:** A candidate may rank differently if the generator and the deployed model behave differently. Re-evaluate finalists with the production model and settings.
**Personal and confidential data:** Before sending user data to an external generator or evaluator, check organizational data-handling requirements. Remove sensitive information where possible or evaluate in an approved environment.
**Fluency versus performance:** Awkward wording can score well, while a prompt that reads naturally is not necessarily more effective. Consider maintainability and reviewability alongside task scores.
When should you use APE, and when not?
APE can reduce search time for stable tasks with repeated inputs and clear labels or scoring rules, provided candidate prompts can be executed automatically. If the objective is not agreed upon, there is no evaluation data to determine what counts as good, or a small prompt change has major safety or legal consequences, first establish the objective and review process. Decide whether automated search is worthwhile or whether a person should test a few hypotheses directly, accounting for model-call costs and operational burden.
Sources
[Zhou et al., “Large Language Models Are Human-Level Prompt Engineers” (2022)](https://arxiv.org/abs/2211.01910) — APE candidate generation, execution-based selection, iterative search, and evaluation across 24 tasks.
[Pryzant et al., “Automatic Prompt Optimization with ‘Gradient Descent’ and Beam Search” (EMNLP 2023)](https://aclanthology.org/2023.emnlp-main.494/) — ProTeGi textual gradients, beam search, bandit-based selection, and evaluation setup.
[Yang et al., “Large Language Models as Optimizers” (ICLR 2024)](https://arxiv.org/abs/2309.03409) — OPRO meta-prompt, iterative use of candidates and scores, and exploration-exploitation trade-offs.