Inspiration

AI agents are starting to buy things, pay invoices and renew subscriptions. Handing an agent a card or an API key is a liability: it can overspend, pay the wrong person, or be tricked into doing either. Dashboards that show payments after the fact don't help, because the money has already moved.

Merchant-side tooling lets agents pay. Nothing answered the buyer-side question: was this agent allowed to spend this money, and can I prove it? I built Countersign to enforce the rules before money moves and to keep proof afterwards.

What it does

An agent submits a payment intent with its own identity token. Countersign:

  1. Authenticates the agent. The request can't choose who it is.
  2. Evaluates it against an immutable, versioned policy with a pure, deterministic engine: per-transaction limit, daily and monthly caps, recipient allowlist and a countersign threshold.
  3. Reserves budget atomically, so concurrent requests can't overspend a cap.
  4. Routes the result: DENIED (recorded, never sent to PayPal), PENDING_APPROVAL (a human reviews the exact amount and payee) or AUTHORIZED.
  5. Pays through the PayPal Payouts API (Sandbox) only after authorization, with three layers of idempotency. It settles on PayPal's signature-verified webhook, with a 15-second reconciliation poll as a safety net.
  6. Records every step in an append-only ledger chained with HMAC-SHA256, verifiable in one click.

The agent can never approve its own payment.

AI, used where it fits. Gemini turns a plain-English instruction ("pay approved vendors up to $75 a payment, $200 a day, approve above $30") into structured policy limits and shows a diff against the active version. A human reviews and activates it. "Why was this denied?" and "what if the agent paid $80?" are answered deterministically from stored records, so those facts are exact and never guessed. Nothing in the AI can approve, execute or change a limit.

How we built it

I built it solo.

  • Backend: TypeScript, Node and Express. The policy engine, money math (integer minor units) and policy validation are pure functions with no I/O, so they are easy to test and fail closed.
  • PayPal: Payouts REST API in Sandbox. Idempotency at three layers (Idempotency-Key, PayPal-Request-Id, deterministic sender_batch_id). Webhooks are verified through PayPal's signature-verification endpoint and de-duplicated by event ID.
  • Security: peppered HMAC-SHA256 agent tokens checked in constant time, admin auth, rate limiting, and approvals bound to a snapshot of the request and revalidated against the current agent, policy, recipient and budget before any money moves.
  • Frontend: React 19, Vite and Tailwind. The Audit Ledger and Payment Intents views are AG Grid tables (sorting, filters, quick search, pagination, pinned column, CSV export of exactly what is shown), loaded on demand.
  • AI: Gemini via @google/genai, with a model fallback chain, per-call timeouts, a second API key and an answer cache. Without a key it falls back to deterministic answers.
  • Developer experience: an OpenAPI 3.0 spec for the agent API, validated with Redocly and served by the app, plus a typed TypeScript SDK generated with APIMatic and run against the live deployment.
  • Quality: 97 automated tests, including ledger tamper detection, idempotency conflicts, approval revalidation, webhook and payout reconciliation, and a concurrency test (ten simultaneous $20 requests against a $120 cap: exactly six succeed).
  • Deployment: a Docker image with a prebuilt frontend, behind TLS.

Challenges we ran into

  • Money correctness under concurrency. Budget is reserved the moment a request is accepted and settled or released later, so ten parallel requests can't each pass the same cap check.
  • Settlement you can trust. PayPal confirms asynchronously. Countersign settles on signed webhooks and also polls, so a missed webhook never leaves a payout stuck.
  • Keeping the AI honest. An assistant that explains a denial must not invent the reason. I made explanations and simulations deterministic and limited the model to drafting and open questions. Testing against the real API also surfaced a retired default model, rate limits and timeouts, which led to the fallback chain and the cache.
  • A verifiable ledger. The hash covers the full event record. Truncating the tail can't be detected without an external anchor, so I documented that limitation.

Accomplishments that we're proud of

  • Real PayPal Sandbox payouts, approved by a human, confirmed by PayPal's signed webhook and visible in the recipient's account.
  • A deterministic core the AI cannot override, with a test that catches the assistant giving the wrong reason for a denial.
  • A ledger you can verify end to end in one click, and 97 tests behind the claims.

What we learned

Put the model where judgment helps (turning language into structure) and keep it away from where correctness matters (decisions, limits, explanations of facts). Free-tier AI quotas shape the design more than I expected.

What's next for Countersign

  • Advisory lookalike-recipient detection with embeddings (flag "Second Signature Cloud Ltd" next to an approved "Second Signature Cloud"), still never a decider.
  • An MCP server generated from the OpenAPI spec, so any agent can request payments as a tool.
  • Per-workspace PayPal onboarding, live Payouts and Orders, approvals from Slack or email, an external anchor for the ledger head hash, and Postgres as the default store.

Built With

Share this project:

Updates

Submission history