# Invoice Data Extraction Agent

> An invoice-extraction agent that parses invoices into validated JSON with per-field confidence, validates totals, and flags illegible or mismatched fields for review.

- **Category:** Document Processing
- **Difficulty:** Intermediate
- **Setup time:** 40 min
- **Stacks:** Claude, LangGraph, OpenAI
- **Version:** 2.0.0 (updated 2026-06-21)
- **Source:** https://agent-kits.com/kit/invoice-extractor

## Overview

Extracts invoices into validated JSON: vendor, number, date, line items, tax, and totals.

Attaches a confidence to every field and validates that line items reconcile to the stated total.

Flags illegible fields and math mismatches for human review instead of guessing or silently correcting.

Defensive: never fabricates a missing value, never auto-fixes a discrepancy, and keeps financial data in scope.

## When to use it

- You process many invoices and want them turned into structured data automatically.
- You need per-field confidence and validation, not just raw OCR text.
- You want low-confidence or inconsistent documents routed to a human rather than passed through.
- You're feeding an AP, ERP, or bookkeeping system that needs clean, validated fields.

## When not to use it

- You want a value filled in for every field regardless of legibility — it won't guess.
- You expect it to silently reconcile totals that don't add up.
- You can't route low-confidence extractions to human review.
- You can't handle invoice data with appropriate confidentiality.

## Architecture

- **Document intake** — Receives the invoice (PDF, image, or scan) under confidential handling and prepares it for extraction.
- **OCR / parse layer** — Extracts text and layout from digital or scanned documents, preserving positions that help locate fields.
- **Field extraction** — Identifies header fields, line items, and totals, capturing the printed values without inventing absent ones.
- **Confidence scoring** — Assigns a confidence to each field from legibility and clarity, so weak extractions are visible rather than hidden.
- **Validation engine** — Checks that line items, tax, and totals reconcile and records any discrepancy instead of adjusting numbers.
- **Review gate** — A deterministic gate routes documents with low confidence, illegible key fields, or math mismatches to human review.
- **Structured output** — Emits validated JSON with per-field confidence and review flags for downstream AP/ERP systems.

## Files in this bundle

- `system-prompt.md` — the agent's system prompt
- `tools.json` — tool/function definitions the agent expects
- `workflow.md` — step-by-step run loop
- `.env.example` — environment variables to fill in
- `kit.json` — full structured metadata
- `starters/` — minimal starter snippets
- `LICENSE` — Apache-2.0 (code, configuration, and schema)
- `LICENSE-CONTENT` — CC BY 4.0 (specification text and documentation)
- `NOTICE` — attribution and trademark notice
- `run.py` — **runnable demo**: real agent loop with mock tools (`pip install anthropic`, set `ANTHROPIC_API_KEY`, `python run.py`)
- `agentaz.json` / `agentaz.schema.json` — AgentAz™ governance spec + schema

## Implementation notes

- Never emit a fabricated value: a missing or illegible field must be null with low confidence and a flag, because a guessed invoice number or amount corrupts everything downstream.
- Report printed values exactly and never silently reconcile mismatched totals; a discrepancy is a signal (missing line, OCR error, vendor mistake), not something to paper over.
- Attach per-field confidence and route low-confidence or illegible-key-field documents to human review rather than passing them through as clean.
- Validate the arithmetic (line items → subtotal → total) and surface the exact issue, which catches both extraction errors and genuine invoice problems.
- Normalize format (dates, number formatting, currency) without altering the underlying value.
- Backtest against labeled invoices with field accuracy and a hard-zero fabricated-value metric before automating downstream posting.
- A cheaper OCR/parse pass handles clean digital invoices, so the strong model is reserved for messy scans and validation reasoning.

## Variations

- **Basic — Extract to JSON:** Extracts invoice fields and line items into structured JSON with per-field confidence. Single document, on demand.
- **Advanced — Validated extraction:** Adds total/tax validation, no-guess null handling, confidence thresholds, and automatic routing of low-confidence or mismatched documents to review.
- **Enterprise — Document pipeline at scale:** Adds batch processing, multi-format and multi-language support, ERP/AP integration, human-in-the-loop review queues, and accuracy monitoring over time.

## FAQ

**Does it guess values it can't read?**

No — that's its core constraint. A missing, illegible, or ambiguous field is returned as null with low confidence and flagged for review, never filled with a plausible-looking guess that would corrupt downstream processing.

**What happens when the totals don't add up?**

It reports the printed values exactly and records the specific discrepancy (for example, line items summing to less than the subtotal), then routes the document to review. It never silently changes a number to make the math balance.

**How do I know which fields to trust?**

Every field carries a confidence score based on legibility and clarity, and the document gets an overall confidence with explicit review reasons, so weak extractions are visible rather than hidden.

**What about low-quality scans?**

It extracts what's clearly legible at appropriate confidence and flags the rest for review, rather than forcing a full extraction from an unreadable image.

**Can it feed our ERP/AP system automatically?**

Yes for high-confidence, validated invoices. Low-confidence or mismatched documents are routed to a review queue first, so only clean, validated data flows through automatically.

**Is invoice data handled securely?**

Yes. It treats invoice contents as confidential financial data and keeps them in scope throughout extraction and output.


---

© 2026 AgentKits · www.agent-kits.com

**Open source.** Code, configuration, and the AgentAz™ schema are licensed under
**Apache-2.0**; specification text and documentation under **CC BY 4.0** (with
attribution to AgentKits, link to https://www.agent-kits.com). See LICENSE, LICENSE-CONTENT,
and NOTICE in this bundle. "AgentAz" and "AgentKits" are trademarks of AgentKits.

**Flagship reference blueprint.** View the source, AgentAz™ spec, and frozen v1.0 schema
in the public registry: https://github.com/agent-kits/agentaz
Original: https://agent-kits.com/kit/invoice-extractor
