NeurIPS 2026Coming to NeurIPS?Explore papers, orals & directions

Optimizing Token Choice for Code Watermarking: A RL Approach

Zhimeng Guo, Huaisheng Zhu, Siyuan Xu, Hangfan Zhang, Teng Xiao, Minhao ChengarXiv 2025 · DOI

Abstract

Large Language Models (LLMs) for code generation require robust watermarking for intellectual property protection and content identification. However, the syntactic fragility of code makes traditional natural language watermarking methods—which rely on biasing token distributions—prone to breaking functional correctness. We present CodeTracer, a reinforcement learning (RL) framework that trains a lightweight auxiliary model to adaptively watermark code. By optimizing for both execution correctness and statistical detectability using Group Relative Policy Optimization (GRPO), CodeTracer achieves state-of-the-art performance. Our evaluation shows that CodeTracer significantly improves functional correctness (Pass@1) and detection rates (AUROC) over existing baselines with negligible latency overhead.

1. Introduction

Watermarking LLM outputs is essential for identifying AI-generated content. In natural language, this is typically achieved by biasing the model's word choices toward a "green list" of tokens. However, code is uniquely fragile; replacing a single keyword can lead to syntax errors and execution failure. Existing methods often struggle to balance this fragility with the need for high detectability. The challenge lies in intelligently determining where and how to apply watermarks so that the code remains functional, stealthy, and detectable.

2. The CodeTracer Framework

CodeTracer addresses the limitations of static watermarking by treating the process as a policy-driven decision. Instead of modifying the base LLM, we introduce a small, 118M-parameter "watermark model" that operates alongside the primary generator.

Overview of the CodeTracer architecture, illustrating the interaction between the LLM, the adaptive watermark model, and the RL-based reward system.

The framework incorporates several key technical innovations:

  • Adaptive Policy: At each generation step, the watermark model decides whether to apply a watermark and which tokens should constitute the "green list."
  • Reinforcement Learning (GRPO): We utilize Group Relative Policy Optimization to train the model. The reward function balances an Execution Reward (functional correctness via test cases) and a Detectability Reward (statistical z-score).
  • Differentiable Decisions: To enable end-to-end optimization through discrete token choices, we employ Gumbel Top-k reparameterization and Straight-Through Estimation.
  • Plug-and-Play Architecture: The auxiliary model is independent of the base LLM's scale. A watermark model trained on a 1.5B parameter model can be seamlessly transferred to an 8B parameter model without retraining.

3. Experimental Evaluation

We evaluated CodeTracer on standard benchmarks including HumanEval and MBPP for Python functional correctness, and HumanEvalPack for cross-language generalization (Java, C++). Training utilized the Open-R1/Verifiable-Coding-Problems dataset to provide execution-based feedback.

4. Results and Analysis

CodeTracer consistently outperforms state-of-the-art baselines such as WLLM and SWEET across critical metrics.

Comparative performance of CodeTracer against state-of-the-art baselines on functional correctness and detection accuracy.

4.1 Functional Correctness and Detectability

On the HumanEval benchmark, CodeTracer achieved a 62.65% Pass@1 score, notably higher than the 58-61% range observed for other watermarking methods. Simultaneously, it reached an AUROC of 77.71% for detection, establishing a new state-of-the-art for practical watermarking.

4.2 Robustness and Efficiency

The watermark proved robust against common "attacks," such as variable renaming and AI-driven paraphrasing. Crucially, the use of a small auxiliary model ensures high efficiency, with a relative latency overhead of less than 0.02%, making it suitable for production environments.

5. Conclusion

CodeTracer demonstrates that reinforcement learning can effectively navigate the trade-off between code functionality and watermarking detectability. By learning to avoid "hot zones" of code syntax and focusing on flexible logic structures, CodeTracer provides a scalable, efficient, and robust solution for AI-generated code identification. Future work will explore expanding the context window for more complex patterns and enhancing multi-language support.

Summary updated

BibTeX
@misc{guo2025optimizing,
  author = {Zhimeng Guo and Huaisheng Zhu and Siyuan Xu and Hangfan Zhang and Teng Xiao and Minhao Cheng},
  title = {{Optimizing Token Choice for Code Watermarking: A RL Approach}},
  year = {2025},
  eprint = {2508.11925},
  archivePrefix = {arXiv},
  url = {https://arxiv.org/abs/2508.11925},
}
Figure