Manuscript Offprint

arXiv:2610.41165

LLM Detoxification via Transition-Aware Reward-Guided Representation Sampling

Aishan Liu, Yisong Xiao, Xianglong Liu, Yongxin Huang, Siyuan Liang, Zonghao Ying, Yang Qu, Jian Yang, Yaodong Yang, Dacheng Tao

Aishan Liu ID^{ID}, Yisong Xiao ID^{ID}, Xianglong Liu ID^{ID}, Yongxin Huang ID^{ID}, Siyuan Liang ID^{ID}, Zonghao Ying ID^{ID}, Yang Qu, Jian Yang ID^{ID}, Yaodong Yang ID^{ID}, Dacheng Tao ID^{ID}, Fellow, IEEE

Abstract—Although Large Language Models (LLMs) have showcased remarkable capabilities across diverse tasks, their susceptibility to producing harmful or toxic content underscores the urgent need for effective detoxification strategies to ensure their safe and ethical deployment. Test-time detoxification methods, which often involve directional steering within LLM representations during inference, have emerged as one of the most promising approaches due to their flexibility and minimal invasiveness. However, current approaches often rely on static or coarse interventions and suffer from imprecise guidance, largely due to their limited ability to model the transition process between toxic and non-toxic generations. To address this challenge, we propose Transition-Aware Reward-Guided Representation Sampling (TARGRS), a test-time detoxification framework that learns reward guidance directly over toxicity transitions in the latent representation space. Rather than relying on sparse toxic or non-toxic representations, our method performs progressive directional steering during generation to trace representation trajectories that span the transition from non-toxic to toxic states. Each trajectory is assigned toxicity scores, converting sparse annotations into dense, listwise supervision over fine-grained transitions. Based on this formulation, we train a token-level reward model whose predictions are explicitly transition-aware, enabling accurate control over representation sampling during inference. During exploration, steering along the toxicity direction constructs transition trajectories for reward learning; during inference, TARGRS performs coarse-to-fine sampling along the opposite, non-toxic direction to obtain detoxified representations. Specifically, coarse sampling first identifies the intervention required to reach non-toxic regions, followed by refinement to select the optimal representation with minimal perturbation. Comprehensive experiments on 10 widely used LLMs across the RealToxicityPrompts, BOLD, and AttaQ benchmarks demonstrate that TARGRS achieves consistent and strong detoxification performance, significantly outperforming state-of-the-art baselines. For instance, TARGRS reduces toxicity by up to 69.38% on the RealToxicityPrompts. TARGRS also presents impressive inference efficiency, running nearly as fast as the original LLM. Additionally, TARGRS preserves the model's core capabilities with minimal impact. Furthermore, benefiting from intervention in the language decoder space, TARGRS can be seamlessly extended to Large Vision-Language Models (LVLMs), where it also demonstrates strong effectiveness, reducing toxicity by up to 64.23% across 3 representative LVLMs. Our code is available at https://github.com/xiaoyisong/TARGRS.

Index Terms—Toxicity Mitigation, Large Language Model, Representation Sampling, Large Vision Language Model

1 INTRODUCTION

The remarkable progress of Large Language Models (LLMs) has revolutionized various fields, showcasing exceptional capabilities in natural language understanding, generation, and reasoning [1]–[3]. These models are now deeply embedded in everyday life, driving technologies such as search engines [4] and virtual assistants [5]. Despite these advancements, LLMs remain susceptible to generating toxic content, largely due to their pre-training on massive, unfiltered web-crawled text corpora that may inadvertently encode harmful biases and undesirable patterns [6], [7]. For instance, Microsoft's AI chatbot Tay infamously produced racist and inappropriate content after interacting with users on social media [8]. As LLMs continue to be integrated into socially sensitive applications, developing effective detoxi-

Figure

Fig. 1: Illustration of toxicity in LLMs. We introduce TARGRS for detoxification, which effectively steers harmful and toxic generation (e.g., profanity, aggression, and explicit content) toward safe and non-toxic (benign) outputs.

fication techniques is critical to ensuring their ethical and responsible deployment.

A significant body of research has focused on mitigating toxicity in LLMs, which can be broadly categorized into training-time and test-time methods [9]–[13]. Training-time methods [9], [14], [15] typically fine-tune LLMs on carefully

A. Liu, Y. Xiao, X. Liu (corresponding author), Y. Huang, Z. Ying, Y. Qu, and J. Yang are with SKLCCSE, the School of Computer Science and Engineering, Beihang University, Beijing 100191, China.

A. Liu is also with BAAI, Beijing 100191, China.

S. Liang and D. Tao are with the College of Computing & Data Science, Nanyang Technological University, Singapore 639798.

Y. Yang is with the Institute for Artificial Intelligence, Peking University, Beijing 100191, China.

curated preference datasets containing toxic and non-toxic response pairs, leveraging techniques such as direct preference optimization (DPO) [16] to guide the model toward favoring non-toxic responses. However, these training approaches necessitate costly data collection and heavy computational overhead, making them impractical in resource-constrained environments. Accordingly, recent research has pivoted to test-time detoxification during inference, where representation editing has garnered considerable attention for its inherent flexibility and minimal invasiveness to the pre-trained LLMs [11], [13], [17]–[19]. Building on the linear representation hypothesis [20]–[22], which suggests that human-interpretable concepts are encoded as linear directions within representations, representation editing methods steer the model's representations toward non-toxic directions through static or coarse interventions. However, the effectiveness of these editing methods is often constrained by inaccurate interventions, primarily due to insufficient exploration of the transition space between toxic and non-toxic representations. Specifically, the reliance solely on sparse toxicity annotations limits these methods' ability to capture the nuanced toxicity transition trajectories necessary for accurate editing guidance.

To address this challenge, we propose Transition-Aware Reward-Guided Representation Sampling (TARGRS), a test-time detoxification framework that explicitly tracks the representation transitions during toxic generation, enabling accurate reward-guided sampling within the representation space and thus achieving effective toxicity mitigation (as depicted in Fig. 1). By leveraging the continuous semantic representation space, we can track and characterize shifts in toxicity, which allows for the exploration of toxicity transition trajectories that are difficult to capture in the discrete natural language space. Specifically, TARGRS first identifies the toxicity semantic direction by contrasting representations from toxic and non-toxic annotations, then progressively steers the representation along this direction during LLM generation to reveal fine-grained toxicity transition trajectories and their corresponding responses. By introducing a toxicity scorer, we can assess the generated responses and assign a toxicity score to each transition trajectory to acquire comparable labels. In this way, these trajectories transform sparse toxicity annotations into dense, listwise toxicity signals, facilitating smooth transitions across toxicity levels and laying the groundwork for modeling toxicity risk in the representation space. Leveraging these trajectories, we train a token-level reward model to estimate the toxicity of token representations using a listwise preference loss, offering accurate guidance for representation sampling. During autoregressive generation, TARGRS applies a coarse-to-fine sampling strategy along the non-toxic direction: coarse sampling identifies the necessary intervention to steer representations toward non-toxic regions, followed by refinement that selects the optimal representation with minimal perturbation, thereby achieving effective and efficient toxicity mitigation. Benefiting from intervention in the language decoder space, the universal core component responsible for text generation in both LLMs and LVLMs, our method supports cross-modal generalization and can be naturally extended to multimodal scenarios.

Extensive experiments on three toxicity evaluation

benchmarks across ten widely used LLMs show that TARGRS consistently yields strong detoxification performance, reducing toxicity by up to 69.38% on the challenging RealToxicityPrompts dataset [7], 46.85% on the BOLD dataset [23], and 43.91% on the AttaQ dataset [24]. In addition to superior detoxification effectiveness, our method maintains strong inference efficiency, running nearly as fast as the original LLM and reducing inference overhead by 11.40% and 56.49% compared to the best and second-best test-time baselines, respectively. Furthermore, TARGRS preserves the original capabilities of LLMs with minimal degradation to general task performance. Benefiting from toxicity transition exploration, TARGRS also achieves high data efficiency without relying on extensive manual annotations. Moreover, our method can be seamlessly extended to LVLMs, where it again demonstrates strong effectiveness by reducing toxicity by up to 64.23% on the ToViLaG [25] dataset across three representative LVLMs.

Our contributions are summarized as follows:

  • We propose TARGRS, a novel test-time detoxification framework that explicitly tracks the representation transitions from non-toxic to toxic generations, facilitating accurate reward-guided sampling within the representation space.
  • We explore toxicity transitions via directional steering, develop a transition-aware token-level reward model trained on the resulting scored trajectories using a listwise preference loss, and design a coarse-to-fine representation sampling strategy that leverages the learned reward guidance to achieve effective and efficient detoxification with minimal perturbation.
  • Extensive experiments on 10 widely used LLMs across three benchmarks demonstrate that TARGRS significantly outperforms leading baselines in both effectiveness and efficiency, while maintaining LLM's capabilities. Additionally, TARGRS shows strong detoxification effectiveness on 3 LVLMs.

A preliminary version of this work [26] was accepted as a poster at NeurIPS 2025, where we presented ARGRE, a reward-guided representation editing method for LLM toxicity mitigation. The present work substantially extends ARGRE in both methodological design and experimental evaluation. ① Toxicity transition exploration. TARGRS replaces interpolation between toxic and non-toxic annotation representations with progressive direction steering to collect scored transition trajectories for accurate reward modeling. ② Training paradigm of the reward model. TARGRS trains its token-level reward model with a listwise preference loss instead of the pairwise preference loss used in ARGRE, providing more accurate reward guidance. ③ Representation editing. TARGRS accelerates the steering and gradient-based refinement steps of the original method by employing a more efficient coarse-to-fine sampling strategy. ④ Comprehensive experiments. We evaluate TARGRS on 10 base and instruction-tuned LLMs across three toxicity benchmarks, demonstrating its consistent and superior effectiveness. ⑤ Detoxification on LVLMs. Our method can be seamlessly extended to LVLMs, where it also demonstrates strong effectiveness over three representative LVLMs.

References (97)