Inspiration

AI agents can already shop: search flights, compare prices, read reviews. But the moment an agent has to pay, someone hands it a credit card, and a process you cannot fully predict suddenly has unlimited blast radius. One prompt injection hidden in a product review and the agent is buying a $900 television. Agent commerce is stuck at look-but-don't-touch because there is no safe way to let an agent spend. The builder red-teams LLM agents for a living; this project is the payment primitive that work kept pointing at.

What it does

Scoped Purchase Tokens splits an agent's world in two. Free actions (search, compare, read reviews, check details) are fully autonomous. Spending is the one gated action, and it can only happen through a single-use token a human mints, scoped to a maximum amount, one merchant from a service-side registry, and a short expiry. The token service pre-builds the PayPal order; the agent receives only a proposal handle and the ability to capture that one order. It never sees payment credentials, it cannot change the amount or the recipient, and a declined capture burns the token. Prompt-inject it all you want: the worst it can do is the one purchase that was approved.

Two modes share the same ceiling. In Verify mode the agent proposes the exact purchase ("United 1234, $340") and the human approves that purchase; PayPal's buyer approval of the fixed order is the authorization. In YOLO mode the human approves a budget envelope instead: one PayPal authorization at the ceiling ($400) against one chosen merchant, and the agent picks the best option inside it. The ceiling is enforced by the service, not by PayPal: in the sandbox we measured captures of $401 and $460 landing on a $400 authorization, inside PayPal's documented overcapture tolerance, so the token service checks every capture against the envelope itself and refuses anything above it before money moves. Every event lands in a hash-chained ledger that separates service-observed money events from agent-reported free actions, so the audit trail cannot be quietly rewritten.

How we built it

Three components, Python standard library only. The shopper is an LLM agent (Anthropic Claude, Opus-class, via OpenRouter) that plans through typed tools: search, read reviews, propose, wait for approval, buy. The token service owns the money path with three separated credentials (agent, human, webhook): the agent can propose and capture by handle but gets a 403 on the approve route, and the model only ever sees an opaque proposal handle, never the token secret. The PayPal adapter speaks Orders v2 in both intents (CAPTURE for Verify, AUTHORIZE with final capture for YOLO envelopes), sends an idempotency key on every mutating call so a replayed capture cannot double-charge, and verifies webhooks by signature on the raw body, then re-fetches the event and re-reads the order before the ledger believes it. The sandbox host is hard-coded; there is no live-money code path. A small web UI renders the approval card from service-derived fields only (merchant resolved from the registry, amount from the pre-built order), with anything the agent says quarantined in a clearly marked untrusted box.

The design survived an adversarial review before the wiring was finished, and the review changed it: the original claim that PayPal's order object enforces scope is false for YOLO (the final item is unknown when the human approves), which is why YOLO is an authorize-ceiling instead of a pre-built order. The demo merchants are two sandbox business accounts we own; PayPal does not validate that a payee exists when an order is created (proven in Spike 3), so the service-side registry is the real payee gate, and the submission says so plainly.

Challenges we ran into

The first live run failed in an instructive way: the model produced valid JSON with the right item, price, and payee, but guessed the propose mode ("request_approval", "draft", "confirm"), because the tool contract was documented nowhere. The failure was the contract, not the model. The fix was to write the exact tool contracts into the planner's instructions and make every refusal self-describing (a rejected mode now names the valid values). The re-run proposed correctly on the first attempt. Sandbox reality was the other teacher: several design questions (can an order pay a second merchant account? does a capture before buyer approval fail cleanly?) could only be answered by running against PayPal, which is what the spike scripts are for.

Accomplishments that we're proud of

  • A real LLM ran the full Verify loop end to end (search, read reviews, propose, approval, buy) and captured $340.00, with the poisoned review sitting in its context: the injection was delivered and ignored, and the ledger's hash chain verified.
  • The wired token service minted a token against a real PayPal sandbox order (order 9PB863717L580384J), and a capture attempted before buyer approval was refused while the token stayed minted and usable.
  • Spike 3 proved the payee registry design against live sandbox behavior (second-merchant order 2GK47964H7611905J permitted; nonexistent payee also permitted at create, confirming the registry must be the gate).
  • 44 automated tests green: 24 service, 15 agent, 5 end-to-end and injection cases over real HTTP, including the $900 out-of-scope television capture being refused while the in-scope flight still captured on the same token.
  • The adversarial review is part of the repo, findings and all, including the places it proved the original design wrong.

What we learned

Agents do not fail at intelligence first; they fail at contracts. A model that cannot see the exact rules of its tools will invent plausible ones. And payment scope is a property you have to be able to point at: if the enforcement lives in a prompt, it is a suggestion; if it lives in PayPal's authorization and a service that refuses out-of-scope captures before money moves, it is a boundary.

What's next for Scoped Purchase Tokens

The code, the adversarial review, and full run instructions are public in the repo linked here. Next: add the PayPal JavaScript approval button stage so the buyer approves without leaving the page, add per-day envelope caps, and generalize the merchant registry so agent platforms can offer scoped spending as a primitive to their own users.

Built With

  • anthropic-claude
  • javascript
  • llm-agents
  • openrouter
  • paypal-orders-v2
  • paypal-webhooks
  • prompt-injection-testing
  • python
  • rest-api
  • sqlite
Share this project:

Updates

Submission history