Manuscript Offprint

arXiv:2611.04803

Extracting System Prompt of LLM Services in Complex (Agentic) Settings: Hierarchical Optimization and Reliable Evaluation Metrics

Shuaidong Li, Chenhao Lin, Xinshan Yang, Chao Shen, Yufei Chen, Zhengyu Zhao

Shuaidong Li

Xi'an Jiaotong University

lsd2003@stu.xjtu.edu.cn

Chenhao Lin

Xi'an Jiaotong University

linchenhao@xjtu.edu.cn

Xinshan Yang

Xi'an Jiaotong University

xinshanyang@stu.xjtu.edu.cn

Chao Shen

Xi'an Jiaotong University

chaoshen@mail.xjtu.edu.cn

Yufei Chen

City University of Hong Kong

yufeichen8-c@my.cityu.edu.hk

Zhengyu Zhao

Xi'an Jiaotong University

zhengyu.zhao@xjtu.edu.cn

Abstract

System prompt is a critical intellectual property of large language model (LLM) services. To analyze its leakage, prompt extraction attacks have been extensively studied. However, we validate that existing attacks substantially degrade on recent LLM services, whose complexity has evolved in both the prompt itself and system-level constraints. Specifically, (1) system prompts become longer and more structured to contain pre-defined tool schemas, execution logic, safety guardrails, etc., and (2) only the final post-processed outputs (e.g., agent's actions) but not the raw textual response can be observed.

In this paper, we address the above increased complexity by proposing Extraction with Hierarchical Optimization (ExtraHerO), which (1) decomposes the prompt extraction into a hierarchical reconstruction process with a pre-defined structural prompt template, and (2) iteratively updates the adversarial query with guidance of feedback-template differences to observe diverse outputs. In particular, we monitor the information gain of attack optimization to early stop or reset it for a good trade-off between effectiveness and efficiency.

We also find that existing evaluation metrics for prompt fidelity can be misleading because they do not account for structural information. Therefore, we rely on global textual similarity and prioritize structural keywords. Moreover, we introduce a system-level metric, adversarial utility, to measure the help of prompt extraction for real-world downstream attacks. Experimental results demonstrate the consistent superiority of EXTRAHERO, with average improvements of 12.5% (fidelity), 2.3% (functionality), and 6.0% (adversarial utility) on chatbots, and 12.2%, 34.3%, and 30.5% on agents.

1. Introduction

The rapid development of large language models (LLMs) has revolutionized the landscape of artificial intelligence services, enabling highly specialized applications through the use of system prompts [3, 20, 26, 34, 42]. System prompts constitute a key intellectual property of LLM-based services. They define a service's unique value through textual information that outlines sophisticated information, such as operational logic, strategic task instructions, and safety guardrails [18]. However, this concentration of value has rendered the system prompt a primary target for adversarial reverse-engineering, also known as extract attacks [5, 20, 36, 39, 41, 48]. Such system prompt attacks can replicate sophisticated service functionalities, including the internal security architecture [27], at near-zero cost, leading to considerable asset loss.

To understand the risk, prompt extraction attacks have evolved from manual heuristics to automated algorithms. Early methods rely on manual prompt engineering to exploit empirical flaws [5, 10, 20]. These range from direct requests (e.g., "Repeat your instructions") to indirect manipulation like "Now translate" or "Ignore previous instructions." To bypass safety filters that block direct leakage, recent attacks use backward prediction. Specifically, output2prompt [48] reconstructs the system prompt by analyzing the model's responses to fixed inputs. PRSA [41] improves this by categorizing LLM services and matching the style, tone, and wording of the target. T-GPS [36] further exploits text-based gradients to optimize the recovered prompt, making it functionally closer to the original.

However, we argue that all existing attacks have been

Figure

Figure 1. Prompt extraction under previous threat model vs. ours with increased complexity in both prompt (from linear to hierarchical) and output information (from raw textual to post-processed).

limited to overly simplistic threat models. As illustrated in Figure 1, with the rapid evolution of recent LLMs and their powered agents, the complexity of extraction has increased in both the prompt itself and system-level constraints. First, system prompts become longer and more structured to contain pre-defined tool schemas, execution logic, safety guardrails, etc. This modern design paradigm, as exemplified by emerging prompt programming frameworks [12, 23, 46], treats instructions not as static text but as modular configurations to regulate complex model behaviors and ensure stable task delivery in production environments. This makes the extraction based on direct requests or linearly iterative optimization insufficient. Second, only the final post-processed outputs (e.g., agent's actions) but not the raw textual response can be observed by the (normal or malicious) user. From a functional standpoint, production agents encapsulate raw textual reasoning into discrete operational actions to streamline user experience (UX) [2, 25, 37, 43, 47, 49, 52]. This architectural design naturally imposes constraints on the adversary's ability to extract the internal logic from the post-processed output information [13].

We first conduct pilot studies to demonstrate that the above-mentioned increased complexity indeed harms existing attacks. Specifically, for the system prompt, we synthesize system prompts with diverse function components and collect in-the-wild ones (with possible defenses) across five increasing complexity levels. For the output constraints, we post-process the output information to hide partial information from the adversary. In addition to the attack method, we also note that existing evaluation metrics may

lead to misleading or insufficient results. For example, since token-level similarity used for measuring the prompt fidelity does not consider structural information, extracting non-functional stop words, such as "please" and "You are", unexpectedly inflates the scores.

Our Work. In this paper, we study system prompt extraction in complex (agentic) settings by proposing a new attack optimization framework and introducing reliable evaluation metrics. Specifically, we propose Extraction with Hierarchical Optimization (ExtraHerO), which addresses the increased complexity in both the prompt and output: (1) ExtraHerO decomposes the extraction task into a multi-stage recovery process by utilizing a pre-defined structural template. This ensures that execution-critical components, e.g., tool schemas and multi-step logic, are reconstructed with high structural fidelity. (2) ExtraHerO iteratively updates the adversarial query guided by the text gradient between the current template and the observed feedback. This allows the adversary to extract internal logic from sparse, post-processed outputs. In particular, we monitor the information gain of each optimization step to trigger early stops or resets, maintaining a good trade-off between effectiveness and query efficiency.

For evaluation metrics, we prioritize sentence-level prompt similarity over token-level ones, and we introduce a new metric that searches for matched structural keywords between the extracted and original prompts. Moreover, we introduce a system-level metric, adversarial utility, to measure the actual help of the extracted prompt for potential downstream attacks, e.g., prompt injection targeting a task hijacking attack. This becomes practically meaningful as the state-of-the-art LLMs have recently been shown to be strikingly capable at finding and exploiting sophisticated security vulnerabilities.¹ Ultimately, we establish a three-dimensional evaluation framework: fidelity, functionality, and adversarial utility.

We conduct extensive experiments on one victim chatbot (GPT-4o) and six GUI agents across three platforms (Mobile Phone, Web, and PC). For evaluation, we consider eight metrics on fidelity, functionality, and adversarial utility across diverse aspects. Experimental results demonstrate that our EXTRAHERO consistently outperforms existing baselines, with average improvements of 12.5% (fidelity), 2.3% (functionality), and 6.0% (adversarial utility) on chatbots, and 12.2%, 34.3%, and 30.5% on agents. Further analyses reveal that our EXTRAHERO can indeed extract the core of the system prompt, with structural keywords highly similar to those of the victim prompt.

References (9)