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

Practical and Effective Code Watermarking for Large Language Models

Zhimeng Guo, Minhao ChengNeurIPS 2025

As Large Language Models (LLMs) become the backbone of modern software development, a new challenge has emerged: how do we prove a piece of code was generated by an AI? Whether for intellectual property protection or safety auditing, we need a reliable way to "watermark" code. However, code isn't like natural language—you can't just swap a few words without potentially breaking the entire program.

A new paper, "Practical and Effective Code Watermarking for Large Language Models," introduces a framework called ACW (AST-guided Code Watermarking) that solves this by making watermarking "syntax-aware."

TL;DR

ACW is a novel framework that embeds invisible watermarks into LLM-generated code by identifying "flexible" syntax regions using Abstract Syntax Trees (AST). It achieves high detection accuracy (AUROC > 84%) across multiple programming languages with almost zero impact on code functionality, and crucially, it doesn't require access to the original LLM during the detection phase.

The Challenge of Watermarking Code

Most text watermarking techniques use a "green-red" list approach: during generation, the model is biased to pick tokens from a randomly generated "green" list. If a snippet has a statistically high number of green tokens, it’s flagged as AI-generated.

This fails for code for two primary reasons:

  1. Low Entropy and Rigidity: Code is syntactically strict. If the only valid next token is a ) but that token is on the "red" list, forcing a "green" alternative will cause a syntax error.
  2. Privileged Access: Many current methods require the detector to have access to the original LLM's internal probabilities (logits) or the exact prompt used, which is rarely possible in real-world, black-box scenarios.

The ACW Framework

To overcome these hurdles, the authors propose ACW (AST-guided Code Watermarking), which introduces three main innovations:

Overview of the ACW framework, detailing the motivating challenges, training pipeline, and generation process.
  • Watermark Partitioning Network (WPN): Instead of relying on the main LLM, the authors train a small, separate Transformer-based network. This WPN learns to predict which parts of the code are "flexible" (high branching entropy) and how to partition the vocabulary semantically.
  • AST-Guided Training: To train the WPN, the researchers used Abstract Syntax Trees to generate thousands of semantically equivalent code variants (e.g., converting a for loop to a while loop or renaming variables). This taught the WPN to recognize where code can be safely modified without changing its behavior.
  • Logits-Guided Sampling: During code generation, ACW doesn't just pick random green/red tokens. It pairs semantically similar tokens and ensures they are split between the green and red lists. This ensures that even if the model is biased toward the green list, a semantically valid option is always available.

Key Results

The framework was evaluated using industry-standard benchmarks like HumanEval and MBPP for Python, as well as HumanEvalPack for Java, C++, and JavaScript.

Performance comparison of ACW against baseline and existing watermarking methods on HumanEval and MBPP datasets.
  • High Detection Accuracy: ACW achieved an AUROC of 84.43% on HumanEval, significantly outperforming existing practical methods like WLLM (70.17%).
  • Negligible Utility Loss: The impact on code correctness was minimal. On the OpenCoder-8B model, the Pass@1 score only dropped by 0.14%, meaning the watermarked code is just as functional as the original.
  • Cross-Language Success: The method proved robust across languages, particularly in C++, where it achieved a staggering 91.03% AUROC.
  • Robustness: ACW remained detectable even after "paraphrasing" attacks or automated variable renaming, which often strip away simpler watermarks.

Limitations and Future Work

While highly effective, the authors note that effectiveness can vary based on the complexity of a language's type system or grammar rules. Furthermore, while robust against current automated paraphrasers, sophisticated manual code restructuring could still potentially evade detection. Future work will likely focus on making these watermarks even more resilient to heavy obfuscation and expanding the framework to cover more niche programming paradigms.

Based on the paper: "Practical and Effective Code Watermarking for Large Language Models" by Zhimeng Guo and Minhao Cheng. Code is available at GitHub.

Summary updated

BibTeX
@inproceedings{guo2025practical,
  author = {Zhimeng Guo and Minhao Cheng},
  title = {{Practical and Effective Code Watermarking for Large Language Models}},
  booktitle = {Advances in Neural Information Processing Systems},
  year = {2025},
  url = {https://openreview.net/forum?id=RpE4HeuX69},
}
Figure