Claude API Data Extraction from PDF: A Practical Guide
Extracting structured data from PDFs with the Claude API means sending the document (or its text/images) to Claude along with instructions on what fields to pull out, then parsing Claude's response — usually JSON — into your application. Claude handles messy, unstructured PDF layouts like invoices, contracts, and reports far better than regex or traditional OCR pipelines because it understands context, not just text position.
This matters because PDFs are notoriously inconsistent. An invoice from one vendor puts the total at the bottom right; another puts it in a table header. Traditional extraction tools need custom templates per document type. Claude reads the content the way a human would, which means one well-designed prompt can handle dozens of PDF variants without per-template engineering.
How PDF Extraction Works with Claude
There are two practical approaches, depending on how your PDF is structured and what Claude API access you're using.
1. Native PDF support
Claude's models support PDF input directly as a document content block. You base64-encode the PDF and send it in the request, and Claude processes both the text and visual layout (tables, charts, scanned pages) in context.
curl https://api.subtoapi.app/v1/messages \
-H "Authorization: Bearer $SUBTOAPI_KEY" \
-H "content-type: application/json" \
-d '{
"model": "claude-sonnet-4",
"max_tokens": 1024,
"messages": [
{
"role": "user",
"content": [
{
"type": "document",
"source": {
"type": "base64",
"media_type": "application/pdf",
"data": "<BASE64_PDF_DATA>"
}
},
{
"type": "text",
"text": "Extract invoice number, vendor name, total amount, and due date. Return valid JSON only."
}
]
}
]
}'
This is the simplest path and works well for PDFs under typical size limits. If you're already using SubToAPI's wrapper around your Claude access, this exact request format works against /v1/messages — see the quickstart and messages docs for request limits and model options.
2. Pre-extracted text or OCR pipeline
For scanned PDFs with poor text layers, or very large documents, extract text first (with a library like pdf-parse, pdfplumber, or an OCR step), then send the plain text to Claude. This reduces payload size and lets you chunk long documents across multiple requests.
import fs from "fs";
import pdf from "pdf-parse";
const buffer = fs.readFileSync("invoice.pdf");
const { text } = await pdf(buffer);
const response = await fetch("https://api.subtoapi.app/v1/messages", {
method: "POST",
headers: {
"Authorization": `Bearer ${process.env.SUBTOAPI_KEY}`,
"content-type": "application/json"
},
body: JSON.stringify({
model: "claude-sonnet-4",
max_tokens: 1024,
messages: [
{
role: "user",
content: `Extract the following fields as JSON: invoice_number, vendor, total, due_date.\n\nDocument text:\n${text}`
}
]
})
});
const data = await response.json();
console.log(data.content[0].text);
Designing Prompts for Reliable Extraction
The quality of your extracted data depends almost entirely on prompt specificity. A few rules that consistently improve accuracy:
- Name every field explicitly. Don't say "extract the important data" — list each field and its expected type (string, number, date format).
- Specify the output format. Ask for JSON only, and give an example schema in the prompt so Claude mirrors the structure exactly.
- Handle missing fields. Tell Claude what to return when a field isn't present (
null, empty string) so your parser doesn't break on inconsistent output. - Set a strict system instruction. Something like "You are a data extraction engine. Only output valid JSON, no explanation" cuts down on conversational preamble wrapping your JSON.
Example schema instruction:
Return a JSON object with exactly these keys:
{
"invoice_number": string,
"vendor": string,
"total_amount": number,
"due_date": "YYYY-MM-DD" or null
}
No markdown formatting, no commentary.
Using Tool Calls for Structured Output
If you need guaranteed schema compliance rather than hoping Claude follows formatting instructions, use tool/function calling. Define a tool with a strict JSON schema for the extracted fields, and Claude will return a tool call matching that schema instead of free-form text. This is the most reliable method for production pipelines processing many PDF types. See the tool use docs for the request structure, and streaming docs if you're extracting from very long documents and want partial results as they're generated.
Batching and Scaling Extraction
For high-volume use cases — processing hundreds of invoices, contracts, or forms daily — a few practical considerations:
- Rate limits and concurrency. Run extractions in parallel but respect your plan's rate limits. Monitor usage so a burst of uploads doesn't throttle your pipeline.
- Cost control. Large PDFs consume significant tokens, especially multi-page scanned documents. Strip unnecessary pages before sending when you only need specific sections.
- Validation layer. Always validate Claude's JSON output against your schema before writing to a database. Malformed JSON is rare but not impossible, especially with inconsistent source documents.
- Usage visibility. If multiple team members or services are hitting the same extraction pipeline, having per-key usage metadata matters for debugging cost spikes and attributing load. SubToAPI's dashboard tracks this per API key, which is useful once PDF extraction is running across a team rather than a single script.
If you're evaluating providers for this kind of workload, pricing and seat structure matter as volume grows — see pricing for Solo, Team, and Scale tiers, and sign up for a free trial if you want to test extraction against your own PDF samples before committing.
Common Pitfalls
- Sending the entire PDF when you only need one page — this wastes tokens and increases the chance of irrelevant content polluting the output.
- Vague prompts like "summarize this PDF" when you actually need structured fields — be explicit about extraction, not summarization.
- Not handling multi-page tables that span page breaks — mention this explicitly in your prompt if your PDFs have long tables.
- Skipping validation and assuming every response is clean JSON — build a try/catch around your JSON parsing step regardless of how good your prompt is.
questions
Can Claude extract data from scanned (image-based) PDFs? Yes. Claude's document support processes visual content, so scanned pages with tables, stamps, or handwriting are often readable without a separate OCR step, though accuracy depends on scan quality.
What's the best output format for extracted PDF data? JSON, defined explicitly in your prompt or enforced via tool calling. Tool calling gives the strongest schema guarantees for production pipelines.
How do I handle PDFs too large for a single request? Split by page ranges or sections, extract text with a library first, then send manageable chunks to Claude, reassembling results afterward.