您现在可以向 Claude 询问您提供的 PDF 中的任何文本、图片、图表和表格。一些示例用例:

  • 分析财务报告并理解图表/表格
  • 从法律文档中提取关键信息
  • 文档翻译协助
  • 将文档信息转换为结构化格式

开始之前

检查 PDF 要求

Claude 可以处理任何标准 PDF。但是,在使用 PDF 支持时,您应确保您的请求大小满足以下要求:

要求限制
最大请求大小32MB
每个请求的最大页数100
格式标准 PDF(无密码/加密)

请注意,这两个限制都适用于整个请求负载,包括与 PDF 一起发送的任何其他内容。

由于 PDF 支持依赖于 Claude 的视觉能力,它受到与其他视觉任务相同的限制和注意事项的约束。

支持的平台和模型

PDF 支持目前通过直接 API 访问和 Google Vertex AI 支持:

  • Claude Opus 4 (claude-opus-4-20250514)
  • Claude Sonnet 4 (claude-sonnet-4-20250514)
  • Claude Sonnet 3.7 (claude-3-7-sonnet-20250219)
  • Claude Sonnet 3.5 模型 (claude-3-5-sonnet-20241022, claude-3-5-sonnet-20240620)
  • Claude Haiku 3.5 (claude-3-5-haiku-20241022)

此功能将很快在 Amazon Bedrock 上得到支持。

对于非 PDF 文件,如 .csv、.xlsx、.docx、.md 或 .txt 文件,请参阅使用其他文件格式


使用 Claude 处理 PDF

发送您的第一个 PDF 请求

让我们从使用 Messages API 的简单示例开始。您可以通过三种方式向 Claude 提供 PDF:

  1. 作为对在线托管的 PDF 的 URL 引用
  2. 作为 document 内容块中的 base64 编码 PDF
  3. 通过来自Files APIfile_id

选项 1:基于 URL 的 PDF 文档

最简单的方法是直接从 URL 引用 PDF:

 curl https://api.anthropic.com/v1/messages \
   -H "content-type: application/json" \
   -H "x-api-key: $ANTHROPIC_API_KEY" \
   -H "anthropic-version: 2023-06-01" \
   -d '{
     "model": "claude-opus-4-20250514",
     "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?"
         }]
     }]
 }'

选项 2:Base64 编码的 PDF 文档

如果您需要从本地系统发送 PDF 或当 URL 不可用时:

# Method 1: Fetch and encode a remote PDF
curl -s "https://assets.anthropic.com/m/1cd9d098ac3e6467/original/Claude-3-Model-Card-October-Addendum.pdf" | base64 | tr -d '\n' > pdf_base64.txt

# Method 2: Encode a local PDF file
# base64 document.pdf | tr -d '\n' > pdf_base64.txt

# Create a JSON request file using the pdf_base64.txt content
jq -n --rawfile PDF_BASE64 pdf_base64.txt '{
    "model": "claude-opus-4-20250514",
    "max_tokens": 1024,
    "messages": [{
        "role": "user",
        "content": [{
            "type": "document",
            "source": {
                "type": "base64",
                "media_type": "application/pdf",
                "data": $PDF_BASE64
            }
        },
        {
            "type": "text",
            "text": "What are the key findings in this document?"
        }]
    }]
}' > request.json

# Send the API request using the JSON file
curl https://api.anthropic.com/v1/messages \
  -H "content-type: application/json" \
  -H "x-api-key: $ANTHROPIC_API_KEY" \
  -H "anthropic-version: 2023-06-01" \
  -d @request.json

选项 3:Files API

对于您将重复使用的 PDF,或当您想要避免编码开销时,请使用Files API

# First, upload your PDF to the Files API
curl -X POST https://api.anthropic.com/v1/files \
  -H "x-api-key: $ANTHROPIC_API_KEY" \
  -H "anthropic-version: 2023-06-01" \
  -H "anthropic-beta: files-api-2025-04-14" \
  -F "file=@document.pdf"

# Then use the returned file_id in your message
curl https://api.anthropic.com/v1/messages \
  -H "content-type: application/json" \
  -H "x-api-key: $ANTHROPIC_API_KEY" \
  -H "anthropic-version: 2023-06-01" \
  -H "anthropic-beta: files-api-2025-04-14" \
  -d '{
    "model": "claude-opus-4-20250514", 
    "max_tokens": 1024,
    "messages": [{
      "role": "user",
      "content": [{
        "type": "document",
        "source": {
          "type": "file",
          "file_id": "file_abc123"
        }
      },
      {
        "type": "text",
        "text": "What are the key findings in this document?"
      }]
    }]
  }'

PDF 支持的工作原理

当您向 Claude 发送 PDF 时,会发生以下步骤:

1

系统提取文档的内容。

  • 系统将文档的每一页转换为图像。
  • 从每一页提取文本,并与每一页的图像一起提供。
2

Claude 分析文本和图像以更好地理解文档。

  • 文档以文本和图像的组合形式提供以供分析。
  • 这允许用户询问 PDF 视觉元素的见解,如图表、图解和其他非文本内容。
3

Claude 响应,如果相关则引用 PDF 的内容。

Claude 在响应时可以引用文本和视觉内容。您可以通过将 PDF 支持与以下功能集成来进一步提高性能:

  • 提示缓存:提高重复分析的性能。
  • 批处理:用于大量文档处理。
  • 工具使用:从文档中提取特定信息以用作工具输入。

估算您的成本

PDF 文件的令牌计数取决于从文档中提取的总文本以及页数:

  • 文本令牌成本:每页通常使用 1,500-3,000 个令牌,具体取决于内容密度。适用标准 API 定价,无额外 PDF 费用。
  • 图像令牌成本:由于每页都转换为图像,因此适用相同的基于图像的成本计算

您可以使用令牌计数来估算特定 PDF 的成本。


优化 PDF 处理

提高性能

遵循这些最佳实践以获得最佳结果:

  • 在请求中将 PDF 放在文本之前
  • 使用标准字体
  • 确保文本清晰易读
  • 将页面旋转到正确的直立方向
  • 在提示中使用逻辑页码(来自 PDF 查看器)
  • 在需要时将大型 PDF 分割成块
  • 启用提示缓存以进行重复分析

扩展您的实现

对于大量处理,请考虑这些方法:

使用提示缓存

缓存 PDF 以提高重复查询的性能:

# Create a JSON request file using the pdf_base64.txt content
jq -n --rawfile PDF_BASE64 pdf_base64.txt '{
    "model": "claude-opus-4-20250514",
    "max_tokens": 1024,
    "messages": [{
        "role": "user",
        "content": [{
            "type": "document",
            "source": {
                "type": "base64",
                "media_type": "application/pdf",
                "data": $PDF_BASE64
            },
            "cache_control": {
              "type": "ephemeral"
            }
        },
        {
            "type": "text",
            "text": "Which model has the highest human preference win rates across each use-case?"
        }]
    }]
}' > request.json

# Then make the API call using the JSON file
curl https://api.anthropic.com/v1/messages \
  -H "content-type: application/json" \
  -H "x-api-key: $ANTHROPIC_API_KEY" \
  -H "anthropic-version: 2023-06-01" \
  -d @request.json

处理文档批次

使用 Message Batches API 进行大量工作流程:

# Create a JSON request file using the pdf_base64.txt content
jq -n --rawfile PDF_BASE64 pdf_base64.txt '
{
  "requests": [
      {
          "custom_id": "my-first-request",
          "params": {
              "model": "claude-opus-4-20250514",
              "max_tokens": 1024,
              "messages": [
                {
                    "role": "user",
                    "content": [
                        {
                            "type": "document",
                            "source": {
 "type": "base64",
 "media_type": "application/pdf",
 "data": $PDF_BASE64
                            }
                        },
                        {
                            "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-4-20250514",
              "max_tokens": 1024,
              "messages": [
                {
                    "role": "user",
                    "content": [
                        {
                            "type": "document",
                            "source": {
 "type": "base64",
 "media_type": "application/pdf",
 "data": $PDF_BASE64
                            }
                        },
                        {
                            "type": "text",
                            "text": "Extract 5 key insights from this document."
                        }
                    ]
                }
              ]
          }
      }
  ]
}
' > request.json

# Then make the API call using the JSON file
curl https://api.anthropic.com/v1/messages/batches \
  -H "content-type: application/json" \
  -H "x-api-key: $ANTHROPIC_API_KEY" \
  -H "anthropic-version: 2023-06-01" \
  -d @request.json

下一步