~/academy $ claude learn files-and-pdfs
PDFs, the Files API and citations
How to send PDFs and documents to the Claude API, upload once with the Files API, and turn on citations so answers point to the exact source passage. 8 min
$ Read
Three ways to send a PDF
A PDF goes in a document content block, with its source as a public url, base64 data, or a file_id from the Files API. Place the document before your question in the message: Claude does better that way.
Each page is sent as extracted text and as an image, so Claude can read charts, tables and diagrams as well as the words. Budget for it: a page typically uses 1,500 to 3,000 text tokens plus up to 1,568 image tokens, billed at normal input prices.
Limits, checked 18 November 2026: a request can be up to 128 MB on the first-party API and include up to 600 pages (100 when the context window is under 1M tokens). The PDF must be standard, with no password or encryption. Dense files can fill the context window before the page limit, so split very large documents.
import base64
import anthropic
client = anthropic.Anthropic()
with open("supplier-contract.pdf", "rb") as f:
pdf_data = base64.standard_b64encode(f.read()).decode("utf-8")
message = client.messages.create(
model="claude-opus-5-5",
max_tokens=2048,
messages=[{
"role": "user",
"content": [
{"type": "document", "source": {"type": "base64", "media_type": "application/pdf", "data": pdf_data}},
{"type": "text", "text": "List the notice period, renewal date and any penalty clauses."},
],
}],
)Lesson notes: PDFs, the Files API and citations
Everything in this lesson as text, for revision or to read without the interactive steps. By the end you will be able to: You ask Claude questions about your own PDFs and show users exactly which page each answer came from.
Step 1 of 6 · Read
Three ways to send a PDF
A PDF goes in a
documentcontent block, with its source as a publicurl,base64data, or afile_idfrom the Files API. Place the document before your question in the message: Claude does better that way.Each page is sent as extracted text and as an image, so Claude can read charts, tables and diagrams as well as the words. Budget for it: a page typically uses 1,500 to 3,000 text tokens plus up to 1,568 image tokens, billed at normal input prices.
Limits, checked 18 November 2026: a request can be up to 128 MB on the first-party API and include up to 600 pages (100 when the context window is under 1M tokens). The PDF must be standard, with no password or encryption. Dense files can fill the context window before the page limit, so split very large documents.
A local PDF as base64 import base64 import anthropic client = anthropic.Anthropic() with open("supplier-contract.pdf", "rb") as f: pdf_data = base64.standard_b64encode(f.read()).decode("utf-8") message = client.messages.create( model="claude-opus-5-5", max_tokens=2048, messages=[{ "role": "user", "content": [ {"type": "document", "source": {"type": "base64", "media_type": "application/pdf", "data": pdf_data}}, {"type": "text", "text": "List the notice period, renewal date and any penalty clauses."}, ], }], )Step 2 of 6 · Read
Upload once with the Files API
If you ask about the same file many times, upload it once and refer to it by
file_id. The Files API is out of beta, so it needs no beta header, and uploading, listing and deleting are free. You pay only for the content as input tokens when you use it. Limits: 500 MB per file and 1 TB per organisation.Files belong to a workspace, not a user. Any key in that workspace can read any file in it. Never accept a
file_idfrom an end user: keep the mapping between your users and their files in your own database. For a product with many customers, the docs suggest a separate workspace per customer.PDFs and plain text use
documentblocks, images useimageblocks. Word and Excel files are not supported directly: convert them to text, or Word to PDF if it contains images. Files you upload cannot be downloaded again; delete them withclient.files.delete(file_id)when you are done.Upload and reuse with open("staff-handbook.pdf", "rb") as f: uploaded = client.files.upload(file=("staff-handbook.pdf", f, "application/pdf")) response = client.messages.create( model="claude-opus-5-5", max_tokens=1024, messages=[{ "role": "user", "content": [ {"type": "document", "source": {"type": "file", "file_id": uploaded.id}}, {"type": "text", "text": "What is the policy on working from abroad?"}, ], }], )Step 3 of 6 · Read
Citations: show where the answer came from
Add
"citations": {"enabled": True}to each document block. It is all or nothing: one request cannot mix documents with and without citations. The answer then comes back as severaltextblocks, and the ones that rely on your documents carry acitationslist with the quotedcited_textand its location.For PDFs the location is a page range (numbered from 1). For plain text it is a character range (counted from 0). The
cited_textdoes not count towards your output tokens, so this is cheaper and more reliable than asking for quotes in the prompt. Citations cannot be combined with structured outputs.Turn citations on content = [ { "type": "document", "source": {"type": "file", "file_id": uploaded.id}, "title": "Staff handbook 2026", "citations": {"enabled": True}, }, {"type": "text", "text": "How much notice do I need to give for annual leave?"}, ] response = client.messages.create(model="claude-opus-5-5", max_tokens=1024, messages=[{"role": "user", "content": content}]) for block in response.content: if block.type == "text": print(block.text) for c in block.citations or []: print(" source:", c.cited_text)Step 4 of 6 · Quiz
Check your understanding
In your multi-customer app, the browser sends a `file_id` with each question so your server knows which uploaded contract to use. What is the problem?
- A user could send someone else's file ID and read their document, because files are shared across the whole workspace.
- File IDs expire after five minutes.
- The Files API needs a beta header.
Show the answer
A user could send someone else's file ID and read their document, because files are shared across the whole workspace. Never accept file IDs from end users. Look up the right file on your server from the signed-in user, and consider one workspace per customer.
Step 5 of 6 · Sort
Which content block?
Decide how each file goes into a Messages request.
- A signed PDF contract
- A JPEG photo of a receipt
- Plain-text meeting notes
- A PNG screenshot of a dashboard
- A .txt export from your CRM
- A WebP product photo
Show the answers
- A signed PDF contract: document block. PDFs use document blocks.
- A JPEG photo of a receipt: image block. JPEG, PNG, GIF and WebP use image blocks.
- Plain-text meeting notes: document block. Plain text is a document block, and supports citations.
- A PNG screenshot of a dashboard: image block. Screenshots are images.
- A .txt export from your CRM: document block. Upload with a text/plain type and use a document block.
- A WebP product photo: image block. WebP is a supported image format.
Step 6 of 6 · Quiz
Check your understanding
Roughly how many input tokens might a dense 40-page PDF report use?
- About 4,000
- Nothing, PDFs are free
- About 2 million
- Somewhere between about 60,000 and 185,000
Show the answer
Somewhere between about 60,000 and 185,000 40 pages at 1,500 to 3,000 text tokens is 60,000 to 120,000, plus up to 1,568 image tokens a page (about 63,000). Count it with the token counting endpoint to be sure.