Spending limits for AI shopping agents: Claude classifies every purchase, your rules decide, and PayPal Vault charges only what's allowed.
Inspiration
Agentic commerce is coming: AI agents that reorder groceries, book travel, renew subscriptions. The missing piece isn't the agent. It's the guardrail. People want to delegate spending without giving an LLM an unlimited wallet.
What it does
An autonomous shopping agent (Claude Haiku 5.5) proposes a purchase in free text. A second Claude call, constrained to a JSON schema, extracts merchant, amount and category, treating the agent's text as untrusted. A deterministic rules engine checks it against your budget rules (per-purchase limits, monthly category caps, blocked merchants) and:
- approves: a real PayPal sandbox charge via Vault + merchant-initiated Orders v2 (no buyer redirect, because the buyer pre-authorized the wallet once)
- blocks: never reaches PayPal
- escalates: lands in an approval queue, where one click charges or denies it
Every decision carries a plain-English reason, and every human decision is an audit row. The transactions view is an AG Grid with status badges, quick filter, sorting, and links to the PayPal sandbox capture.
How we built it
- PayPal: OAuth2 client credentials → Vault v3 setup token → buyer approval →
payment token (vault id). Each approved purchase is
POST /v2/checkout/orderswithpayment_source.paypal.vault_idandstored_credential {payment_initiator: MERCHANT, usage: SUBSEQUENT}. That order completes with the capture inline, and we handle the explicit/captureshape too. A uniquePayPal-Request-Idon every POST gives idempotency. - AI: Anthropic Python SDK,
claude-haiku-5-5. Call 1 generates the proposal. Call 2 usesmessages.parsewith a Pydantic model (structured outputs) whose category is a closed enum, so the rules engine only gets validated, typed data. - Backend: FastAPI, SQLAlchemy 2, SQLite. Money is integer cents end to end. Approvals use an atomic compare-and-set UPDATE, so a double click can't double-charge.
- Frontend: React + TypeScript + Vite, AG Grid Community.
- Testing: TDD with pytest, 66 tests. The rules engine has a table-driven suite. The Claude and PayPal clients are tested against injected fakes that assert the exact request payloads. API tests run the full flow with dependency overrides.
Challenges
- The original sandbox app lacked the Vault scope (403s). Printing the
scopefield from the OAuth response diagnosed it, and a fresh app with "Save payment methods" fixed it. - Vault setup tokens expire if they aren't approved promptly.
- Vaulted CAPTURE orders return
COMPLETEDstraight from create, so a naive create-then-capture flow fails. The client handles both shapes.
What's next
Per-agent budgets and API keys, time-window rules (no purchases after midnight), anomaly detection on merchant drift, webhooks for refunds/disputes, and an MCP server so any agent framework can call Guardrail as its payment tool.
AI tools used
- Claude Haiku 5.5 (Anthropic API), at runtime: the shopping agent's purchase proposals, and structured-output extraction/classification of each proposal.
- Claude Code (Claude Opus 5.5), during development: pair-programming, test-first implementation, code review, and documentation drafting.
Built with
python, fastapi, sqlalchemy, sqlite, anthropic, claude, paypal-rest-api, paypal-vault, react, typescript, vite, ag-grid, pytest
Built With
- ag-grid
- anthropic
- claude
- fastapi
- paypal-rest-api
- paypal-vault
- python
- react
- sqlalchemy
- sqlite
- typescript
- vite