KEY TAKEAWAY

Building a production-grade 3-way invoice matching pipeline requires separating deterministic data validation from non-deterministic exception resolution. By combining Temporal for durable workflow orchestration, DuckDB for fast tabular reconciliation, and Claude Sonnet agents for unstructured line-item matching, enterprise finance teams can automate over 85% of accounts payable discrepancies safely without human intervention.

Invoice IngestionDuckDB Pre-CheckClaude AgentERP Voucher Post

Architecture flow: Raw invoice items pass through DuckDB in-memory reconciliation, with unresolved anomalies conditionally routed to Claude AI Agents before posting to the core ERP system.

88%
Automated exception resolution rate
<1.2s
DuckDB query execution latency
14 hrs
Weekly finance team effort saved

The Challenge: High-Volume Invoice Discrepancies in Legacy Enterprise ERPs

In mid-to-large enterprises processing tens of thousands of vendor invoices every month, 3-way matching—reconciling Purchase Orders (POs), Goods Receipt Notes (GRNs), and Vendor Invoices—remains one of the most labor-intensive operations in finance. Traditional rules engines fail whenever vendor line items do not match internal stock keeping unit (SKU) nomenclature, or when line items are bundled across multiple delivery dockets.

Why Traditional Rules Engines and Pure LLM Chains Fail

Two common anti-patterns emerge when teams attempt to modernize accounts payable (AP) workflows. First, relying solely on rigid SQL JOIN statements results in hundreds of minor price or description discrepancies flagged for manual human review every day. A minor text variant such as 'M8 Stainless Bolt (Pack of 100)' versus internal SKU 'BOLT-M8-SS-100PK' breaks deterministic string matching instantly.

Second, passing raw invoice documents directly to an LLM agent without pre-validation creates expensive, non-deterministic architectures. Large language models often struggle with exact multi-line arithmetic calculations, leading to tax computation hallucinations and unacceptably high API latency under high concurrency.

Architecting the Hybrid Pipeline: Temporal + DuckDB + Claude

To solve this, we decouple execution into three distinct layers: durable workflow orchestration via Temporal, in-memory analytical reconciliation via DuckDB, and targeted semantic reconciliation via Claude 3.5 Sonnet agents.

Layer 1: Deterministic In-Memory Reconciliation with DuckDB

Invoices ingested via Webhooks or SFTP are normalized into structured schemas. Using PyArrow and DuckDB, we perform immediate schema validation and execute analytical joins against open purchase orders. DuckDB allows us to run window functions, unit price variance checks, and Levenshtein string distances across 100,000 active PO lines in under 50 milliseconds directly in worker memory.

Layer 2: State-Durable Workflow Orchestration

We wrap the reconciliation lifecycle inside a Temporal workflow. Temporal guarantees state persistence across network retries, vendor API rate limits, and multi-day human approval delays. By using Temporal's durable execution engine, every step—from ledger verification to ERP webhook callback—is guaranteed to execute exactly once, even if underlying Kubernetes worker nodes restart mid-transaction.

Layer 3: Agentic Exception Resolution

When DuckDB flags a line-item discrepancy that falls outside deterministic fuzzy thresholds, the workflow routes the unmapped payload to a specialized Claude 3.5 Sonnet activity. The agent is constrained by strict JSON schema outputs, returning a confidence score, semantic line mapping, and structured reasoning for the ledger audit trail.

Step-by-Step Implementation Guide

Let's examine how these components interact in a production deployment.

Step 1: DuckDB In-Memory Reconciliation Activity

We write a Python activity that loads batch invoice line items alongside active line items from the ERP database into an in-memory DuckDB instance. We calculate unit price variations and string similarity scores in a single SQL pass:

SELECT invoice_id, po_id, inv_line_id, po_line_id, (inv_unit_price - po_unit_price) AS price_diff, jaro_winkler_similarity(inv_desc, po_desc) AS text_sim FROM invoice_lines JOIN po_lines ON invoice_lines.po_number = po_lines.po_number WHERE ABS(inv_unit_price - po_unit_price) <= 0.05 AND jaro_winkler_similarity(inv_desc, po_desc) >= 0.85;

Lines matching both price tolerance (within 5 cents) and description similarity (>= 0.85) are automatically cleared for payment posting.

Step 2: Constructing the Agent Exception Activity

When DuckDB flags an unmapped record with high price variance or low description similarity, Temporal hands off execution to the Anthropic API activity. The system prompt injects top-5 catalog candidates found via DuckDB full-text search and instructs Claude to evaluate semantic identity:

System: You are a financial audit assistant. Match invoice line items to internal inventory items based on unit measures, manufacturer part numbers, and semantic descriptions. Respond ONLY in valid JSON conforming to the ExceptionResolution schema.

The model returns a JSON response containing the selected po_line_id, a confidence score between 0.00 and 1.00, and a concise textual audit justification.

Step 3: Human-in-the-Loop Routing and Signal Handling

If Claude's confidence score drops below 0.85, the Temporal workflow pauses execution and emits a web event to Slack or ServiceNow. The workflow enters a state-blocked state awaiting a Temporal Signal: workflow.await(lambda: self.human_approval_received). Once an AP manager clicks 'Approve' or 'Reject' in the internal dashboard, the signal resumes the workflow thread, posting the approved voucher to the legacy ERP API without losing state context.

Operational Results and ROI Metrics

In production enterprise deployments, this hybrid pattern processes 88% of invoice exceptions fully autonomously, reducing invoice cycle times from 4.2 days down to less than 15 minutes. By isolating the LLM agent to evaluate only unresolved edge cases, token expenditure drops by over 80% compared to full-document processing pipelines.

If your team is evaluating complex enterprise workflows or modernizing legacy ERP data pipelines, booking a Systems Audit & Blueprint engagement with our engineering team can help identify high-yield automation points across your existing operational stack.

Deterministic SQL engines must handle mathematical verification, while LLM agents should only be invoked for high-ambiguity semantic mapping when explicit rules fail.

Want this level of rigor applied to your own analytics stack?

This comes from running BA/BI systems audits for real Indian enterprises — where the actual fix is decided by which stage of your analytics function is broken, not by which tool has the best demo. A Systems Audit tells you exactly where to start.

Book a Systems Audit