top of page

Automate Accounts Payable Data Extraction with Claude and Python

2 minutes ago
4 min read



How We Use Claude AI + Python to Automate Accounts Payable Data Extraction

Manual invoice entry is one of the most quietly expensive processes in any finance department. It's not that any single invoice takes long to process — it's that the cost compounds across hundreds of invoices a month, across every distributor, every format, every edge case. In this post, we break down exactly how RUSA Analytics built an AI extraction agent using Claude AI and Python that turns a raw distributor invoice PDF into clean, structured JSON — automatically.

Why Manual Entry Doesn't Scale

Before automating anything, it's worth being precise about what "manual entry" actually costs a business:

  • Processing bottlenecks. Every invoice has to wait its turn in a queue for someone to open it, read it, and type the relevant fields into a system. As volume grows, so does the backlog.

  • Inconsistent extraction. Different people interpret "rebate terms" or "compliance notes" differently. Two people processing the same invoice might record slightly different numbers.

  • Hidden errors. A mistyped percentage or a missed due date doesn't announce itself — it just sits in the system until it causes a downstream problem, usually during a payout or an audit.

None of this is because finance teams aren't careful. It's because manual data entry, by nature, doesn't scale cleanly, no matter how disciplined the process is.

Setting Up the Extraction Pipeline

Our approach starts with a simple but important architectural decision: use an AI agent purpose-built for a single job — reading an invoice PDF and extracting a defined set of fields — rather than a general-purpose "read this document" tool. This keeps the agent focused, predictable, and easy to test.

The setup itself is straightforward. We connect to the Claude API from Python, and before running anything, we verify that the environment is configured correctly — specifically, checking that the ANTHROPIC_API_KEY environment variable is set. This one check saves a lot of debugging time later, since a missing or malformed API key is the single most common failure point in any AI pipeline.

python

API_KEY = os.getenv("ANTHROPIC_API_KEY")
if not API_KEY:
    raise ValueError("ANTHROPIC_API_KEY environment variable is not set.")

client = Anthropic(api_key=API_KEY)
MODEL = "claude-sonnet-4-5"

Reading the Invoice: What's Actually on the Page

Before writing a single line of extraction logic, we looked at what a real distributor invoice actually contains. Using a sample invoice from a distributor (Northgate Foods, in our test data), we identified the fields that consistently carry the information downstream systems need:

  • Distributor Name — who the invoice is from

  • Rebate Agreement — the rebate percentage applied and any conditions attached to it

  • Payment Due Date — when the invoice needs to be settled

  • Compliance Notes — standard terms apply, exclusivity clauses, and late payment penalties

  • Total Amount Due — the final invoice figure

These aren't arbitrary choices — they're the exact fields that feed into the validation stage later in the pipeline. If extraction misses or misreads any of these, every downstream step inherits that error.

Agent 1: The Extraction Logic

The core of the extraction agent is a carefully written system prompt. Rather than asking the model to "summarize the invoice" (too vague, too inconsistent), we tell it precisely what role it's playing and what output structure we expect:

"You are a fine invoice analyst. Extract only what is visible in the document. Identify the distributor name, rebate terms, payment due date, compliance flags, if present. You will output the result as structured JSON, no other text."

This kind of precise, role-based prompting matters more than people expect. A vague prompt produces inconsistent output — sometimes a paragraph, sometimes a list, sometimes missing fields entirely. A precise prompt with an explicit output format produces the same structure every single time, which is exactly what you need when the output feeds directly into another automated system.

python

def agent1_extraction(invoice_text):
    system_prompt = """
    You are a fine invoice analyst. Extract only what is visible in the document.
    Identify the distributor name, rebate terms, payment due date,
    compliance flags, if present. You will output the result as
    structured JSON, no other text.
    """
    response = client.messages.create(
        model=MODEL,
        max_tokens=1024,
        system=system_prompt,
        messages=[{"role": "user", "content": invoice_text}]
    )
    return response

From PDF to Structured JSON

Once the agent runs, the output isn't a block of prose — it's clean, structured JSON that any downstream system can consume without further parsing:

json

{
  "distributor_name": "Northgate Foods",
  "rebate_terms": "5% volume rebate",
  "payment_due_date": "2026-09-15",
  "compliance_flag": "Standard terms apply. No exclusivity clause."
}

This is the payload that gets passed to the next stage of the pipeline — validation against the distributor master dataset. Because the output is already structured and predictable, the validation agent doesn't need to do any additional cleanup or parsing; it can work directly with the JSON.

Why This Approach Works Better Than a Generic OCR Tool

It's worth addressing the obvious question: why not just use a standard OCR or template-matching tool? The answer comes down to flexibility. Traditional OCR-based extraction tools rely heavily on consistent document layouts — the moment a distributor changes their invoice template, or a new distributor with a completely different format joins the pipeline, template-based extraction breaks.

An AI extraction agent, by contrast, reads the document the way a person would — by understanding context and meaning, not by matching pixel positions. That means the same agent handles a Northgate Foods invoice and a completely different distributor's invoice without needing a new template built for each one.

What Comes Next in the Pipeline

Extraction is stage one of a larger system. Once a distributor invoice has been converted into structured JSON, that data flows into:

  1. Validation — checking the extracted terms against a trusted distributor master dataset (covered in our next post)

  2. Exception handling — flagging mismatches or unknown distributors for review

  3. Snowflake — storing validated records in a governed warehouse

  4. Power BI — surfacing the results in a live analytics dashboard

Each stage builds on the one before it, but it all starts here — with getting clean, structured data out of a messy PDF, reliably, every single time.

The Bigger Picture

Manual invoice entry isn't a people problem — it's a process problem. The people doing it are careful and competent; the process itself simply doesn't scale. An AI extraction agent removes the bottleneck at the very first step of the pipeline, so everything downstream — validation, storage, reporting — starts from clean, consistent data instead of whatever a human happened to type in during a busy week.

 
 
 

Comments


bottom of page