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,頁數較多時請留意上下文長度與費用。
其他錯誤請見常見錯誤。
最後更新於