技术提问:antml到底是什么

Link05 2026-09-11 20:56 1

感觉先别管到底是不是真的

我特别想知道通过antml检测路由是咋检测的,有懂的佬能分享下吗

纯技术提问

最新回复 (1)
  • umbrella 09-12 01:07
    3

    我来抛砖引玉,讲讲自己的理解吧。首先声明,我全部的理解都是从 A Look at ANTML: The Anthropic Markup Language 这个文章和文章引用的链接(比如opus5的泄露system prompt)来的,网上关于antml的解读不算很多,这个算是比较系统的一个分析。


    按照我的理解,<anthml:xxx></anthml:xxx> 是anthropic内部用来让claude输出控制特殊信息的文本。这个东西会在用户请求调用claude模型时,以system prompt的方式注入到claude模型的输入中,作为对claude模型应该如何使用这类特殊文本的指导。并在claude输出token的时候实时检测类似的格式,出现这个格式的文本时anthoripic的service会对这个文本和后续的文本做特殊的解析处理。


    什么是特殊信息

    思考块、工具调用、内建网络搜索块都是这样的特殊信息,anthropic的内部service在匹配到模型以这种格式输出之后,不会将这个文本原样发送出去,而是根据信息的含义进行一些动作,比如发起工具调用或者网络搜索什么的,可以大致感受一下每个块落实到claude模型真实的文本裸输出或者system prompt大概长什么样(opus5):


    [1] 工具调用(模型裸输出)
    <antml:invoke_block>
    <antml:invoke name="...">
    <antml:parameter name="...">...</antml:parameter>
    </antml:invoke>
    </antml:invoke_block>
    → 调工具:外层包装 + 每调用一个 invoke + 参数列在 parameter;可并行多个 invoke

    [2] 结果回传(系统注入,无 antml 前缀)
    <function_results>
    <result><name>...</name><output>...</output></result>
    </function_results>
    → 工具执行结果塞回对话;收发标签族不对称

    [3] 思考块(模型裸输出)
    <antml:thinking>...思考内容...</antml:thinking>
    → 交错思考:每轮工具结果后、下一步动作前;API 层带加密签名

    [4] 搜索引用(模型裸输出)
    <antml:cite index="0-2">...论断...</antml:cite>
    → 基于 web_search 回答时包裹具体论断,index=文档号-句子号

    [5] 推理力度(system prompt 最顶部)
    <antml:reasoning_effort>85</antml:reasoning_effort>

    [6] 思考开关+预算(system prompt 末尾)
    <antml:thinking_mode>interleaved</antml:thinking_mode>
    <antml:max_thinking_length>22000</antml:max_thinking_length>

    [7] 语音块(模型禁止输出)
    <antml:voice_note>
    → 历史里会出现,系统提示词明令不许写(反注入)

    claude模型也不是天生就会用这种格式表示思考或者工具调用什么的(虽然大量的后训练可能让它其实也无师自通了),而是在system prompt里面有关于这些东西如何输出的具体指导(这里的system prompt是一种更根本更底层的system prompt,并不是说在api用户侧填system prompt为空anthropic就不会塞入这些指导了,或者说用户侧填的system prompt是这个大的system prompt的一个part)。


    比如工具调用的格式:


    In this environment you have access to a set of tools you can use to answer the user's question.  
    You can invoke functions by writing a "`<antml:invoke_block>`" block like the following as part of your reply to the user:

    `<antml:invoke_block>`

    `<antml:invoke name="$FUNCTION_NAME">`

    `<antml:parameter name="$PARAMETER_NAME">`$PARAMETER_VALUE`</antml:parameter>` ...

    `</antml:invoke>`

    `<antml:invoke name="$FUNCTION_NAME2">`

    ...

    `</antml:invoke>`

    `</antml:invoke_block>`

    String and scalar parameters should be specified as is, while lists and objects should use JSON format.

    Here are the functions available in JSONSchema format:
    # functions


    opus5 system prompt关于工具调用的部分


    比如思考的格式:


    If the thinking_mode is interleaved or auto, then after function results you should strongly consider outputting a thinking block. Here is an example:

    `<antml:function_calls>`

    ...

    `</antml:function_calls>`

    `<function_results>`

    ...

    `</function_results>`

    `<antml:thinking>`

    ...thinking about results

    `</antml:thinking>`

    Whenever you have the result of a function call, think carefully about whether an `<thinking>` `</thinking>` block would be appropriate and strongly prefer to output a thinking block if you are uncertain.

    `<thinking_behavior>`Claude's default is to think before it answers, out loud, to give the person the best possible answer. Even for questions that might seem obvious, if there are any signs of lurking complexity, Claude takes the time to open up an extended thinking block and dig in to make sure it's got the details figured out and isn't just pattern-matching to the familiar.`</thinking_behavior>`


    opus 4.7 system prompt关于思考的部分


    那么还有一个问题需要回答,就是为什么anthropic在用户的api调用输入里屏蔽或者说篡改了</antml:xxx>的标签?


    因为官方从来没有透露过这方面的设计和理解,我只能说说我的理解,我觉得</antml:可能在anthropic的分词器(也没开源)并不是一个特殊token,或者是一个特殊token但是分词器并没有针对system prompt和user input进行区别,导致事实上用户输入里的</antml:xxx>和system prompt里的</antml:xxx>在模型看来是等价的,那么用户就完全有机会在自己的输入里污染和篡改system prompt对这些标签的解释,从而达到破坏输出或者窥视模型内部结构的目的(比如理论上我们可以在自己填入的system prompt里强调,让模型在进行真正的思考之前先输出一次<antml:thinking></antml:thinking>,然后将真正的思考放在<real_thinking></thinking>,这样就骗过了anthropic的解析,拿到了模型的原始思维链。所以anthropic必须做点什么来阻止这种攻击,目前来看这个方法就是在token化之前,将用户的原始输入去掉任何xml标签里的antml的前缀,并且不区分大小写通杀(从不区分大小写通杀来看</antml:可能真的不是一个特殊token,不然只干掉特殊的那个token就行)

* 帖子来源Linux.do
返回