您可以向 Claude 询问您提供的 PDF 中的任何文本、图片、图表和表格。一些示例用例:
Claude 可以处理任何标准 PDF。请确保您的请求大小符合以下要求:
| 要求 | 限制 |
|---|---|
| 最大请求大小 | 32 MB(因平台而异) |
| 每个请求的最大页数 | 600(当请求的上下文窗口小于 100 万令牌时为 100) |
| 格式 | 标准 PDF(无密码/加密) |
这两个限制均针对整个请求负载,包括与 PDF 一起发送的任何其他内容。对于大型 PDF,建议使用 Files API 上传并通过 file_id 引用,以保持请求负载较小。
由于 PDF 支持依赖于 Claude 的视觉能力,因此它与其他视觉任务具有相同的限制和注意事项。
所有活跃模型均支持 PDF 处理。有关通过 Amazon Bedrock 的 Converse API 实现 PDF 支持的信息,请参阅 Amazon Bedrock PDF 支持。
通过 Converse API(Amazon Bedrock 上的 Claude(Opus 4.6 及更早版本)的一部分)使用 PDF 支持时,有两种不同的文档处理模式:
Converse 文档聊天(原始模式 - 仅文本提取)
Claude PDF 聊天(新模式 - 完整视觉理解)
如果在使用 Converse API 时 Claude 无法看到 PDF 中的图像或图表,您可能需要启用引用标志。如果未启用,Converse 将回退到仅基本文本提取。
从使用 Messages API 的简单示例开始。您可以通过三种方式向 Claude 提供 PDF:
document 内容块中的 base64 编码 PDFfile_id最简单的方法是直接通过 URL 引用 PDF:
client = anthropic.Anthropic()
message = client.messages.create(
model="claude-opus-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",
},
},
{"type": "text", "text": "What are the key findings in this document?"},
],
}
],
)
print(message.content)响应会在 content 中以文本块的形式返回 Claude 的分析结果,并在 usage 中显示令牌消耗:
{
"id": "msg_01Hfp8YuFjQ55VgWbpdHDehB",
"type": "message",
"role": "assistant",
"model": "claude-opus-5",
"content": [
{
"type": "text",
"text": "This document is an addendum to the Claude 3 model card, reporting updated evaluation results. The key findings include..."
}
],
"stop_reason": "end_turn",
"usage": {
"input_tokens": 45000,
"output_tokens": 300
}
}如果您需要从本地系统发送 PDF,或者 URL 不可用时:
import base64
import httpx
# 首先,加载并编码 PDF
pdf_url = "https://assets.anthropic.com/m/1cd9d098ac3e6467/original/Claude-3-Model-Card-October-Addendum.pdf"
pdf_data = base64.standard_b64encode(
httpx.get(pdf_url, follow_redirects=True).content
).decode("utf-8")
# 替代方案:从本地文件加载
# with open("document.pdf", "rb") as f:
# pdf_data = base64.standard_b64encode(f.read()).decode("utf-8")
# 使用 base64 编码发送给 Claude
client = anthropic.Anthropic()
message = client.messages.create(
model="claude-opus-5",
max_tokens=1024,
messages=[
{
"role": "user",
"content": [
{
"type": "document",
"source": {
"type": "base64",
"media_type": "application/pdf",
"data": pdf_data,
},
},
{"type": "text", "text": "What are the key findings in this document?"},
],
}
],
)
print(message.content)对于您将重复使用的 PDF,或者当您希望避免编码开销时,请使用 Files API(测试版):
client = anthropic.Anthropic()
# 上传 PDF 文件
with open("/path/to/document.pdf", "rb") as f:
file_upload = client.beta.files.upload(file=("document.pdf", f, "application/pdf"))
# 在消息中使用已上传的文件
message = client.beta.messages.create(
model="claude-opus-5",
max_tokens=1024,
betas=["files-api-2025-04-14"],
messages=[
{
"role": "user",
"content": [
{
"type": "document",
"source": {"type": "file", "file_id": file_upload.id},
},
{"type": "text", "text": "What are the key findings in this document?"},
],
}
],
)
print(message.content)当您向 Claude 发送 PDF 时,会执行以下步骤:
系统提取文档的内容。
Claude 同时分析文本和图像,以更好地理解文档。
Claude 作出响应,并在相关时引用 PDF 的内容。
PDF 文件的令牌数量取决于从文档中提取的文本总量和页数:
您可以使用令牌计数来估算特定 PDF 的成本。
遵循以下最佳实践以获得最佳结果:
对于大批量处理,请考虑以下方法:
使用提示缓存缓存 PDF,以提高重复查询的性能:
import base64
import httpx
# 首先,加载并编码 PDF
pdf_url = "https://assets.anthropic.com/m/1cd9d098ac3e6467/original/Claude-3-Model-Card-October-Addendum.pdf"
pdf_data = base64.standard_b64encode(
httpx.get(pdf_url, follow_redirects=True).content
).decode("utf-8")
# 使用缓存的文档创建消息
client = anthropic.Anthropic()
message = client.messages.create(
model="claude-opus-5",
max_tokens=1024,
messages=[
{
"role": "user",
"content": [
{
"type": "document",
"source": {
"type": "base64",
"media_type": "application/pdf",
"data": pdf_data,
},
"cache_control": {"type": "ephemeral"},
},
{
"type": "text",
"text": "Which model has the highest human preference win rates across each use-case?",
},
],
}
],
)
print(message.content)使用 Message Batches API 在一个请求中处理多个 PDF:
import base64
import httpx
# 首先,加载并编码 PDF
pdf_url = "https://assets.anthropic.com/m/1cd9d098ac3e6467/original/Claude-3-Model-Card-October-Addendum.pdf"
pdf_data = base64.standard_b64encode(
httpx.get(pdf_url, follow_redirects=True).content
).decode("utf-8")
# 创建一批使用该文档的请求
client = anthropic.Anthropic()
message_batch = client.messages.batches.create(
requests=[
{
"custom_id": "my-first-request",
"params": {
"model": "claude-opus-5",
"max_tokens": 1024,
"messages": [
{
"role": "user",
"content": [
{
"type": "document",
"source": {
"type": "base64",
"media_type": "application/pdf",
"data": pdf_data,
},
},
{
"type": "text",
"text": "Which model has the highest human preference win rates across each use-case?",
},
],
}
],
},
},
{
"custom_id": "my-second-request",
"params": {
"model": "claude-opus-5",
"max_tokens": 1024,
"messages": [
{
"role": "user",
"content": [
{
"type": "document",
"source": {
"type": "base64",
"media_type": "application/pdf",
"data": pdf_data,
},
},
{
"type": "text",
"text": "Extract 5 key insights from this document.",
},
],
}
],
},
},
]
)
print(message_batch)批处理以异步方式进行。要检查进度并在处理结束后检索结果,请参阅批处理。
Claude 的视觉能力使其能够理解和分析图像,为多模态交互开启了令人兴奋的可能性。
在 Claude Cookbook 示例中探索 PDF 处理的实际示例。
查看 PDF 支持的完整 API 文档。
| Supported platforms |
|
|---|
Was this page helpful?