關於「zero data retention」(零資料保留),即 ZDR 如何適用於此功能,請參閱 API 與資料保留。
您可以向 Claude 詢問您提供的 PDF 中的任何文字、圖片、圖表和表格。一些範例使用案例:
Claude 可以處理任何標準 PDF。請確保您的請求大小符合以下要求:
| 要求 | 限制 |
|---|---|
| 最大請求大小 | 32 MB(因平台而異) |
| 每個請求的最大頁數 | 600(當請求的上下文視窗低於 1M tokens 時為 100) |
| 格式 | 標準 PDF(無密碼/加密) |
這兩個限制都是針對整個請求負載,包括與 PDF 一起發送的任何其他內容。對於大型 PDF,請考慮使用 Files API 上傳並透過 file_id 引用,以保持請求負載較小。
密集的 PDF(許多小字體頁面、複雜表格或大量圖形)可能在達到頁數限制之前就填滿上下文視窗。即使使用 Files API,包含大型 PDF 的請求也可能在達到頁數限制之前失敗。請嘗試將文件分割成多個部分;對於大型檔案,由於每一頁都會被處理為圖像,對嵌入的圖像進行降採樣也會有所幫助。
由於 PDF 支援依賴於 Claude 的視覺能力,因此它與其他視覺任務一樣受到相同的限制和考量。
PDF 支援可在 Claude API、Amazon Bedrock(請參閱 Amazon Bedrock PDF 支援)、Claude Platform on AWS、Google Cloud 和 Microsoft Foundry 上使用。所有現行模型都支援 PDF 處理。
當透過 Converse API(Claude on Amazon Bedrock(Opus 4.6 及更早版本)的一部分)使用 PDF 支援時,有兩種不同的文件處理模式:
重要: 若要在 Converse API 中存取 Claude 完整的視覺 PDF 理解能力,您必須啟用引用(citations)。如果未啟用引用,API 將回退到僅進行基本文字提取。深入了解使用引用。
Converse Document Chat(原始模式 - 僅文字提取)
Claude PDF Chat(新模式 - 完整視覺理解)
如果在使用 Converse API 時 Claude 無法看到您 PDF 中的圖像或圖表,您可能需要啟用引用標誌。如果沒有它,Converse 將回退到僅進行基本文字提取。
這是 Converse API 的已知限制。對於需要在不使用引用的情況下進行視覺 PDF 分析的應用程式,請考慮改用 InvokeModel API。
純文字檔案(如 .txt、.csv 或 .md)可以直接在 document 區塊中使用:使用 MIME 類型 text/plain 將它們上傳到 Files API,並透過 file_id 引用。二進位格式(如 .xlsx 或 .docx)不支援在 document 區塊中使用,必須先轉換為文字或 PDF。請參閱使用其他檔案格式。
從使用 Messages API 的簡單範例開始。您可以透過三種方式向 Claude 提供 PDF:
document 內容區塊中的 base64 編碼 PDFfile_id在 Amazon Bedrock 和 Google Cloud 上,目前僅提供 base64 編碼的來源。在 Microsoft Foundry 上,託管於 Azure 的部署不支援 Files API。
最簡單的方法是直接從 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 中顯示 token 消耗量:
{
"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(beta):
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 檔案的 token 數量取決於從文件中提取的總文字量和頁數:
您可以使用 token 計數來估算您特定 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 文件。
Was this page helpful?