Step 0 — Orient from the ledger, not from memory
This is a fresh session — there is no prior turn to resume. {{ap_ledger}} is
the memory.
- Open
{{ap_ledger}} and read every existing row: vendor, invoice number,
amount, PO reference, and flag, so this run's duplicate check has the full
history, not just what one person remembers.
- Read the PO tab: open PO number, vendor, authorized total, and the agreed
price per line item — this is what invoices get matched against.
Step 1 — Pull new invoice email
Check {{invoice_label}} in Gmail for messages not yet reflected in
{{ap_ledger}} (match on Gmail message ID or thread ID recorded in the
ledger's last column). For each one, download the invoice attachment
(PDF, image, or embedded table).
Step 2 — Extract vendor, amount, and line items
From each attachment, extract:
- Vendor name and, if present, vendor ID/tax ID.
- Invoice number and invoice date.
- Total amount due.
- Every line item: description, quantity, unit price, line total.
- The PO number referenced on the invoice, if any.
If the attachment is unreadable (corrupt, wrong format, no amount visible),
record it in the ledger as unreadable with the email link and skip
matching — don't guess at numbers.
Step 3 — Match against the PO
- If the invoice references a PO number, look it up in the PO tab.
- No PO found (either no PO number on the invoice, or the number doesn't
exist in the tab) → flag missing PO.
- PO found → compare every line item's unit price against the PO's
agreed price for that item:
- Within tolerance (default: 2%, or whatever
{{ap_ledger}}'s notes
specify) → matches, no flag on price.
- Over tolerance on any line → flag overcharge, noting the line item,
PO price, and invoiced price.
- Compare the invoice total against the PO's remaining authorized balance
(PO total minus amounts already invoiced against it, per the ledger). An
invoice that would exceed the PO's authorized total → flag overcharge
regardless of per-line pricing.
Step 4 — Check for duplicates
Compare the new invoice against every existing row in {{ap_ledger}} for the
same vendor:
- Same invoice number already recorded → duplicate, exact.
- Different invoice number but same amount, same or near-identical line
items, and a date within 30 days of a prior invoice from the same vendor →
duplicate, likely resend — flag it, don't silently merge or drop it.
Step 5 — Record every invoice
Append one row per invoice to {{ap_ledger}}, flagged or clean: vendor,
invoice number, date, amount, PO reference (or "missing"), line items (or a
link to the attachment), flag (clean / duplicate / overcharge /
missing-po / unreadable), and the Gmail message ID so this run and future
runs can tell what's already processed.
Step 6 — Post the batch for approval
Post to {{approval_channel}}: every invoice processed this run, grouped
clean vs. flagged, each with its ledger row and (for flags) the specific PO
line or prior invoice it conflicts with. Ask for approval to proceed to
payment through the normal AP process — do not imply the agent has already
approved or scheduled anything.
Step 7 — Stop — never touch payment
Your last action is the Slack post. You never schedule a payment, mark an
invoice as paid, or write to any payment rail or accounting system beyond
{{ap_ledger}}. A clean match is still a person's decision to pay.
1---2name: ap-invoice-match3description: >-4 Extraction, PO matching, duplicate detection, and overcharge tolerance for5 processing incoming vendor invoices from {{invoice_label}} against the POs6 and ledger in {{ap_ledger}}. Load this before extracting a single invoice so7 every one is checked to the same standard and only genuine exceptions reach8 a human, with no invoice ever scheduled for payment by the agent.9metadata:10 source: https://skillmd.com/skills/kortix-ai/ap-invoice-match11---1213<skill name="ap-invoice-match">1415<overview>16Process every new vendor invoice landing in `{{invoice_label}}` without missing17a duplicate or an overcharge, and without ever moving toward payment. A cron18fires every `{{cadence}}` against a fresh session; the session has no memory of19the last run, so it reads `{{ap_ledger}}` first, pulls new invoice email,20extracts vendor/amount/line items, matches each against the PO tab and every21prior invoice for that vendor, records the result, and posts the batch to22Slack. Recording and flagging are the agent's job; approving and paying are23not — those stay with a person on every invoice, clean or flagged.24</overview>2526<when-to-load>27- The `{{cadence}}` cron fires the invoice-processing run.28- A human asks the agent to re-check a specific invoice or vendor for29 duplicates or overcharges.30- A human asks why an invoice was flagged or missing a PO.31</when-to-load>3233<workflow>3435## Step 0 — Orient from the ledger, not from memory3637This is a fresh session — there is no prior turn to resume. `{{ap_ledger}}` is38the memory.39401. Open `{{ap_ledger}}` and read every existing row: vendor, invoice number,41 amount, PO reference, and flag, so this run's duplicate check has the full42 history, not just what one person remembers.432. Read the PO tab: open PO number, vendor, authorized total, and the agreed44 price per line item — this is what invoices get matched against.4546## Step 1 — Pull new invoice email4748Check `{{invoice_label}}` in Gmail for messages not yet reflected in49`{{ap_ledger}}` (match on Gmail message ID or thread ID recorded in the50ledger's last column). For each one, download the invoice attachment51(PDF, image, or embedded table).5253## Step 2 — Extract vendor, amount, and line items5455From each attachment, extract:5657- Vendor name and, if present, vendor ID/tax ID.58- Invoice number and invoice date.59- Total amount due.60- Every line item: description, quantity, unit price, line total.61- The PO number referenced on the invoice, if any.6263If the attachment is unreadable (corrupt, wrong format, no amount visible),64record it in the ledger as **unreadable** with the email link and skip65matching — don't guess at numbers.6667## Step 3 — Match against the PO68691. If the invoice references a PO number, look it up in the PO tab.702. **No PO found** (either no PO number on the invoice, or the number doesn't71 exist in the tab) → flag **missing PO**.723. **PO found** → compare every line item's unit price against the PO's73 agreed price for that item:74 - Within tolerance (default: 2%, or whatever `{{ap_ledger}}`'s notes75 specify) → matches, no flag on price.76 - Over tolerance on any line → flag **overcharge**, noting the line item,77 PO price, and invoiced price.784. Compare the invoice total against the PO's remaining authorized balance79 (PO total minus amounts already invoiced against it, per the ledger). An80 invoice that would exceed the PO's authorized total → flag **overcharge**81 regardless of per-line pricing.8283## Step 4 — Check for duplicates8485Compare the new invoice against every existing row in `{{ap_ledger}}` for the86same vendor:8788- Same invoice number already recorded → **duplicate**, exact.89- Different invoice number but same amount, same or near-identical line90 items, and a date within 30 days of a prior invoice from the same vendor →91 **duplicate**, likely resend — flag it, don't silently merge or drop it.9293## Step 5 — Record every invoice9495Append one row per invoice to `{{ap_ledger}}`, flagged or clean: vendor,96invoice number, date, amount, PO reference (or "missing"), line items (or a97link to the attachment), flag (`clean` / `duplicate` / `overcharge` /98`missing-po` / `unreadable`), and the Gmail message ID so this run and future99runs can tell what's already processed.100101## Step 6 — Post the batch for approval102103Post to `{{approval_channel}}`: every invoice processed this run, grouped104clean vs. flagged, each with its ledger row and (for flags) the specific PO105line or prior invoice it conflicts with. Ask for approval to proceed to106payment through the normal AP process — do not imply the agent has already107approved or scheduled anything.108109## Step 7 — Stop — never touch payment110111Your last action is the Slack post. You never schedule a payment, mark an112invoice as paid, or write to any payment rail or accounting system beyond113`{{ap_ledger}}`. A clean match is still a person's decision to pay.114115</workflow>116117<guardrails>118- **No payment action, ever.** The agent has no connector or instruction that119 schedules a payment or marks an invoice as paid — not even for a clean,120 fully matched invoice. That step is always a human's.121- **Record, don't discard.** An unreadable, duplicate, or overcharged invoice122 is still recorded in `{{ap_ledger}}` with its flag — never silently123 dropped.124- **No force-matching.** An invoice with no PO, or a PO match that doesn't125 clear tolerance, is flagged as-is. Don't invent a PO reference or round an126 overcharge down to make it look clean.127- **One flag reason per exception.** State exactly which line, PO, or prior128 invoice triggered a duplicate/overcharge/missing-PO flag so the approver129 doesn't have to re-derive it.130- **Scoped, brokered credentials.** Gmail, Sheets, and Slack access are131 injected into the sandbox at runtime, scoped to this agent's grant.132- **Idempotent per run.** Match new invoices against the ledger's Gmail133 message IDs before processing so the same email is never recorded twice134 across runs.135</guardrails>136137</skill>