<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Kirthi Sagar</title>
    <description>The latest articles on DEV Community by Kirthi Sagar (@kirthi-sagar).</description>
    <link>https://dev.to/kirthi-sagar</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F3780008%2F5bb350b9-33f3-43d6-baff-2a1c08104dc6.png</url>
      <title>DEV Community: Kirthi Sagar</title>
      <link>https://dev.to/kirthi-sagar</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/kirthi-sagar"/>
    <language>en</language>
    <item>
      <title>How I Built an AI Dunning Agent That Recovers Failed Payments on Autopilot</title>
      <dc:creator>Kirthi Sagar</dc:creator>
      <pubDate>Mon, 31 Aug 2026 00:28:14 +0000</pubDate>
      <link>https://dev.to/kirthi-sagar/how-i-built-an-ai-dunning-agent-that-recovers-failed-payments-on-autopilot-fpl</link>
      <guid>https://dev.to/kirthi-sagar/how-i-built-an-ai-dunning-agent-that-recovers-failed-payments-on-autopilot-fpl</guid>
      <description>&lt;p&gt;I built Dunning for the All Things Agentic Hackathon. This post covers why I made it, how it works under the hood, and the mistakes I made along the way. I am publishing this piece of content for the purpose of entering the All Things Agentic Hackathon on Devpost.&lt;/p&gt;

&lt;h2&gt;
  
  
  The Problem Nobody Talks About
&lt;/h2&gt;

&lt;p&gt;If you run a SaaS business, you lose money to failed payments every single month. A credit card expires. A bank declines a charge. The customer never even knows it happened, and you just lost recurring revenue.&lt;/p&gt;

&lt;p&gt;The industry calls this involuntary churn, and most companies either ignore it or send a couple of generic retry emails that get buried in inboxes. Collections agencies charge 25-30% of what they recover. That felt broken to me.&lt;/p&gt;

&lt;h2&gt;
  
  
  What Dunning Actually Does
&lt;/h2&gt;

&lt;p&gt;Dunning is a full-stack web application with an AI agent at its core. When a payment fails, the agent takes over. It figures out who the debtor is, looks at their timezone and jurisdiction, picks the right tone and legal language, and sends a sequence of recovery emails. If the debtor responds, the agent negotiates directly - it can offer payment plans, small discounts, or just answer questions about the invoice.&lt;/p&gt;

&lt;p&gt;The whole thing runs without human intervention. You connect your billing platform, set your rules, and the agent handles the rest.&lt;/p&gt;

&lt;p&gt;You can see it live at &lt;a href="https://www.dunning.website" rel="noopener noreferrer"&gt;dunning.website&lt;/a&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  The Tech Stack
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Frontend:&lt;/strong&gt; Next.js 14, deployed on Vercel. Server components, a dashboard for managing invoices and rules, a billing page where customers can pick their subscription tier.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Backend:&lt;/strong&gt; Python/FastAPI, deployed on Google Cloud Run. This handles authentication (Clerk), the REST API, webhook processing, and orchestrates the AI agent.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;AI Agent:&lt;/strong&gt; This is the heart of it. The negotiator agent uses Gemini through Google Cloud. It has access to tools like generate_recovery_link (creates a real checkout page via DodoPayments) and send_email (sends through Resend). The agent decides when to escalate tone, when to offer a discount, and when to stop emailing entirely.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Database:&lt;/strong&gt; PostgreSQL on Cloud SQL. Stores invoices, dunning rules, organization policies, success fees, and the full conversation thread between the agent and each debtor.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Payments:&lt;/strong&gt; DodoPayments for subscription billing and checkout link generation. Each recovery email includes a unique payment link that the debtor can click to pay immediately.&lt;/p&gt;

&lt;h2&gt;
  
  
  How the Agent Works
&lt;/h2&gt;

&lt;p&gt;The agent is not a chatbot. It runs in the background on a cron schedule. Every cycle, it:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Queries the database for invoices in an active dunning state&lt;/li&gt;
&lt;li&gt;Checks the dunning rules to see what action comes next&lt;/li&gt;
&lt;li&gt;Generates a personalized email using Gemini, adjusting the tone based on how many days past due the invoice is&lt;/li&gt;
&lt;li&gt;Creates a one-click payment link through DodoPayments&lt;/li&gt;
&lt;li&gt;Sends the email via Resend&lt;/li&gt;
&lt;li&gt;Updates the invoice state in the database&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;If the debtor replies to any of these emails, the response comes in through a Resend webhook. The agent reads the reply, generates a response, and continues the conversation. It can negotiate - if the debtor asks for a payment plan, the agent evaluates whether the organization's rules allow it and responds accordingly.&lt;/p&gt;

&lt;h2&gt;
  
  
  Mistakes I Made (And Fixed)
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Database schema drift.&lt;/strong&gt; I added new columns to the SQLAlchemy model (jurisdiction, debtor_country, debtor_timezone) but forgot to run the Alembic migration on the production database. The invoices page returned a 500 error for hours before I tracked it down to a missing column. The fix was a manual ALTER TABLE against the Cloud SQL instance.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Hardcoded API URLs.&lt;/strong&gt; My billing portal endpoint was manually constructing URLs to the DodoPayments live API instead of using the Python SDK. When I switched from the live API key to the test key, those hardcoded URLs broke with a 401. I replaced every requests.post call with the official SDK methods, which respect the environment setting automatically.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Secret overwriting.&lt;/strong&gt; My GitHub Actions workflow used gcloud run deploy --set-secrets which replaces all secrets on the service. One deploy wiped out the database URL, Clerk keys, and everything else. I changed it to --update-secrets which only adds or updates the ones you specify.&lt;/p&gt;

&lt;h2&gt;
  
  
  What I Would Do Differently
&lt;/h2&gt;

&lt;p&gt;The agent currently creates a new DodoPayments product for every single recovery email. That works, but it clutters the product catalog. A better approach would be to create products once and reuse them.&lt;/p&gt;

&lt;p&gt;I would also add a proper queue (Cloud Tasks or Pub/Sub) instead of running the agent on a cron timer. Right now, if the cron job takes too long, it could overlap with the next one.&lt;/p&gt;

&lt;h2&gt;
  
  
  Try It
&lt;/h2&gt;

&lt;p&gt;The project is live at &lt;a href="https://www.dunning.website" rel="noopener noreferrer"&gt;dunning.website&lt;/a&gt;. If you run a SaaS and failed payments are quietly eating your revenue, give it a look.&lt;/p&gt;




&lt;p&gt;&lt;em&gt;Built for the &lt;a href="https://allthingsagentichackathon.devpost.com/" rel="noopener noreferrer"&gt;All Things Agentic Hackathon&lt;/a&gt; using Gemini, Google Cloud Run, and the Agent Development Kit.&lt;/em&gt;&lt;/p&gt;

</description>
      <category>agents</category>
      <category>ai</category>
      <category>saas</category>
      <category>startup</category>
    </item>
  </channel>
</rss>
