Build an AP Agent That Knows When to Ask for Help
In this article
This guide walks you through building an accounts payable (AP) agent that processes invoices, matches them to purchase orders, and—crucially—recognizes when it lacks the information or authority to proceed, then escalates to a human. You'll end up with a system that reduces errors, prevents duplicate payments, and keeps your approval workflow intact.
What You'll Need
- Access to your AP system or accounting software (e.g., QuickBooks, Xero, SAP, or a custom ERP).
- A way to receive invoice data—either via email, OCR, or an API.
- Basic familiarity with your organization's approval thresholds and vendor master data.
- A development environment or a no-code automation platform (like Zapier, Make, or Power Automate) if you're not coding from scratch.
Step-by-Step
- Define the agent's scope and boundaries. List exactly what the agent can do autonomously: match invoice to PO, verify math, flag duplicate invoice numbers, and update payment status. Also list what it must never do without approval: paying over a certain amount, adding new vendors, or overriding a PO mismatch.
- Set up the data ingestion. Connect the agent to your invoice intake channel. If invoices arrive as PDFs via email, use OCR (like Tesseract or a cloud OCR service) to extract vendor name, invoice number, date, line items, and total. If you have an API, map those fields directly.
- Build the validation layer. Write rules to check: (a) vendor exists in your master list, (b) invoice number is unique, (c) line item totals add up to the grand total, (d) tax and shipping are calculated correctly. Any failure here triggers a 'needs review' flag.
- Implement PO matching logic. For each invoice line, attempt to match against an open purchase order. Compare quantity, unit price, and item description. If all match, mark as 'ready to pay'. If there's a minor discrepancy (e.g., price variance under 5%), set a rule to auto-approve or escalate based on your policy.
- Create escalation triggers. This is the heart of 'knowing when to ask for help.' Define conditions that automatically route to a human: any invoice over your approval threshold, any new vendor, any mismatch over your tolerance, any missing PO, any duplicate invoice number, or any invoice from a blocked vendor. Also include a 'confidence score'—if the OCR confidence is below 80%, escalate.
- Design the human handoff. When an invoice is escalated, create a task in your AP system or project management tool (e.g., Asana, Trello) with a clear summary: why it was escalated, what data is missing, and what action is needed. Send a notification to the responsible person via email or Slack.
- Add a feedback loop. When a human resolves an escalation, capture their decision (approve, reject, edit). Use that to refine your rules—for example, if a human approves a 6% price variance, you might raise your tolerance to 7% next time, but only if it's consistent with policy.
- Test with historical data. Run the agent against past invoices (with known outcomes) to see how often it would have escalated unnecessarily or missed a red flag. Adjust thresholds accordingly.
- Deploy in a sandbox first. Run the agent in a test environment for a week, processing real invoices but not actually paying anything. Monitor escalations and accuracy.
- Go live with monitoring. Once confident, switch to production. Set up daily reports showing how many invoices were processed, how many were escalated, and why. Review these weekly to catch drift.
Common Mistakes
- Over-automating approvals. Letting the agent approve anything without a human in the loop for the first month. Start conservative—escalate more, not less.
- Ignoring OCR errors. OCR often misreads numbers (e.g., '0' as 'O' or '1' as 'l'). Always include a confidence check and escalate low-confidence extractions.
- Hard-coding tolerance levels. A 5% price variance might be fine for office supplies but too high for raw materials. Use category-specific rules.
- No audit trail. If the agent makes a wrong decision, you need to know why. Log every action, including the data it used and the rule that fired.
- Forgetting about vendor master updates. If a vendor changes their remittance address, your agent might pay the old one. Flag any invoice where the address differs from the master.
Tips
- Start with a narrow scope. Only handle invoices under a certain amount and only for vendors with existing POs. Expand gradually.
- Use a 'human-in-the-loop' design. The agent should never be the final approver. It prepares and suggests; a human confirms.
- Make escalation messages actionable. Instead of 'Invoice needs review,' say 'Invoice #1234 from Acme Corp totals $5,200, but PO #5678 has a remaining balance of $4,800. Please approve overage or request a new PO.'
- Schedule regular rule reviews. Every quarter, review escalation logs to see if any rules are causing unnecessary work or missing real issues.
- Document everything. Write down the logic for each rule so that when a new team member joins (or you revisit in six months), you know why the agent behaves as it does.
Troubleshooting
- Agent is escalating too many invoices. Check your tolerance thresholds—they might be too tight. Also verify that your PO matching logic is correctly handling partial shipments and multiple invoices per PO.
- Agent is approving things it shouldn't. Review your escalation triggers—did you accidentally set the approval threshold too high? Also check for 'edge cases' like negative amounts or zero-quantity lines that might slip through.
- OCR is misreading invoice totals. If you're seeing frequent math errors, try a different OCR engine or add a post-processing step that cross-checks the extracted total against the sum of line items.
- Duplicate invoices are being paid. Ensure your duplicate check is based on vendor name + invoice number, not just invoice number (since different vendors can have the same invoice number).
- The agent is slow. If processing takes too long, consider batching invoices or moving to a faster OCR service. Also check if your API calls are rate-limited.
- Human reviewers are ignoring escalations. Make sure the notifications are routed to the right person and include a direct link to the invoice. If they're still ignored, set up an SLA that re-escalates after 24 hours.
Frequently Asked Questions
- What if my invoices don't have POs? For non-PO invoices, the agent should still validate the vendor and math, but always escalate for approval. You can set a rule that any invoice without a PO goes to a human.
- Can the agent handle multi-currency invoices? Yes, but you'll need to add a currency conversion step. Use current exchange rates and flag any invoice where the converted amount is over your threshold.
- How do I handle credit memos? Treat them as negative invoices. The agent should match them to the original invoice if possible, and escalate if it can't find a match.
- What if a vendor sends a duplicate invoice with a different invoice number? That's tricky. The agent can't know it's a duplicate unless you add a check for similar amounts and vendor within a short time window. Escalate those to be safe.
- Is it okay to let the agent auto-pay small invoices? It's common, but only after you've tested thoroughly. Start with a very low threshold (e.g., $100) and monitor for a month before increasing.
- How often should I update the agent's rules? At least quarterly, or whenever your AP policies change. Also after any major incident (e.g., a wrong payment) to add a new rule to prevent it.