<?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: Alejandro Navarro Boned</title>
    <description>The latest articles on DEV Community by Alejandro Navarro Boned (@adexinspire).</description>
    <link>https://dev.to/adexinspire</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.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F3782533%2F6ead1d02-eaf0-4dde-aa6f-4ec4e6ce0a33.png</url>
      <title>DEV Community: Alejandro Navarro Boned</title>
      <link>https://dev.to/adexinspire</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/adexinspire"/>
    <language>en</language>
    <item>
      <title>How I Built a Credits-Based Billing System for AI SaaS Products</title>
      <dc:creator>Alejandro Navarro Boned</dc:creator>
      <pubDate>Fri, 20 Feb 2026 11:45:26 +0000</pubDate>
      <link>https://dev.to/adexinspire/how-i-built-a-credits-based-billing-system-for-ai-saas-products-537c</link>
      <guid>https://dev.to/adexinspire/how-i-built-a-credits-based-billing-system-for-ai-saas-products-537c</guid>
      <description>&lt;h2&gt;
  
  
  The Problem with Subscriptions for AI Products
&lt;/h2&gt;

&lt;p&gt;If you're building an AI product, you've probably thought about billing. The obvious choice is subscriptions — $20/month, $50/month, etc.&lt;/p&gt;

&lt;p&gt;But subscriptions don't work well for AI:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Heavy users&lt;/strong&gt; eat your margin. One power user can cost you $100/month in API calls on a $20 plan.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Light users&lt;/strong&gt; feel ripped off. They used the product twice and got charged $20.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Unpredictable costs.&lt;/strong&gt; You can't forecast your API spend when usage varies wildly.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;That's why every major AI company uses credits or tokens: OpenAI, Anthropic, Replicate, ElevenLabs.&lt;/p&gt;

&lt;h2&gt;
  
  
  The Solution: Credits-Based Billing
&lt;/h2&gt;

&lt;p&gt;I built &lt;a href="https://adexinspire.gumroad.com/l/Shipai" rel="noopener noreferrer"&gt;ShipAI&lt;/a&gt;, a Next.js boilerplate with credits billing baked in. Here's how the system works.&lt;/p&gt;

&lt;h3&gt;
  
  
  The RESERVE → STREAM → FINALIZE Pattern
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;
typescript
// Step 1: Reserve credits BEFORE the AI call
// Uses SELECT FOR UPDATE for atomic locking
const reservation = await supabase.rpc('reserve_credits', {
  user_id: userId,
  amount: estimatedCost
});

// Step 2: Stream the AI response
const stream = await openai.chat.completions.create({
  model: 'gpt-4o',
  messages,
  stream: true
});

// Step 3: Count actual tokens and refund unused credits
await supabase.rpc('finalize_credits', {
  user_id: userId,
  reserved: estimatedCost,
  actual: calculateCost(inputTokens, outputTokens, model)
});
This pattern ensures:

Users are never overcharged (unused credits are refunded)
You never give away free API calls (credits are reserved first)
No race conditions (SELECT FOR UPDATE provides row-level locking)
Per-Model Pricing
Different models have different costs. ShipAI handles this automatically:

Model   Input (per 1K tokens)   Output (per 1K tokens)
GPT-4o  1 credit    3 credits
GPT-4o Mini 0.5 credits 1 credit
Claude Sonnet   1 credit    3 credits
Claude Haiku    0.3 credits 0.8 credits
Users choose their model, and the credit cost adjusts automatically.

Security Considerations
Credits are money. They need to be treated like financial transactions:

CHECK constraint: credits &amp;gt;= 0 — credits can never go negative at the database level
Idempotent webhooks: Stripe can send duplicate events. The add_credits function uses stripe_session_id as a unique key to prevent double-crediting.
Row-Level Security: Users can only read their own credit balance and transaction history.
Input validation: Max 100 messages per request, 32KB payload limit.
The Full Stack
ShipAI includes everything you need beyond just billing:

Next.js 15 with App Router
Supabase Auth (email, Google OAuth, magic links)
Stripe Checkout with pre-configured webhooks
Multi-model chat UI with streaming responses
Usage dashboard with token tracking
TypeScript + Tailwind CSS
One-click Vercel deploy
Try It
ShipAI is available for $69 (launch price, regular $149): Get ShipAI

If you're building an AI product and don't want to spend weeks on billing infrastructure, check it out. Happy to answer questions in the comments.
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

</description>
      <category>nextjs</category>
      <category>webdev</category>
      <category>typescript</category>
      <category>ai</category>
    </item>
  </channel>
</rss>
