Images and PDFs

Claude API · Practical · 12 min

Many real tasks start from a screenshot, an invoice or a report rather than plain text.

Claude can look at pictures and read documents. You attach them as content blocks inside a user message, next to a text block with your question.

An image block has type: "image" and a source. A document block for PDFs has type: "document". The source can be:

  • base64: the file's bytes turned into text with Base64 encoding, plus a media_type such as image/png or application/pdf.
  • url: a public link the API downloads for you.
  • file: an id from the Files API (next lesson), so you upload once and reuse.

Supported image formats are JPEG, PNG, GIF (first frame only) and WebP. Put images before the question; Claude does best with that order. Crop or resize big images yourself, because oversized ones are scaled down and small text may become unreadable.

For PDFs Claude gets both the extracted text and a picture of each page, so it can read tables and charts. That makes PDFs token-hungry: roughly 1,500 to 3,000 tokens of text per page plus the page image. Requests are limited in size (32 MB for a normal request) and number of pages, so split very large documents.

Know the limits: Claude will not identify people by name, counts of many small objects are approximate, and it can misread blurry or tiny images. Check important numbers.

Example

import base64
import anthropic

client = anthropic.Anthropic()
with open("invoice.pdf", "rb") as f:
    pdf_b64 = base64.standard_b64encode(f.read()).decode("utf-8")

msg = client.messages.create(
    model="claude-opus-5-5",
    max_tokens=500,
    messages=[{"role": "user", "content": [
        {"type": "document",
         "source": {"type": "base64", "media_type": "application/pdf", "data": pdf_b64}},
        {"type": "image",
         "source": {"type": "url", "url": "https://example.com/receipt.jpg"}},
        {"type": "text", "text": "Do the invoice and receipt totals match?"},
    ]}],
)
print(msg.content[0].text)

Remember

  • Images use image blocks, PDFs use document blocks
  • Sources: base64, url, or a Files API file id
  • Put media before the question; PDFs cost text plus image tokens per page

Watch out for

  • Sending huge photos: they are downscaled anyway and cost more; resize first.

Check yourself

  1. Why does a 50-page PDF use many tokens?

Sign in to answer, see the explanations, and get this lesson back as a quick review later.

Official docs: platform.claude.com/docs/en/build-with-claude/pdf-support