SPEAR: Code-Augmented Agentic Prompt Optimization

arXiv cs.CL Papers

Summary

SPEAR is a code-augmented agentic prompt optimizer that uses a Python sandbox for structural error analysis, achieving state-of-the-art performance on multiple LLM evaluation suites including industrial judge tasks, BBH, and GSM8K.

arXiv:2605.26275v1 Announce Type: new Abstract: Automatic prompt engineering (APE) rewrites prompts to improve downstream task performance, but existing APE loops treat the optimizer itself as a fixed pipeline. We port the code-as-action paradigm of CodeAct (Wang et al., 2024a) to APE and propose SPEAR (Sandboxed Prompt Engineer with Active Roll-back), a free-form agentic optimizer with four tools -- evaluate, python, set_prompt, finish -- that decides autonomously how and when to use them. The distinctive tool is the Python sandbox: the optimizer writes and executes arbitrary Python on the current evaluation DataFrame, performing structural error analysis (confusion matrices, error clustering, per group metrics) the agent itself authors. Two guardrails turn the long-horizon agent into a monotone-improving optimizer: auto-rollback on metric regression, and an optional guard metric floor. We evaluate on three industrial LLM-as-judge suites (13 judge tasks across recruiter-intake, conversational-memory, and query-refinement systems) plus seven BBH tasks and GSM8K. SPEAR wins every industrial task on the primary metric ($\kappa$ 0.857 vs 0.359 on tool-selection; F1-macro 0.815 vs 0.763 on filter-relevance; $\kappa$ 0.254 vs 0.218 on the hardest extraction dimension). On BBH-7 SPEAR averages 0.938 accuracy vs GEPA 0.628 and TextGrad 0.484. Ablations show the Python tool is the largest single lever on complex judge tasks ($\Delta \approx +0.79\kappa$ on the 5-class tool-selection judge, $\Delta \approx +0.35\kappa$ on the hardest extraction dimension when removed); its irreplaceable contribution is class-pair confusion aggregation that a long-context LLM cannot extract reliably from the raw eval DataFrame.
Original Article
View Cached Full Text

Cached at: 05/27/26, 09:02 AM

# SPEAR: Code-Augmented Agentic Prompt Optimization
Source: [https://arxiv.org/abs/2605.26275](https://arxiv.org/abs/2605.26275)
[View PDF](https://arxiv.org/pdf/2605.26275)

> Abstract:Automatic prompt engineering \(APE\) rewrites prompts to improve downstream task performance, but existing APE loops treat the optimizer itself as a fixed pipeline\. We port the code\-as\-action paradigm of CodeAct \(Wang et al\., 2024a\) to APE and propose SPEAR \(Sandboxed Prompt Engineer with Active Roll\-back\), a free\-form agentic optimizer with four tools \-\- evaluate, python, set\_prompt, finish \-\- that decides autonomously how and when to use them\. The distinctive tool is the Python sandbox: the optimizer writes and executes arbitrary Python on the current evaluation DataFrame, performing structural error analysis \(confusion matrices, error clustering, per group metrics\) the agent itself authors\. Two guardrails turn the long\-horizon agent into a monotone\-improving optimizer: auto\-rollback on metric regression, and an optional guard metric floor\. We evaluate on three industrial LLM\-as\-judge suites \(13 judge tasks across recruiter\-intake, conversational\-memory, and query\-refinement systems\) plus seven BBH tasks and GSM8K\. SPEAR wins every industrial task on the primary metric \($\\kappa$ 0\.857 vs 0\.359 on tool\-selection; F1\-macro 0\.815 vs 0\.763 on filter\-relevance; $\\kappa$ 0\.254 vs 0\.218 on the hardest extraction dimension\)\. On BBH\-7 SPEAR averages 0\.938 accuracy vs GEPA 0\.628 and TextGrad 0\.484\. Ablations show the Python tool is the largest single lever on complex judge tasks \($\\Delta \\approx \+0\.79\\kappa$ on the 5\-class tool\-selection judge, $\\Delta \\approx \+0\.35\\kappa$ on the hardest extraction dimension when removed\); its irreplaceable contribution is class\-pair confusion aggregation that a long\-context LLM cannot extract reliably from the raw eval DataFrame\.

## Submission history

From: Huimin Han \[[view email](https://arxiv.org/show-email/73d3999a/2605.26275)\] **\[v1\]**Mon, 25 May 2026 19:01:10 UTC \(327 KB\)

Similar Articles

From Monolithic to Modular: Segment-level Automatic Prompt Optimization

arXiv cs.AI

This paper introduces SAPO, a segment-level automatic prompt optimization method that decomposes prompts into role, context, tasks, and output format, then applies targeted improvements based on weak and strong examples. Evaluated across several benchmarks, SAPO outperforms strong APO baselines including APE, OPRO, EvoPrompt, GEPA, and StraGO on GPT-3.5-Turbo and GPT-4o-mini.

SePO: Self-Evolving Prompt Agent for System Prompt Optimization

arXiv cs.CL

SePO (Self-Evolving Prompt Optimization) proposes a self-referential prompt agent that optimizes both task agents' system prompts and its own system prompt through an evolutionary search, outperforming Manual-CoT, TextGrad, and MetaSPO across five benchmarks including AIME'25, ARC-AGI-1, and GPQA.

Self-Supervised Prompt Optimization

Papers with Code Trending

This paper introduces Self-Supervised Prompt Optimization (SPO), a framework that optimizes prompts for LLMs without external references by using output comparisons, significantly reducing costs and data requirements.

SAGE: Stochastic Prompt Optimization via Agent-Guided Exploration

arXiv cs.CL

Introduces SPO, a stochastic search framework for automatic prompt optimization, with three strategies including SAGE, an agent-guided multi-agent pipeline. Evaluated on benchmarks and deployed on a mental-health chatbot, showing improvements in retention through continuous optimization.