• Pricing
  • Blog
Log inBook a demo

Balance + Margin Template

Balance-based billing with automatic cost-plus pricing. Real model cost from the AI catalog, your margin on top, automatic deduction per request.

Templates
Live DemoSource
Balance + Margin Template

Next.js template for AI products that bill cost plus margin against a prepaid balance. Each AI request automatically deducts the real model cost from the AI catalog plus your configured margin.

Two plans (Starter, Pro) with different margins, a multi-model selector, and streaming generation.

Stack: Next.js 16, AI SDK (Gateway), Better Auth, Drizzle ORM, PostgreSQL, Commet SDK

Quick Start#

bash
commet create my-app -t balance-ai
cd my-app
docker compose up -d
pnpm install
pnpm dev

What's Included#

  • Two plans with different balances and margins (Starter 50%, Pro 30%)

  • Multi-model selector — switch between Claude, GPT, Gemini in one click

  • Automatic token tracking via tracked() middleware

  • AI model catalog pricing — real cost per model from Commet

  • Balance deduction per request (cost + margin)

  • Block on exhaustion — requests stop when balance runs out

  • AI text generation demo with streaming

  • Checkout flow with Commet-hosted payments

  • Customer billing portal

How It Works#

Request flow
User sends prompt → AI Gateway → Response
                        ↓
              tracked() middleware reports:
              - model, inputTokens, outputTokens
                        ↓
              Commet calculates:
              - Real cost (from catalog)
              - Margin (from plan config)
              - Total deducted from balance

Key Files#

  • lib/auth/auth.ts — Better Auth + Commet plugin with usage module

  • app/api/generate/route.ts — AI endpoint with tracked() middleware

  • app/(dashboard)/dashboard/generate-form.tsx — Model selector + streaming

  • app/pricing/page.tsx — Dynamic pricing with checkout

  • app/checkout/page.tsx — Checkout session creation

  • lib/env.ts — Environment validation with t3-env

  • lib/commet.ts — SDK initialization

Learn More#

  • Consumption Models — Balance model details

  • AI Token Billing

  • Customer Portal

Run commet create my-app -t balance-ai to start from this template, or browse the source on GitHub.

Subscription billing built for developers and agents

Get startedBook a demo

Accept global payments, handle usage-based pricing, subscriptions, and seats — without building your own infrastructure.

Frameworks

  • Next.js
  • Remix
  • Nuxt
  • SvelteKit
  • Astro
  • Express
  • Hono
  • Django
  • FastAPI

Resources

  • Documentation
  • Guides
  • Blog
  • Templates
  • Changelog
  • Glossary
  • Billing for AI models

Company

  • Solutions
  • About
  • Open Source

Compare

  • Stripe Billing
  • Orb
  • Chargebee
  • Lago
  • Autumn
  • Yuno

AI

  • Commet Agents
  • MCP Server
  • Skills
  • Skills on GitHub
  • Claude Code
  • Codex
  • Cursor
Follow us
© 2026 Commet Co.TermsPrivacy PolicyCookie Policy