{
  "schema": "agentflows.config/1",
  "id": "invoice-triage",
  "version": "1",
  "title": "Invoice triage",
  "short": "Read vendor invoice PDFs, check them against the PO register, vendor master and AP ledger, and route each one for approval.",
  "owner": "Accounts payable",
  "items": [
    "inv-a",
    "inv-b",
    "inv-c",
    "inv-d",
    "inv-e",
    "inv-f",
    "inv-g"
  ],
  "steps": [
    {
      "kind": "tool",
      "id": "read_pdf",
      "title": "Read the PDF text layer",
      "tool": "read_pdf_text"
    },
    {
      "kind": "model",
      "id": "extract",
      "title": "Extract invoice fields",
      "system": "You extract data from vendor invoices for an accounts payable team. Copy values exactly as printed. Don't correct arithmetic, don't fill in values that aren't printed, and don't guess. Answer with one JSON object and nothing else.",
      "user": "Extract these fields from the invoice text below. The invoice was sent to Larkfield Supply Co.; the vendor is the company that issued it.\n\nReturn JSON with exactly these keys:\n{\n  \"vendor_name\": string,\n  \"invoice_number\": string, as printed,\n  \"invoice_date\": \"YYYY-MM-DD\",\n  \"due_date\": \"YYYY-MM-DD\" or null,\n  \"po_number\": string or null,\n  \"line_items\": [{\"description\": string, \"quantity\": number, \"unit_price\": number, \"amount\": number}],\n  \"subtotal\": number,\n  \"tax\": number,\n  \"total\": number,\n  \"remit_to\": {\"bank_name\": string or null, \"routing_number\": string or null, \"account_number\": string or null},\n  \"phone_numbers_on_invoice\": [string],\n  \"notices\": [string]\n}\nAmounts are plain numbers without currency symbols or commas, as printed (don't recompute them). \"notices\" holds any printed notice, stamp or request, word for word; [] if there is none.\n\nInvoice text (extracted from {{file}}):\n\"\"\"\n{{text}}\n\"\"\"",
      "maxTokens": 16384,
      "optional": false
    },
    {
      "kind": "tool",
      "id": "lookup",
      "title": "Look up vendor, PO and ledger",
      "tool": "lookup_records"
    },
    {
      "kind": "model",
      "id": "review",
      "title": "Recommend a route",
      "system": "You are an accounts payable reviewer at Larkfield Supply Co.. You recommend a route for one invoice under the AP policy. You can't pay, approve or contact anyone; a person decides after reading your recommendation. Answer with one JSON object and nothing else.",
      "user": "AP policy:\n\"\"\"\n{{policy}}\n\"\"\"\n\nInvoice data (extracted from the PDF by a previous step):\n{{invoice}}\n\nVendor master record for this vendor (null if there is no match):\n{{vendor}}\n\nPO register entry for the invoice's PO number (null if none). \"remaining\" is the amount minus billed to date:\n{{po}}\n\nAP ledger entries already recorded for this vendor:\n{{ledger}}\n\nCheck the invoice against the policy and the records. Return JSON:\n{\"route\": one of {{routes}}, \"issues\": [one short string per problem you found; [] if none], \"note\": \"one or two sentences for the approver\"}",
      "maxTokens": 16384,
      "optional": true
    },
    {
      "kind": "tool",
      "id": "check_rules",
      "title": "Apply the AP policy in code",
      "tool": "check_rules"
    },
    {
      "kind": "tool",
      "id": "guardrail",
      "title": "Keep the stricter route",
      "tool": "guardrail"
    },
    {
      "kind": "checkpoint",
      "id": "approval",
      "title": "AP approval",
      "checkpoint": "ap_approval"
    }
  ],
  "tools": [
    {
      "name": "read_pdf_text",
      "does": "Returns the text layer of one invoice PDF.",
      "access": "read",
      "data": "The 7 sample invoice PDFs",
      "production": "Read access to the AP inbox folder only; no access to the mailbox itself."
    },
    {
      "name": "lookup_records",
      "does": "Finds the vendor master row, the PO and the vendor's AP ledger rows.",
      "access": "read",
      "data": "Vendor master, PO register, AP ledger (fixtures)",
      "production": "Read-only ERP role scoped to vendors, POs and AP history. No write role."
    },
    {
      "name": "check_rules",
      "does": "Applies the AP routing policy as code: duplicates, bank details, arithmetic, PO balance, thresholds.",
      "access": "compute",
      "data": "Extracted fields + looked-up records",
      "production": "Runs inside the workflow; needs no system access."
    },
    {
      "name": "guardrail",
      "does": "Keeps the stricter of the model's route and the coded route.",
      "access": "compute",
      "data": "Both routes",
      "production": "Runs inside the workflow; the model can't skip it."
    },
    {
      "name": "queue_action",
      "does": "After a person approves, records the follow-up (payment run row, vendor email draft or task) in a local list.",
      "access": "sandbox-write",
      "data": "In-memory list in your browser",
      "production": "Creates a payment proposal or task for a person to release. Never releases a payment."
    }
  ],
  "permissions": [
    {
      "resource": "Invoice PDFs",
      "grant": "read",
      "why": "Needed to extract fields."
    },
    {
      "resource": "Vendor master, PO register, AP ledger",
      "grant": "read",
      "why": "Needed for duplicate, bank and PO checks."
    },
    {
      "resource": "Vendor master (edit bank details)",
      "grant": "none",
      "why": "A bank change request in an invoice is the classic payment fraud pattern; only a person who called the number on file may change it."
    },
    {
      "resource": "Payment run",
      "grant": "write after approval",
      "why": "A proposal row is added only after the approver accepts the route; releasing payment stays a separate human step."
    },
    {
      "resource": "Email to vendors",
      "grant": "draft only",
      "why": "Correction requests are drafted for a person to send."
    },
    {
      "resource": "Phone numbers or links printed on the invoice",
      "grant": "none",
      "why": "The policy uses contact details from the vendor master only."
    }
  ],
  "checkpoints": [
    {
      "id": "ap_approval",
      "reviewer": "AP lead (controller for invoices over $10,000)",
      "sees": [
        "The invoice PDF",
        "Extracted fields",
        "Every flag from the coded checks",
        "The model's note",
        "Whether the guardrail overrode the model"
      ],
      "onApprove": "The follow-up for that route is recorded: a payment-run proposal, a vendor email draft or a task. Nothing is paid.",
      "onReject": "The invoice goes back to the AP queue with the reviewer's reason; nothing is recorded."
    }
  ],
  "runner": {
    "temperature": 0.2
  },
  "fixtures": {
    "company": {
      "name": "Larkfield Supply Co.",
      "address": [
        "2200 Mill Race Road",
        "Columbus, OH 43215"
      ],
      "email": "ap@larkfield.example.com"
    },
    "vendorMaster": [
      {
        "id": "V-1001",
        "name": "Brightline Packaging Inc.",
        "phoneOnFile": "(614) 555-0142",
        "routing": "999000111",
        "account": "9900004417",
        "terms": "Net 30"
      },
      {
        "id": "V-1002",
        "name": "Harlow Freight LLC",
        "phoneOnFile": "(317) 555-0168",
        "routing": "999000133",
        "account": "9900002290",
        "terms": "Net 15"
      },
      {
        "id": "V-1003",
        "name": "Kestrel Facility Services",
        "phoneOnFile": "(513) 555-0119",
        "routing": "999000155",
        "account": "9900007051",
        "terms": "Net 30"
      },
      {
        "id": "V-1004",
        "name": "Ostara Industrial Parts Co.",
        "phoneOnFile": "(419) 555-0185",
        "routing": "999000177",
        "account": "9900005532",
        "terms": "Net 45"
      },
      {
        "id": "V-1005",
        "name": "Pinegate Office Supply",
        "phoneOnFile": "(216) 555-0127",
        "routing": "999000199",
        "account": "9900001180",
        "terms": "Net 30"
      }
    ],
    "poRegister": [
      {
        "po": "PO-23988",
        "vendorId": "V-1001",
        "description": "Corrugated shipping boxes, October",
        "amount": 1120.6,
        "billedToDate": 1120.6
      },
      {
        "po": "PO-24017",
        "vendorId": "V-1001",
        "description": "Stretch film and carton tape, November",
        "amount": 1843.6,
        "billedToDate": 0
      },
      {
        "po": "PO-24031",
        "vendorId": "V-1004",
        "description": "Line 2 conveyor drive replacement",
        "amount": 18500,
        "billedToDate": 0
      },
      {
        "po": "PO-24044",
        "vendorId": "V-1002",
        "description": "Blanket PO: LTL freight, Q4",
        "amount": 6000,
        "billedToDate": 4350
      },
      {
        "po": "PO-24052",
        "vendorId": "V-1003",
        "description": "Janitorial service, November",
        "amount": 2400,
        "billedToDate": 0
      },
      {
        "po": "PO-24060",
        "vendorId": "V-1005",
        "description": "Office furniture, receiving desk",
        "amount": 612.36,
        "billedToDate": 612.36
      },
      {
        "po": "PO-24071",
        "vendorId": "V-1005",
        "description": "Toner, paper and desk supplies",
        "amount": 1123.14,
        "billedToDate": 0
      }
    ],
    "apLedger": [
      {
        "vendorId": "V-1002",
        "invoiceNumber": "HF-77650",
        "invoiceDate": "2026-10-06",
        "total": 2900,
        "po": "PO-24044",
        "status": "paid 2026-10-21"
      },
      {
        "vendorId": "V-1001",
        "invoiceNumber": "BL-20931",
        "invoiceDate": "2026-10-14",
        "total": 1120.6,
        "po": "PO-23988",
        "status": "paid 2026-11-12"
      },
      {
        "vendorId": "V-1003",
        "invoiceNumber": "KFS-1031",
        "invoiceDate": "2026-10-31",
        "total": 2400,
        "po": "PO-23990",
        "status": "paid 2026-11-28"
      },
      {
        "vendorId": "V-1002",
        "invoiceNumber": "HF-77812",
        "invoiceDate": "2026-11-03",
        "total": 1450,
        "po": "PO-24044",
        "status": "paid 2026-11-18"
      },
      {
        "vendorId": "V-1005",
        "invoiceNumber": "55120",
        "invoiceDate": "2026-11-10",
        "total": 612.36,
        "po": "PO-24060",
        "status": "approved, in the 2026-12-10 payment run"
      }
    ],
    "policy": "Larkfield Supply Co. accounts payable routing policy (rev. 2026-10)\n\nPick exactly one route. When more than one applies, use the first in this list.\n1. reject_duplicate: the AP ledger already has an invoice from the same vendor with the same invoice number (ignore spaces, hyphens and letter case).\n2. hold_verify_bank: the remit-to routing or account number differs from the vendor master, or the vendor is not in the vendor master. Verify only by calling the phone number in the vendor master, never a number printed on the invoice.\n3. hold_possible_duplicate: the AP ledger has an invoice from the same vendor, on the same PO, with the same total, dated within 45 days of this invoice, under a different number.\n4. return_to_vendor: the arithmetic doesn't hold (quantity x unit price differs from a line amount, line amounts don't add up to the subtotal, or subtotal + tax differs from the total), allowing 1 cent for rounding.\n5. hold_po_review: no PO number, the PO is not in the PO register, the PO belongs to another vendor, or the invoice total is more than the PO's remaining balance (PO amount minus billed to date).\n6. approve_controller: none of the above, and the total is more than $10,000.00.\n7. approve_ap: none of the above, and the total is $10,000.00 or less.\n\nNo route pays an invoice. Every route goes to a person, who approves before anything is paid, sent or recorded.",
    "routes": [
      "reject_duplicate",
      "hold_verify_bank",
      "hold_possible_duplicate",
      "return_to_vendor",
      "hold_po_review",
      "approve_controller",
      "approve_ap"
    ],
    "invoices": [
      {
        "id": "inv-a",
        "file": "brightline-bl-21407.pdf"
      },
      {
        "id": "inv-b",
        "file": "ostara-oip-88213.pdf"
      },
      {
        "id": "inv-c",
        "file": "brightline-bl20931-second-notice.pdf"
      },
      {
        "id": "inv-d",
        "file": "kestrel-kfs-1108.pdf"
      },
      {
        "id": "inv-e",
        "file": "pinegate-55187.pdf"
      },
      {
        "id": "inv-f",
        "file": "harlow-hf-78140.pdf"
      },
      {
        "id": "inv-g",
        "file": "pinegate-55190.pdf"
      }
    ]
  },
  "files": [
    {
      "path": "agentflows/fixtures/invoice-triage/brightline-bl-21407.pdf",
      "sha256": "35ab06d3a4118e1d5d74bf1e52a412bdbfeee3c96aa5e39d45f6f211ed551bde"
    },
    {
      "path": "agentflows/fixtures/invoice-triage/ostara-oip-88213.pdf",
      "sha256": "c44c9bf621e1476f3ddc0f4bb11691ad0192d968ad68cf1241659d6664065161"
    },
    {
      "path": "agentflows/fixtures/invoice-triage/brightline-bl20931-second-notice.pdf",
      "sha256": "2138ff2471e5fc6392aee88dc34d2d1ba7619cd665b0fdb323aecaef18f42a5a"
    },
    {
      "path": "agentflows/fixtures/invoice-triage/kestrel-kfs-1108.pdf",
      "sha256": "1476ddacf2d9661fede0096ee8db2f8f9039b4f1d05871029459b136a4929e61"
    },
    {
      "path": "agentflows/fixtures/invoice-triage/pinegate-55187.pdf",
      "sha256": "8e6a43c0135f813c77b28fe64f761bd6b643c72296602a735bd82449a46a4e94"
    },
    {
      "path": "agentflows/fixtures/invoice-triage/harlow-hf-78140.pdf",
      "sha256": "81ddc5303466b7966c25462ea5cd1424199296724f5261d16c4d60fcca21ebfa"
    },
    {
      "path": "agentflows/fixtures/invoice-triage/pinegate-55190.pdf",
      "sha256": "0c2c7c5e82fc3651528d6b9455a1420d5525990bfbf7a7b0245f1bbc8c97c05d"
    }
  ],
  "configHash": "5ccc121c8a9cf60115a932501db0cd147608581752eecc5ac9cf1c5a84b2a887"
}
