Reverse Prompt Engineering: A Zero-Shot, Genetic Algorithm Approach to Language Model Inversion

Hanqing Li, Diego Klabjan2025

Abstract

We explore the problem of Language Model Inversion (LMI) under strict black-box, zero-shot, and limited data conditions. Traditional methods often require large volumes of output data and extensive training, which is frequently unrealistic in real-world scenarios. We propose Reverse Prompt Engineering (RPE), a training-free framework that reconstructs prompts using as few as five text outputs. By leveraging a large language model (LLM) as an optimizer within a genetic algorithm-inspired process, RPE consistently yields coherent and semantically meaningful prompts. Experimental results across multiple datasets, including the challenging $RE_{hard}$ benchmark, indicate that RPE achieves higher-quality recovery and functional alignment than current state-of-the-art methods.

Introduction: The Challenge of Prompt Inversion

In the "LLM-as-a-service" ecosystem, the specific instructions or "system prompts" used to guide models are often proprietary. Deducing these hidden prompts from model outputs—Language Model Inversion—is a critical task for security auditing and understanding model behavior. However, existing LMI methods suffer from two primary flaws:

  1. Data Hunger: They typically require a large number of outputs (e.g., 64) to recover a single prompt.
  2. Poor Generalization: Most rely on models trained on specific datasets. When faced with "out-of-domain" prompts, they often produce garbled, non-linguistic text.

We introduce a "black-box" approach that operates without access to model weights or prior training, succeeding even under strict data constraints.

Methodology: The RPE Framework

The core of our approach is $RPE_{GA}$, an iterative framework that treats prompt reconstruction as an optimization problem. Unlike traditional Genetic Algorithms (GA) that use random mutations, $RPE_{GA}$ utilizes the reasoning capabilities of an LLM to perform semantic edits.

The Optimization Loop

The process follows a classic optimization cycle:

  • Initialization: The LLM is provided with a few sample outputs and asked to speculate on the underlying prompt, generating a population of candidate prompts.
  • Evaluation: Candidate prompts are fed back into the LLM to generate new outputs. These are compared to the original target outputs. The researchers found that the most effective evaluation metric combines the mean and the maximum similarity scores (e.g., ROUGE-1), helping identify prompts that can replicate at least one of the diverse outputs in the set.
  • Mutation and Crossover: The LLM acts as the mutation operator. It identifies differences between the candidate's outputs and the target outputs, summarizes those differences, and uses that summary to revise and improve the candidate prompts.
The detailed mutation and revision process used in RPE to iteratively improve candidate prompts.

Experimental Setup

We evaluated RPE across five diverse datasets: Awesome ChatGPT Prompts (role-based), MetaMathQA (mathematical), TruthfulQA (fact-checking), Alpaca-GPT4 (general instructions), and Dolly Creative Writing. To specifically test complexity, we developed two benchmarks: $RE_{easy}$ and $RE_{hard}$, consisting of 100 simple and 100 complex system instructions, respectively.

Key Results and Performance

RPE consistently outperformed the state-of-the-art baseline, output2prompt, across all metrics. Notably, RPE achieved these results using only 5 outputs, while the baseline required 64 to reach peak performance.

On the $RE_{hard}$ dataset, RPE showed a marked improvement in cosine similarity, demonstrating its ability to capture complex system instructions that typically cause other models to fail.

Comparison of Cosine Similarity (%) on the RE_hard dataset between RPE and output2prompt across different target models.

Human Evaluation and Utility

Beyond quantitative metrics, human evaluators preferred RPE-recovered prompts in creative and marketing tasks. In a study comparing marketing plans and song lyrics, RPE-generated content saw a 90.5% preference rate over standard high-quality templates. This suggests that RPE captures the "intent" of the original prompt more effectively than previous methods.

Discussion and Limitations

A key insight from this research is the effectiveness of the LLM as its own optimizer. By performing semantic edits based on observed errors, the model avoids the "garbled text" issue common in T5-based inversion models.

However, RPE is not without limitations. The iterative nature of the genetic algorithm requires multiple LLM calls, making it computationally more expensive and slower than single-pass models. Additionally, the quality of recovery is highly dependent on the "signal" in the target outputs; if a model provides extremely brief or uninformative responses, the inversion process becomes significantly more difficult.

Conclusion

Reverse Prompt Engineering represents a significant step forward in zero-shot language model inversion. By combining genetic algorithms with the reasoning power of LLMs, we can recover complex instructions with minimal data, opening new avenues for model auditing and synthetic data generation.

Summary updated

BibTeX
@misc{li2025reverse,
  author = {Hanqing Li and Diego Klabjan},
  title = {{Reverse Prompt Engineering: A Zero-Shot, Genetic Algorithm Approach to Language Model Inversion}},
  year = {2025},
  doi = {10.18653/v1/2025.emnlp-main.1333},
}
Figure