The Document AI Engineer extracts structured data from unstructured documents - invoices, contracts, forms, reports. The pipeline: extract raw text (PDF parser or OCR), chunk and clean it, then use an LLM with a structured output schema to extract the data you need. Vision-capable LLMs eliminate the separate OCR step for most use cases.
PDF Text Extraction Pipeline
import pdfParse from 'pdf-parse';
import Anthropic from '@anthropic-ai/sdk';
const client = new Anthropic();
// Option A: Text-based PDF (fastest, no vision needed)
async function extractFromTextPdf(buffer: Buffer): Promise<string> {
const data = await pdfParse(buffer);
return data.text;
}
// Option B: Scanned/image PDF - use vision model directly
async function extractFromScannedPdf(base64Image: string) {
const response = await client.messages.create({
model: 'claude-opus-5',
max_tokens: 2048,
messages: [{
role: 'user',
content: [
{
type: 'image',
source: { type: 'base64', media_type: 'image/png', data: base64Image },
},
{
type: 'text',
text: 'Extract all text from this document image. Preserve the structure - headings, tables, line items.',
},
],
}],
});
return response.content[0].type === 'text' ? response.content[0].text : '';
}
Structured Data Extraction with JSON Schema
interface InvoiceData {
vendor: string;
invoiceNumber: string;
date: string;
dueDate?: string;
lineItems: { description: string; quantity: number; unitPrice: number; total: number }[];
subtotal: number;
tax?: number;
total: number;
currency: string;
}
async function extractInvoiceData(rawText: string): Promise<InvoiceData> {
const response = await client.messages.create({
model: 'claude-opus-5',
max_tokens: 2048,
messages: [{
role: 'user',
content: `Extract invoice data from the following text and return as JSON.
Use null for fields not present. Infer the currency symbol if not stated.
Invoice text:
${rawText}
Return JSON matching this schema:
{
"vendor": string,
"invoiceNumber": string,
"date": "YYYY-MM-DD",
"dueDate": "YYYY-MM-DD" | null,
"lineItems": [{ "description": string, "quantity": number, "unitPrice": number, "total": number }],
"subtotal": number,
"tax": number | null,
"total": number,
"currency": "USD" | "EUR" | "GBP" | ...
}`,
}],
});
const text = response.content[0].type === 'text' ? response.content[0].text : '{}';
// Strip markdown code fences if model wraps JSON in them
const clean = text.replace(/^```json\n?/, '').replace(/```$/, '').trim();
return JSON.parse(clean) as InvoiceData;
}
Document Classification
- Classify before extracting - a pipeline optimised for invoices will perform poorly on contracts; route to the right extractor first
- Use a fast, cheap model for classification (Haiku 4.5); save expensive models (Opus 5) for extraction
- For high volumes, fine-tune a small classification model on your document types - much cheaper than LLM calls for a simple routing task
- Validate extracted data against business rules: total = sum of line items ± tax; dates are in the past; amounts are positive numbers
- Return confidence scores and route low-confidence extractions to human review, especially for financial documents where errors have downstream consequences