Self-Reflection Fine-Tuning: Enhancing Agent Security against Prompt Injection Attacks from Failure Experience
cs.LG, cs.CR
Submitted: 2026-10-03
Updated: 2026-10-03
Code: https://github.com/Eden-Wang1710/srft-repo
License: http://creativecommons.org/licenses/by/4.0/
The gist: Large language model (LLM) agents are increasingly deployed in tool-augmented environments, but their reliance on external inputs makes them highly vulnerable to prompt injection attacks that can
Terminology
Abstract
Large language model (LLM) agents are increasingly deployed in tool-augmented environments, but their reliance on external inputs makes them highly vulnerable to prompt injection attacks that can hijack task objectives. Existing safety alignment methods rely on static expert trajectories or preference optimization, limiting their ability to generalize to adaptive attack patterns. In this work, we propose Self-Reflection Fine-Tuning (SRFT), a training framework that enables agents to improve robustness by learning from their own failure experiences under adversarial conditions. Instead of passively imitating expert behaviors, SRFT exposes the agent to compromised trajectories constructed via injected attacks, and leverages an expert model to generate structured self-reflection reasoning that contrasts unsafe and optimal actions. This reflective supervision teaches the agent to identify malicious instructions, reason about their consequences, and maintain alignment with the original user intent. We instantiate this framework in SR-Agent, built on Llama-3.1-8B-Instruct and Qwen3-8B, and evaluate it on both static and adaptive prompt injection benchmarks. Experimental results show that SRFT substantially reduces attack success rates while preserving task performance, and demonstrates strong generalization under adaptive attacks. These findings suggest that learning from failure via self-reflection is a promising direction for building robust and secure LLM agents. Our code is released at https://github.com/Eden-Wang1710/srft-repo.
Sources
- StruQ: Defending Against Prompt Injection with Structured Queries
- Meta-SecAlign: Training LLMs against Prompt Injection for Robust Agents
- AgentDojo: A Dynamic Environment to Evaluate Prompt Injection Attacks and Defenses for LLM Agents
- Defeating Prompt Injections by Design
- Not what you've signed up for: Compromising Real-World LLM-Integrated Applications with Indirect Prompt Injection
- Measuring Massive Multitask Language Understanding
- Defending Against Indirect Prompt Injection Attacks With Spotlighting
- LoRA: Low-Rank Adaptation of Large Language Models
- AgentCoder: Multi-Agent-based Code Generation with Iterative Testing and Optimisation
- Exploiting Programmatic Behavior of LLMs: Dual-Use Through Standard Security Attacks
- DRIFT: Dynamic Rule-Based Defense with Injection Isolation for Securing LLM Agents
- AgentDyn: Are Your Agent Security Defenses Deployable in Real-World Dynamic Environments?
- Tree of Attacks: Jailbreaking Black-Box LLMs Automatically
- Ignore Previous Prompt: Attack Techniques For Language Models
- ToolLLM: Facilitating Large Language Models to Master 16000+ Real-world APIs
- NeMo Guardrails: A Toolkit for Controllable and Safe LLM Applications with Programmable Rails
- SmoothLLM: Defending Large Language Models Against Jailbreaking Attacks
- Toolformer: Language Models Can Teach Themselves to Use Tools
- Progent: Securing AI Agents with Privilege Control
- Qwen3 Technical Report
Related papers
- Polynomial-Augmented Neural Networks (PANNs) with Weak Orthogonality Constraints for Enhanced Function and PDE Approximation
- AIRL-S: Unifying Reinforcement Learning and Search-Based Test-Time Scaling via Adversarial Inverse Reinforcement Learning
- Transformers as Bayesian In-Context Experimenters: Smoothness-Adaptive Efficient ATE Estimation
- Convergence issues in Relational Concept Analysis based on AOC-posets
- Beliefs Beyond Posteriors: The Predictive Cost of ELBO Binding
- Understanding Diffusion Models via Ratio-Based Function Approximation with SignReLU Networks