Introduction
Large Language Models (LLMs) are notoriously sensitive to the phrasing of instructions. Currently, "prompt engineering" is largely a manual, trial-and-error process—often described as "voodoo"—that requires significant human expertise to achieve optimal results. While automated methods exist, they typically require access to internal model weights (differentiable methods) or rely on directionless random perturbations (black-box methods).
This paper introduces ProTeGi (Prompt Optimization with Textual Gradients), an algorithm that automates this process by mimicking numerical gradient descent using natural language. By using an LLM to critique its own performance and rewrite prompts based on those critiques, ProTeGi achieves performance gains of up to 31% over initial human-written prompts.
The ProTeGi Algorithm
ProTeGi operates by creating a "textual gradient"—a natural language description of a prompt's shortcomings—to guide optimization. The framework mirrors the steps of numerical gradient descent but functions entirely within the space of natural language:
- Textual Gradients ($\nabla$): The algorithm identifies a mini-batch of training data where the current prompt failed. It prompts an LLM to analyze these errors and produce a "gradient"—a summary of the prompt's flaws (e.g., "The prompt fails to detect subtle sarcasm").
- Prompt Editing ($\delta$): A second LLM call takes the current prompt and the textual gradient to generate new candidate prompts that specifically address the identified flaws.
- Beam Search: To avoid local optima, the algorithm maintains a "beam" of the top-$b$ best-performing prompts at each iteration.
- Bandit Selection: To minimize API costs, the authors employ "Multi-armed Bandit" algorithms (such as Successive Rejects). This allows the system to quickly discard poor candidates using a small subset of data, focusing the evaluation budget on the most promising prompts.
Experimental Evaluation
The authors evaluated ProTeGi across four diverse NLP classification tasks:
- Jailbreak Detection: Identifying attempts to bypass AI safety filters.
- Ethos: Hate speech detection.
- Liar: Fake news detection.
- Sarcasm: Arabic sarcasm detection.
Key Results
ProTeGi consistently outperformed existing baselines, including directionless Monte Carlo search and Reinforcement Learning-based methods (like GrIPS), by an average of 4-8%. It also proved superior to "agentic" loops like AutoGPT, which often struggle to maintain task focus over multiple iterations.
Discussion and Insights
A core insight of this work is that LLMs are remarkably effective critics. By asking a model to describe why it failed, the optimization process becomes "directional" rather than a random walk. In this context, moving in the "opposite semantic direction" of a gradient means editing the prompt to explicitly counteract identified flaws.
Limitations and Future Work
Despite its success, ProTeGi faces challenges regarding API costs and latency due to the volume of calls required for iterative refinement. Additionally, while highly effective for classification, future research is needed to adapt these textual gradients for more complex tasks like code generation or long-form summarization. Finally, controlling the "semantic step size"—how much a prompt changes in a single iteration—remains an open area for investigation, as natural language lacks a direct equivalent to a numerical learning rate.
Summary updated