Claude 长上下文与 PDF
本页说明 Claude 的上下文长度与 PDF 输入方法。模型 ID 与接口地址见 Claude 系列概览。
1M 上下文
除 Haiku 4.5(200K)外,其余模型的上下文均为 1M token,无需传入官方的 1M 上下文 beta 请求头。超出模型上下文时返回 prompt is too long,见常见报错。
PDF 输入
Anthropic 原生接口
claude_pdf.py
import base64
import os
import anthropic
client = anthropic.Anthropic(base_url="https://api.ofox.ai/anthropic", api_key=os.environ["OFOX_API_KEY"])
pdf_b64 = base64.b64encode(open("invoice.pdf", "rb").read()).decode()
message = client.messages.create(
model="anthropic/claude-haiku-4.5",
max_tokens=1024,
messages=[{"role": "user", "content": [
{"type": "document", "source": {"type": "base64", "media_type": "application/pdf", "data": pdf_b64}},
{"type": "text", "text": "这张发票的总金额是多少?"},
]}],
)
print(message.content[0].text)PDF 的每一页按文本与页面图像计入输入 token,页数较多时请留意上下文长度与费用。
其他报错见常见报错。
最后更新于