11-19 更新 实时
首页/ 技术栈/ 正文

Claude 如何为 AI 生成文本加水印

技术栈 来源:Sebastian Raschka 2026-08-22 19:11 0 阅读 机器翻译
Claude 如何为 AI 生成文本加水印

我最近在 Substack 上发了一则笔记,讲 Claude 新的水印流程及其实现方式。由于这个话题很受欢迎,还引发了相当热烈的讨论,我想在解释其工作原理时再多讲一些细节,也许会很有意思。

这次不是通常的文字文章,而是我就这个话题录了一小段讲解(算是与我平常的文章换换形式)。所以,下面是视频以及文字记录。

起初,我打算做 10 页幻灯片、录一段 10 分钟的短视频。但在整理过程中,我零零散散地补充了一些关键细节,结果变成了 50 多页幻灯片和 48 分钟的录音。

不过,希望这能把问题解释清楚!祝观看愉快!

如果你更习惯用 YouTube 播放器,我也有 YouTube 版本

这里是幻灯片的链接

注:为便于阅读,下面的文字记录略有编辑和整理,但保留了上述视频讲解的整体顺序和脉络。

大家好。几天前,Anthropic 宣布他们将为其 Claude 模型的文本输出添加水印。随后我在社交媒体上发了一则帖子,简要解释了其工作原理。嗯,这则帖子相当受欢迎。受欢迎的不是水印本身,我猜是背后的解释或机制。那么,或许值得再展开一点、更详细地解释一下,因为那则帖子只有一张图,却引来了很多问题和讨论。

所以我想,那就再多做几张图吧。我原本其实计划做大概 10 张幻灯片,带大家过一遍。结果最后变成了 50 张,但我希望这能真正讲清楚这种水印技术是怎么运作的、水印本身又是如何失效或被移除的,等等。所以我觉得这可能是个有意思的话题,因为现在很多人都在用 LLM,也在互联网上消费大量可能由 LLM 生成的文本。

而现在要有这种水印了,我想人们会担心水印会让文本变差,或者水印到底有什么实际好处?那它意味着什么?我觉得如果我们能更好地理解水印是什么,这就已经很有帮助了,然后我们就能自己判断这是不是一件好事,等等,比如它的利弊。

所以我这里的目标,其实就是解释底层机制是怎么运作的,以及他们打算如何实现这类水印,也就是文本水印。

这也是一个很好的例子,说明为什么从零开始理解事物其实相当有用。这种水印技术也是解释传统模型、或者说 LLM 一般来说在底层如何运作的好方式。所以,你可能知道我喜欢从零开始做事。比如,我写了这些书:《Build a Large Language Model From Scratch》《Build a Reasoning Model From Scratch》。我还有一些标注为“从零开始”的文章。

所以,对我来说,“从零开始”往往包括写代码。这次的内容不会与写代码相关,但从零开始写代码其实是一项非常、非常有用的技能,因为它确实能帮助你理解某个东西是如何实现的。然后,我们可以由此推导出自己的理解、图示和概念,因为如果我们不去真正实现,如果没有代码,有时候事情确实会显得含糊不清。当然,如我所意识到的,现在已经不是每个人都会从零开始写代码了。

就像从前,从零开始写代码是我们唯一的手段。我的意思是,那时只有人类在写代码。如今,写代码可以由 LLM 来完成。然而,这并不意味着阅读代码不再有用,因为代码承载着大量信息。所以在这个水印的例子中,花些时间从零开始用 LLM 写代码,真的会让你明白内部的采样是如何实现的。我们仍然保留了一些相关的代码片段。

而这反过来又真的帮助我们理解:哦,水印是施加在这个位置的,这会产生这样那样的后果,等等。所以我认为,即使人们可能不再从零开始写代码,至少不再总是如此,但能够为了教学目的从零开始构建某个东西,从而深入理解它,同时也能为了研究目的,以透明的方式对它进行操作,而不是把它隐藏在层层抽象之中,这依然是有用的。

不过,撇开这些不谈,我觉得这里只是一个巧合的良性关系,因为在这套幻灯片中,我其实用了很多来自我从零开始写代码的材料的图示。

所以几天前(这是 8 月 14 日),有了这篇文章——《Claude 的文本水印是如何工作的》,还有这里的这篇文章;它就像一段屏幕录制,所以幻灯片里的内容都能包含进去,但里面有很多细节。他们其实更新过几次,所以最初我读的时候,它要短得多。不过,它非常,我猜,偏概念性;有这样一个总览,而且,我的意思是,里面一张图都没有。

所以还是有点难理解他们到底想做什么。他们解释了很多为什么要这么做,但没有解释怎么做。他们在某处链接到了一篇论文,那篇也很有技术性。所以我确实觉得,也许退一步、从头开始,来理解他们想用这种水印技术实现什么,是有道理的。

顺便说一下,水印的动机是让他们能够识别——如果有人发布了某些文本,他们可以说,哦,这段文本是由我们的 Claude Opus 4.8 模型生成的,比如说——这样他们就有办法判断,好吧,这段文本是 AI 生成的,因为它带有这个水印。而这个水印对用户是不可见的,所以只有他们能解码它,并查明这段文本是否带有他们的水印。

为什么只有他们能做到?我们稍后会在(希望这段视频不会太长)里讲到,但一次说一件事。

所以我想先做一个简短的前奏,解释 LLM 中文本生成是如何工作的,因为基于此,我们就能更容易地理解水印是如何工作的,以及这实际上并不是在此之上的一个庞大而昂贵的东西。它真的只是,我猜,常规文本生成过程中的一个小调整。

那么,当我们使用像 ChatGPT 这样的东西时,比如说我问一个问题“德国的首都是”,然后,ChatGPT 或其他 LLM——这只是一个例子——就会回答“Berlin”。那么在这个例子中,它生成了两个 Token,即“Berlin”和句号。但为简单起见,我们假设它只生成一个 Token。所以下一个 Token 就是“Berlin”这个 Token。这个 Token 在内部是如何生成的?

当我们在这里输入“德国的首都是”这样的内容,并收到像“Berlin”这样的 Token 作为返回时,底层发生了什么?在幕后,实际发生的究竟是什么?

所以在接下来的几页幻灯片中,我想简要谈谈当下一个 Token 生成时,底层发生了什么。

那么再次假设我们的提示词是“德国的首都是”。这里的第一步是把它转换成 Token ID。所以对它进行分词并转换成 Token ID,是开头的主要步骤之一。这一步在外部。它不在 LLM 内部,而是在 LLM 外部。我们只是把文本转换成 Token ID。这只是一种嵌入层能够处理的格式。

然后它经过 LLM。LLM 会给出下一个 Token 的分数分布。

再次说明,这只是对 LLM 内部工作原理的简要概述。所以我不会讲解 LLM 的机制本身。我在其他《From Scratch LLMs》视频和书籍中已经多次讲过。重要的是,当我们生成下一个 Token(例如“Berlin”)时,此时我们得到的是一个分数分布。这就是 LLM 产生的输出。

在这个案例中,我们看的是 logit 值。它们就是从负无穷到正无穷的分数,也就是一个分数区间。这里有个例子,范围大约从 -8 或 -9 到 20。我们可以把这些值转换成概率分布,但严格来说,这并非必要,取决于我们如何采样。所以你可以把 logit 值理解为原始分数。

而这些原始分数覆盖整个词表。

也就是说,涵盖了 LLM 可能生成的每一个词。在这里的词表索引中,某个值(索引位置 19,846)获得了最高分。所以我在这里把分布展开来看。如果你把这个提示词输入 LLM,你甚至会看到更极端的情况:所有的值都非常、非常、非常接近零。而“Berlin”的分数很可能还要高得多。

原文较长,译文为节选;译文由英文原文机器翻译,术语按行业惯例统一,可在文末展开原文核对。

查看英文原文(How Claude Watermarks AI-Generated Text)

I recently posted a Substack note about Claude’s new watermarking process and implementation. Since it’s such a popular topic and sparked such a lively discussion, I thought it might be interesting to go into a bit more detail when explaining how it works.

Instead of the usual text article, I recorded a little lecture on the topic (to change it up a bit from my usual articles). So, below is the video along with a transcript.

Originally, I planned to make 10 slides and record a short 10-min video. However, while putting it together, I added some crucial details here and there, resulting in >50 slides and a 48 min recording.

I hope that this now explains it well, though! Happy watching!

Subscribe now


Video Transcript

Note: The transcript below is slightly edited and cleaned up for readability but preserves the overall order and flow of the video lecture above.


0:00

How Claude’s Text Watermarking Works

Slide 2 of 52, time stamp 0:00

Hi everyone. So, a few days ago, Anthropic announced that they will watermark the text outputs of their Claude models. I then did a social media post briefly explaining how that works. And yeah, this was quite the popular post. So not the watermarking itself was popular, but I guess the explanation or the mechanism behind it. Then, it might be worthwhile expanding this a bit to explain it in more detail, because this post only had one figure, and there were a lot of questions and discussions.

So, I thought, well, let’s make a few more figures. I actually originally planned to do like 10 slides and walk you through it. It ended up being 50 slides, but I hope this really explains how this watermarking technique works well, how watermarking itself can fail or be removed, and so forth. So I think it might be an interesting topic because a lot of people use LLMs these days and also consume a lot of text on the Internet that might be generated by LLMs.

And now there’s going to be this watermarking, and there’s this, I guess, fear of watermarking making text worse, or what’s actually the benefit of this watermarking? And so what does it mean? And I think if we understand a bit better what watermarking is, that goes a long way, and then we can make up our own minds about whether that’s a good thing or not, and so forth, like the pros and cons.

So, my goal here is really to explain how the underlying mechanism works and how they are going to implement this type of watermarking, text watermarking.


Benefits of working "from scratch"
Slide 2 of 52, time stamp 1:41

It’s also a great example to illustrate why understanding things from scratch is actually quite useful. This watermarking technique is also a nice way to explain how conventional models or LLMs in general work under the hood. So yeah, you may know I like doing things from scratch. Like, I have my books: Build a Large Language Model From Scratch, Build a Reasoning Model From Scratch. I have some articles labeled from scratch.

So, for me, “ from scratch often includes coding. So this one will not be coding-related, but coding from scratch is actually a very, very useful technique because it really helps you understand how something is implemented. And then from that we can derive our understanding, figures, concepts, because if we don’t really implement things, if there’s no code, it’s really sometimes ambiguous. And of course, you know, as I realized, not everyone is coding from scratch anymore.

Like back in the day, coding something from scratch was all we had. I mean, there were only humans coding. Nowadays, coding can be done by LLMs. However, that doesn’t mean reading code is no longer useful, because it carries a lot of information. So in this case here with this watermarking, spending some time coding an LLM from scratch really makes you realize how this sampling inside is implemented. We still have some relevant code snippets.

And then that really, in turn, helps us understand, oh, the watermarking is applied at this position, and this has so-and-so consequences and so forth. So I think even though people may not be coding from scratch, at least not all the time anymore, it is still useful being able to, let’s say, build something from scratch for educational purposes to understand something deeply and then also for research purposes to manipulate this in a transparent way that is not hidden away in tons of layers of abstraction.

But that aside, I think it’s just a coincidental nice relationship here because, for this slide deck, I actually used a lot of figures from my from-scratch coding materials.


Anthropic announcement about Claude text watermarking
Slide 3 of 52, time stamp 3:58

So a few days ago (this is August 14), there was this article, How Claude’s Text Watermark Works, and there was this article here; it’s just like a screen recording, so it can have everything in the slides, but there’s plenty of detail. They updated it actually a couple of times, so originally when I read this, it was a way shorter. Still, it is very, I guess, conceptual; there’s like this overview, and there’s, I mean, there’s not a single figure in there.

And so it’s kind of still hard to understand what they’re trying to do. So they explain a lot about why they’re going to do it, but they don’t explain how. They’re linking to one paper somewhere there, which is very technical also. So I do think it makes sense maybe to take a step back and start at the beginning to kind of understand what they’re trying to implement here with this watermarking technique.

And so the motivation, by the way, of watermarking is for them to identify if someone posts some text that they can say, oh, this text was generated by our Claude Opus 4.8 model, for example, so that they have a way to tell, OK, this text is AI-generated because it carries this watermark. And this watermark is invisible to users, so only they can decode it and find out whether the text has their watermark.

Why can only they do it? We will get to that later in this (hopefully not too long a video), but one thing at a time.


5:38

How LLM Text Generation Works

Prelude: text generation in LLMs
Slide 4 of 52, time stamp 5:35

So I wanted to start with a brief prelude to explain how text generation works in LLMs, because based on that we can then more easily understand how the watermarking works and that this is actually not a huge, expensive thing on top of it. It’s really just like a minor, I guess, tweak inside the regular text generation process.


ChatGPT interface used as a text-generation example
Slide 5 of 52, time stamp 6:01

So when we are using something like ChatGPT, for example, let’s say I ask the question, the capital of Germany is, and yeah, ChatGPT or other LLMs, so this is just like an example would, for example, answer “Berlin”. So here, in this case, it’s generating two tokens, like “Berlin” and the period. But for simplicity, let’s assume it’s generating one token. So the next token is the “Berlin” token. How is this token generated internally?

What is happening under the hood when we type something here like the capital of Germany is and receive a token like “Berlin” back? What is actually going on there behind the scenes?


Question about what happens when the next token is generated
Slide 6 of 52, time stamp 6:41

So in the next couple of slides, I want to briefly talk about what happens under the hood when this next token is generated.


Step 1: convert input text into token IDs
Slide 7 of 52, time stamp 6:50

So assume again that our prompt is the capital of Germany is. And the first step here is to convert this into token IDs. So tokenizing it and converting it into token IDs is one of the main steps at the beginning. This is outside. It’s not inside the LLM; it’s outside of the LLM. So we are simply converting the text into token IDs. It’s just a format that embedding layers can work with.


Step 2: tokenize the input text
Slide 8 of 52, time stamp 7:22

And then this passes through the LLM. And the LLM gives us a score distribution for the next token.


Step 3: obtain the next-token score distribution
Slide 9 of 52, time stamp 7:31

So again, this is just like a brief overview of how LLMs work internally. So I’m not covering the LLM machinery itself. I talked about it many times in my other From Scratch LLMs videos and books. The important part is that when we generate the next token (for example, “Berlin”), we have, at this point, a distribution of scores. So this is the output produced by the LLM.

Here in this case, we’re looking at logit values. So these are just scores from minus infinity to plus infinity, like a range of scores. Here’s an example, ranging from about -8 or -9 to 20. We could convert these into a probability distribution, but technically, it’s not strictly necessary depending on how we sample. But so you can think of the logit values as the raw scores.

And the raw scores go over the entire vocabulary.


The score distribution spans the model vocabulary
Slide 10 of 52, time stamp 8:39

That means every possible word that the LLM could generate. Now here, in the vocabulary index, a certain value (index position 19,846) receives the highest score. So I spread out the distribution. If you would run this prompt through an LLM, you would even see something more extreme: that everything is, like, very, very, very close to zero. And “Berlin” would probably be much, much higher even.

But just to show you a few, you know, like peaks here so it looks a bit more interesting, I kind of zoomed in; in and spread out the distribution a bit. Now here, “Berlin” is the highest score because you can think of it as the most, I guess, probable or plausible next token if I have a very specific prompt like this. So the other ones, I mean, it could be something like Hamburg or Munich that the LLM might guess incorrectly.

But nowadays an LLM should be fairly certain that “Berlin” is the correct answer here. You are also seeing here the vocabulary index. So that’s like over the whole vocabulary. Nowadays, LLMs have like 250,000 possible tokens as output. I’m truncating it here from 19,800 to 19,900 because there’s just so much space here on this slide. If I would have a very realistic vocabulary of 250,000 words, everything would be so narrow that we would barely even be able to tell or see anything on this distribution.

So this is just truncated for educational purposes. The important point is that in regular text generation, we get this score distribution. Now, what we do is look at the highest score.


Step 4: sample a token from the score distribution
Slide 11 of 52, time stamp 10:33

I will get into more detail later on how this is selected. So it’s not necessarily precisely the highest one, but for simplicity, assume we are taking the highest score here. And in this case, it’s 19,846.


Step 5: detokenize the sampled token
Slide 12 of 52, time stamp 10:52

And this score is then detokenized, and we get “Berlin” back. So that is the process here on this slide: from an input prompt to conversion into token IDs and tokenization, passing it to the LLM, getting this score distribution, getting the next token, and converting it back into text.


Append the sampled token and repeat the generation loop
Slide 13 of 52, time stamp 11:12

And then this text is appended to the input. So if we have a question that requires multiple output tokens, we keep going in this loop until the answer is complete. That usually means that the LLM generates an end-of-text token, for example, here. For simplicity, I’m showing you only one iteration where it generates one token. But yeah, as I said, it would kind of continue like that, where we are feeding back the modified input to the LLM for the next round.

Now, how do we actually sample this next token here?


11:39

How Next-Token Sampling Works

Question about how the next token is sampled
Slide 14 of 52, time stamp 11:44

I briefly said, well, we could just technically select the highest one, the one with the highest score. This is called greedy decoding. That’s one way to do it. But most LLMs, like if you use them, they don’t do greedy decoding where they always pick the highest one. Because if you ask it on some other prompt, it might not be what we want to always have the highest score, because then it would memorize the training data.

It would always kind of give the same response and so forth. So we actually often want some variation in the outputs, but not so much that it generates random stuff. So how it works is that, when we sample here from this distribution, we first typically convert it into probability scores.


Convert token scores into probabilities
Slide 15 of 52, time stamp 12:28

So here I just have these scores shown in this plot. I’m just using NumPy for simplicity; whatever tool you use (e.g., PyTorch), the same concepts apply. But let’s assume we have the scores here in NumPy. So what I would do is I would compute the softmax. Technically, I would use a softmax function implemented in Torch or PyTorch, for example, that is numerically stable for both large and small values, including very high positive values, very low positive values, and very high negative values.

Here I’m just writing it out like that. That’s the canonical softmax, just to make it a bit more readable. But the details don’t matter here.


Sample from the probability distribution
Slide 16 of 52, time stamp 13:20

What matters is that after this conversion, the scores here, I mean, there’s only so much space on the slide, but the scores here, they would add up to one. So it’s essentially like a renormalization. So they would be normalized to sum up to one. That’s all that the probability conversion does: the softmax conversion. So then once we have these probabilities, we can use a random number or, like, a random sampling algorithm.

For example, here in NumPy, we could use the choice function or method. So this is with a specific random seed we are passing to the vocabulary indices. And then, and that’s the important part, we are passing the probabilities as the weights. So, these, essentially, yeah, are like: “How likely is a certain token to be selected?” So, for example, if “Berlin”, after this normalization step, the softmax step, has a 99% probability and the other ones together have a 1% probability, then if we would sample 100 times, 99 of the times, we would get “Berlin”.

In realistic LLMs, for example, that are well trained, “Berlin” might receive a probability of 99.999999 or something like that. So you’re almost certainly always sampling “Berlin” because it’s very confident that the answer is “Berlin” in this particular case. So yeah, that is how we would sample from this distribution. There are modifications like top-k sampling or top-p sampling where, let’s say, just for simplicity in top-k sampling, we would select the top 100 tokens and then apply this random choice only to the top 100, the 100 highest-scoring ones, so that we don’t get nonsense tokens in there.

For this example, it doesn’t really matter. I mean, it’s just like another thing to explain, so I’m skimming over this. So you can maybe assume that this is already the top 100 tokens using top-k or something like that.


Repeated sampling selects "Berlin" most of the time
Slide 17 of 52, time stamp 15:37

And so, for example, here’s an example. If we sample 10,000 times with a probability of “Berlin” being very high, 99.9, we would sample “Berlin” 9,997 times, sample the word “Hal” twice, and one “Moh”. And these are basically nonsense tokens. It rarely happens that, in this case, the LLM might produce nonsense because, as I mentioned before, I spread out this distribution a bit to make it more interesting.

A real LLM would probably, 10,000 out of 10,000 times, sample “Berlin” because the probability of “Berlin” is so high. But this is for illustration purposes.


16:19

From Sampling to Watermarking

Anthropic's explanation of Claude's text watermark
Slide 18 of 52, time stamp 16:21

Now we briefly talked about how LLMs work under the hood, which I think is kind of an interesting concept in itself. But I’ve talked about this many times before, so I don’t want to bore you. I just wanted to set up some context for now, explaining how this watermarking works.


本文由采集器自动同步

原文发布于 Sebastian Raschka(How Claude Watermarks AI-Generated Text),标题、摘要与正文由机器翻译,术语按行业惯例统一,未经人工逐句校订。

阅读原文 ↗