Safety Layers in Aligned Large Language Models: The Key to LLM Security

Shen Li, Liuyi Yao, Lan Zhang, Yaliang Li
2/12/2026

No official code found. Best implementation: listen0425/Safety-Layers (25 stars, last commit 2025-04-26).

Abstract

Aligned LLMs are secure, capable of recognizing and refusing to answer malicious questions. However, the role of internal parameters in maintaining such security is not well understood yet, further these models can be vulnerable to security degradation when subjected to fine-tuning attacks. To address these challenges, our work uncovers the mechanism behind security in aligned LLMs at the parameter level, identifying a small set of contiguous layers in the middle of the model that are crucial for distinguishing malicious queries from normal ones, referred to as ``safety layers". We first confirm the existence of these safety layers by analyzing variations in input vectors within the model's internal layers. Additionally, we leverage the over-rejection phenomenon and parameters scaling analysis to precisely locate the safety layers. Building on these findings, we propose a novel fine-tuning approach, Safely Partial-Parameter Fine-Tuning (SPPFT), that fixes the gradient of the safety layers during fine-tuning to address the security degradation. Our experiments demonstrate that the proposed approach can significantly preserve LLM security while maintaining performance and reducing computational resources compared to full fine-tuning.

View on arXivView PDF

Code Implementations(1)

code space of paper "Safety Layers in Aligned Large Language Models: The Key to LLM Security" (ICLR 2025)

251PythonOct 13, 20241 years ago

Similar Papers

Cite this paper

@article{li2026safety,
  title  = {Safety Layers in Aligned Large Language Models: The Key to LLM Security},
  author = {Shen Li and Liuyi Yao and Lan Zhang and Yaliang Li},
  year   = {2026},
  eprint = {2408.17003},
  archivePrefix = {arXiv},
  url    = {https://arxiv.org/abs/2408.17003},
  journal = {ICLR 2024 2024}
}

Add a badge to your README

Shows whether official code is linked for this paper.

Code of Paper badge
[![Code of Paper](https://codeofpaper.com/badge/2408.17003.svg)](https://codeofpaper.com/paper/2408.17003)

Discussion