Claude API PDF Document Extraction Tool Guide
Can Claude's API extract data from PDFs?
Yes. Claude's models accept PDF documents directly in the Messages API as base64-encoded content blocks, which means you can build a document extraction tool without a separate OCR pipeline. Claude reads both the text layer and the visual layout of a PDF — tables, headers, scanned pages, charts — and returns structured output based on your prompt. This makes it a practical replacement for brittle regex-based parsers or template-matched OCR tools when you're dealing with invoices, contracts, resumes, or scanned forms.
The short version: you encode the PDF as base64, attach it as a document content block alongside your instructions, and ask for the exact output format you need (JSON, markdown table, plain text). The model handles layout variance — which is the part that usually breaks traditional extraction scripts — because it's reasoning over the rendered page, not just raw text.
How PDF extraction works in the Claude API
A Claude API request for document extraction has three parts:
- The document block — base64-encoded PDF bytes with a
media_typeofapplication/pdf. - The instruction — a clear description of what to extract and in what shape.
- The output contract — ideally a JSON schema or example so parsing downstream is deterministic.
Here's a minimal example using the native Anthropic Messages format:
const fs = require("fs");
const pdfBase64 = fs.readFileSync("invoice.pdf").toString("base64");
const response = await fetch("https://api.anthropic.com/v1/messages", {
method: "POST",
headers: {
"x-api-key": process.env.ANTHROPIC_API_KEY,
"anthropic-version": "2023-06-01",
"content-type": "application/json",
},
body: JSON.stringify({
model: "claude-3-5-sonnet-20241022",
max_tokens: 1024,
messages: [
{
role: "user",
content: [
{
type: "document",
source: {
type: "base64",
media_type: "application/pdf",
data: pdfBase64,
},
},
{
type: "text",
text: "Extract invoice number, due date, line items, and total as JSON.",
},
],
},
],
}),
});
This pattern works well for one-off scripts. The problems start once you need this in production: handling large PDFs without blowing your context window, streaming long outputs, managing per-app API keys for different services pulling from the same Claude account, and tracking usage so you know which extraction job cost what.
Building a reliable extraction tool, not just a script
A few practical things matter more than the prompt itself once you move from "demo" to "tool":
- Chunk large PDFs. If a document has 80+ pages, split it before sending — Claude handles multi-page PDFs well, but very large files increase latency and cost per call unnecessarily.
- Pin the output format. Ask for strict JSON and validate it server-side. Don't trust free-text extraction to stay parseable across every document variant.
- Handle scanned vs. digital PDFs the same way. Claude's vision capability means scanned image-only PDFs work the same as text-layer PDFs — you don't need a separate OCR fallback branch.
- Stream for long documents. If you're extracting full contract summaries or multi-section reports, streaming avoids timeouts on the client side. See
/docs/streamingfor implementation details if you're routing through SubToAPI. - Separate API keys per integration. If your extraction tool is one of several apps using Claude (a chatbot, an internal dashboard, a batch job), giving each one its own key makes usage and cost per workload visible instead of lumped into one account total.
That last point is where a lot of teams hit friction. Anthropic's raw API gives you one key tied to your account. If you're building a PDF extraction microservice that other internal tools or client apps call into, you often want scoped, revocable keys and usage visibility per application — not per Anthropic account.
Where SubToAPI fits
SubToAPI wraps your existing Claude access in a standard HTTPS API with application-level keys (sub_live_...), so you can issue a separate key per extraction service, rotate or revoke access without touching your main account, and see token usage broken down by key. For a PDF extraction tool specifically, that means your invoice-processing service, your contract-review tool, and your internal document search can each get their own key, their own rate limits, and their own usage dashboard — while all billing stays in one place.
The request shape is the same Messages-style format, so moving an existing extraction script over is mostly a base-URL and header swap:
curl https://api.subtoapi.app/v1/messages \
-H "Authorization: Bearer $SUBTOAPI_KEY" \
-H "content-type: application/json" \
-d '{
"model": "claude-3-5-sonnet-20241022",
"max_tokens": 1024,
"messages": [
{
"role": "user",
"content": [
{ "type": "document", "source": { "type": "base64", "media_type": "application/pdf", "data": "'"$(base64 -i invoice.pdf)"'" } },
{ "type": "text", "text": "Extract vendor name, invoice number, and total due as JSON." }
]
}
]
}'
If you need tool use alongside extraction — for example, calling a function to save parsed invoice data to your database right after extraction — that's documented at /docs/tools. The full request/response reference for document and message handling lives at /docs/messages, and /docs/quickstart walks through generating your first key.
Getting started
If you're prototyping a PDF extraction tool today, start with the raw Anthropic API to validate your prompt and output schema against a handful of real documents. Once you're ready to run it as a production service — especially one other apps or team members will call — set up a dedicated key so usage and costs are tracked separately. Plans start at €9/month for solo use, with team seat pricing at €19 and €49 for larger workloads; see /pricing for the breakdown, or go straight to /signup to generate a key and test it against your own PDFs.
questions
Does Claude's API support scanned (image-only) PDFs, or only text-layer PDFs? Both. Claude processes the PDF visually as well as reading any embedded text layer, so scanned documents and photographed pages work without a separate OCR step.
What's the maximum PDF size I can send to the Claude API? There are practical limits tied to request payload size and model context window. For documents beyond roughly 100 pages, split the PDF into sections and process them separately for better reliability and lower latency.
Can I get structured JSON output instead of free text from PDF extraction? Yes — specify the exact JSON schema or give a concrete example in your prompt, and validate the response server-side. Claude follows explicit format instructions reliably but isn't guaranteed to produce valid JSON on every call, so validation is still necessary.