This article is published in English.
Reverse Prompting: Turning a Good LLM Session into a Reusable Prompt
Learn how to extract a one-shot prompt from a successful multi-turn LLM conversation, and how prompt inversion recovers prompts when you only have the outputs.
The prompt that finally works is rarely the one you started with: it emerges over several rounds of corrections, then vanishes when you close the chat. Reverse prompting inverts the usual workflow. You first reach an output you like, then have the model reconstruct the instructions that would reliably produce it. You will learn how to capture those instructions from a conversation, how to recover an approximate prompt from outputs alone, and where both techniques stop being trustworthy.
Why the working prompt usually gets lost
Reverse prompting relies on current frontier models being good at reflecting on their own context. It works by hand in a chat window or scripted as a pipeline step, and it pays off most in code generation and agentic workflows, where requirements are numerous and easy to forget.
A typical refinement loop
Picture a developer adding a user registration endpoint to a Python service. The goal is production-quality FastAPI code: validated input, structured error responses, logging and tests. The opening request is deliberately loose, something like "write a FastAPI endpoint for user registration", and the first answer is predictably minimal. So the developer iterates:
- Ask for Pydantic models that validate the email address and enforce password strength.
- Ask for proper HTTP exceptions plus logging.
- Ask for a single, consistent JSON shape for every error.
- Ask for pytest cases that cover the happy path and the validation failures.
A few turns later the code meets the team's bar, gets copied into the repository, and the conversation is forgotten, along with every constraint and correction that shaped it. The next endpoint starts again from a vague one-liner.
Extracting a one-shot prompt from the conversation
The conversation-based version of reverse prompting fixes this with one extra message. Once the output is right, you append a meta-instruction asking the model to review the whole exchange and compress it into a single self-contained prompt. The instruction below spells out what that prompt has to contain: the role, the accumulated context, every agreed constraint, the output format, the quality criteria and any examples. Notice the last two lines. They ask for the prompt alone, with no commentary, so the result can be pasted straight into a fresh session.
Now that we have reached this final output, reverse-engineer
the entire conversation. Look at every correction, added constraint,
tone adjustment, format decision, and the result.
Produce one complete, standalone prompt that would generate
this exact output quality in a single shot with no follow-ups.
The prompt must explicitly state:
- Role and expertise level
- All background context and requirements established
- Every constraint and rule settled on
- Output format and structure
- Tone, style, and quality criteria
- Any examples or reference patterns used
Output only the prompt itself, ready to copy into a fresh session.
No explanation.
What comes back is an explicit specification of everything implicit in the dialogue. A fresh session using it should produce comparable output on the first try; if it still needs follow-ups, something was missed.
Treat the result as an engineering asset:
- Commit it to the repository next to the code it produces, so changes are reviewed and versioned.
- Share it with teammates so the same standards apply regardless of who runs it.
- Replace the task-specific parts with parameters (entity name, fields, error codes) and reuse it for similar endpoints.
Recovering a prompt from outputs alone
The conversational method only works while you still have the history. A more general technique, known as prompt inversion or reverse prompt engineering (RPE), reconstructs an approximation of the prompt from nothing but the text it generated.
Stated formally: some hidden prompt X produced an output O. You want to find a prompt P whose output N is semantically and functionally close to O. You have black-box access only, meaning no logits and no training data, just the ability to send prompts and read responses.
Sampling candidates and validating them
The straightforward approach has three steps:
- Give the model the output O and ask it to infer the prompt behind it. A single guess tends to overfit or invent constraints that were never there, so do this several times with different temperatures and sampling settings to collect a pool of candidate prompts.
- Run each candidate through the model and compare what it generates with O, using an overlap metric such as ROUGE-1 (the F1 score over shared unigrams).
- Keep the candidate with the highest score.
The validation step is what makes this more than guessing: you measure which candidate actually reproduces the target instead of trusting the model's opinion.
Evolving candidates like a genetic algorithm
You can also treat the candidates as a population and evolve them. Each generation:
- Measure fitness as the average similarity between the outputs a candidate regenerates and the original O.
- Keep the best performers.
- Mutate the weaker ones by giving the model the current prompt together with the observed differences, and letting it rephrase, add or remove constraints, or restructure it.
- Repeat until the scores stop improving or the budget runs out.
This approach needs no training. It has been reported to yield coherent, reusable prompts from just five sample outputs, and closed-source models are no obstacle, since all it needs is an inexpensive similarity function.
Limits you should plan for
Reconstruction is always an approximation. The recovered prompt is often longer and more explicit than whatever produced the output, because it has to state nuances that were originally carried by context.
Two more caveats:
- Reverse-engineered prompts encode the quirks of the model they were derived from. A prompt tuned against one model may need adjustment before it performs as well on another, so re-validate when you switch.
- Lexical metrics like ROUGE-1 reward shared words, not correct behaviour. For code or structured output, consider adding checks that matter to you, such as whether the generated tests pass or the JSON validates, alongside the similarity score.
Where coding agents fit in
Agentic coding tools such as OpenAI Codex and Claude Code are optimized for the forward direction, with strong context engineering, long agent loops and heavy use of prompt caching. Still, both can stop before writing code and ask you clarifying multiple-choice questions, which surfaces requirements early. Iterate with the agent until the result is strong, then extract a master prompt from that session for one-shot reuse. For a complementary way to structure the prompts you extract, see our guide to building production-ready LLM prompts with a seven-layer framework.
Key takeaways
- A long prompting session's real value is its accumulated constraints; capture them before closing the chat.
- A single meta-instruction at the end of a successful conversation turns it into a standalone, versionable prompt.
- When only outputs exist, generate many candidate prompts and let a similarity metric, not the model's confidence, pick the winner.
- Recovered prompts are approximations tied to a specific model, so validate them in a fresh session and again whenever the model changes.