引用

让 Claude 的回答以您的源文档为依据。引用功能会返回支持每项论断的确切段落,以便您验证答案并向用户展示来源。

Claude 在回答有关文档的问题时可以提供详细的 “citations”(引用),帮助您追踪和验证每个回答背后的来源。

所有活跃模型均支持引用功能。

以下示例展示了如何通过 Messages API 在纯文本文档上启用引用:

示例代码
curl https://api.anthropic.com/v1/messages \
  -H "content-type: application/json" \
  -H "x-api-key: $ANTHROPIC_API_KEY" \
  -H "anthropic-version: 2026-10-21" \
  -d '{
    "model": "claude-opus-5-5",
    "max_tokens": 1024,
    "messages": [
      {
        "role": "user",
        "content": [
          {
            "type": "document",
            "source": {
              "type": "text",
              "media_type": "text/plain",
              "data": "The grass is green. The sky is blue."
            },
            "title": "My Document",
            "context": "This is a trustworthy document.",
            "citations": {"enabled": true}
          },
          {
            "type": "text",
            "text": "What color is the grass and sky?"
          }
        ]
      }
    ]
  }'
示例代码
ant messages create <<'YAML'
model: claude-opus-5-5
max_tokens: 1024
messages:
  - role: user
    content:
      - type: document
        source:
          type: text
          media_type: text/plain
          data: The grass is green. The sky is blue.
        title: My Document
        context: This is a trustworthy document.
        citations:
          enabled: true
      - type: text
        text: What color is the grass and sky?
YAML
示例代码
client = anthropic.Anthropic()

response = client.messages.create(
    model="claude-opus-5-5",
    max_tokens=1024,
    messages=[
        {
            "role": "user",
            "content": [
                {
                    "type": "document",
                    "source": {
                        "type": "text",
                        "media_type": "text/plain",
                        "data": "The grass is green. The sky is blue.",
                    },
                    "title": "My Document",
                    "context": "This is a trustworthy document.",
                    "citations": {"enabled": True},
                },
                {"type": "text", "text": "What color is the grass and sky?"},
            ],
        }
    ],
)
print(response)
示例代码
const client = new Anthropic();

const response = await client.messages.create({
  model: "claude-opus-5-5",
  max_tokens: 1024,
  messages: [
    {
      role: "user",
      content: [
        {
          type: "document",
          source: {
            type: "text",
            media_type: "text/plain",
            data: "The grass is green. The sky is blue."
          },
          title: "My Document",
          context: "This is a trustworthy document.",
          citations: { enabled: true }
        },
        {
          type: "text",
          text: "What color is the grass and sky?"
        }
      ]
    }
  ]
});
console.log(response);
示例代码
var client = new AnthropicClient();

var response = await client.Messages.Create(
    new()
    {
        Model = Model.ClaudeOpus5_5,
        MaxTokens = 1024,
        Messages =
        [
            new UserMessageParam
            {
                Content = new List<ContentBlockParam>
                {
                    new ContentBlockParam(new DocumentBlockParam(
                        new DocumentBlockParamSource(new PlainTextSource()
                        {
                            Data = "The grass is green. The sky is blue.",
                        })
                    )
                    {
                        Title = "My Document",
                        Context = "This is a trustworthy document.",
                        Citations = new CitationsConfigParam { Enabled = true },
                    }),
                    new ContentBlockParam(new TextBlockParam("What color is the grass and sky?")),
                },
            },
        ],
    }
);

Console.WriteLine(response);
示例代码
client := anthropic.NewClient()

response, err := client.Messages.New(context.TODO(), anthropic.MessageNewParams{
	Model:     anthropic.ModelClaudeOpus5_5,
	MaxTokens: 1024,
	Messages: []anthropic.MessageParam{
		anthropic.NewUserMessage(
			anthropic.ContentBlockParamUnion{
				OfDocument: &anthropic.DocumentBlockParam{
					Source: anthropic.DocumentBlockParamSourceUnion{
						OfText: &anthropic.PlainTextSourceParam{
							Data: "The grass is green. The sky is blue.",
						},
					},
					Title:     anthropic.String("My Document"),
					Context:   anthropic.String("This is a trustworthy document."),
					Citations: anthropic.CitationsConfigParam{Enabled: anthropic.Bool(true)},
				},
			},
			anthropic.NewTextBlock("What color is the grass and sky?"),
		),
	},
})
if err != nil {
	log.Fatal(err)
}
fmt.Println(response)
示例代码
AnthropicClient client = AnthropicOkHttpClient.fromEnv();

PlainTextSource source = PlainTextSource.builder()
    .data("The grass is green. The sky is blue.")
    .build();

DocumentBlockParam documentParam = DocumentBlockParam.builder()
    .source(source)
    .title("My Document")
    .context("This is a trustworthy document.")
    .citations(CitationsConfigParam.builder()
        .enabled(true)
        .build())
    .build();

TextBlockParam textBlockParam = TextBlockParam.builder()
    .text("What color is the grass and sky?")
    .build();

MessageCreateParams params = MessageCreateParams.builder()
    .model(Model.CLAUDE_OPUS_5_5)
    .maxTokens(1024)
    .addUserMessageOfContentBlockParams(
        List.of(
            ContentBlockParam.ofDocument(documentParam),
            ContentBlockParam.ofText(textBlockParam)
        )
    )
    .build();

Message message = client.messages().create(params);
System.out.println(message);
示例代码
$client = new Client();

$response = $client->messages->create(
    maxTokens: 1024,
    messages: [
        [
            'role' => 'user',
            'content' => [
                [
                    'type' => 'document',
                    'source' => [
                        'type' => 'text',
                        'media_type' => 'text/plain',
                        'data' => 'The grass is green. The sky is blue.',
                    ],
                    'title' => 'My Document',
                    'context' => 'This is a trustworthy document.',
                    'citations' => ['enabled' => true],
                ],
                [
                    'type' => 'text',
                    'text' => 'What color is the grass and sky?',
                ],
            ],
        ],
    ],
    model: Model::CLAUDE_OPUS_5_5,
);

echo json_encode($response, JSON_PRETTY_PRINT);
示例代码
client = Anthropic::Client.new

response = client.messages.create(
  model: Anthropic::Model::CLAUDE_OPUS_5_5,
  max_tokens: 1024,
  messages: [
    {
      role: "user",
      content: [
        {
          type: "document",
          source: {
            type: "text",
            media_type: "text/plain",
            data: "The grass is green. The sky is blue."
          },
          title: "My Document",
          context: "This is a trustworthy document.",
          citations: { enabled: true }
        },
        {
          type: "text",
          text: "What color is the grass and sky?"
        }
      ]
    }
  ]
)

puts response

引用的工作原理

按以下步骤将引用功能与 Claude 集成:

提供文档并启用引用

  • 以任一受支持的格式包含文档:PDF、纯文本或自定义内容文档。
  • 在每个文档上设置 citations.enabled=true。目前,在一个请求中必须对所有文档全部启用或全部不启用引用。
  • 目前仅支持文本引用。尚不支持图像引用。

文档被处理

  • 文档内容会被 “chunked”(分块),以定义可能引用的最小粒度。例如,句子分块允许 Claude 引用单个句子,或将多个连续句子串联起来以引用一个段落或更长的篇幅。

    • 对于 PDF: 按照 PDF 支持中所述提取文本,并将内容分块为句子。目前不支持引用 PDF 中的图像。
    • 对于纯文本文档: 内容被分块为可供引用的句子。
    • 对于自定义内容文档: 您提供的内容块按原样使用,不做进一步分块。

Claude 提供带引用的回答

  • 回答现在可能包含多个文本块,每个文本块可以包含 Claude 提出的陈述以及支持该陈述的引用列表。

  • 引用指向源文档中的特定位置。这些引用的格式取决于被引用文档的类型。

    • 对于 PDF: 引用包含页码范围(从 1 开始索引)。
    • 对于纯文本文档: 引用包含字符索引范围(从 0 开始索引)。
    • 对于自定义内容文档: 引用包含与所提供的原始内容列表相对应的内容块索引范围(从 0 开始索引)。
  • 提供文档索引以指示引用来源,该索引根据您原始请求中所有文档的列表从 0 开始编号。

可引用与不可引用的内容

  • 文档 source 内容中的文本可以被引用。
  • title 和 context 是可选字段,会传递给模型,但不用于被引用的内容。
  • title 有长度限制,因此 context 字段可用于以文本或字符串化 JSON 的形式存储文档元数据。

引用索引

  • 文档索引根据请求中所有文档内容块的列表(跨越所有消息)从 0 开始编号。
  • 字符索引从 0 开始,结束索引为不包含(exclusive)。
  • 页码从 1 开始,结束页码为不包含。
  • 内容块索引根据自定义内容文档中提供的 content 列表从 0 开始编号,结束索引为不包含。

令牌成本

  • 由于系统提示的附加内容和文档分块,启用引用会导致输入令牌略有增加。
  • 不过,引用功能在输出令牌方面非常高效。在内部,模型以标准化格式输出引用,然后这些引用被解析为被引用文本和文档位置索引。cited_text 字段是为方便起见而提供的,不计入输出令牌。
  • 在后续对话轮次中传回时,cited_text 也不计入输入令牌。

功能兼容性

引用功能可与其他 API 功能配合使用,包括 “prompt caching”(提示缓存)、令牌计数和批处理。

获取带引用的结构化数据

要为每个提取的值获取引用,请提示 Claude 每个字段输出一行作为回答。然后在您的代码中将这些行解析为结构化数据。

避免使用仅要求输出 JSON 的用户提示或系统提示,例如 "只返回一个 JSON 对象,不要输出其他文本"。使用此类提示的请求会成功,但响应中可能只包含很少的引用,甚至没有引用。

以下提示要求从本页顶部示例中的纯文本文档中提取两个字段。请用它替换该示例中的问题进行发送:

示例代码
Extract these fields from the document: grass_color, sky_color.
Answer with one line per field in the form "field: value", and cite the document for each line. After the colon, write only the value, with no explanation or quotation.

在以下响应中,每个值都有各自的引用:

示例代码
{
  "content": [
    { "type": "text", "text": "grass_color: " },
    {
      "type": "text",
      "text": "green",
      "citations": [
        {
          "type": "char_location",
          "cited_text": "The grass is green. ",
          "document_index": 0,
          "document_title": "My Document",
          "start_char_index": 0,
          "end_char_index": 20
        }
      ]
    },
    { "type": "text", "text": "\nsky_color: " },
    {
      "type": "text",
      "text": "blue",
      "citations": [
        {
          "type": "char_location",
          "cited_text": "The sky is blue.",
          "document_index": 0,
          "document_title": "My Document",
          "start_char_index": 20,
          "end_char_index": 36
        }
      ]
    }
  ]
}

要解析响应,请按照以下步骤操作:

  1. 按顺序读取 text 块,并将每个块的文本添加到当前行。在每个换行符处开始新的一行。
  2. 将每个块的 citations 附加到包含该块部分文本的每一行。一个带引用的块可以只包含一个值(如本示例所示),也可以包含整行。
  3. 跳过不含冒号的行。将其余每一行在第一个冒号处拆分为字段名和值,并去除两者的首尾空格。跳过名称不属于您的字段的行,例如结尾说明。
  4. 检查每个字段是否都有值且至少有一个引用。API 不保证这两点。

将提示缓存与引用结合使用

引用和提示缓存可以有效地结合使用。

响应中生成的引用块无法直接缓存,但它们所引用的源文档可以被缓存。为了优化性能,请将 cache_control 应用于您的顶层文档内容块。

示例代码
curl https://api.anthropic.com/v1/messages \
  -H "content-type: application/json" \
  -H "x-api-key: $ANTHROPIC_API_KEY" \
  -H "anthropic-version: 2026-10-21" \
  -d '{
    "model": "claude-opus-5-5",
    "max_tokens": 1024,
    "messages": [
      {
        "role": "user",
        "content": [
          {
            "type": "document",
            "source": {
              "type": "text",
              "media_type": "text/plain",
              "data": "This is a very long document with thousands of words..."
            },
            "citations": {"enabled": true},
            "cache_control": {"type": "ephemeral"}
          },
          {
            "type": "text",
            "text": "What does this document say about API features?"
          }
        ]
      }
    ]
  }'
示例代码
ant messages create --model claude-opus-5-5 --max-tokens 1024 <<'YAML'
messages:
  - role: user
    content:
      - type: document
        source:
          type: text
          media_type: text/plain
          data: This is a very long document with thousands of words...
        citations:
          enabled: true
        cache_control:
          type: ephemeral
      - type: text
        text: What does this document say about API features?
YAML
示例代码
client = anthropic.Anthropic()

# 长文档内容(例如技术文档)
long_document = (
    "This is a very long document with thousands of words..." + " ... " * 1000
)  # Minimum cacheable length

response = client.messages.create(
    model="claude-opus-5-5",
    max_tokens=1024,
    messages=[
        {
            "role": "user",
            "content": [
                {
                    "type": "document",
                    "source": {
                        "type": "text",
                        "media_type": "text/plain",
                        "data": long_document,
                    },
                    "citations": {"enabled": True},
                    "cache_control": {
                        "type": "ephemeral"
                    },  # Cache the document content
                },
                {
                    "type": "text",
                    "text": "What does this document say about API features?",
                },
            ],
        }
    ],
)
print(response)
示例代码
const client = new Anthropic();

// 长文档内容(例如技术文档)
const longDocument =
  "This is a very long document with thousands of words..." + " ... ".repeat(1000); // Minimum cacheable length

const response = await client.messages.create({
  model: "claude-opus-5-5",
  max_tokens: 1024,
  messages: [
    {
      role: "user",
      content: [
        {
          type: "document",
          source: {
            type: "text",
            media_type: "text/plain",
            data: longDocument
          },
          citations: { enabled: true },
          cache_control: { type: "ephemeral" } // Cache the document content
        },
        {
          type: "text",
          text: "What does this document say about API features?"
        }
      ]
    }
  ]
});
console.log(response);
示例代码
var client = new AnthropicClient();

// 长文档内容(例如技术文档)
var longDocument =
    "This is a very long document with thousands of words..."
    + string.Concat(Enumerable.Repeat(" ... ", 1000)); // Minimum cacheable length

var response = await client.Messages.Create(
    new()
    {
        Model = Model.ClaudeOpus5_5,
        MaxTokens = 1024,
        Messages =
        [
            new UserMessageParam
            {
                Content = new List<ContentBlockParam>
                {
                    new ContentBlockParam(new DocumentBlockParam(
                        new DocumentBlockParamSource(new PlainTextSource() { Data = longDocument })
                    )
                    {
                        Citations = new CitationsConfigParam { Enabled = true },
                        CacheControl = new CacheControlEphemeral(), // Cache the document content
                    }),
                    new ContentBlockParam(new TextBlockParam("What does this document say about API features?")),
                },
            },
        ],
    }
);

Console.WriteLine(response);
示例代码
client := anthropic.NewClient()

// 长文档内容(例如技术文档)
longDocument := "This is a very long document with thousands of words..." +
	strings.Repeat(" ... ", 1000) // Minimum cacheable length

response, err := client.Messages.New(context.TODO(), anthropic.MessageNewParams{
	Model:     anthropic.ModelClaudeOpus5_5,
	MaxTokens: 1024,
	Messages: []anthropic.MessageParam{
		anthropic.NewUserMessage(
			anthropic.ContentBlockParamUnion{
				OfDocument: &anthropic.DocumentBlockParam{
					Source: anthropic.DocumentBlockParamSourceUnion{
						OfText: &anthropic.PlainTextSourceParam{Data: longDocument},
					},
					Citations:    anthropic.CitationsConfigParam{Enabled: anthropic.Bool(true)},
					CacheControl: anthropic.NewCacheControlEphemeralParam(), // Cache the document content
				},
			},
			anthropic.NewTextBlock("What does this document say about API features?"),
		),
	},
})
if err != nil {
	log.Fatal(err)
}
fmt.Println(response)
示例代码
AnthropicClient client = AnthropicOkHttpClient.fromEnv();

// 长文档内容(例如技术文档)
String longDocument =
    "This is a very long document with thousands of words..."
        + " ... ".repeat(1000); // Minimum cacheable length

DocumentBlockParam documentParam = DocumentBlockParam.builder()
    .source(PlainTextSource.builder()
        .data(longDocument)
        .build())
    .citations(CitationsConfigParam.builder()
        .enabled(true)
        .build())
    .cacheControl(CacheControlEphemeral.builder().build()) // Cache the document content
    .build();

TextBlockParam textBlockParam = TextBlockParam.builder()
    .text("What does this document say about API features?")
    .build();

MessageCreateParams params = MessageCreateParams.builder()
    .model(Model.CLAUDE_OPUS_5_5)
    .maxTokens(1024)
    .addUserMessageOfContentBlockParams(
        List.of(
            ContentBlockParam.ofDocument(documentParam),
            ContentBlockParam.ofText(textBlockParam)
        )
    )
    .build();

Message message = client.messages().create(params);
System.out.println(message);
示例代码
$client = new Client();

// 长文档内容(例如技术文档)
$longDocument =
    'This is a very long document with thousands of words...'
    . str_repeat(' ... ', 1000); // Minimum cacheable length

$response = $client->messages->create(
    maxTokens: 1024,
    messages: [
        [
            'role' => 'user',
            'content' => [
                [
                    'type' => 'document',
                    'source' => [
                        'type' => 'text',
                        'media_type' => 'text/plain',
                        'data' => $longDocument,
                    ],
                    'citations' => ['enabled' => true],
                    'cache_control' => ['type' => 'ephemeral'], // Cache the document content
                ],
                [
                    'type' => 'text',
                    'text' => 'What does this document say about API features?',
                ],
            ],
        ],
    ],
    model: Model::CLAUDE_OPUS_5_5,
);

echo json_encode($response, JSON_PRETTY_PRINT);
示例代码
client = Anthropic::Client.new

# 长文档内容(例如技术文档)
long_document =
  "This is a very long document with thousands of words..." +
  " ... " * 1000 # Minimum cacheable length

response = client.messages.create(
  model: Anthropic::Model::CLAUDE_OPUS_5_5,
  max_tokens: 1024,
  messages: [
    {
      role: "user",
      content: [
        {
          type: "document",
          source: {
            type: "text",
            media_type: "text/plain",
            data: long_document
          },
          citations: { enabled: true },
          cache_control: { type: "ephemeral" } # Cache the document content
        },
        {
          type: "text",
          text: "What does this document say about API features?"
        }
      ]
    }
  ]
)

puts response

在此示例中:

  • 文档内容通过文档块上的 cache_control 进行缓存。
  • 文档上启用了引用。
  • Claude 可以在受益于已缓存文档内容的同时生成带引用的响应。
  • 使用同一文档的后续请求将受益于已缓存的内容。

文档类型

选择文档类型

引用功能支持三种文档类型。文档可以直接在消息中提供(base64、文本或 URL),也可以通过 Files API 上传并通过 file_id 引用:

类型最适用于分块方式引用格式
纯文本简单文本文档、散文句子字符索引(从 0 开始)
PDF包含文本内容的 PDF 文件句子页码(从 1 开始)
自定义内容列表、转录文本、特殊格式、更细粒度的引用无额外分块块索引(从 0 开始)

纯文本文档

纯文本文档会自动分块为句子。您可以内联提供它们,也可以通过其 file_id 引用它们:

内联文本

本页顶部的入门示例展示了每种 SDK 中完整的纯文本请求。文档块使用 text 源:

示例代码
{
  "type": "document",
  "source": {
    "type": "text",
    "media_type": "text/plain",
    "data": "Plain text content..."
  },
  "title": "Document Title",
  "context": "Context about the document that will not be cited from",
  "citations": { "enabled": true }
}

Files API

这些示例将通过 Files API 上传的文件作为 document 源进行引用。

示例代码
curl -X POST https://api.anthropic.com/v1/messages \
  -H "x-api-key: $ANTHROPIC_API_KEY" \
  -H "anthropic-version: 2026-10-21" \
  -H "content-type: application/json" \
  -d '{
    "model": "claude-opus-5-5",
    "max_tokens": 1024,
    "messages": [
      {
        "role": "user",
        "content": [
          {
            "type": "document",
            "source": {"type": "file", "file_id": "'"$FILE_ID"'"},
            "title": "Document Title",
            "context": "Context about the document that will not be cited from",
            "citations": {"enabled": true}
          },
          {
            "type": "text",
            "text": "Summarize this document."
          }
        ]
      }
    ]
  }'
示例代码
ant messages create <<YAML
model: claude-opus-5-5
max_tokens: 1024
messages:
  - role: user
    content:
      - type: document
        source:
          type: file
          file_id: $FILE_ID
        title: Document Title
        context: Context about the document that will not be cited from
        citations:
          enabled: true
      - type: text
        text: Summarize this document.
YAML
示例代码
cited_response = client.messages.create(
    model="claude-opus-5-5",
    max_tokens=1024,
    messages=[
        {
            "role": "user",
            "content": [
                {
                    "type": "document",
                    "source": {"type": "file", "file_id": file_id},
                    "title": "Document Title",
                    "context": "Context about the document that will not be cited from",
                    "citations": {"enabled": True},
                },
                {"type": "text", "text": "Summarize this document."},
            ],
        }
    ],
)
print(cited_response)
示例代码
const citedResponse = await client.messages.create({
  model: "claude-opus-5-5",
  max_tokens: 1024,
  messages: [
    {
      role: "user",
      content: [
        {
          type: "document",
          source: { type: "file", file_id: uploaded.id },
          title: "Document Title",
          context: "Context about the document that will not be cited from",
          citations: { enabled: true }
        },
        {
          type: "text",
          text: "Summarize this document."
        }
      ]
    }
  ]
});
console.log(citedResponse);
示例代码
var citedResponse = await client.Messages.Create(
    new MessageCreateParams
    {
        Model = Model.ClaudeOpus5_5,
        MaxTokens = 1024,
        Messages =
        [
            new UserMessageParam
            {
                Content = new List<ContentBlockParam>
                {
                    new DocumentBlockParam
                    {
                        Source = new FileDocumentSource { FileID = fileId },
                        Title = "Document Title",
                        Context = "Context about the document that will not be cited from",
                        Citations = new CitationsConfigParam { Enabled = true },
                    },
                    new TextBlockParam { Text = "Summarize this document." },
                }
            }
        ]
    });

Console.WriteLine(citedResponse);
示例代码
citedMsg, err := client.Messages.New(context.Background(),
	anthropic.MessageNewParams{
		Model:     anthropic.ModelClaudeOpus5_5,
		MaxTokens: 1024,
		Messages: []anthropic.MessageParam{
			anthropic.NewUserMessage(
				anthropic.ContentBlockParamUnion{
					OfDocument: &anthropic.DocumentBlockParam{
						Source: anthropic.DocumentBlockParamSourceUnion{
							OfFile: &anthropic.FileDocumentSourceParam{FileID: fileID},
						},
						Title:     anthropic.String("Document Title"),
						Context:   anthropic.String("Context about the document that will not be cited from"),
						Citations: anthropic.CitationsConfigParam{Enabled: anthropic.Bool(true)},
					},
				},
				anthropic.NewTextBlock("Summarize this document."),
			),
		},
	})
if err != nil {
	log.Fatal(err)
}
fmt.Println(citedMsg)
示例代码
MessageCreateParams citedParams = MessageCreateParams.builder()
    .model(Model.CLAUDE_OPUS_5_5)
    .maxTokens(1024)
    .addUserMessageOfContentBlockParams(List.of(
        ContentBlockParam.ofDocument(DocumentBlockParam.builder()
            .fileSource(fileId)
            .title("Document Title")
            .context("Context about the document that will not be cited from")
            .citations(CitationsConfigParam.builder()
                .enabled(true)
                .build())
            .build()),
        ContentBlockParam.ofText(TextBlockParam.builder()
            .text("Summarize this document.")
            .build())
    ))
    .build();

Message citedMessage = client.messages().create(citedParams);
System.out.println(citedMessage);
示例代码
$citedResponse = $client->messages->create(
    maxTokens: 1024,
    messages: [
        [
            'role' => 'user',
            'content' => [
                [
                    'type' => 'document',
                    'source' => ['type' => 'file', 'fileID' => $fileId],
                    'title' => 'Document Title',
                    'context' => 'Context about the document that will not be cited from',
                    'citations' => ['enabled' => true],
                ],
                ['type' => 'text', 'text' => 'Summarize this document.'],
            ],
        ],
    ],
    model: 'claude-opus-5-5',
);

echo $citedResponse;
示例代码
cited_response = client.messages.create(
  model: Anthropic::Model::CLAUDE_OPUS_5_5,
  max_tokens: 1024,
  messages: [
    {
      role: "user",
      content: [
        {
          type: "document",
          source: { type: "file", file_id: file_id },
          title: "Document Title",
          context: "Context about the document that will not be cited from",
          citations: { enabled: true }
        },
        {
          type: "text",
          text: "Summarize this document."
        }
      ]
    }
  ]
)

puts cited_response
示例代码
{
  "type": "char_location",
  "cited_text": "The exact text being cited", // not counted toward output tokens
  "document_index": 0,
  "document_title": "Document Title",
  "start_char_index": 0, // 0-indexed
  "end_char_index": 50 // exclusive
}

PDF 文档

PDF 文档可以以 base64 编码数据、URL 或 file_id 的形式提供。PDF 文本会被提取并分块为句子。由于尚不支持图像引用,因此作为文档扫描件且不包含可提取文本的 PDF 无法被引用。

Base64

示例代码
PDF_BASE64=$(base64 /path/to/document.pdf | tr -d '\n')

curl https://api.anthropic.com/v1/messages \
  -H "content-type: application/json" \
  -H "x-api-key: $ANTHROPIC_API_KEY" \
  -H "anthropic-version: 2026-10-21" \
  -d '{
    "model": "claude-opus-5-5",
    "max_tokens": 1024,
    "messages": [
      {
        "role": "user",
        "content": [
          {
            "type": "document",
            "source": {
              "type": "base64",
              "media_type": "application/pdf",
              "data": "'"$PDF_BASE64"'"
            },
            "title": "Document Title",
            "context": "Context about the document that will not be cited from",
            "citations": {"enabled": true}
          },
          {
            "type": "text",
            "text": "Summarize this document."
          }
        ]
      }
    ]
  }'
示例代码
ant messages create <<'YAML'
model: claude-opus-5-5
max_tokens: 1024
messages:
  - role: user
    content:
      - type: document
        source:
          type: base64
          media_type: application/pdf
          data: "@/path/to/document.pdf"
        title: Document Title
        context: Context about the document that will not be cited from
        citations:
          enabled: true
      - type: text
        text: Summarize this document.
YAML
示例代码
client = anthropic.Anthropic()

pdf_base64 = base64.standard_b64encode(
    pathlib.Path("/path/to/document.pdf").read_bytes()
).decode()

response = client.messages.create(
    model="claude-opus-5-5",
    max_tokens=1024,
    messages=[
        {
            "role": "user",
            "content": [
                {
                    "type": "document",
                    "source": {
                        "type": "base64",
                        "media_type": "application/pdf",
                        "data": pdf_base64,
                    },
                    "title": "Document Title",
                    "context": "Context about the document that will not be cited from",
                    "citations": {"enabled": True},
                },
                {"type": "text", "text": "Summarize this document."},
            ],
        }
    ],
)
print(response)
示例代码
const client = new Anthropic();

const pdfBase64 = Buffer.from(await readFile("/path/to/document.pdf")).toString("base64");

const response = await client.messages.create({
  model: "claude-opus-5-5",
  max_tokens: 1024,
  messages: [
    {
      role: "user",
      content: [
        {
          type: "document",
          source: {
            type: "base64",
            media_type: "application/pdf",
            data: pdfBase64
          },
          title: "Document Title",
          context: "Context about the document that will not be cited from",
          citations: { enabled: true }
        },
        {
          type: "text",
          text: "Summarize this document."
        }
      ]
    }
  ]
});
console.log(response);
示例代码
var client = new AnthropicClient();

var pdfBase64 = Convert.ToBase64String(await File.ReadAllBytesAsync("/path/to/document.pdf"));

var response = await client.Messages.Create(
    new()
    {
        Model = Model.ClaudeOpus5_5,
        MaxTokens = 1024,
        Messages =
        [
            new UserMessageParam
            {
                Content = new List<ContentBlockParam>
                {
                    new ContentBlockParam(new DocumentBlockParam(
                        new DocumentBlockParamSource(new Base64PdfSource() { Data = pdfBase64 })
                    )
                    {
                        Title = "Document Title",
                        Context = "Context about the document that will not be cited from",
                        Citations = new CitationsConfigParam { Enabled = true },
                    }),
                    new ContentBlockParam(new TextBlockParam("Summarize this document.")),
                },
            },
        ],
    }
);

Console.WriteLine(response);
示例代码
client := anthropic.NewClient()

pdfBytes, err := os.ReadFile("/path/to/document.pdf")
if err != nil {
	log.Fatal(err)
}
pdfBase64 := base64.StdEncoding.EncodeToString(pdfBytes)

response, err := client.Messages.New(context.TODO(), anthropic.MessageNewParams{
	Model:     anthropic.ModelClaudeOpus5_5,
	MaxTokens: 1024,
	Messages: []anthropic.MessageParam{
		anthropic.NewUserMessage(
			anthropic.ContentBlockParamUnion{
				OfDocument: &anthropic.DocumentBlockParam{
					Source: anthropic.DocumentBlockParamSourceUnion{
						OfBase64: &anthropic.Base64PDFSourceParam{
							Data: pdfBase64,
						},
					},
					Title:     anthropic.String("Document Title"),
					Context:   anthropic.String("Context about the document that will not be cited from"),
					Citations: anthropic.CitationsConfigParam{Enabled: anthropic.Bool(true)},
				},
			},
			anthropic.NewTextBlock("Summarize this document."),
		),
	},
})
if err != nil {
	log.Fatal(err)
}
fmt.Println(response)
示例代码
AnthropicClient client = AnthropicOkHttpClient.fromEnv();

byte[] pdfBytes = Files.readAllBytes(Path.of("/path/to/document.pdf"));
String pdfBase64 = Base64.getEncoder().encodeToString(pdfBytes);

DocumentBlockParam documentParam = DocumentBlockParam.builder()
    .source(Base64PdfSource.builder()
        .data(pdfBase64)
        .build())
    .title("Document Title")
    .context("Context about the document that will not be cited from")
    .citations(CitationsConfigParam.builder()
        .enabled(true)
        .build())
    .build();

MessageCreateParams params = MessageCreateParams.builder()
    .model(Model.CLAUDE_OPUS_5_5)
    .maxTokens(1024)
    .addUserMessageOfContentBlockParams(
        List.of(
            ContentBlockParam.ofDocument(documentParam),
            ContentBlockParam.ofText(TextBlockParam.builder()
                .text("Summarize this document.")
                .build())
        )
    )
    .build();

Message message = client.messages().create(params);
System.out.println(message);
示例代码
$client = new Client();

$pdfBase64 = base64_encode(file_get_contents('/path/to/document.pdf'));

$response = $client->messages->create(
    maxTokens: 1024,
    messages: [
        [
            'role' => 'user',
            'content' => [
                [
                    'type' => 'document',
                    'source' => [
                        'type' => 'base64',
                        'media_type' => 'application/pdf',
                        'data' => $pdfBase64,
                    ],
                    'title' => 'Document Title',
                    'context' => 'Context about the document that will not be cited from',
                    'citations' => ['enabled' => true],
                ],
                [
                    'type' => 'text',
                    'text' => 'Summarize this document.',
                ],
            ],
        ],
    ],
    model: 'claude-opus-5-5',
);

echo json_encode($response, JSON_PRETTY_PRINT);
示例代码
client = Anthropic::Client.new

pdf_base64 = Base64.strict_encode64(File.binread("/path/to/document.pdf"))

response = client.messages.create(
  model: Anthropic::Model::CLAUDE_OPUS_5_5,
  max_tokens: 1024,
  messages: [
    {
      role: "user",
      content: [
        {
          type: "document",
          source: {
            type: "base64",
            media_type: "application/pdf",
            data: pdf_base64
          },
          title: "Document Title",
          context: "Context about the document that will not be cited from",
          citations: { enabled: true }
        },
        {
          type: "text",
          text: "Summarize this document."
        }
      ]
    }
  ]
)

puts response

URL

示例代码
curl https://api.anthropic.com/v1/messages \
  -H "content-type: application/json" \
  -H "x-api-key: $ANTHROPIC_API_KEY" \
  -H "anthropic-version: 2026-10-21" \
  -d '{
    "model": "claude-opus-5-5",
    "max_tokens": 1024,
    "messages": [
      {
        "role": "user",
        "content": [
          {
            "type": "document",
            "source": {
              "type": "url",
              "url": "https://assets.anthropic.com/m/1cd9d098ac3e6467/original/Claude-3-Model-Card-October-Addendum.pdf"
            },
            "title": "Document Title",
            "context": "Context about the document that will not be cited from",
            "citations": {"enabled": true}
          },
          {
            "type": "text",
            "text": "Summarize this document."
          }
        ]
      }
    ]
  }'
示例代码
ant messages create <<'YAML'
model: claude-opus-5-5
max_tokens: 1024
messages:
  - role: user
    content:
      - type: document
        source:
          type: url
          url: https://assets.anthropic.com/m/1cd9d098ac3e6467/original/Claude-3-Model-Card-October-Addendum.pdf
        title: Document Title
        context: Context about the document that will not be cited from
        citations:
          enabled: true
      - type: text
        text: Summarize this document.
YAML
示例代码
client = anthropic.Anthropic()

response = client.messages.create(
    model="claude-opus-5-5",
    max_tokens=1024,
    messages=[
        {
            "role": "user",
            "content": [
                {
                    "type": "document",
                    "source": {
                        "type": "url",
                        "url": "https://assets.anthropic.com/m/1cd9d098ac3e6467/original/Claude-3-Model-Card-October-Addendum.pdf",
                    },
                    "title": "Document Title",
                    "context": "Context about the document that will not be cited from",
                    "citations": {"enabled": True},
                },
                {"type": "text", "text": "Summarize this document."},
            ],
        }
    ],
)
print(response)
示例代码
const client = new Anthropic();

const response = await client.messages.create({
  model: "claude-opus-5-5",
  max_tokens: 1024,
  messages: [
    {
      role: "user",
      content: [
        {
          type: "document",
          source: {
            type: "url",
            url: "https://assets.anthropic.com/m/1cd9d098ac3e6467/original/Claude-3-Model-Card-October-Addendum.pdf"
          },
          title: "Document Title",
          context: "Context about the document that will not be cited from",
          citations: { enabled: true }
        },
        {
          type: "text",
          text: "Summarize this document."
        }
      ]
    }
  ]
});
console.log(response);
示例代码
var client = new AnthropicClient();

var response = await client.Messages.Create(
    new()
    {
        Model = Model.ClaudeOpus5_5,
        MaxTokens = 1024,
        Messages =
        [
            new UserMessageParam
            {
                Content = new List<ContentBlockParam>
                {
                    new ContentBlockParam(new DocumentBlockParam(
                        new DocumentBlockParamSource(new UrlPdfSource()
                        {
                            Url = "https://assets.anthropic.com/m/1cd9d098ac3e6467/original/Claude-3-Model-Card-October-Addendum.pdf",
                        })
                    )
                    {
                        Title = "Document Title",
                        Context = "Context about the document that will not be cited from",
                        Citations = new CitationsConfigParam { Enabled = true },
                    }),
                    new ContentBlockParam(new TextBlockParam("Summarize this document.")),
                },
            },
        ],
    }
);

Console.WriteLine(response);
示例代码
client := anthropic.NewClient()

response, err := client.Messages.New(context.TODO(), anthropic.MessageNewParams{
	Model:     anthropic.ModelClaudeOpus5_5,
	MaxTokens: 1024,
	Messages: []anthropic.MessageParam{
		anthropic.NewUserMessage(
			anthropic.ContentBlockParamUnion{
				OfDocument: &anthropic.DocumentBlockParam{
					Source: anthropic.DocumentBlockParamSourceUnion{
						OfURL: &anthropic.URLPDFSourceParam{
							URL: "https://assets.anthropic.com/m/1cd9d098ac3e6467/original/Claude-3-Model-Card-October-Addendum.pdf",
						},
					},
					Title:     anthropic.String("Document Title"),
					Context:   anthropic.String("Context about the document that will not be cited from"),
					Citations: anthropic.CitationsConfigParam{Enabled: anthropic.Bool(true)},
				},
			},
			anthropic.NewTextBlock("Summarize this document."),
		),
	},
})
if err != nil {
	log.Fatal(err)
}
fmt.Println(response)
示例代码
AnthropicClient client = AnthropicOkHttpClient.fromEnv();

DocumentBlockParam documentParam = DocumentBlockParam.builder()
    .source(UrlPdfSource.builder()
        .url("https://assets.anthropic.com/m/1cd9d098ac3e6467/original/Claude-3-Model-Card-October-Addendum.pdf")
        .build())
    .title("Document Title")
    .context("Context about the document that will not be cited from")
    .citations(CitationsConfigParam.builder()
        .enabled(true)
        .build())
    .build();

MessageCreateParams params = MessageCreateParams.builder()
    .model(Model.CLAUDE_OPUS_5_5)
    .maxTokens(1024)
    .addUserMessageOfContentBlockParams(
        List.of(
            ContentBlockParam.ofDocument(documentParam),
            ContentBlockParam.ofText(TextBlockParam.builder()
                .text("Summarize this document.")
                .build())
        )
    )
    .build();

Message message = client.messages().create(params);
System.out.println(message);
示例代码
$client = new Client();

$response = $client->messages->create(
    maxTokens: 1024,
    messages: [
        [
            'role' => 'user',
            'content' => [
                [
                    'type' => 'document',
                    'source' => [
                        'type' => 'url',
                        'url' => 'https://assets.anthropic.com/m/1cd9d098ac3e6467/original/Claude-3-Model-Card-October-Addendum.pdf',
                    ],
                    'title' => 'Document Title',
                    'context' => 'Context about the document that will not be cited from',
                    'citations' => ['enabled' => true],
                ],
                [
                    'type' => 'text',
                    'text' => 'Summarize this document.',
                ],
            ],
        ],
    ],
    model: 'claude-opus-5-5',
);

echo json_encode($response, JSON_PRETTY_PRINT);
示例代码
client = Anthropic::Client.new

response = client.messages.create(
  model: "claude-opus-5-5",
  max_tokens: 1024,
  messages: [
    {
      role: "user",
      content: [
        {
          type: "document",
          source: {
            type: "url",
            url: "https://assets.anthropic.com/m/1cd9d098ac3e6467/original/Claude-3-Model-Card-October-Addendum.pdf"
          },
          title: "Document Title",
          context: "Context about the document that will not be cited from",
          citations: { enabled: true }
        },
        {
          type: "text",
          text: "Summarize this document."
        }
      ]
    }
  ]
)

puts response

Files API

这些示例将通过 Files API 上传的文件作为 document 源进行引用。

示例代码
curl -X POST https://api.anthropic.com/v1/messages \
  -H "x-api-key: $ANTHROPIC_API_KEY" \
  -H "anthropic-version: 2026-10-21" \
  -H "content-type: application/json" \
  -d '{
    "model": "claude-opus-5-5",
    "max_tokens": 1024,
    "messages": [
      {
        "role": "user",
        "content": [
          {
            "type": "document",
            "source": {"type": "file", "file_id": "'"$FILE_ID"'"},
            "title": "Document Title",
            "context": "Context about the document that will not be cited from",
            "citations": {"enabled": true}
          },
          {
            "type": "text",
            "text": "Summarize this document."
          }
        ]
      }
    ]
  }'
示例代码
ant messages create <<YAML
model: claude-opus-5-5
max_tokens: 1024
messages:
  - role: user
    content:
      - type: document
        source:
          type: file
          file_id: $FILE_ID
        title: Document Title
        context: Context about the document that will not be cited from
        citations:
          enabled: true
      - type: text
        text: Summarize this document.
YAML
示例代码
cited_response = client.messages.create(
    model="claude-opus-5-5",
    max_tokens=1024,
    messages=[
        {
            "role": "user",
            "content": [
                {
                    "type": "document",
                    "source": {"type": "file", "file_id": file_id},
                    "title": "Document Title",
                    "context": "Context about the document that will not be cited from",
                    "citations": {"enabled": True},
                },
                {"type": "text", "text": "Summarize this document."},
            ],
        }
    ],
)
print(cited_response)
示例代码
const citedResponse = await client.messages.create({
  model: "claude-opus-5-5",
  max_tokens: 1024,
  messages: [
    {
      role: "user",
      content: [
        {
          type: "document",
          source: { type: "file", file_id: uploaded.id },
          title: "Document Title",
          context: "Context about the document that will not be cited from",
          citations: { enabled: true }
        },
        {
          type: "text",
          text: "Summarize this document."
        }
      ]
    }
  ]
});
console.log(citedResponse);
示例代码
var citedResponse = await client.Messages.Create(
    new MessageCreateParams
    {
        Model = Model.ClaudeOpus5_5,
        MaxTokens = 1024,
        Messages =
        [
            new UserMessageParam
            {
                Content = new List<ContentBlockParam>
                {
                    new DocumentBlockParam
                    {
                        Source = new FileDocumentSource { FileID = fileId },
                        Title = "Document Title",
                        Context = "Context about the document that will not be cited from",
                        Citations = new CitationsConfigParam { Enabled = true },
                    },
                    new TextBlockParam { Text = "Summarize this document." },
                }
            }
        ]
    });

Console.WriteLine(citedResponse);
示例代码
citedMsg, err := client.Messages.New(context.Background(),
	anthropic.MessageNewParams{
		Model:     anthropic.ModelClaudeOpus5_5,
		MaxTokens: 1024,
		Messages: []anthropic.MessageParam{
			anthropic.NewUserMessage(
				anthropic.ContentBlockParamUnion{
					OfDocument: &anthropic.DocumentBlockParam{
						Source: anthropic.DocumentBlockParamSourceUnion{
							OfFile: &anthropic.FileDocumentSourceParam{FileID: fileID},
						},
						Title:     anthropic.String("Document Title"),
						Context:   anthropic.String("Context about the document that will not be cited from"),
						Citations: anthropic.CitationsConfigParam{Enabled: anthropic.Bool(true)},
					},
				},
				anthropic.NewTextBlock("Summarize this document."),
			),
		},
	})
if err != nil {
	log.Fatal(err)
}
fmt.Println(citedMsg)
示例代码
MessageCreateParams citedParams = MessageCreateParams.builder()
    .model(Model.CLAUDE_OPUS_5_5)
    .maxTokens(1024)
    .addUserMessageOfContentBlockParams(List.of(
        ContentBlockParam.ofDocument(DocumentBlockParam.builder()
            .fileSource(fileId)
            .title("Document Title")
            .context("Context about the document that will not be cited from")
            .citations(CitationsConfigParam.builder()
                .enabled(true)
                .build())
            .build()),
        ContentBlockParam.ofText(TextBlockParam.builder()
            .text("Summarize this document.")
            .build())
    ))
    .build();

Message citedMessage = client.messages().create(citedParams);
System.out.println(citedMessage);
示例代码
$citedResponse = $client->messages->create(
    maxTokens: 1024,
    messages: [
        [
            'role' => 'user',
            'content' => [
                [
                    'type' => 'document',
                    'source' => ['type' => 'file', 'fileID' => $fileId],
                    'title' => 'Document Title',
                    'context' => 'Context about the document that will not be cited from',
                    'citations' => ['enabled' => true],
                ],
                ['type' => 'text', 'text' => 'Summarize this document.'],
            ],
        ],
    ],
    model: 'claude-opus-5-5',
);

echo $citedResponse;
示例代码
cited_response = client.messages.create(
  model: Anthropic::Model::CLAUDE_OPUS_5_5,
  max_tokens: 1024,
  messages: [
    {
      role: "user",
      content: [
        {
          type: "document",
          source: { type: "file", file_id: file_id },
          title: "Document Title",
          context: "Context about the document that will not be cited from",
          citations: { enabled: true }
        },
        {
          type: "text",
          text: "Summarize this document."
        }
      ]
    }
  ]
)

puts cited_response
示例代码
{
  "type": "page_location",
  "cited_text": "The exact text being cited", // not counted toward output tokens
  "document_index": 0,
  "document_title": "Document Title",
  "start_page_number": 1, // 1-indexed
  "end_page_number": 2 // exclusive
}

自定义内容文档

自定义内容文档让您可以控制引用粒度。不会进行额外的分块,各块将按照所提供的内容块提供给模型。

示例代码
curl https://api.anthropic.com/v1/messages \
  -H "content-type: application/json" \
  -H "x-api-key: $ANTHROPIC_API_KEY" \
  -H "anthropic-version: 2026-10-21" \
  -d '{
    "model": "claude-opus-5-5",
    "max_tokens": 1024,
    "messages": [
      {
        "role": "user",
        "content": [
          {
            "type": "document",
            "source": {
              "type": "content",
              "content": [
                {"type": "text", "text": "First chunk"},
                {"type": "text", "text": "Second chunk"}
              ]
            },
            "title": "Document Title",
            "context": "Context about the document that will not be cited from",
            "citations": {"enabled": true}
          },
          {
            "type": "text",
            "text": "Summarize this document."
          }
        ]
      }
    ]
  }'
示例代码
ant messages create <<'YAML'
model: claude-opus-5-5
max_tokens: 1024
messages:
  - role: user
    content:
      - type: document
        source:
          type: content
          content:
            - type: text
              text: First chunk
            - type: text
              text: Second chunk
        title: Document Title
        context: Context about the document that will not be cited from
        citations:
          enabled: true
      - type: text
        text: Summarize this document.
YAML
示例代码
client = anthropic.Anthropic()

response = client.messages.create(
    model="claude-opus-5-5",
    max_tokens=1024,
    messages=[
        {
            "role": "user",
            "content": [
                {
                    "type": "document",
                    "source": {
                        "type": "content",
                        "content": [
                            {"type": "text", "text": "First chunk"},
                            {"type": "text", "text": "Second chunk"},
                        ],
                    },
                    "title": "Document Title",
                    "context": "Context about the document that will not be cited from",
                    "citations": {"enabled": True},
                },
                {"type": "text", "text": "Summarize this document."},
            ],
        }
    ],
)
print(response)
示例代码
const client = new Anthropic();

const response = await client.messages.create({
  model: "claude-opus-5-5",
  max_tokens: 1024,
  messages: [
    {
      role: "user",
      content: [
        {
          type: "document",
          source: {
            type: "content",
            content: [
              { type: "text", text: "First chunk" },
              { type: "text", text: "Second chunk" }
            ]
          },
          title: "Document Title",
          context: "Context about the document that will not be cited from",
          citations: { enabled: true }
        },
        {
          type: "text",
          text: "Summarize this document."
        }
      ]
    }
  ]
});
console.log(response);
示例代码
var client = new AnthropicClient();

var response = await client.Messages.Create(
    new()
    {
        Model = Model.ClaudeOpus5_5,
        MaxTokens = 1024,
        Messages =
        [
            new UserMessageParam
            {
                Content = new List<ContentBlockParam>
                {
                    new ContentBlockParam(new DocumentBlockParam(
                        new DocumentBlockParamSource(new ContentBlockSource()
                        {
                            Content = new ContentBlockSourceContent(new List<MessageContentBlockSourceContent>
                            {
                                new TextBlockParam("First chunk"),
                                new TextBlockParam("Second chunk"),
                            }),
                        })
                    )
                    {
                        Title = "Document Title",
                        Context = "Context about the document that will not be cited from",
                        Citations = new CitationsConfigParam { Enabled = true },
                    }),
                    new ContentBlockParam(new TextBlockParam("Summarize this document.")),
                },
            },
        ],
    }
);

Console.WriteLine(response);
示例代码
client := anthropic.NewClient()

response, err := client.Messages.New(context.TODO(), anthropic.MessageNewParams{
	Model:     anthropic.ModelClaudeOpus5_5,
	MaxTokens: 1024,
	Messages: []anthropic.MessageParam{
		anthropic.NewUserMessage(
			anthropic.ContentBlockParamUnion{
				OfDocument: &anthropic.DocumentBlockParam{
					Source: anthropic.DocumentBlockParamSourceUnion{
						OfContent: &anthropic.ContentBlockSourceParam{
							Content: anthropic.ContentBlockSourceContentUnionParam{
								OfContentBlockSourceContent: []anthropic.ContentBlockSourceContentItemUnionParam{
									{OfText: &anthropic.TextBlockParam{Text: "First chunk"}},
									{OfText: &anthropic.TextBlockParam{Text: "Second chunk"}},
								},
							},
						},
					},
					Title:     anthropic.String("Document Title"),
					Context:   anthropic.String("Context about the document that will not be cited from"),
					Citations: anthropic.CitationsConfigParam{Enabled: anthropic.Bool(true)},
				},
			},
			anthropic.NewTextBlock("Summarize this document."),
		),
	},
})
if err != nil {
	log.Fatal(err)
}
fmt.Println(response)
示例代码
AnthropicClient client = AnthropicOkHttpClient.fromEnv();

DocumentBlockParam documentParam = DocumentBlockParam.builder()
    .source(ContentBlockSource.builder()
        .contentOfBlockSource(
            List.of(
                ContentBlockSourceContent.ofText(TextBlockParam.builder()
                    .text("First chunk")
                    .build()),
                ContentBlockSourceContent.ofText(TextBlockParam.builder()
                    .text("Second chunk")
                    .build())
            )
        )
        .build())
    .title("Document Title")
    .context("Context about the document that will not be cited from")
    .citations(CitationsConfigParam.builder()
        .enabled(true)
        .build())
    .build();

MessageCreateParams params = MessageCreateParams.builder()
    .model(Model.CLAUDE_OPUS_5_5)
    .maxTokens(1024)
    .addUserMessageOfContentBlockParams(
        List.of(
            ContentBlockParam.ofDocument(documentParam),
            ContentBlockParam.ofText(TextBlockParam.builder()
                .text("Summarize this document.")
                .build())
        )
    )
    .build();

Message message = client.messages().create(params);
System.out.println(message);
示例代码
$client = new Client();

$response = $client->messages->create(
    maxTokens: 1024,
    messages: [
        [
            'role' => 'user',
            'content' => [
                [
                    'type' => 'document',
                    'source' => [
                        'type' => 'content',
                        'content' => [
                            ['type' => 'text', 'text' => 'First chunk'],
                            ['type' => 'text', 'text' => 'Second chunk'],
                        ],
                    ],
                    'title' => 'Document Title',
                    'context' => 'Context about the document that will not be cited from',
                    'citations' => ['enabled' => true],
                ],
                [
                    'type' => 'text',
                    'text' => 'Summarize this document.',
                ],
            ],
        ],
    ],
    model: Model::CLAUDE_OPUS_5_5,
);

echo json_encode($response, JSON_PRETTY_PRINT);
示例代码
client = Anthropic::Client.new

response = client.messages.create(
  model: Anthropic::Model::CLAUDE_OPUS_5_5,
  max_tokens: 1024,
  messages: [
    {
      role: "user",
      content: [
        {
          type: "document",
          source: {
            type: "content",
            content: [
              { type: "text", text: "First chunk" },
              { type: "text", text: "Second chunk" }
            ]
          },
          title: "Document Title",
          context: "Context about the document that will not be cited from",
          citations: { enabled: true }
        },
        {
          type: "text",
          text: "Summarize this document."
        }
      ]
    }
  ]
)

puts response
示例代码
{
  "type": "content_block_location",
  "cited_text": "The exact text being cited", // not counted toward output tokens
  "document_index": 0,
  "document_title": "Document Title",
  "start_block_index": 0, // 0-indexed
  "end_block_index": 1 // exclusive
}

响应结构

启用引用后,响应将包含多个带有引用的文本块:

示例代码
{
  "content": [
    { "type": "text", "text": "According to the document, " },
    {
      "type": "text",
      "text": "the grass is green",
      "citations": [
        {
          "type": "char_location",
          "cited_text": "The grass is green. ",
          "document_index": 0,
          "document_title": "Example Document",
          "start_char_index": 0,
          "end_char_index": 20
        }
      ]
    },
    { "type": "text", "text": " and " },
    {
      "type": "text",
      "text": "the sky is blue",
      "citations": [
        {
          "type": "char_location",
          "cited_text": "The sky is blue.",
          "document_index": 0,
          "document_title": "Example Document",
          "start_char_index": 20,
          "end_char_index": 36
        }
      ]
    },
    {
      "type": "text",
      "text": ". Information from page 5 states that "
    },
    {
      "type": "text",
      "text": "water is essential",
      "citations": [
        {
          "type": "page_location",
          "cited_text": "Water is essential for life.",
          "document_index": 1,
          "document_title": "PDF Document",
          "start_page_number": 5,
          "end_page_number": 6
        }
      ]
    },
    {
      "type": "text",
      "text": ". The custom document mentions "
    },
    {
      "type": "text",
      "text": "important findings",
      "citations": [
        {
          "type": "content_block_location",
          "cited_text": "These are important findings.",
          "document_index": 2,
          "document_title": "Custom Content Document",
          "start_block_index": 0,
          "end_block_index": 1
        }
      ]
    }
  ]
}

流式传输支持

对于 “streaming”(流式传输)响应,引用以 content_block_delta 事件中的 citations_delta 增量类型到达。每个增量包含一条引用,用于添加到当前 text 内容块的 citations 列表中。

示例代码
event: message_start
data: {"type": "message_start", ...}

event: content_block_start
data: {"type": "content_block_start", "index": 0, ...}

event: content_block_delta
data: {"type": "content_block_delta", "index": 0,
       "delta": {"type": "text_delta", "text": "According to..."}}

event: content_block_delta
data: {"type": "content_block_delta", "index": 0,
       "delta": {"type": "citations_delta",
                 "citation": {
                     "type": "char_location",
                     "cited_text": "...",
                     "document_index": 0,
                     ...
                 }}}

event: content_block_stop
data: {"type": "content_block_stop", "index": 0}

event: message_stop
data: {"type": "message_stop"}

后续步骤

流式传输消息

了解事件流程,以及与 citations_delta 一同到达的其他内容块增量类型。

搜索结果

将 RAG 管道中的搜索结果作为具有内置引用支持的一等内容块传入。

PDF 支持

了解 Claude 如何从 PDF 中提取文本,以及基于页码的引用如何映射回您的源文件。

Files API

一次上传文档,即可在多个引用请求中通过 file_id 引用它们。

可用性

引用在所有模型上可用,并具备零数据保留和HIPAA 就绪资格。

API 支持

Messages API

  • 可用性:可用

Managed Agents

  • 可用性:不可用

数据处理

数据处理概览

项目零数据保留HIPAA
状态具备资格(不包括 涵盖的模型)具备资格

适用的端点:

  • /v1/messages
文档内容持续同步更新