Prompt optimization usually works in one of two ways. Either an algorithm searches over prompt candidates, or an optimizer inspects individual examples and proposes revisions. Both approaches have limits. Search methods miss systematic patterns. Example-driven updates get swayed by local failures rather than recurring ones. And most methods lack memory, so each iteration starts fresh without learning from the last.

Megagon Labs solved this with Reflective Prompt Tuning, a framework that uses structured tool calls to let an LLM simulate the workflow of a human prompt engineer. An optimizer model calls a diagnostic function that evaluates the target model over an entire dataset, clusters recurring failure modes, and returns a structured report. The optimizer then revises the prompt based on that report plus a memory of all prior reports.

The results are solid. On multi-hop reasoning, gains reached 12.9 points. On mathematical reasoning, 12.4 points. On domain-specific numerical reasoning, 11.7 points.

The Problem with Current Approaches

Most automated prompt optimizers update prompts based on individual examples or small batches. They see one failure, propose one fix, and move on. This makes optimization sensitive to local quirks rather than systematic issues.

Some methods use fixed pipelines. ACE updates an auxiliary playbook of reusable strategies inserted into a fixed prompt template. This improves stability but limits the optimizer's ability to make arbitrary prompt-level revisions. The template constrains what can change.

Most methods also lack explicit memory over prior diagnostic reports and revisions. The optimizer treats each update in isolation. It cannot track whether a persistent failure has already been addressed or whether a previous edit made things worse.

Finally, prompt selection is typically driven by task performance alone. Confidence calibration, reliability, and broader properties sit outside the optimization criterion.

How RPT Works

Reflective Prompt Tuning has two stages. Both are implemented through LLM function calls.

Stage One: Diagnostic Feedback

The optimizer starts with a seed prompt. It invokes a diagnostic function that runs the target model on the full optimization set and returns a structured report.

The diagnostic function first collects outputs from every example in the set. It records reasoning traces, final answers, and reported confidence scores. It computes aggregate task metrics plus calibration metrics like Brier score.

Then it identifies failed examples. For each failure, a critique LLM generates concise diagnoses. Each failed instance can yield up to three diagnoses to improve coverage and reduce sensitivity to individual critiques.

Next, the function clusters semantically similar diagnoses into recurring failure topics. This aggregation compresses local critiques into a compact summary of systematic failures. Instead of "example 47 got it wrong," the report says "the model consistently misinterprets comparative statements in multi-hop questions" with representative examples.

The final diagnostic report contains the current prompt, aggregate metrics, and the clustered failure topics with summaries and representative cases.

Stage Two: Reflective Revision

Given the current diagnostic report and an accumulated memory of all prior reports, the optimizer identifies which recurring failures indicate prompt shortcomings and generates a revision.

The memory is key. By conditioning on prior reports, the optimizer tracks persistent failures, previous revision attempts, and performance changes over time. A prompt edit might improve some metrics while worsening others. A failure might require several revisions to resolve. Repeated failures might indicate ineffective prior edits. The memory makes credit assignment possible across iterations.

The optimizer treats diagnostic reports as evidence. It inspects aggregate metrics, recurring failure topics, representative examples, and previous prompt changes. Then it produces the next prompt.

Confidence-Aware Optimization

RPT also supports confidence-aware optimization. The target model reports confidence alongside its answers. The diagnostic function assesses whether that confidence is appropriate. Overconfident wrong answers and underconfident right answers both get flagged.

The final prompt selection jointly accounts for task performance and calibration. It rewards higher task scores while penalizing miscalibration through a negative Brier-score term. This means the selected prompt is not just accurate. It is also well-calibrated.

The Numbers

Megagon Labs tested RPT on three reasoning tasks with GPT-4.1 as the target model.

On HotPotQA, a multi-hop reasoning benchmark, RPT improved by 12.9 points over the initial prompt. On LiveBench-Math, a mathematical reasoning benchmark, it improved by 12.4 points. On Formula, a domain-specific numerical reasoning task, it improved by 11.7 points.

These gains remained competitive against state-of-the-art baselines including ACE, GEPA, and MIPRO. RPT did not just improve over the starting prompt. It matched or exceeded dedicated optimization systems.

Confidence-aware experiments showed that incorporating calibration signals into both diagnostic feedback and final prompt selection improved calibration alongside task performance.

Why Function Calling Matters

The function-call interface is not just implementation detail. It shapes what the optimizer can do.

By packaging the diagnostic as a callable function, RPT treats evaluation as a structured operation with defined inputs and outputs. The optimizer does not free-text its way through analysis. It invokes a function, gets a report, and reasons over structured data. This is closer to how engineers actually debug prompts than open-ended text generation.

The structured report format also makes the feedback actionable. Failure topics are labeled, described, and accompanied by representative examples. The optimizer can target specific failure modes rather than guessing at what went wrong.

Memory persistence means the optimizer builds a model of the prompt's failure landscape over time. This turns optimization from a series of independent guesses into a directed search.

What the Optimizer Learns

Analysis of optimization traces shows that RPT produces targeted prompt revisions aligned with diagnosed failure modes. When the diagnostic reports that the model misinterprets comparative statements, the next prompt adds explicit instructions about handling comparisons. When calibration issues surface, the next prompt adds confidence-guidance language.

The revisions are interpretable. You can read the diagnostic report and see why the prompt changed. This matters for production teams who need to understand and approve prompt changes rather than blindly accepting black-box optimizer outputs.

The Bottom Line

Reflective Prompt Tuning is a practical advance in automated prompt optimization. It addresses the core limitations of existing methods by using structured diagnostics, accumulated memory, and confidence-aware selection.

The function-call architecture is the right choice for this problem. It gives the optimizer structured visibility into systematic failures and the ability to track revisions over time. The 12.9, 12.4, and 11.7 point gains show that this approach works across different reasoning domains.

For teams building on API-gated models, this is a credible path to better prompts without manual trial and error. The code is open source at github.com/megagonlabs/RPT.


Suggested title: This Framework Uses Tool Calls to Teach LLMs to Fix Their Own Prompts

Word count: ~970

Fact-check notes:

  • Paper: "Reflective Prompt Tuning through Language Model Function-Calling" by Farima Fatahi Bayat, Moin Aminnaseri, Pouya Pezeshkpour, Estevam Hruschka, Megagon Labs. arXiv:2605.21781, May 2026. Verified from arXiv.
  • Gains: HotPotQA +12.9 points, LiveBench-Math +12.4 points, Formula +11.7 points confirmed from abstract and results section.
  • Compared against ACE, GEPA, and MIPRO confirmed from experimental setup section.
  • GPT-4.1 as target model confirmed from experimental setup.
  • Code released at https://github.com/megagonlabs/RPT confirmed from abstract.
  • Confidence-aware optimization with Brier score confirmed from methodology and results sections.
  • ClusterFusion for failure mode clustering confirmed from methodology section 2.2.1.