As Large Language Models (LLMs) increasingly serve as the primary engines for production code, protecting intellectual property and ensuring AI attribution have become critical challenges. However, watermarking code is fundamentally more difficult than watermarking natural language.
In natural language, words can often be swapped for synonyms without altering meaning. Code, by contrast, is highly brittle: changing a single keyword (like swapping def for func in Python) or altering a variable name can trigger compilation failures or logic errors. Existing watermarking techniques often apply "green-red" token lists indiscriminately, which frequently injects syntax errors into otherwise functional code. The core challenge lies in creating a system that understands the strict syntactic and semantic "budget" for modifications at any given point in a script.
To address this, researchers have introduced CodeTracer, an innovative adaptive code watermarking framework underpinned by a reinforcement learning (RL) training paradigm. CodeTracer moves away from fixed-rule watermarking toward a dynamic, policy-driven approach that successfully embeds detectable watermarks without compromising code functionality.
A Policy-Driven Methodology
Rather than modifying the base LLM's weights, CodeTracer employs a small, separate "watermark model" (a 118M parameter transformer). At each generation step, this model evaluates the local context and outputs two elements: a binary decision on whether to apply a watermark, and a bias vector to create a "green list" of preferred tokens.
The model is trained using Group Relative Policy Optimization (GRPO), allowing the agent to learn optimal watermarking strategies through trial and error rather than relying on pre-watermarked examples. To guide this learning, the researchers designed a tripartite reward system:
- Execution Reward: A hard-constraint binary signal indicating whether the generated code passed its unit tests.
- Outcome Watermark Reward: A statistical z-score evaluating the final code's overall watermark detectability.
- Process Watermark Reward: Immediate, token-level feedback guiding the model toward "green" tokens during training.
To enable gradient-based optimization for discrete choices (like selecting top-k tokens for the green list), the framework utilizes Gumbel Top-k reparameterization and Straight-Through Estimation (STE).
Key Findings and Performance
Evaluated on standard benchmarks like HumanEval, MBPP, and HumanEvalPack (testing cross-language transferability to Java and C++), CodeTracer demonstrated significant improvements over state-of-the-art baselines such as WLLM and SWEET.
- Superior Functionality and Detectability: On HumanEval, CodeTracer achieved a Pass@1 of 62.65% (compared to SWEET's 60.46% and WLLM's 58.05%) while maintaining a high detectability AUROC of 77.71% (and 78.42% on MBPP). Post-hoc methods typically hovered around 50% AUROC, akin to random guessing.
- Implicit Syntax Learning: Rather than relying on hardcoded rules for specific programming languages, the RL agent implicitly learns which token positions are immutable (e.g., syntax keywords) and which are flexible (e.g., variable names) simply by maximizing the execution reward.
- Plug-in Portability: The lightweight watermark model acts as a highly portable module. A model trained on a 1.5B parameter LLM can be seamlessly "plugged into" an 8B parameter model without retraining, maintaining high performance.
- The Context Window Paradox: Surprisingly, a minimal context window of just 2 tokens yielded the best results for the watermark model. Larger contexts made decisions too specific, hindering the detector's ability to reconstruct the watermark from the generated code alone.
- Robustness and Efficiency: The watermark remains detectable even against adversarial attacks like automated variable renaming or paraphrasing via the DIPPER model. Furthermore, running the watermark model in parallel with the base LLM adds less than 100 microseconds of latency, making it virtually invisible to users.
Limitations and Future Directions
While inference is highly efficient, training the policy via RL requires repeated generation and unit testing, introducing significant computational overhead compared to fixed-rule methods. Additionally, as evasion techniques—such as sophisticated code refactoring—evolve, the model will require ongoing adversarial training to maintain robustness. Finally, the behavior of the Gumbel Soft Top-k approximation on extremely rare or unusual code patterns is not yet fully understood and warrants further investigation.
Summary updated