<?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: Velobase</title>
    <description>The latest articles on DEV Community by Velobase (@velobasex).</description>
    <link>https://dev.to/velobasex</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%2F3930714%2F64013cd4-f59e-41ea-96c7-6aa0456c28bb.jpg</url>
      <title>DEV Community: Velobase</title>
      <link>https://dev.to/velobasex</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/velobasex"/>
    <language>en</language>
    <item>
      <title>I stored AI SaaS credits as a single integer. Then the refunds started.</title>
      <dc:creator>Velobase</dc:creator>
      <pubDate>Thu, 25 Jun 2026 02:53:09 +0000</pubDate>
      <link>https://dev.to/velobasex/i-stored-ai-saas-credits-as-a-single-integer-then-the-refunds-started-2hg</link>
      <guid>https://dev.to/velobasex/i-stored-ai-saas-credits-as-a-single-integer-then-the-refunds-started-2hg</guid>
      <description>&lt;p&gt;&lt;em&gt;Once in a while someone on the team writes up a mistake worth keeping. This one is about billing, from the engineer who lived it.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;The scary part of launching an AI SaaS is not the empty launch day. It's the first week when real people start paying, burning credits, retrying requests, asking for refunds, running cron jobs, and finding every shortcut you left in the billing code.&lt;/p&gt;

&lt;p&gt;Usage-based billing sounds simple when it's a pricing page. It gets messy when every AI action costs money, every retry can double-charge someone, and every support ticket starts with a number your database cannot explain.&lt;/p&gt;

&lt;p&gt;My first credit system was one column on the users table: &lt;code&gt;credits&lt;/code&gt;. Buy a pack, it goes up. Run a generation, it goes down. I built it in an afternoon and felt clever about how little code it took.&lt;/p&gt;

&lt;p&gt;It held up right until real money showed up. One Tuesday I opened the billing dashboard to a refund, a chargeback from a name I didn't recognize, and a buyer who'd been charged twice because his request timed out and retried. Then an email landed that I still remember word for word:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;"Why do I have 37 credits? I bought 500 and I've barely touched this."&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;I opened a psql shell and ran the only query I had:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;select&lt;/span&gt; &lt;span class="n"&gt;credits&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="n"&gt;users&lt;/span&gt; &lt;span class="k"&gt;where&lt;/span&gt; &lt;span class="n"&gt;id&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s1"&gt;'...'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;-- 37&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Thirty-seven. That was the whole story my database could tell me.&lt;/p&gt;

&lt;p&gt;Not where the other 463 went. Not when. Not why. Every &lt;code&gt;credits = credits - 1&lt;/code&gt; had overwritten the last number and dropped the reason on the floor. I started a reply with "let me look into this" and realized there was nothing to look into.&lt;/p&gt;

&lt;p&gt;That email is why I tore the whole thing out and rebuilt it as a ledger. Here's the model, and the exact moment that taught me each part of it.&lt;/p&gt;

&lt;h2&gt;
  
  
  A counter only remembers the last thing that happened
&lt;/h2&gt;

&lt;p&gt;With a counter, a customer's entire financial history with your product is one mutable integer that any code path can stomp on. The grant, the spend, the refund, the bug, the fix for the bug. They all collapse into a single number, and the moment they do, you've thrown away every question worth asking:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;"Why is this balance what it is?" You'd need every change that produced it.&lt;/li&gt;
&lt;li&gt;"Refund this purchase." You'd need to know what that purchase actually granted.&lt;/li&gt;
&lt;li&gt;"Did a retry double-credit this user?" You'd need to spot the duplicate write.&lt;/li&gt;
&lt;li&gt;"Do our credits reconcile with Stripe?" You'd need a paper trail on both sides.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;A single number answers none of them. Once refunds exist, you don't want the balance. You want the list of things that produced it.&lt;/p&gt;

&lt;h2&gt;
  
  
  Stop updating the balance. Start appending rows.
&lt;/h2&gt;

&lt;p&gt;The fix is boring, which turns out to be the point. You never update a balance again. You write one signed row per change, and you add them up when you need the number.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;create&lt;/span&gt; &lt;span class="k"&gt;table&lt;/span&gt; &lt;span class="n"&gt;credit_entries&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;
  &lt;span class="n"&gt;id&lt;/span&gt;              &lt;span class="nb"&gt;bigint&lt;/span&gt; &lt;span class="k"&gt;generated&lt;/span&gt; &lt;span class="n"&gt;always&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="k"&gt;identity&lt;/span&gt; &lt;span class="k"&gt;primary&lt;/span&gt; &lt;span class="k"&gt;key&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="n"&gt;user_id&lt;/span&gt;         &lt;span class="n"&gt;uuid&lt;/span&gt; &lt;span class="k"&gt;not&lt;/span&gt; &lt;span class="k"&gt;null&lt;/span&gt; &lt;span class="k"&gt;references&lt;/span&gt; &lt;span class="n"&gt;users&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;id&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
  &lt;span class="n"&gt;amount&lt;/span&gt;          &lt;span class="nb"&gt;integer&lt;/span&gt; &lt;span class="k"&gt;not&lt;/span&gt; &lt;span class="k"&gt;null&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="c1"&gt;-- +grant, -spend, never zero&lt;/span&gt;
  &lt;span class="n"&gt;reason&lt;/span&gt;          &lt;span class="nb"&gt;text&lt;/span&gt; &lt;span class="k"&gt;not&lt;/span&gt; &lt;span class="k"&gt;null&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;    &lt;span class="c1"&gt;-- 'purchase' | 'generation' | 'refund' | 'signup_bonus' | 'clawback'&lt;/span&gt;
  &lt;span class="n"&gt;ref_type&lt;/span&gt;        &lt;span class="nb"&gt;text&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;             &lt;span class="c1"&gt;-- 'stripe_payment' | 'job' | 'credit_entry'&lt;/span&gt;
  &lt;span class="n"&gt;ref_id&lt;/span&gt;          &lt;span class="nb"&gt;text&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;             &lt;span class="c1"&gt;-- the thing that caused this row&lt;/span&gt;
  &lt;span class="n"&gt;idempotency_key&lt;/span&gt; &lt;span class="nb"&gt;text&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;             &lt;span class="c1"&gt;-- dedupe retries&lt;/span&gt;
  &lt;span class="n"&gt;created_at&lt;/span&gt;      &lt;span class="n"&gt;timestamptz&lt;/span&gt; &lt;span class="k"&gt;not&lt;/span&gt; &lt;span class="k"&gt;null&lt;/span&gt; &lt;span class="k"&gt;default&lt;/span&gt; &lt;span class="n"&gt;now&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="k"&gt;create&lt;/span&gt; &lt;span class="k"&gt;unique&lt;/span&gt; &lt;span class="k"&gt;index&lt;/span&gt; &lt;span class="k"&gt;on&lt;/span&gt; &lt;span class="n"&gt;credit_entries&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;idempotency_key&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="k"&gt;where&lt;/span&gt; &lt;span class="n"&gt;idempotency_key&lt;/span&gt; &lt;span class="k"&gt;is&lt;/span&gt; &lt;span class="k"&gt;not&lt;/span&gt; &lt;span class="k"&gt;null&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;No &lt;code&gt;updated_at&lt;/code&gt;. Rows are written once and never touched again. The balance is a sum:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;select&lt;/span&gt; &lt;span class="n"&gt;coalesce&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;sum&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;amount&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="n"&gt;balance&lt;/span&gt;
&lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="n"&gt;credit_entries&lt;/span&gt;
&lt;span class="k"&gt;where&lt;/span&gt; &lt;span class="n"&gt;user_id&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="err"&gt;$&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Remember the 37-credit guy? On the ledger, his mystery is just his history, in order:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;+500   purchase                    2026-05-02 09:14
 -463  generation (x463 entries)   2026-05-02 -&amp;gt; 05-09
-----
   37  balance
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Four hundred and sixty-three generations in a week from someone who'd "barely touched it." I pasted the history into my reply. He wrote back: "oh no. that's my cron job."&lt;/p&gt;

&lt;p&gt;Closed in one message, because the data could finally speak for itself.&lt;/p&gt;

&lt;h2&gt;
  
  
  The retry that gives away free credits
&lt;/h2&gt;

&lt;p&gt;This is the one that actually cost me money. Stripe's webhook delivery is at-least-once, not exactly-once. Duplicate deliveries are the contract, not a bug.&lt;/p&gt;

&lt;p&gt;One afternoon Stripe fired &lt;code&gt;checkout.session.completed&lt;/code&gt;, my handler granted 500 credits, my server was slow to send back its 200, so Stripe assumed the delivery failed and fired the same &lt;code&gt;evt_&lt;/code&gt; id again. Then a third time.&lt;/p&gt;

&lt;p&gt;The buyer pinged me an hour later:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;"lol why do I suddenly have 1,500 credits"&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Same event, same signature, processed three times, and I had paid for it.&lt;/p&gt;

&lt;p&gt;The tempting fix is to check before you write. Don't. That's a race, and two deliveries can both pass the check:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// DON'T: two concurrent deliveries both read "not seen" and both grant&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;seen&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nx"&gt;db&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;query&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;creditEntries&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;findFirst&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;
  &lt;span class="na"&gt;where&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nf"&gt;eq&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;idempotencyKey&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;eventId&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
&lt;span class="p"&gt;});&lt;/span&gt;

&lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="nx"&gt;seen&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nf"&gt;grant&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;userId&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;500&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Let the database be the referee. Derive a key from the source event and let a unique index throw the duplicate away:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nx"&gt;db&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;insert&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;creditEntries&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;values&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;
    &lt;span class="nx"&gt;userId&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="na"&gt;amount&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;500&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="na"&gt;reason&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;purchase&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="na"&gt;refType&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;stripe_payment&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="na"&gt;refId&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;paymentId&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="na"&gt;idempotencyKey&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;stripeEventId&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="p"&gt;})&lt;/span&gt;
  &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;onConflictDoNothing&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;
    &lt;span class="na"&gt;target&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;creditEntries&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;idempotencyKey&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="p"&gt;});&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The second and third deliveries hit the index and do nothing. The grant happens once. You didn't have to make your webhook perfectly exactly-once. The data model swallowed the retry for you.&lt;/p&gt;

&lt;h2&gt;
  
  
  The double-spend that takes ten milliseconds
&lt;/h2&gt;

&lt;p&gt;Picture a user on hotel wifi mashing "Generate" twice, or someone with two tabs open. They have one credit left.&lt;/p&gt;

&lt;p&gt;Request A reads the balance: 1. Ten milliseconds later, before A has written anything, Request B reads the balance: 1. Both decide they're fine. Both generate. The balance lands at -1, and they got two images for the price of less than one.&lt;/p&gt;

&lt;p&gt;The bug is reading the balance in app code and then writing based on a number that's already stale. Do the check and the write in one transaction, so the read only sees committed rows:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nx"&gt;db&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;transaction&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;async &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;tx&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;balance&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nx"&gt;tx&lt;/span&gt;
    &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;select&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt; &lt;span class="na"&gt;balance&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;sql&lt;/span&gt;&lt;span class="s2"&gt;`coalesce(sum(amount), 0)`&lt;/span&gt; &lt;span class="p"&gt;})&lt;/span&gt;
    &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="k"&gt;from&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;creditEntries&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;where&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;eq&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;creditEntries&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;userId&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;userId&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;

  &lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;balance&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="nx"&gt;cost&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;throw&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;InsufficientCredits&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;

  &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nx"&gt;tx&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;insert&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;creditEntries&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;values&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;
    &lt;span class="nx"&gt;userId&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="na"&gt;amount&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nx"&gt;cost&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="na"&gt;reason&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;generation&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="na"&gt;refType&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;job&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="na"&gt;refId&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;jobId&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="na"&gt;idempotencyKey&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;jobId&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="p"&gt;});&lt;/span&gt;
&lt;span class="p"&gt;});&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Run it at serializable, or take a per-user advisory lock, so two transactions can't both clear the check against the same starting balance. Using &lt;code&gt;jobId&lt;/code&gt; as the key also means a retried job won't get billed twice.&lt;/p&gt;

&lt;h2&gt;
  
  
  A refund is just one more row
&lt;/h2&gt;

&lt;p&gt;Forty days after a sale, a chargeback rolls in. The credits it paid for were spent two weeks ago. With a counter you're stuck choosing between editing a history you no longer have and pretending it didn't happen.&lt;/p&gt;

&lt;p&gt;On a ledger you append:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;insert&lt;/span&gt; &lt;span class="k"&gt;into&lt;/span&gt; &lt;span class="n"&gt;credit_entries&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;user_id&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;amount&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;reason&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;ref_type&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;ref_id&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;values&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="err"&gt;$&lt;/span&gt;&lt;span class="k"&gt;user&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="mi"&gt;500&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'refund'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'credit_entry'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="err"&gt;$&lt;/span&gt;&lt;span class="n"&gt;original_entry_id&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The balance drops honestly, goes negative if it has to, and the new row points straight at the purchase it cancels.&lt;/p&gt;

&lt;p&gt;Same move when you catch a farmed signup: append a negative clawback instead of reaching into a column and hoping you did the arithmetic right. Refund, chargeback, fraud, a manual fix from support. All the same operation. Write one more row.&lt;/p&gt;

&lt;p&gt;Boring on purpose, and boring is exactly what you want anywhere near money.&lt;/p&gt;

&lt;h2&gt;
  
  
  "Won't the SUM get slow?"
&lt;/h2&gt;

&lt;p&gt;At launch, that balance query was 2ms and I never thought about it. Somewhere around four million rows it was 200ms, and the p95 on every endpoint that checked a balance started to crawl.&lt;/p&gt;

&lt;p&gt;The fix isn't to abandon the ledger. You periodically write a checkpoint row that snapshots the balance up to a point in time, then sum only what came after it.&lt;/p&gt;

&lt;p&gt;The ledger stays the source of truth, and the checkpoint is a cache you can rebuild any time you stop trusting it. Don't build it before a profiler asks you to. An index on &lt;code&gt;(user_id, created_at)&lt;/code&gt; carries you further than you'd guess.&lt;/p&gt;

&lt;p&gt;One more habit worth starting early: the day you pay affiliates or hand out referral bonuses, make the movement double-entry. Every credit out of one account is a credit into another, so the books always sum to zero. The first time mine didn't, it was off by a single cent, and double-entry meant I caught it that afternoon instead of six weeks later in a reconciliation nobody had scheduled.&lt;/p&gt;

&lt;h2&gt;
  
  
  What I'd tell past me
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Store integer units, never a float. Float math is how you wake up to balances ending in &lt;code&gt;0.000001&lt;/code&gt; that drift over time.&lt;/li&gt;
&lt;li&gt;Never delete a ledger row. Wrong entry? Append a reversal. A &lt;code&gt;DELETE&lt;/code&gt; breaks the audit trail for good.&lt;/li&gt;
&lt;li&gt;Give every entry a reason and a reference to whatever caused it. That one column is the difference between answering a dispute in ten seconds and guessing.&lt;/li&gt;
&lt;li&gt;Keep a cached balance if you want the speed, but treat the ledger as the only truth and re-derive the moment the two disagree.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The honest tradeoff: this is more code than &lt;code&gt;credits -= 1&lt;/code&gt;. It's also the difference between reading a customer his own history back and typing "let me look into this" with nothing to look into.&lt;/p&gt;

&lt;p&gt;We kept rebuilding this same stack for AI products: Stripe checkout, usage-based billing, usage metering, customer credits, refunds, idempotent webhooks, and a ledger that support can actually explain. So we open-sourced it as Harness, MIT-licensed: &lt;a href="https://github.com/velobase/velobase-harness" rel="noopener noreferrer"&gt;github.com/velobase/velobase-harness&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;If you're building an AI SaaS and don't want your first real users to become your billing test suite, it may save you a few painful weeks.&lt;/p&gt;

&lt;p&gt;If you've run this in production: how do you handle the SUM-vs-snapshot tradeoff? Running totals, checkpoint rows, a cached balance you reconcile on a cron? I'd genuinely like to hear what held up.&lt;/p&gt;

</description>
      <category>ai</category>
      <category>postgres</category>
      <category>webdev</category>
      <category>programming</category>
    </item>
    <item>
      <title>AI can write the code. It can't tell you when the product is done.</title>
      <dc:creator>Velobase</dc:creator>
      <pubDate>Thu, 18 Jun 2026 13:00:00 +0000</pubDate>
      <link>https://dev.to/velobasex/ai-can-write-the-code-it-cant-tell-you-when-the-product-is-done-4oh6</link>
      <guid>https://dev.to/velobasex/ai-can-write-the-code-it-cant-tell-you-when-the-product-is-done-4oh6</guid>
      <description>&lt;p&gt;I built an AI slide-deck generator on top of &lt;a href="https://github.com/velobase/velobase-harness" rel="noopener noreferrer"&gt;Velobase Harness&lt;/a&gt; — type a topic, get a presentation. The AI had a demo running almost immediately: it wrote an outline, generated the slides, exported a file. On screen, it looked finished.&lt;/p&gt;

&lt;p&gt;Here's it running:&lt;/p&gt;

&lt;p&gt;  &lt;iframe src="https://www.youtube.com/embed/LbbOkHjecE0"&gt;
  &lt;/iframe&gt;
&lt;/p&gt;

&lt;p&gt;It wasn't. All it had proven was that one user could make one deck one time.&lt;/p&gt;

&lt;p&gt;A real product is a different animal. It has to handle a hundred people generating at once, bill each of them correctly, recover when a step dies halfway, review its own output and redraw the bad parts, and export a PPTX that actually opens in PowerPoint. None of that showed up in the demo — and the AI didn't think to add it, because I hadn't told it to.&lt;/p&gt;

&lt;p&gt;That turned out to be the whole lesson of the project: &lt;strong&gt;when you build with AI, the hard part isn't describing the feature. It's defining what "done" means.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Harness gave me the boring foundation — auth, payments, credits, admin, database, queues, object storage, deployment. That stuff shouldn't be rebuilt for every new idea, so I didn't. It let me point the AI at the only part that was actually mine: the PPT generation itself.&lt;/p&gt;

&lt;p&gt;And that's where "looks done" and "is done" kept pulling apart. Four places made it obvious.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Concurrency.&lt;/strong&gt; AI treats "it worked once on my machine" as "the feature is complete." But 100 users generating decks of 10–20 slides each is not one big task you run serially in a single worker. The instruction that actually works: split the pipeline into &lt;em&gt;plan → slide → finalize&lt;/em&gt; queues, generate each slide as its own job, let workers scale out, and don't let third-party image polling hog a worker slot. Then write a test that proves &lt;em&gt;concurrent&lt;/em&gt; generation works — not that one deck succeeds.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Billing.&lt;/strong&gt; Tell the AI "deduct credits when generating," and it'll deduct once, up front, and call it done. A real product needs a state machine: reserve credits first, settle against what was actually spent (model tokens, image generation, redraws, export), refund on failure, pause when the balance runs out, resume after top-up. That's not a button handler.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Self-review.&lt;/strong&gt; If retries happen silently in the backend, the user just stares at a spinner. To make self-review a &lt;em&gt;feature&lt;/em&gt;, the AI has to persist intermediate results and surface the states — generating, checking, redrawing — along with scores and attempts. Now the wait reads as "the system is improving the slide," not "it's stuck."&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Export.&lt;/strong&gt; A slide that looks perfect in the browser can come out broken as PPTX — layers, fonts, aspect ratio, cropping all drift. So the requirement can't be "generate a PPTX file." It has to be "the exported PPTX must visually match the web preview."&lt;/p&gt;

&lt;p&gt;Once I saw the pattern, the brief I gave the AI stopped being a feature list and started being acceptance criteria:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Do not build only a working demo.
Build this as a production-ready SaaS product.

This product is based on Velobase Harness.
Harness already provides auth, payments, credits, queues, object storage, and deployment.
Implement PPT generation as an independent business module.

Requirements:
1. Support at least 100 users generating presentations concurrently.
2. Split generation into plan, slide, and finalize queues.
3. Generate each slide independently with concurrency, retries, and failure recovery.
4. Connect all model calls, image generation, editing, and export to credit billing.
5. Pause generation when credits are insufficient and resume after top-up.
6. Run AI review and deterministic layout checks after each slide.
7. Persist intermediate states and show generation, self-check, and redraw progress in real time.
8. Upload generated images to object storage instead of relying on temporary provider URLs.
9. Prioritize visual consistency for PPTX export.
10. Tests must cover concurrency, billing, failure recovery, and export fidelity — not just one successful run.
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here's what the project made unambiguous: AI is already good at writing code. It just doesn't know what makes something shippable. It will treat one successful run as completion, a local demo as a system, a page that renders as a correct export. The part only a human can supply isn't the feature description — it's the acceptance criteria, the engineering boundaries, the failure cases, the business rules.&lt;/p&gt;

&lt;p&gt;That's the real split. Harness covers the common SaaS foundation so it's already there. AI fills in the business logic fast — &lt;em&gt;once the boundaries are clear&lt;/em&gt;. Faster shipping doesn't come from thinking less. It comes from turning your thinking into sharper input.&lt;/p&gt;

&lt;p&gt;If you've shipped something AI-built past the demo stage: what was the gap between "it ran" and "I'd let customers near it" that bit you hardest?&lt;/p&gt;

</description>
      <category>ai</category>
      <category>saas</category>
      <category>opensource</category>
      <category>webdev</category>
    </item>
    <item>
      <title>The boring 80% nobody warns you about when an AI demo becomes a real product</title>
      <dc:creator>Velobase</dc:creator>
      <pubDate>Wed, 17 Jun 2026 04:55:33 +0000</pubDate>
      <link>https://dev.to/velobasex/the-boring-80-nobody-warns-you-about-when-an-ai-demo-becomes-a-real-product-3dnm</link>
      <guid>https://dev.to/velobasex/the-boring-80-nobody-warns-you-about-when-an-ai-demo-becomes-a-real-product-3dnm</guid>
      <description>&lt;p&gt;This month the front page filled up with AI agents going off the rails — one reportedly ran its operator's bill into the ground, another tore through a Linux box unsupervised. Funny, until it's your credits and your customers. We learned that lesson the slower, more expensive way.&lt;/p&gt;

&lt;p&gt;Our team shipped an AI demo in a weekend. Turning it into something people could actually pay for took the next four months, and almost none of that time went into the AI part.&lt;/p&gt;

&lt;p&gt;That gap surprised us more than it should have. The model call was maybe 30 lines. Everything around it — getting paid, knowing who to thank for a signup, stopping one script from burning $400 of credits overnight — was the real product. Below is what we got wrong on the way there, in roughly the order it hurt.&lt;/p&gt;

&lt;h2&gt;
  
  
  Pitfall 1: "We'll just add Stripe later"
&lt;/h2&gt;

&lt;p&gt;Later is a lie. The moment you charge for AI usage, billing stops being a checkout button and becomes a metering problem.&lt;/p&gt;

&lt;p&gt;A flat $20/month subscription is easy. The trouble starts when usage is the cost. A heavy user can run you negative on a plan a light user is wildly overpaying for. So you end up needing both: a subscription &lt;em&gt;and&lt;/em&gt; a usage meter that counts tokens (or images, or minutes) and reconciles them against credits the customer actually has left.&lt;/p&gt;

&lt;p&gt;What we underestimated was the bookkeeping. Every generation has to decrement a balance, that decrement has to survive a crash mid-request, and the customer needs a dashboard that agrees with what Stripe charged them — to the cent — or you get support tickets you can't answer. We rebuilt this twice. The second time we finally separated "what the model did" (an event) from "what we charged" (a ledger entry), and the bugs stopped.&lt;/p&gt;

&lt;p&gt;If you take one thing from this post: &lt;strong&gt;model the usage event and the billing entry as two different things from day one.&lt;/strong&gt; Collapsing them feels simpler and costs you a weekend later.&lt;/p&gt;

&lt;h2&gt;
  
  
  Pitfall 2: Free credits are a free lunch — for abusers
&lt;/h2&gt;

&lt;p&gt;We gave new signups some free credits so people could try the thing without a card. Reasonable. Within a week someone had scripted a few hundred throwaway accounts and was draining the free tier in a loop.&lt;/p&gt;

&lt;p&gt;There's no single fix. What actually worked was a stack of cheap, boring checks layered together:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Rate limits per account and per IP (Redis, sliding window).&lt;/li&gt;
&lt;li&gt;A CAPTCHA on signup — we used Turnstile, but anything that adds friction for bots helps.&lt;/li&gt;
&lt;li&gt;Blocking disposable email domains at signup.&lt;/li&gt;
&lt;li&gt;Small signup signals (is this account an hour old with 200 generations?) feeding a guest quota.&lt;/li&gt;
&lt;li&gt;And the unglamorous one: &lt;strong&gt;credit clawbacks&lt;/strong&gt;, so when you do catch fraud after the fact, you can actually take the credits back instead of eating the loss.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;None of these is clever. The lesson was that abuse control isn't a feature you build once — it's a posture. You assume the free tier will be attacked and you make abuse slightly more annoying than it's worth.&lt;/p&gt;

&lt;h2&gt;
  
  
  Pitfall 3: You will not know which channel actually works
&lt;/h2&gt;

&lt;p&gt;We spent money on a few channels and had no idea which one produced paying customers. Client-side analytics told us about pageviews. It told us nothing trustworthy about &lt;em&gt;purchases&lt;/em&gt;, because ad blockers and the gap between "clicked an ad on Monday" and "paid on Thursday" quietly destroy attribution.&lt;/p&gt;

&lt;p&gt;The thing that fixed it was moving attribution server-side: stamp the acquisition source when the account is created, carry it through, and fire the conversion from the backend when money actually changes hands — including the offline-conversion uploads back to Google Ads and the pixel events platforms expect. Suddenly "this campaign cost $X and produced Y paying users" was a sentence we could finish.&lt;/p&gt;

&lt;p&gt;If you're running any paid acquisition for an AI product, do the server-side version early. The client-side numbers feel fine right up until you try to make a budget decision with them.&lt;/p&gt;

&lt;h2&gt;
  
  
  Pitfall 4: An affiliate program is an accounting system wearing a marketing hat
&lt;/h2&gt;

&lt;p&gt;This one humbled us. "Let people refer others for a cut" sounds like a referral link and a counter. It is not.&lt;/p&gt;

&lt;p&gt;The instant real money is owed to real people, you're running double-entry accounting whether you meant to or not. A referred customer asks for a refund — now you have to claw back the commission you already credited the affiliate. Someone wants to cash out (in our case people asked for USDT). Promo codes stack in ways you didn't intend. Get any of this slightly wrong and you're not shipping a bug, you owe someone the wrong amount of money.&lt;/p&gt;

&lt;p&gt;We ended up treating the whole thing as a proper ledger — every credit and clawback is a balanced entry — because that's the only way the numbers stay defensible when an affiliate disputes their payout.&lt;/p&gt;

&lt;h2&gt;
  
  
  Pitfall 5: The "rest of the backend" is a second full-time job
&lt;/h2&gt;

&lt;p&gt;Auth. Background workers for anything slower than a request (we used BullMQ on Redis). Lifecycle email so trials don't silently lapse. An admin panel so you can actually look at a customer when they email you. Then Docker, then Kubernetes, then CI/CD so a deploy isn't a held breath.&lt;/p&gt;

&lt;p&gt;Each of these is a known, solved problem. The cost isn't difficulty — it's &lt;em&gt;count&lt;/em&gt;. There are a dozen of them, none is the thing you set out to build, and together they're where the months go.&lt;/p&gt;

&lt;h2&gt;
  
  
  So what did we do about it
&lt;/h2&gt;

&lt;p&gt;We kept ripping these pieces out of one product and into something reusable, and eventually it became its own open-source project: &lt;a href="https://github.com/velobase/velobase-harness" rel="noopener noreferrer"&gt;Velobase Harness&lt;/a&gt; — MIT licensed, Next.js 15 / React 19 / tRPC / Prisma / Postgres, with the usage billing, server-side attribution, affiliate ledger, anti-abuse stack, workers, and deploy config already wired together. It's the scaffolding we wish we'd had before pitfall 1.&lt;/p&gt;

&lt;p&gt;We're not going to pretend it's the only way to do this — plenty of people would (and do) assemble these from separate services, and that's a legitimate choice. We just got tired of rebuilding the same plumbing for every AI idea.&lt;/p&gt;

&lt;p&gt;What we're actually curious about, and the reason we wrote this down: &lt;strong&gt;for those of you who've taken an AI product past the demo — which of these bit you hardest?&lt;/strong&gt; And did you build the billing/abuse/attribution layer yourself, reach for something off-the-shelf, or just... not, and hope? We're collecting war stories and genuinely want to hear how other people drew the line.&lt;/p&gt;

</description>
      <category>ai</category>
      <category>product</category>
      <category>saas</category>
      <category>softwareengineering</category>
    </item>
  </channel>
</rss>
