# Claude 长上下文与 PDF:1M 上下文、document 与 file 内容块 Source: https://ofox.io/zh/docs/develop/models/claude/pdf 本页说明 Claude 的上下文长度与 PDF 输入方法。模型 ID 与接口地址见 [Claude 系列概览](https://ofox.io/zh/docs/develop/models/claude)。 ## 1M 上下文 除 Haiku 4.5(200K)外,其余模型的上下文均为 1M token,**无需**传入官方的 1M 上下文 beta 请求头。超出模型上下文时返回 `prompt is too long`,见[常见报错](https://ofox.io/zh/docs/develop/models/claude#errors)。 ## PDF 输入 **Anthropic 原生接口** ```python filename="claude_pdf.py" import base64 import os import anthropic client = anthropic.Anthropic(base_url="https://api.ofox.io/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) ``` **OpenAI 兼容接口** ```python filename="claude_pdf_openai.py" import base64 import os from openai import OpenAI client = OpenAI(base_url="https://api.ofox.io/v1", api_key=os.environ["OFOX_API_KEY"]) pdf_b64 = base64.b64encode(open("invoice.pdf", "rb").read()).decode() resp = client.chat.completions.create( model="anthropic/claude-haiku-4.5", max_tokens=1024, messages=[{"role": "user", "content": [ {"type": "file", "file": {"filename": "invoice.pdf", "file_data": f"data:application/pdf;base64,{pdf_b64}"}}, {"type": "text", "text": "这张发票的总金额是多少?"}, ]}], ) print(resp.choices[0].message.content) ``` PDF 的每一页按文本与页面图像计入输入 token,页数较多时请留意上下文长度与费用。 其他报错见[常见报错](https://ofox.io/zh/docs/develop/models/claude#errors)。