Claude Platform Docs
Messages處理檔案

PDF 支援

使用 Claude 處理 PDF:從您的文件中擷取文字、分析圖表並理解視覺內容。

您可以向 Claude 詢問您所提供的 PDF 中的任何文字、圖片、圖表和表格。一些範例使用情境:

  • 分析財務報告並理解圖表/表格
  • 從法律文件中擷取關鍵資訊
  • 協助文件翻譯
  • 將文件資訊轉換為結構化格式

開始之前

檢查 PDF 需求

Claude 可處理任何標準 PDF。請確保您的請求大小符合以下需求:

需求限制
最大請求大小第一方 Claude API 上為 128 MB(因平台而異;請參閱請求大小限制)
每個請求的最大頁數600(當請求的上下文視窗小於 1M tokens 時為 100)
格式標準 PDF(無密碼/加密)

這兩項限制皆適用於整個請求負載,包括與 PDF 一同傳送的任何其他內容。對於大型 PDF,請考慮使用 Files API 上傳並透過 file_id 引用,以保持請求負載較小。

由於 PDF 支援仰賴 Claude 的視覺能力,因此它受到與其他視覺任務相同的限制與注意事項約束。

支援的平台與模型

所有現行模型皆支援 PDF 處理。

使用 Claude 處理 PDF

傳送您的第一個 PDF 請求

從使用 Messages API 的範例開始。若要將 PDF 提供給 Claude,請在 document 內容區塊可檢視預覽,內含 API 參考文件連結——按 Tab 鍵直到焦點進入卡片,按 Escape 鍵返回。中傳送,並使用三種來源類型之一。您的平台決定了您可以使用哪些類型(請參閱可用性)。在這些類型中,請依據 PDF 的位置、大小以及使用頻率來選擇:

  1. 線上託管的 PDF:透過其 URL 引用(選項 1:基於 URL 的 PDF 文件)。
  2. 位於您本機系統上或沒有 URL 的 PDF:以 base64 編碼資料的形式傳送(選項 2:Base64 編碼的 PDF 文件)。
  3. 大型 PDF 或您會重複使用的 PDF:透過 Files API 上傳,並以其 file_id 引用(選項 3:Files API)。

選項 1:基於 URL 的 PDF 文件

透過其 URL 引用 PDF:

client = anthropic.Anthropic()
message = client.messages.create(
    model="claude-opus-5-5",
    max_tokens=1024,
    messages=[
        {
            "role": "user",
            "content": [
                {
                    "type": "document",
                    "source": {
                        "type": "url",
                        "url": "https://code.saschaberens.de/_h/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 消耗量:

Output
{
  "id": "msg_01Hfp8YuFjQ55VgWbpdHDehB",
  "type": "message",
  "role": "assistant",
  "model": "claude-opus-5-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
  }
}

選項 2:Base64 編碼的 PDF 文件

在請求本身中以 base64 編碼資料的形式傳送 PDF。以下大多數範例會下載範例 PDF 並將其編碼,因此不需要本機檔案。若要在這些範例中傳送本機檔案,請將下載並編碼 PDF 的程式碼行替換為被註解掉的程式碼行:

import base64
import httpx2

# 首先,載入 PDF 並進行編碼
pdf_url = "https://code.saschaberens.de/_h/assets.anthropic.com/m/1cd9d098ac3e6467/original/Claude-3-Model-Card-October-Addendum.pdf"
pdf_data = base64.standard_b64encode(
    httpx2.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-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)

選項 3:Files API

對於您會重複使用的 PDF,或當您想避免編碼開銷時,請使用 Files API:

client = anthropic.Anthropic()

# 上傳 PDF 檔案
with open("/path/to/document.pdf", "rb") as f:
    file_upload = client.files.upload(file=("document.pdf", f, "application/pdf"))

# 在訊息中使用已上傳的檔案
message = client.messages.create(
    model="claude-opus-5-5",
    max_tokens=1024,
    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)

PDF 支援的運作方式

當您將 PDF 傳送給 Claude 時,會發生以下步驟:

  1. 系統擷取文件的內容。

    • 系統將文件的每一頁轉換為影像。
    • 系統會擷取每一頁的文字,並與該頁的影像一同提供。
  2. Claude 同時分析文字和圖片。

    • Claude 以文字與影像組合的形式接收每份文件。
    • 由於 Claude 會接收每一頁的影像,您可以針對 PDF 的視覺元素(例如圖表、示意圖及其他非文字內容)尋求見解。
  3. Claude 在相關時於回應中運用 PDF 的內容。

    Claude 在回應中可以同時運用文字與視覺內容。

估算您的成本

PDF 檔案的 token 數量取決於從文件中擷取的文字總量以及頁數:

  • 文字 token 成本:視內容密度而定,每頁通常使用 1,500–3,000 個 token。適用標準 API 定價,無額外的 PDF 費用。
  • 圖片 token 成本:由於 API 會將頁面圖片調整至符合標準解析度層級的限制,因此在每個模型上,每頁的圖片最多使用 1,568 個視覺 token。關於如何計算圖片的視覺 token,請參閱解析度與 token 費用。

您可以使用 token 計數來估算您特定 PDF 的成本。

最佳化 PDF 處理

您可以將 PDF 支援與其他功能結合使用:

提升效能

請遵循以下最佳實務以獲得最佳結果:

  • 在請求中將 PDF 放在文字之前
  • 使用標準字型
  • 確保文字清晰易讀
  • 將頁面旋轉至正確的直立方向
  • 在提示中使用邏輯頁碼(來自 PDF 檢視器)
  • 必要時將大型 PDF 分割成多個區塊
  • 針對重複分析啟用提示快取

擴展您的實作

對於大量處理,請考慮以下方法:

使用提示快取

使用「prompt caching」(提示快取)快取 PDF,以提升重複查詢的效能:

import base64
import httpx2

# 首先,載入 PDF 並進行編碼
pdf_url = "https://code.saschaberens.de/_h/assets.anthropic.com/m/1cd9d098ac3e6467/original/Claude-3-Model-Card-October-Addendum.pdf"
pdf_data = base64.standard_b64encode(
    httpx2.get(pdf_url, follow_redirects=True).content
).decode("utf-8")

# 使用已快取的文件建立訊息
client = anthropic.Anthropic()
message = 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_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 httpx2

# 首先,載入 PDF 並進行編碼
pdf_url = "https://code.saschaberens.de/_h/assets.anthropic.com/m/1cd9d098ac3e6467/original/Claude-3-Model-Card-October-Addendum.pdf"
pdf_data = base64.standard_b64encode(
    httpx2.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-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-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)

批次以非同步方式處理。若要檢查進度並在處理結束後擷取結果,請參閱批次處理。

後續步驟

了解如何計算圖片的視覺 token,以及 PDF 與其他視覺任務共有哪些限制。

在 Claude Cookbook 食譜中探索 PDF 處理的實用範例。

查看 PDF 支援的完整 API 文件。

可用性

PDF 支援
API
  • Messages API
  • Managed Agents
模型
  • 所有模型

適用端點

  • /v1/messages
零資料保留符合資格

不含 涵蓋的模型

HIPAA符合資格

HIPAA 適用資格僅適用於透過 Messages API 內嵌傳送的 PDF,不適用於透過 Files API 傳送的 PDF。

這個頁面有幫助嗎?