Inspiration
Paying freelancers or creators through PayPal usually starts in a spreadsheet. One typo, one repeated invoice or one convincing "my PayPal email has changed" message is enough to send money to the wrong place, and a payout can't be reversed once it has been claimed.
This isn't rare. Business email compromise, including fake invoices and changed payment details, cost about $3 billion in 2025 across 24,768 complaints to the FBI. Duplicate payments run at a median of about 1.5% of disbursements.
Larger companies buy accounts-payable software to catch this. A small business or platform paying people through PayPal Payouts has nothing between the spreadsheet and the Send button. Second Look is that missing check.
What it does
Second Look reviews a PayPal payout run before any money leaves, and helps recover the payouts that don't arrive.
- Start a batch. Upload a payout CSV with invoices (PDF or text) and any related emails, or describe the run in one sentence: "Pay everyone who sent an invoice, except Raj. Add a $50 bonus for Sofia."
- Review. Every line comes back ok, review or block, with the reason, the evidence and, where possible, a one-click fix. It catches changed payment emails, wrong amounts, invoices already paid or repeated, wrong currency, first-time recipients over a limit, missing invoices, and any rule the owner writes in plain English.
- Resolve. Apply a fix, drop a line, tick a review line, or override a block with a written reason. Edit a line, add one, or tell the agent what to change; every changed line is checked again.
- Send. One approval sends everything through PayPal Payouts, one batch per currency, with retries that can't pay twice.
- Track and recover. Statuses arrive by webhook and polling. If a payout comes back unclaimed or returned, a follow-up agent checks PayPal and the recipient's records and proposes cancel, resend, wait or "ask for a new email". The owner approves each step.
- Catch the repeat. Upload the same batch again and every paid line comes back "already paid".
Nothing moves without a human click: the AI reviews and proposes, code enforces the limits, and the owner approves.
How I built it
Next.js 16, React 19 and TypeScript, with SQLite for storage, hosted on Render. Built with Claude Code.
The review is split between code and AI:
- Code builds the facts for each line first: the matching invoice, past payouts to that person, duplicates, and repeated recipients within the batch.
- Claude Sonnet 5.5 judges those facts against the owner's rules and returns structured output: a verdict, reasons, evidence and suggested fixes.
- A code floor then applies on top. An invoice already paid, an amount that differs from the invoice, or an email found on neither the invoice nor the history can never pass as ok, whatever the model says. One-click fixes may only use values found on the invoice or in past payouts. This also limits prompt injection from invoices and emails written by outsiders.
- Claude Haiku 5.5 reads invoice PDFs into fields.
The agent is Claude Sonnet 5.5 with the Anthropic SDK's tool runner, and it is bounded on purpose. No tool sends money, and no drafting tool accepts an email address: recipients come from invoices and history. Every tool call appears in a live log, and every proposal waits for the owner.
PayPal:
- Payouts REST API to send. The sender_batch_id is saved before the call, so a timeout can only be retried under the same ID, and PayPal refuses a second batch.
- Payout item status and cancel, for the follow-up agent.
- Webhooks for PAYMENT.PAYOUTS-ITEM and PAYOUTSBATCH events, with signature verification and ownership checks.
- Transaction Search through the PayPal Server SDK 2.5.0, written with the APIMatic Context Plugin's TypeScript skills, to import payment history and let the agent look up recent payouts.
Big batches (up to 500 lines) are reviewed in 50-line chunks in parallel, with a progress bar. Facts are computed over the whole batch before splitting, so no cross-line context is lost. Each chunk is ready to run as a Render Workflows task with retries.
Challenges we ran into
- Retrying safely. A timeout from PayPal doesn't mean the money stayed put. Each send is claimed with its sender_batch_id before calling PayPal; an unclear answer allows only a retry under the same ID, and PayPal's duplicate error is used to link the batch that already exists.
- Keeping the AI from having the last word on money. Every hard fact (already paid, amount mismatch, unknown email) is enforced by code, so a confident model or an injected instruction in an invoice can't wave a line through.
- PayPal can't tell you before sending whether an email has an account. That case can only be caught afterwards as UNCLAIMED, which is why the follow-up agent exists.
- One currency per PayPal batch. Mixed-currency runs are split into one PayPal batch per currency and tracked together.
- Cost. Sending raw files to the model cost about $0.033 per demo review. Computing facts in code and returning only flagged lines brought it to $0.014.
- Big batches. Splitting 500 lines into chunks without losing duplicates or repeated recipients across chunks meant computing all facts before splitting.
- Sandbox setup. The sender has to be a US business account, or payouts fail with PAYOUT_NOT_AVAILABLE. The Transaction Search permission can take a while to reach new tokens. The Server SDK has no Payouts controller, so sending uses the REST API directly.
Accomplishments that I'm proud of
- On demo data, it caught every planted mistake with no clean line wrongly flagged: 6 of 6 in the demo batch, 7 of 7 in a blind test with mistakes written by someone else, and 12 of 12 in a 500-line batch.
- It's cheap enough to run on every payout run: about $0.014 to review a 7-line batch and $0.18 for 500 lines (about $0.0004 per line). An agent draft costs about $0.04, and a follow-up about $0.02 to $0.03.
- The agent is useful without being trusted with money. It drafts and changes batches and investigates failed payouts, but it can't send a payment or invent an email address.
- Recovery is built in. Unclaimed or returned payouts get a proposed next step, and a resend can never pay twice.
- 43 automated tests cover the money path (safe retries, concurrent sends, webhooks), the code floor, edits and the agent's changes, each against a throwaway database and a fake PayPal. Writing them turned up a dozen edge-case bugs, all fixed before submission.
What I learned
- Let code find the facts and let the model judge them. It's cheaper, exact, and much easier to trust.
- With money, the AI should never be the last check. A code floor under the model turns "usually right" into "never waves through a known-bad line".
- Make an agent safe by limiting its tools, not by writing a cleverer prompt. If no tool can send money, no prompt can make it.
- Idempotency is a design decision you make before the first API call, not a bug fix after a double payment.
What's next for Second Look
- Live payouts after PayPal production approval.
- Separate roles, so one person prepares a run and another approves it.
- Pulling payout runs straight from accounting and invoicing tools instead of a CSV.
- More built-in rules, such as per-recipient limits and monthly budget caps.
- Render Workflows switched on for big batches, with retries per chunk.
Instructions for Judges
Hosted demo: https://second-look-paypal.onrender.com Password: PaypalHTJudge123 Everything runs on the PayPal sandbox with test money. If the first page load is slow, give it up to a minute: the server wakes up on the first visit.
- Review: open New batch, press Reset demo, then Load demo. Seven lines come back with their reasons: line 1 changed email (the change request came from a lookalike domain), line 2 a tenfold amount, line 3 an invoice already paid. Lines 5, 6 and 7 need a tick, and line 4 is clean.
- Send and recover: apply the fixes, tick the review lines and press Approve and send via PayPal. Within about 30 seconds the statuses arrive and Priya's payout shows Unclaimed, because her address has no PayPal account. The follow-up agent runs by itself and proposes a next step, with every tool call shown in its log. To recover her payout, approve a resend proposal, or press Cancel and resend on her card with the address suggested from her invoice: the unclaimed payout is cancelled first, then resent.
- Catch the repeat: open New batch and press Load demo again. Paid lines now come back as already paid.
- Edit and add: press Reset demo, then Load demo. On Omar's 8,500 line press Edit, change the amount to 850 and save; the line is checked again. Edit Raj's amount to 450 and the code check blocks it, because his invoice says 400.
- Agent draft: press Reset demo, choose Ask the agent, attach the five PDFs from day1-test/pdf in the repo, type "Pay everyone who sent an invoice, except Raj. Add a $50 bonus for Sofia." and press Draft with agent. Sofia's line is blocked because the bonus isn't on her invoice: agent drafts pass the same checks.
- Tell the agent what to change: on that batch, type "Remove Lena and add Raj back." Lena's line is dropped, Raj's line is added and checked, and the log shows each step.
- Scale: press Reset demo, then Load big demo. 500 lines are reviewed in 10 parts with a progress bar; day1-test/big-answer-key.md lists the 12 planted mistakes. Please don't send this batch: it creates 500 sandbox payouts that come back unclaimed.
Code: https://github.com/kharesiddhant/second-look-paypal. The README has the architecture, measured costs and setup.
Built With
- anthropic-sdk
- apimatic
- claude
- claude-code
- claude-haiku
- claude-sonnet
- next.js
- paypal-payouts
- paypal-server-sdk
- paypal-transaction-search
- paypal-webhooks
- react
- render
- sqlite
- typescript
- zod