◉ANTIGRAVITY LABJP
Articles/Integrations
⬡ Integrations/2026-04-29Advanced

Building a Stripe Dunning Recovery Pipeline with Antigravity AI Agents — Stop Failed Payments from Becoming Churn

Wire Antigravity AI agents into Stripe Smart Retries to recover failed payments before they turn into churn. Webhooks, idempotency, AI-tailored email copy, Slack escalation, and winback offers — all in one production-ready pattern.

antigravity470stripe9saas10billing3ai-agent18dunning

✦ Premium Article

I run a small SaaS, and one month our MRR dropped sharply for no obvious reason. New signups were growing. Engagement was stable. When I finally chased it down inside the Stripe dashboard, the answer was unsexy: expired cards and one-off network declines. Roughly six out of ten cancellations that month traced back to those.

What hit me wasn't the loss of revenue. It was the realization that "how I handle failed payments" was a survival-line for the product, equal in weight to anything I'd ship as a feature. If I could automate that recovery flow well, it would compound just like new growth.

This article walks through the dunning recovery pipeline I rebuilt afterward, using Antigravity's AI agents at the center. I'll cover capturing failure events, controlling retries, branching email copy by reason, Slack escalation, and a final winback step — all in patterns I run in production today.

Why an AI Agent Belongs at the Center of Dunning

"Dunning" is the polite name for the dance you do with a customer after a charge fails. It looks small from the outside but the decision tree is surprisingly thick:

  • An expired card, an insufficient-funds decline, and a 3DS authentication failure all need different copy and different timing.
  • Pushing more retries lifts recovery rate, but past a point it makes customers feel hounded.
  • If the same customer fails twice in a week, sending another email usually backfires.
  • Corporate cards and personal cards often need different recipients on the notification side.

I tried to encode all this in a single webhook handler with if branches. It got past 150 lines before I lost track of what was firing when. I threw it out.

The shift that worked was moving the decision logic out of the handler entirely and into an Antigravity sub-agent. The webhook handler shrinks down to "validate, normalize, hand off." The agent owns the policy.

Pipeline Overview

The complete shape:

  1. Receive a Stripe webhook (invoice.payment_failed, customer.subscription.updated, etc.)
  2. Idempotency check + event normalization (Cloudflare D1 or Postgres)
  3. Hand off to the Dunning Agent with normalized failure reason, customer profile, and retry history
  4. Decide: keep retrying via Stripe, notify the customer, or trigger a winback offer
  5. Execute: send email through Resend, alert internal Slack, update KV/DB state
  6. Observe: log everything as structured JSON and chart it in Looker Studio

Steps 3 and 4 are where the agent earns its place. Stripe's built-in Smart Retries are good, but they don't speak your domain — "skip notifications during free trial," "give annual subscribers extra grace," "different copy for customers who came back after canceling once before." Encoding those rules in a prompt + tool contract beats an ever-growing nest of conditionals.

✦

Thank you for reading this far.

Continue Reading

What follows includes implementation code, benchmarks, and practical content we hope you'll find useful. This site runs without ads — server and development costs are supported entirely by members like you. If it's been helpful, we'd be truly grateful for your support.

WHAT YOU'LL LEARN
✦Match copy, timing, and channel to each decline reason (expiry, funds, 3DS, network)
✦An idempotent webhook and agent-delegation design that avoids double retries with Smart Retries
✦Three months of real recovery metrics by reason, down to the ~30-failures break-even
Secure payment via Stripe · Cancel anytime
✦

Unlock This Article

Get full access to the rest of this article. Buy once, read anytime. This site is ad-free — your support goes directly toward keeping it running.

or
Unlock all articles with Membership →
Share

Thank You for Reading

Antigravity Lab is ad-free, supported entirely by members like you. We publish practical guides daily with implementation code, benchmarks, and production-ready patterns. If you've found it useful, we'd love to have you on board.

  • ✦Copy-paste ready implementation code
  • ✦New advanced guides published daily
  • ✦$5/mo or $10 for lifetime access
View Membership →

Related Articles

⬡ Integrations2026-10-17
When Antigravity's Browser Agent Stops at the Login Screen: Four Things to Check
The browser agent came back with only a sign-in page. Here are four checks, built on the fact that it runs in a separate Chrome profile, to get its login state sorted.
⬡ Integrations2026-09-05
Pointing GOOGLE_GEMINI_BASE_URL at Your Own Gateway Does Not Change Who Holds the Key
I pointed the Antigravity CLI at a gateway of my own and recorded exactly which credentials travelled with it. Authorization is dropped across a cross-origin redirect, but the API key header survives every hop. Here is the measurement, and how I moved the key off my laptop.
⬡ Integrations2026-05-24
Catching "Running but Doing Nothing" Antigravity Subagents — A 3-Layer Observability Pattern
Logs green, output zero — the silent failure every overnight Antigravity subagent eventually produces. A three-layer design (Heartbeat / Output Trace / Decision Log) that tells missed starts, stale inputs, and write-less loops apart, with the SQL and code to copy.