This feature is eligible for Zero Data Retention (ZDR). When your organization has a ZDR arrangement, data sent through this feature is not stored after the API response is returned.
您可以向 Claude 询问您提供的 PDF 中的任何文本、图片、图表和表格。一些示例用例包括:
Claude 适用于任何标准 PDF。确保您的请求大小符合以下要求:
| 要求 | 限制 |
|---|---|
| 最大请求大小 | 32 MB (因平台而异) |
| 每个请求的最大页数 | 600(对于具有 200k 令牌上下文窗口的模型为 100) |
| 格式 | 标准 PDF(无密码/加密) |
两个限制都适用于整个请求有效负载,包括与 PDF 一起发送的任何其他内容。对于大型 PDF,请考虑使用 Files API 上传并通过 file_id 引用,以保持请求有效负载较小。
密集 PDF(许多小字体页面、复杂表格或大量图形)可能会在达到页面限制之前填满上下文窗口。即使使用 Files API,大型 PDF 的请求也可能在达到页面限制之前失败。尝试将文档分成几个部分;对于大型文件,由于每个页面都作为图像处理,对嵌入式图像进行下采样也可以帮助。
由于 PDF 支持依赖于 Claude 的视觉功能,它受到与其他视觉任务相同的 限制和注意事项。
PDF 支持目前通过直接 API 访问和 Google Vertex AI 支持。所有 活跃模型 都支持 PDF 处理。
PDF 支持现在在 Amazon Bedrock 上可用,具有以下注意事项:
通过 Amazon Bedrock 的 Converse API 使用 PDF 支持时,有两种不同的文档处理模式:
重要: 要在 Converse API 中访问 Claude 的完整视觉 PDF 理解功能,您必须启用引用。如果未启用引用,API 将回退到仅基本文本提取。了解更多关于 使用引用。
Converse Document Chat(原始模式 - 仅文本提取)
Claude PDF Chat(新模式 - 完整视觉理解)
如果客户报告在使用 Converse API 时 Claude 看不到其 PDF 中的图像或图表,他们可能需要启用引用标志。没有它,Converse 会回退到仅基本文本提取。
这是 Converse API 的已知限制。对于需要不带引用的视觉 PDF 分析的应用程序,请考虑改用 InvokeModel API。
对于非 PDF 文件,如 .csv、.xlsx、.docx、.md 或 .txt 文件,请参阅 使用其他文件格式。
让我们从使用 Messages API 的简单示例开始。您可以通过三种方式向 Claude 提供 PDF:
document 内容块中的 base64 编码 PDFfile_id最简单的方法是直接从 URL 引用 PDF:
client = anthropic.Anthropic()
message = client.messages.create(
model="claude-opus-4-7",
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)如果您需要从本地系统发送 PDF 或 URL 不可用时:
import base64
import httpx
# First, load and encode the 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).content).decode("utf-8")
# Alternative: Load from a local file
# with open("document.pdf", "rb") as f:
# pdf_data = base64.standard_b64encode(f.read()).decode("utf-8")
# Send to Claude using base64 encoding
client = anthropic.Anthropic()
message = client.messages.create(
model="claude-opus-4-7",
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()
# Upload the PDF file
with open("document.pdf", "rb") as f:
file_upload = client.beta.files.upload(file=("document.pdf", f, "application/pdf"))
# Use the uploaded file in a message
message = client.beta.messages.create(
model="claude-opus-4-7",
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 的内容。
Claude 可以在响应时引用文本和视觉内容。您可以通过集成 PDF 支持来进一步改进性能:
PDF 文件的令牌计数取决于从文档提取的总文本以及页数:
您可以使用 令牌计数 来估计您特定 PDF 的成本。
遵循这些最佳实践以获得最佳结果:
对于大容量处理,请考虑以下方法:
缓存 PDF 以改进重复查询的性能:
client = anthropic.Anthropic()
message = client.messages.create(
model="claude-opus-4-7",
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": "Analyze this document."},
],
}
],
)使用消息批处理 API 进行大容量工作流:
client = anthropic.Anthropic()
message_batch = client.messages.batches.create(
requests=[
{
"custom_id": "doc1",
"params": {
"model": "claude-opus-4-7",
"max_tokens": 1024,
"messages": [
{
"role": "user",
"content": [
{
"type": "document",
"source": {
"type": "base64",
"media_type": "application/pdf",
"data": pdf_data,
},
},
{"type": "text", "text": "Summarize this document."},
],
}
],
},
}
]
)Was this page helpful?