Skip to main content

The silent failure

Your LLM extracts this from a scanned invoice:
{
  "subtotal": 100,
  "tax": 10,
  "total": 105,
  "currency": "USD"
}
Valid JSON. Every field is a number. Your accounting system writes it to the ledger. But 100 + 10 = 110, not 105. The math is wrong. Schema validation doesn’t check arithmetic. Zod doesn’t check cross-field consistency. Nothing caught it. Your client’s accountant finds the discrepancy three weeks later.

With Boundary

Attempt 1 → { subtotal: 100, tax: 10, total: 105 }
             ✕ subtotal + tax ≠ total (100 + 10 = 110, got 105)
             ↻ violations sent back to model

Attempt 2 → { subtotal: 100, tax: 10, total: 110 }
             ✔ all rules pass
             ✔ ACCEPTED — financials consistent

The contract

import { z } from "zod";
import { defineContract } from "@boundary/contract";

const schema = z.object({
  vendor: z.string(),
  invoiceNumber: z.string(),
  date: z.string(),
  lineItems: z.array(z.object({
    description: z.string(),
    quantity: z.number(),
    unitPrice: z.number(),
    amount: z.number(),
  })),
  subtotal: z.number(),
  tax: z.number(),
  total: z.number(),
  currency: z.string(),
});

const invoiceContract = defineContract({
  schema,
  rules: [
    // invoice must have at least one line item
    (invoice) =>
      invoice.lineItems.length > 0
        || "invoice must have at least one line item",

    // line item amounts must add up to subtotal
    (invoice) => {
      const sum = invoice.lineItems.reduce((s, i) => s + i.amount, 0);
      return Math.abs(sum - invoice.subtotal) < 0.01
        || `line items sum to ${sum}, but subtotal is ${invoice.subtotal}`;
    },

    // subtotal + tax must equal total
    (invoice) =>
      Math.abs(invoice.subtotal + invoice.tax - invoice.total) < 0.01
        || `subtotal (${invoice.subtotal}) + tax (${invoice.tax}) = ${invoice.subtotal + invoice.tax}, but total is ${invoice.total}`,

    // each line item: quantity × unitPrice must equal amount
    (invoice) => {
      const bad = invoice.lineItems.find(
        (i) => Math.abs(i.quantity * i.unitPrice - i.amount) >= 0.01
      );
      return !bad
        || `${bad.description}: ${bad.quantity} × ${bad.unitPrice} = ${bad.quantity * bad.unitPrice}, but amount is ${bad.amount}`;
    },
  ],
});

Full example

const result = await invoiceContract.accept(async (attempt) => {
  const res = await anthropic.messages.create({
    model: "claude-sonnet-4-20250514",
    max_tokens: 1024,
    system: attempt.instructions,
    messages: [
      {
        role: "user",
        content: `Extract structured data from this invoice:\n\n${invoiceText}`,
      },
      ...attempt.repairs.map((r) => ({
        role: r.role as "user" | "assistant",
        content: r.content,
      })),
    ],
  });
  const block = res.content[0];
  return block.type === "text" ? block.text : null;
});

if (result.ok) {
  // ✔ financials consistent
  await accounting.createEntry(result.data);
}

Result: financials consistentEvery invoice that enters your accounting system has been verified: line items sum to subtotal, subtotal + tax equals total, individual amounts match quantity × price. No arithmetic errors reach your ledger.