Exfiltrating an LLM agent's runtime context -- such as the user prompt, execution trajectory, and tool list -- poses severe security and privacy risks to users. Such attacks can be carried out via malicious tools and typically require three conditions: (1) the agent selects the malicious tool for task execution, (2) the agent passes its runtime context as input arguments to the tool, and (3) the tool's implementation transmits these inputs to an attacker-controlled endpoint. Existing work primarily focuses on conditions (1) and (3), leaving condition (2) largely unexplored, despite its critical role in enabling successful context exfiltration. In this work, we bridge this gap by developing ContextLeak, a malicious tool attack that induces the agent to both select the tool and disclose its context as input arguments. We realize this attack by carefully crafting the tool's name and description using reinforcement learning. Specifically, ContextLeak employs an LLM, referred to as the attack LLM, to automatically generate the malicious tool's name and description. To improve attack effectiveness, we fine-tune the attack LLM via reinforcement learning on a set of shadow users with diverse, simulated agent contexts. Our key technical contribution is the design of novel reward functions tailored to the context exfiltration objective, enabling effective reinforcement-learning-based fine-tuning of the attack LLM. Extensive evaluation demonstrates that our attack remains highly effective even when the shadow users' contexts differ substantially from those of the victim users. Moreover, ContextLeak significantly outperforms existing malicious tool attacks when adapted to this setting.
Figures & tables
Figure 1: Illustration of the context exfiltration attack pipeline.
Figure 2: Illustration of fine-tuning the attack LLM in ContextLeak.
Method
User Prompt
Conversation History
Tool List
MTSR
EDS
ES
MTSR
EDS
ES
MTSR
Prec.
Rec.
F1
Combined Attack
0.14
0.84
0.96
0.15
0.57
0.85
0.11
0.59
0.39
0.45
ObliInjection
0.80
0.77
0.93
0.82
0.58
0.85
0.71
0.71
0.51
0.55
PAIR
0.20
0.84
0.96
0.10
0.34
0.71
0.05
0.40
0.20
0.26
TAP
0.20
0.70
0.92
0.17
0.58
0.80
0.10
0.40
0.26
0.30
JudgeDeceiver
0.65
0.75
0.93
0.62
0.59
0.85
0.51
0.80
0.59
0.65
Table 1: Performance of baselines and ContextLeak in exfiltrating different types of context.
LLM
User Prompt
Conversation History
Tool List
MTSR
EDS
ES
MTSR
EDS
ES
MTSR
Prec.
Rec.
F1
GPT-OSS-20B
0.63
0.94
0.98
0.66
0.71
0.86
0.60
0.92
0.73
0.80
Gemma-4-E4B-it
0.71
0.99
1.00
0.70
0.96
0.99
0.60
0.85
0.78
0.80
Qwen-3.5-9B
0.82
1.00
1.00
0.77
0.83
0.97
0.70
0.98
0.83
0.87
GPT-4.1
0.60
0.98
1.00
0.65
0.90
0.96
0.56
0.99
0.87
0.91
GPT-5-mini
0.81
0.91
0.97
0.65
0.94
0.99
0.66
0.94
0.92
0.92
Table 2: Transferability of ContextLeak across different backend LLMs.
Victim User Type
User Prompt
Conversation History
Tool List
MTSR
EDS
ES
MTSR
EDS
ES
MTSR
Prec.
Rec.
F1
IDID
0.92
0.99
1.00
0.89
0.85
0.96
0.86
0.76
0.65
0.68
IDOD
0.82
1.00
1.00
0.82
0.81
0.94
0.76
0.62
0.54
0.57
ODID
0.87
0.99
1.00
0.90
0.90
0.96
0.88
0.87
0.82
0.84
ODOD
0.83
0.98
0.99
0.74
0.88
0.96
0.70
0.93
0.87
0.89
Table 3: Transferability of ContextLeak across victim users from different domains and datasets. IDID, IDOD, ODID, and ODOD represent in-dataset-in-domain, in-dataset-out-domain, out-dataset-in-domain, and out-dataset-out-domain, respectively.
Figure 3: Impact of (a) user prompt length, (b) number of conversation history rounds, and (c) number of benign tools on the attack performance of ContextLeak for each corresponding context type. (d) Sensitivity of ContextLeak to the reward-balancing weight λ on the conversation history context.
Defense
Utility
User Prompt
Conversation History
Tool List
CTSR
IAQ
MTSR
EDS
ES
MTSR
EDS
ES
MTSR
Prec.
Rec.
F1
Llama-3-8B-Instruct
0.18
3.55
0.43
0.69
0.90
0.67
0.30
0.53
0.34
0.60
0.38
0.44
StruQ
0.15
2.83
0.08
0.75
0.95
0.06
0.19
0.37
0.09
0.17
0.08
0.10
SecAlign
0.17
2.57
0.10
0.13
0.28
0.06
0.27
0.45
0.09
0.43
0.21
0.27
Meta-SecAlign
0.17
3.30
0.42
0.68
0.91
0.68
0.30
0.52
0.33
0.78
0.37
0.49
Table 4: Performance of ContextLeak against prevention-based defenses.
Detection
User Prompt
Conv. History
Tool List
FPR
FNR
FPR
FNR
FPR
FNR
PromptGuard
0.02
1.00
0.01
0.99
0.02
1.00
DataSentinel
0.01
1.00
0.00
1.00
0.00
1.00
PromptArmor
0.00
1.00
0.00
0.97
0.00
0.98
Ant MCPScan
0.01
1.00
0.01
1.00
0.01
1.00
Table 5: FPR and FNR of detection-based defenses against ContextLeak.
Appendix figures & tables6 assets
Supplementary material from the paper’s appendix.
Appendix
Figure 4: Evolution of average argument reward Rarg on victim users during fine-tuning of the attack LLM across different types of context.
Figure 10
Figure 5: Our system prompt used for guiding the attack LLM to generate tool name and description.
Figure 6: Prompt used for the strategy-extraction LLM.
Table 6: Performance of ContextLeak on victim users for each domain under in-dataset-in-domain and in-dataset-out-domain settings.
Figure 7: Prompt used for evaluating input argument quality (IAQ).
Artificial intelligence agent security studies attacks and safeguards for tool-using, persistent, and collaborative systems across digital and physical settings.