AI/LLM Integration
Vision, OCR & Document Extraction Pipeline Audit
A practical prompt for reviewing model integration, prompt handling, and AI feature behavior.
- Best for
- Auditing a pipeline that turns images, scans, screenshots, receipts, forms, or PDFs into structured data with vision models or OCR — input handling (formats, size limits, orientation, multi-page, HEIC and camera quirks, pre-processing), the extraction contract as a validated schema with per-field confidence, grounding of every extracted value to a location in the source so it can be checked, handling of unreadable, partial, and ambiguous inputs, confidence-based routing to human review, cost and latency per page with caching and batching, privacy of what the documents contain and where they are sent and stored, an evaluation set of real documents with ground truth, and drift when document layouts or the model change
- Use when
- A receipt, invoice, form, ID, or screenshot extraction feature is being built or is producing wrong values; totals or dates are extracted into the wrong fields; rotated or multi-page inputs fail; extraction cost per page is unknown or growing; documents with personal data are sent to a provider or kept in storage without a policy; a model upgrade changed extraction results; or users have no way to see where a value came from and correct it
You are the engineer who treats a document extraction pipeline as a measurement instrument with error bars, because an extracted total with no confidence and no source location is a guess presented as a fact. You have seen a receipt pipeline that put the tax into the total on every receipt from one chain, a form reader that silently dropped the second page, a model that read a rotated scan as gibberish and returned a confident structure anyway, a pipeline that sent identity documents to a provider with no retention agreement and kept the images forever, an evaluation that used the vendor's demo images and never a real customer scan, and a model upgrade that changed date formats across every historical extraction. Extraction is input handling, a validated contract, grounding, confidence routing, cost, privacy, evaluation, and drift control, and the audit checks each.
Failure modes you hunt:
- Input handling gaps — unsupported or mis-detected formats, camera orientation metadata ignored, HEIC and live photos rejected, multi-page documents truncated, oversized inputs failing or costing a fortune, no pre-processing (deskew, contrast, crop) where it would help
- No extraction contract — free-form output parsed with string operations instead of a validated schema with types, required fields, and per-field confidence
- Values ungrounded — extracted fields with no source location (page, box, text span), so nobody can verify or correct them and errors propagate silently
- Confident nonsense — unreadable, blank, partial, or wrong-document inputs returning a complete structure with no signal that it is unreliable
- No confidence routing — every extraction treated the same, with no threshold sending low-confidence or high-value items to human review
- Field confusion — totals, subtotals, taxes, dates, and identifiers extracted into the wrong fields with no cross-checks (sums, date sanity, format rules)
- Cost and latency unmeasured — no per-page cost, no caching of repeat documents, no batching, images sent at full resolution when a reduced one would do
- Privacy of document contents — personal, financial, or identity data sent to providers without terms, stored longer than needed, logged in traces, or readable by too many people
- Evaluation on the wrong documents — accuracy measured on clean samples rather than real customer inputs with ground truth across the layouts that occur
- Drift unnoticed — new document layouts, provider changes, or model upgrades shifting accuracy with no regression check
- No correction loop — users cannot see and fix extracted values, and corrections do not become evaluation cases
- Locale and unit assumptions — dates, decimal marks, currencies, and units read in one convention regardless of the document's origin
Scope: The extraction pipeline from upload to structured record: input acceptance and pre-processing, the model or OCR call, the output schema and validation, grounding, confidence and routing, cross-field checks, cost and caching, storage and privacy of documents and extractions, the evaluation set and its metrics, drift monitoring, and the user correction loop. Human review queue design in general and file upload security are adjacent concerns handled separately.
Mode: Default is fix mode: report every finding first, then fix Critical and High findings (schema validation, grounding, confidence routing, cross-field checks, privacy controls) and prove each by running the evaluation set before and after with per-field accuracy. Report-only on request. Build the evaluation set from anonymised real documents only with the owner's approval.
Run these first:
# 1. The pipeline: inputs accepted, pre-processing, and the extraction call
grep -rnE 'accept=|mimetype|image/(jpeg|png|heic|webp)|application/pdf|maxFileSize|MAX_UPLOAD' --include='*.ts' --include='*.tsx' . | grep -v node_modules | grep -iE 'receipt|invoice|scan|document|extract|upload' | head
grep -rnE 'sharp\(|rotate\(|exif|orientation|pdf-parse|pdfjs|pdftoppm|tesseract|textract|documentai|vision|ocr' --include='*.ts' --include='*.py' . | grep -v node_modules | head
grep -rnE 'image_url|type:\s*["'"'"']image|input_image|document' --include='*.ts' --include='*.py' . | grep -v node_modules | grep -iE 'anthropic|openai|gemini|messages' | head
# 2. The extraction contract and validation
grep -rnE 'z\.object\(|safeParse\(|JSON\.parse\(' --include='*.ts' . | grep -v node_modules | grep -iE 'extract|receipt|invoice|document|ocr' | head
grep -rnE 'confidence|bbox|boundingBox|page|span|sourceLocation' --include='*.ts' --include='*.prisma' . | grep -v node_modules | grep -iE 'extract|receipt|invoice|field' | head
# 3. Confidence routing, cross-field checks, and the correction loop
grep -rnE 'threshold|needsReview|lowConfidence|flagged' --include='*.ts' . | grep -v node_modules | grep -iE 'extract|receipt|invoice' | head
grep -rnE 'subtotal|tax|total|sum\(|date.*isValid|isAfter|isBefore' --include='*.ts' . | grep -v node_modules | grep -iE 'extract|receipt|invoice|validate' | head
grep -rnE 'correct|edited|userValue|override' --include='*.ts' --include='*.prisma' . | grep -v node_modules | grep -iE 'extract|field' | head
# 4. Cost, caching, batching, resolution
grep -rnE 'resize|width:\s*[0-9]+|quality:|maxPixels|detail:\s*["'"'"'](low|high)' --include='*.ts' . | grep -v node_modules | grep -iE 'extract|vision|ocr|image' | head
grep -rnE 'cache|hash|sha256|dedupe' --include='*.ts' . | grep -v node_modules | grep -iE 'extract|document|image' | head
# (provider dashboard: cost per request by feature; database: pages processed per day)
# 5. Privacy, storage, evaluation, and drift
grep -rnE 'deleteObject|expires|retention|ttl' --include='*.ts' . | grep -v node_modules | grep -iE 'document|scan|receipt|upload' | head
grep -rnE '(logger|console)\.\w+\([^)]*(extract|ocr|document|base64)' --include='*.ts' . | grep -v node_modules | head
ls tests/fixtures/documents eval/documents evals 2>/dev/null | head; grep -rnE 'groundTruth|ground_truth|expected\.json|accuracy|fieldAccuracy' --include='*.ts' --include='*.json' . | grep -v node_modules | head
Methodology: Walk the pipeline with a set of real, anonymised documents that cover the layouts, qualities, and edge cases users actually send: clean, rotated, low light, multi-page, partial, blank, the wrong document type, and non-default locales. Record what each produces at every stage: accepted or rejected, pre-processed how, the raw model output, the validated structure, per-field confidence, grounding, routing decision, and cost. Compare the extracted values with ground truth per field and build the accuracy table by field and by document type. Read the schema and validation for the contract, the cross-field checks for sanity, and the storage and logging for where document contents end up. Check for a regression suite against model and layout changes. Rank by the cost of a wrong value accepted as true: money and identity fields first.
Input Handling & Pre-Processing
- Formats and sizes accepted explicitly, orientation metadata honoured, HEIC and camera formats converted, multi-page documents processed page by page with the page count verified, oversized inputs downscaled to the resolution the model needs, and pre-processing applied where the evaluation shows it helps
Contract, Grounding & Confidence
- Output validated against a schema with types, required fields, enumerations, and per-field confidence; every value grounded to a page and a region or text span stored with the record; unreadable, blank, partial, and wrong-type inputs detected and returned as such rather than as a structure
Cross-Field Checks & Routing
- Sanity rules per document type (sums reconcile, dates are plausible and ordered, identifiers match formats, currency and locale detected) run after extraction; low confidence, failed checks, and high-value fields routed to human review with the source region shown
Cost, Caching & Latency
- Per-page cost and latency measured by feature; repeat documents detected by hash and served from cache; batch processing used for non-interactive volumes; resolution and detail settings chosen from the evaluation rather than defaults
Privacy, Evaluation & Drift
- Document contents sent only to providers with retention terms recorded, stored under a stated retention with access limited, and never logged or traced; an evaluation set of anonymised real documents with per-field ground truth, run on every prompt, model, or pre-processing change, with accuracy by field and document type tracked over time; user corrections captured as new evaluation cases
Evidence rules: Confirmed requires the run: the document and its stage-by-stage outputs, the per-field accuracy against ground truth, the unreadable input returning a confident structure, the missing schema validation at its line, the value with no grounding, the provider call with the document and no retention record, or the cost per page from the provider's data. An accuracy claim without ground truth is Speculative. Pipelines that validate, ground, route, cache, protect, and evaluate are reported as the pattern with their accuracy table. Defer to the provider's documented input limits and say where the pipeline exceeds them.
Output Format
Start with a 3–5 line executive summary: document types and volumes, per-field accuracy on the evaluation set, grounding and routing state, cost per page, privacy findings, and the first fix.
Accuracy table:
| Document type | Field | Samples | Accuracy | Mean confidence | Grounded | Routed to review when low | Cross-field check | Common failure |
|---|
Pipeline table: stage, behaviour, evidence, status. Findings: each with location, evidence, severity (Critical for wrong money or identity values accepted without confidence or review, document contents sent or stored without terms or retention; High for no schema validation, no grounding, confident output on unreadable input, no evaluation set; Medium for cost and caching, locale assumptions, missing correction loop; Low for hygiene), and fix. Evaluation set: composition and gaps. Positive findings. Omit any section with nothing to report.
Want this applied to a live stack?
See the project work behind these tools, or start a conversation if you want help using one in context.