For paid AI products

Track AI costs by workflow, not just tokens.

A source-code Next.js starter for paid AI apps. Add an internal usage layer, workflow-level cost tracking, soft caps, billing snapshots, and admin visibility before margins break.

Source-code starter Outcome cost Retry spend Soft caps Billing snapshots
Margin leakage dashboard Live usage
Cost / report $0.21 profitable
PDF Q&A $0.48 context spike
Retry spend 31% needs review
Workflow Outcome Cost Cap state
report_generation usable $0.21 ok
pdf_question usable $0.48 soft cap
ticket_reply failed retry $0.16 leaking
Every model call is tagged before it runs.

customer, workflow, model, entitlement snapshot, retries, final outcome

What you get

A starter repo for the AI usage layer most teams build too late.

The first version is designed to be practical source code, not a separate SaaS service. Drop it into a paid AI app and route model calls through one controlled boundary.

Next.js starter app

Dashboard, routes, setup docs, environment validation, and a focused demo workflow.

Internal AI usage layer

One typed client for model calls, provider routing, retries, caps, and outcome logging.

AI usage ledger

Self-contained records for customer, workflow, cost, price ID, entitlement, and final outcome.

Admin dashboard

Workflow cost, retry spend, failed runs, outlier users, plan usage, and margin leakage.

Billing connection

Plan snapshots, soft caps, upgrade prompts, and reconciliation-friendly event records.

Docs and support boundaries

Setup guide, architecture notes, buyer license, and clear limits for included support.

The real failure mode

AI billing fails quietly before it fails loudly.

Most teams notice the problem after launch: outlier users, retries, context spikes, failed runs, and billing records that do not explain what happened.

01

Tokens do not map to pricing

Token counts explain provider cost. They do not tell you whether the user received a valuable outcome.

02

Retries hide margin leaks

Agentic workflows can spend heavily on failed or retried runs before anyone checks the dashboard.

03

Usage and billing drift apart

If plan state is looked up later, upgrades and changing limits make reconciliation unreliable.

The V1 system

One internal usage layer for every AI workflow.

Start inside the app, but keep the boundary shaped like a gateway so it can become a relay later.

1

Tag

customer, workflow, environment, price ID, entitlement snapshot

2

Cap

soft limits, context windows, model allowlists, hard risk blocks

3

Route

provider keys stay behind one internal client

4

Log

tokens, cost, latency, retries, failures, final outcome

5

Price

see workflow cost, usage by plan, and quiet margin leaks

Developer contract

Keep product code clean. Put AI accounting behind one call.

The template should feel like a normal Next.js app, but every provider call must pass through a typed usage boundary that records cost and outcome.

lib/ai-usage/client.ts gateway-shaped boundary
await aiUsage.run({
  customerId,
  userId,
  workflow: "pdf_question",
  feature: "rag",
  environment: "production",
  entitlement: planSnapshot,
  caps: {
    maxContextTokens: 12000,
    softDailyCost: 4.00
  },
  action: async ({ model }) => {
    return streamAnswer({ model, question, chunks })
  }
})
ai_usage_events self-contained billing record
customer_id who paid
workflow_id what outcome
provider_cost what it cost
retry_count where margin leaks
active_price_id billing snapshot
usable_result did user get value?
How it works

Wrap the AI call. Log the outcome. Price the workflow.

The buyer should not have to redesign billing architecture from scratch. The template gives them a working pattern they can adapt.

01

Wrap AI workflows

Call aiUsage.run() from product workflows instead of calling providers directly.

02

Record cost and outcome

Log tokens, retries, provider cost, failed runs, usable result, and plan snapshot.

03

Control margin

Use soft caps, workflow dashboards, and billing snapshots to catch margin leaks early.

Use cases

Built for paid AI workflows where usage can get expensive.

Document Q&A

Track retrieval, context windows, file caps, and cost per answered question.

AI support agents

Measure resolved tickets, retries, failed runs, fallback spend, and plan limits.

Report generators

Price reports by completed output while tracking model mix and retry leakage internally.

Agency client apps

Reuse a client-ready pattern for billing, caps, admin visibility, and handoff docs.

Product principle

Users buy outcomes. Owners track economics.

Keep the customer-facing plan simple: reports, documents, tickets, runs. Internally, track the cost and margin behind every unit.

User sees Owner tracks Decision
100 reports / month cost per report keep
500 document questions retrieval + context cost watch
1,000 resolved tickets retry + failure share reprice
50 agent runs / day tool calls + fallback cost soft cap
What ships in V1

The pieces that protect margin and support billing audits.

AI usage ledger

Every event stores customer, workflow, model, tokens, cost, retries, outcome, price ID, and entitlement snapshot.

Workflow profitability

Dashboard views by workflow, model, customer, retry spend, failed runs, and plan usage.

Soft caps first

Warnings and upgrade prompts before hard blocks, unless cost risk is genuinely dangerous.

Privacy-safe defaults

Prompt and body logging stay off by default. Cost metadata remains useful without sensitive content.

Billing snapshots

Events are self-contained enough to re-derive charges after plan changes and mid-cycle upgrades.

Gateway-shaped boundary

One internal AI client today, clean extraction path to relay/gateway later.

Planned pricing

Expected one-time pricing after beta.

Joining early access is free and does not create a purchase or reserve a price. These ranges test pricing expectations for the future source-code release.

Founding Early Access

Expected $49

Planned first paid release for early builders who help shape V1.

  • Paid access opens with beta
  • V1 codebase when released
  • Setup guide
  • Bugfix updates

Agency

$299-$499

For client projects and repeat delivery.

  • Client-use license
  • White-label rights
  • Handoff docs
  • Priority support
FAQ

Positioned for paid AI apps, not generic SaaS.

Is this another SaaS boilerplate?

No. It includes SaaS foundations where needed, but the product focuses on AI usage accounting, cost caps, billing snapshots, and workflow profitability.

Is this a separate gateway service?

Not in V1. It starts as app-level middleware with a gateway-shaped boundary so it can be extracted later.

Do users see tokens and model costs?

No. Users see simple units like reports, documents, tickets, or runs. Owners see internal cost and margin by workflow.

Do you log prompts?

Prompt/body logging is off by default. The ledger records metadata for cost, reliability, caps, and billing audits.

Early access

Building a paid AI app?

Join the early list if you want the first working demo, beta access, or want to help shape the usage layer before V1 ships.